From a18506bada9df6a528301a8eacab6082e91a7127 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Wed, 26 May 2021 15:21:48 +0200 Subject: [WIP] Speed up the mixer --- src/uxn/devices/apu.c | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/uxn/devices/apu.c b/src/uxn/devices/apu.c index ba9fe88..c3168c2 100644 --- a/src/uxn/devices/apu.c +++ b/src/uxn/devices/apu.c @@ -202,29 +202,40 @@ void sound_mix() { if (chan->data == NULL) { continue; } - for(size_t i = 0; i < AUDIO_BUF_LEN; i++) { - // Remember we are using fixed point values. - mix_buffer[i] += chan->data[chan->pos >> 12] * chan->vol; - chan->pos += chan->inc; - - if (chan->pos >= chan->length) { - // If looping is not active disable the channel. - if (chan->loop_length == 0) { - chan->data = NULL; - break; - } - - // Loop the sample. - while (chan->pos >= chan->length) { - chan->pos -= chan->loop_length; + if (chan->pos + chan->inc * AUDIO_BUF_LEN >= chan->length) { + // Sample is going to finish, need to consider this for looping or + // stopping. + for(size_t i = 0; i < AUDIO_BUF_LEN; i++) { + // Remember we are using fixed point values. + mix_buffer[i] += chan->data[chan->pos >> 12] * chan->vol; + chan->pos += chan->inc; + + if (chan->pos >= chan->length) { + // If looping is not active disable the channel. + if (chan->loop_length == 0) { + chan->data = NULL; + break; + } + + // Loop the sample. + while (chan->pos >= chan->length) { + chan->pos -= chan->loop_length; + } } } + } else { + // Sample still have room to go, no need to check for looping or + // end of sample. + for(size_t i = 0; i < AUDIO_BUF_LEN; i++) { + mix_buffer[i] += chan->data[chan->pos>>12] * chan->vol; + chan->pos += chan->inc; + } } } // Downsample and copy to the playing buffer. for (size_t i = 0; i < AUDIO_BUF_LEN; ++i) { - // >> 8 to divide off the volume, >> 2 to divide by 4 channels to + // >> 6 to divide off the volume, >> 2 to divide by 4 channels to // prevent overflow. audio.current_buffer[i] = mix_buffer[i] >> 8; } -- cgit v1.2.1