mixer: factor out mix_one_voice

This commit is contained in:
Jeff Epler 2020-01-12 10:12:42 -06:00
parent 8b61333937
commit e6869c8983

View File

@ -295,39 +295,12 @@ static inline uint32_t mult16signed(uint32_t val, int32_t mul) {
#endif #endif
} }
audioio_get_buffer_result_t audiomixer_mixer_get_buffer(audiomixer_mixer_obj_t* self, static void mix_one_voice(audiomixer_mixer_obj_t* self,
bool single_channel, audiomixer_mixervoice_obj_t* voice, bool voices_active,
uint8_t channel, uint32_t* word_buffer, uint32_t length) {
uint8_t** buffer,
uint32_t* buffer_length) {
if (!single_channel) {
channel = 0;
}
uint32_t channel_read_count = self->left_read_count;
if (channel == 1) {
channel_read_count = self->right_read_count;
}
*buffer_length = self->len;
bool need_more_data = self->read_count == channel_read_count;
if (need_more_data) {
uint32_t* word_buffer;
if (self->use_first_buffer) {
*buffer = (uint8_t*) self->first_buffer;
word_buffer = self->first_buffer;
} else {
*buffer = (uint8_t*) self->second_buffer;
word_buffer = self->second_buffer;
}
self->use_first_buffer = !self->use_first_buffer;
bool voices_active = false;
for (int32_t v = 0; v < self->voice_count; v++) {
audiomixer_mixervoice_obj_t* voice = MP_OBJ_TO_PTR(self->voice[v]);
uint32_t j = 0; uint32_t j = 0;
bool voice_done = voice->sample == NULL; bool voice_done = voice->sample == NULL;
for (uint32_t i = 0; i < self->len / sizeof(uint32_t); i++) { for (uint32_t i = 0; i < length; i++) {
if (!voice_done && j >= voice->buffer_length) { if (!voice_done && j >= voice->buffer_length) {
if (!voice->more_data) { if (!voice->more_data) {
if (voice->loop) { if (voice->loop) {
@ -401,7 +374,39 @@ audioio_get_buffer_result_t audiomixer_mixer_get_buffer(audiomixer_mixer_obj_t*
} }
voice->buffer_length -= j; voice->buffer_length -= j;
voice->remaining_buffer += j; voice->remaining_buffer += j;
}
audioio_get_buffer_result_t audiomixer_mixer_get_buffer(audiomixer_mixer_obj_t* self,
bool single_channel,
uint8_t channel,
uint8_t** buffer,
uint32_t* buffer_length) {
if (!single_channel) {
channel = 0;
}
uint32_t channel_read_count = self->left_read_count;
if (channel == 1) {
channel_read_count = self->right_read_count;
}
*buffer_length = self->len;
bool need_more_data = self->read_count == channel_read_count;
if (need_more_data) {
uint32_t* word_buffer;
if (self->use_first_buffer) {
*buffer = (uint8_t*) self->first_buffer;
word_buffer = self->first_buffer;
} else {
*buffer = (uint8_t*) self->second_buffer;
word_buffer = self->second_buffer;
}
self->use_first_buffer = !self->use_first_buffer;
bool voices_active = false;
for (int32_t v = 0; v < self->voice_count; v++) {
audiomixer_mixervoice_obj_t* voice = MP_OBJ_TO_PTR(self->voice[v]);
mix_one_voice(self, voice, voices_active, word_buffer, self->len / sizeof(uint32_t));
voices_active = true; voices_active = true;
} }