Skip to content

Commit 8587d8e

Browse files
committed
Fix voice ending in the middle of a buffer.
1 parent 2ec7f98 commit 8587d8e

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

shared-module/audioio/Mixer.c

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -244,30 +244,47 @@ audioio_get_buffer_result_t audioio_mixer_get_buffer(audioio_mixer_obj_t* self,
244244
bool voices_active = false;
245245
for (int32_t v = 0; v < self->voice_count; v++) {
246246
audioio_mixer_voice_t* voice = &self->voice[v];
247-
if (voice->sample == NULL) {
248-
continue;
249-
}
250247

251248
uint32_t j = 0;
249+
bool voice_done = voice->sample == NULL;
252250
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) {
254252
if (!voice->more_data) {
255253
if (voice->loop) {
256254
audiosample_reset_buffer(voice->sample, false, 0);
257255
} else {
258256
voice->sample = NULL;
259-
break;
257+
voice_done = true;
260258
}
261259
}
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+
}
268268
}
269269
// 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+
271288
if (!voices_active) {
272289
word_buffer[i] = sample_value;
273290
} else {
@@ -293,20 +310,6 @@ audioio_get_buffer_result_t audioio_mixer_get_buffer(audioio_mixer_obj_t* self,
293310
voices_active = true;
294311
}
295312

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-
}
310313
self->read_count += 1;
311314
} else if (!self->use_first_buffer) {
312315
*buffer = (uint8_t*) self->first_buffer;

0 commit comments

Comments
 (0)