@@ -244,30 +244,47 @@ audioio_get_buffer_result_t audioio_mixer_get_buffer(audioio_mixer_obj_t* self,
244
244
bool voices_active = false;
245
245
for (int32_t v = 0 ; v < self -> voice_count ; v ++ ) {
246
246
audioio_mixer_voice_t * voice = & self -> voice [v ];
247
- if (voice -> sample == NULL ) {
248
- continue ;
249
- }
250
247
251
248
uint32_t j = 0 ;
249
+ bool voice_done = voice -> sample == NULL ;
252
250
for (uint32_t i = 0 ; i < self -> len / sizeof (uint32_t ); i ++ ) {
253
- if (j >= voice -> buffer_length ) {
251
+ if (! voice_done && j >= voice -> buffer_length ) {
254
252
if (!voice -> more_data ) {
255
253
if (voice -> loop ) {
256
254
audiosample_reset_buffer (voice -> sample , false, 0 );
257
255
} else {
258
256
voice -> sample = NULL ;
259
- break ;
257
+ voice_done = true ;
260
258
}
261
259
}
262
- // Load another buffer
263
- audioio_get_buffer_result_t result = audiosample_get_buffer (voice -> sample , false, 0 , (uint8_t * * ) & voice -> remaining_buffer , & voice -> buffer_length );
264
- // Track length in terms of words.
265
- voice -> buffer_length /= sizeof (uint32_t );
266
- voice -> more_data = result == GET_BUFFER_MORE_DATA ;
267
- j = 0 ;
260
+ if (!voice_done ) {
261
+ // Load another buffer
262
+ audioio_get_buffer_result_t result = audiosample_get_buffer (voice -> sample , false, 0 , (uint8_t * * ) & voice -> remaining_buffer , & voice -> buffer_length );
263
+ // Track length in terms of words.
264
+ voice -> buffer_length /= sizeof (uint32_t );
265
+ voice -> more_data = result == GET_BUFFER_MORE_DATA ;
266
+ j = 0 ;
267
+ }
268
268
}
269
269
// First active voice gets copied over verbatim.
270
- uint32_t sample_value = voice -> remaining_buffer [j ];
270
+ uint32_t sample_value ;
271
+ if (voice_done ) {
272
+ // Exit early if another voice already set all samples once.
273
+ if (voices_active ) {
274
+ continue ;
275
+ }
276
+ sample_value = 0 ;
277
+ if (!self -> samples_signed ) {
278
+ if (self -> bits_per_sample == 8 ) {
279
+ sample_value = 0x7f7f7f7f ;
280
+ } else {
281
+ sample_value = 0x7fff7fff ;
282
+ }
283
+ }
284
+ } else {
285
+ sample_value = voice -> remaining_buffer [j ];
286
+ }
287
+
271
288
if (!voices_active ) {
272
289
word_buffer [i ] = sample_value ;
273
290
} else {
@@ -293,20 +310,6 @@ audioio_get_buffer_result_t audioio_mixer_get_buffer(audioio_mixer_obj_t* self,
293
310
voices_active = true;
294
311
}
295
312
296
- // No voice is active so zero out the signal.
297
- if (!voices_active ) {
298
- uint32_t zero = 0 ;
299
- if (!self -> samples_signed ) {
300
- if (self -> bits_per_sample == 8 ) {
301
- zero = 0x7f7f7f7f ;
302
- } else {
303
- zero = 0x7fff7fff ;
304
- }
305
- }
306
- for (uint32_t i = 0 ; i < self -> len / sizeof (uint32_t ); i ++ ) {
307
- word_buffer [i ] = zero ;
308
- }
309
- }
310
313
self -> read_count += 1 ;
311
314
} else if (!self -> use_first_buffer ) {
312
315
* buffer = (uint8_t * ) self -> first_buffer ;
0 commit comments