| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#include "../miniaudio.c" |
| 15 |
|
| 16 |
#include <stdio.h> |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#define SAMPLE_FORMAT ma_format_f32 |
| 22 |
#define CHANNEL_COUNT 2 |
| 23 |
#define SAMPLE_RATE 48000 |
| 24 |
|
| 25 |
ma_uint32 g_decoderCount; |
| 26 |
ma_decoder* g_pDecoders; |
| 27 |
ma_bool32* g_pDecodersAtEnd; |
| 28 |
|
| 29 |
ma_event g_stopEvent; |
| 30 |
|
| 31 |
ma_bool32 are_all_decoders_at_end(void) |
| 32 |
{ |
| 33 |
ma_uint32 iDecoder; |
| 34 |
for (iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { |
| 35 |
if (g_pDecodersAtEnd[iDecoder] == MA_FALSE) { |
| 36 |
return MA_FALSE; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
return MA_TRUE; |
| 41 |
} |
| 42 |
|
| 43 |
ma_uint32 read_and_mix_pcm_frames_f32(ma_decoder* pDecoder, float* pOutputF32, ma_uint32 frameCount) |
| 44 |
{ |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
ma_result result; |
| 51 |
float temp[4096]; |
| 52 |
ma_uint32 tempCapInFrames = ma_countof(temp) / CHANNEL_COUNT; |
| 53 |
ma_uint32 totalFramesRead = 0; |
| 54 |
|
| 55 |
while (totalFramesRead < frameCount) { |
| 56 |
ma_uint64 iSample; |
| 57 |
ma_uint64 framesReadThisIteration; |
| 58 |
ma_uint32 totalFramesRemaining = frameCount - totalFramesRead; |
| 59 |
ma_uint32 framesToReadThisIteration = tempCapInFrames; |
| 60 |
if (framesToReadThisIteration > totalFramesRemaining) { |
| 61 |
framesToReadThisIteration = totalFramesRemaining; |
| 62 |
} |
| 63 |
|
| 64 |
result = ma_decoder_read_pcm_frames(pDecoder, temp, framesToReadThisIteration, &framesReadThisIteration); |
| 65 |
if (result != MA_SUCCESS || framesReadThisIteration == 0) { |
| 66 |
break; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
for (iSample = 0; iSample < framesReadThisIteration*CHANNEL_COUNT; ++iSample) { |
| 71 |
pOutputF32[totalFramesRead*CHANNEL_COUNT + iSample] += temp[iSample]; |
| 72 |
} |
| 73 |
|
| 74 |
totalFramesRead += (ma_uint32)framesReadThisIteration; |
| 75 |
|
| 76 |
if (framesReadThisIteration < (ma_uint32)framesToReadThisIteration) { |
| 77 |
break; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
return totalFramesRead; |
| 82 |
} |
| 83 |
|
| 84 |
void data_callback(ma_device* pDevice, void* pOutput, const void* pInput, ma_uint32 frameCount) |
| 85 |
{ |
| 86 |
float* pOutputF32 = (float*)pOutput; |
| 87 |
ma_uint32 iDecoder; |
| 88 |
|
| 89 |
|
| 90 |
for (iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { |
| 91 |
if (!g_pDecodersAtEnd[iDecoder]) { |
| 92 |
ma_uint32 framesRead = read_and_mix_pcm_frames_f32(&g_pDecoders[iDecoder], pOutputF32, frameCount); |
| 93 |
if (framesRead < frameCount) { |
| 94 |
g_pDecodersAtEnd[iDecoder] = MA_TRUE; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
if (are_all_decoders_at_end()) { |
| 104 |
ma_event_signal(&g_stopEvent); |
| 105 |
} |
| 106 |
|
| 107 |
(void)pInput; |
| 108 |
(void)pDevice; |
| 109 |
} |
| 110 |
|
| 111 |
int main(int argc, char** argv) |
| 112 |
{ |
| 113 |
ma_result result; |
| 114 |
ma_decoder_config decoderConfig; |
| 115 |
ma_device_config deviceConfig; |
| 116 |
ma_device device; |
| 117 |
ma_uint32 iDecoder; |
| 118 |
|
| 119 |
if (argc < 2) { |
| 120 |
printf("No input files.\n"); |
| 121 |
return -1; |
| 122 |
} |
| 123 |
|
| 124 |
g_decoderCount = argc-1; |
| 125 |
g_pDecoders = (ma_decoder*)malloc(sizeof(*g_pDecoders) * g_decoderCount); |
| 126 |
g_pDecodersAtEnd = (ma_bool32*) malloc(sizeof(*g_pDecodersAtEnd) * g_decoderCount); |
| 127 |
|
| 128 |
|
| 129 |
decoderConfig = ma_decoder_config_init(SAMPLE_FORMAT, CHANNEL_COUNT, SAMPLE_RATE); |
| 130 |
for (iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { |
| 131 |
result = ma_decoder_init_file(argv[1+iDecoder], &decoderConfig, &g_pDecoders[iDecoder]); |
| 132 |
if (result != MA_SUCCESS) { |
| 133 |
ma_uint32 iDecoder2; |
| 134 |
for (iDecoder2 = 0; iDecoder2 < iDecoder; ++iDecoder2) { |
| 135 |
ma_decoder_uninit(&g_pDecoders[iDecoder2]); |
| 136 |
} |
| 137 |
free(g_pDecoders); |
| 138 |
free(g_pDecodersAtEnd); |
| 139 |
|
| 140 |
printf("Failed to load %s.\n", argv[1+iDecoder]); |
| 141 |
return -3; |
| 142 |
} |
| 143 |
g_pDecodersAtEnd[iDecoder] = MA_FALSE; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
deviceConfig = ma_device_config_init(ma_device_type_playback); |
| 148 |
deviceConfig.playback.format = SAMPLE_FORMAT; |
| 149 |
deviceConfig.playback.channels = CHANNEL_COUNT; |
| 150 |
deviceConfig.sampleRate = SAMPLE_RATE; |
| 151 |
deviceConfig.dataCallback = data_callback; |
| 152 |
deviceConfig.pUserData = NULL; |
| 153 |
|
| 154 |
if (ma_device_init(NULL, &deviceConfig, &device) != MA_SUCCESS) { |
| 155 |
for (iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { |
| 156 |
ma_decoder_uninit(&g_pDecoders[iDecoder]); |
| 157 |
} |
| 158 |
free(g_pDecoders); |
| 159 |
free(g_pDecodersAtEnd); |
| 160 |
|
| 161 |
printf("Failed to open playback device.\n"); |
| 162 |
return -3; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
ma_event_init(&g_stopEvent); |
| 171 |
|
| 172 |
|
| 173 |
if (ma_device_start(&device) != MA_SUCCESS) { |
| 174 |
ma_device_uninit(&device); |
| 175 |
for (iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { |
| 176 |
ma_decoder_uninit(&g_pDecoders[iDecoder]); |
| 177 |
} |
| 178 |
free(g_pDecoders); |
| 179 |
free(g_pDecodersAtEnd); |
| 180 |
|
| 181 |
printf("Failed to start playback device.\n"); |
| 182 |
return -4; |
| 183 |
} |
| 184 |
|
| 185 |
printf("Waiting for playback to complete...\n"); |
| 186 |
ma_event_wait(&g_stopEvent); |
| 187 |
|
| 188 |
|
| 189 |
ma_device_uninit(&device); |
| 190 |
|
| 191 |
for (iDecoder = 0; iDecoder < g_decoderCount; ++iDecoder) { |
| 192 |
ma_decoder_uninit(&g_pDecoders[iDecoder]); |
| 193 |
} |
| 194 |
free(g_pDecoders); |
| 195 |
free(g_pDecodersAtEnd); |
| 196 |
|
| 197 |
return 0; |
| 198 |
} |