Debian Codegraph @

examples/simple_mixing.c

raw symbols SCIP
1 /*
2 Demonstrates one way to load multiple files and play them all back at the same time.
3
4 When mixing multiple sounds together, you should not create multiple devices. Instead you should create only a single
5 device and then mix your sounds together which you can do by simply summing their samples together. The simplest way to
6 do this is to use floating point samples and use miniaudio's built-in clipper to handling clipping for you. (Clipping
7 is when sample are clamped to their minimum and maximum range, which for floating point is -1..1.)
8
9 ```
10 Usage: simple_mixing [input file 0] [input file 1] ... [input file n]
11 Example: simple_mixing file1.wav file2.flac
12 ```
13 */
14 #include "../miniaudio.c"
15
16 #include <stdio.h>
17
18 /*
19 For simplicity, this example requires the device to use floating point samples.
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; /* <-- Signaled by the audio thread, waited on by the main thread. */
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 The way mixing works is that we just read into a temporary buffer, then take the contents of that buffer and mix it with the
47 contents of the output buffer by simply adding the samples together. You could also clip the samples to -1..+1, but I'm not
48 doing that in this example.
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 /* Mix the frames together. */
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; /* Reached EOF. */
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 /* This example assumes the device was configured to use ma_format_f32. */
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 If at the end all of our decoders are at the end we need to stop. We cannot stop the device in the callback. Instead we need to
101 signal an event to indicate that it's stopped. The main thread will be waiting on the event, after which it will stop the device.
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 /* In this example, all decoders need to have the same output format. */
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 /* Create only a single device. The decoders will be mixed together in the callback. In this example the data format needs to be the same as the decoders. */
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 We can't stop in the audio thread so we instead need to use an event. We wait on this thread in the main thread, and signal it in the audio thread. This
167 needs to be done before starting the device. We need a context to initialize the event, which we can get from the device. Alternatively you can initialize
168 a context separately, but we don't need to do that for this example.
169 */
170 ma_event_init(&g_stopEvent);
171
172 /* Now we start playback and wait for the audio thread to tell us to stop. */
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 /* Getting here means the audio thread has signaled that the device should be stopped. */
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 }