Skip to content

Commit

Permalink
Codechange: Template input buffer type in audio mixer.
Browse files Browse the repository at this point in the history
This reduces code duplication.
  • Loading branch information
PeterN committed Nov 27, 2023
1 parent a856fbe commit cfa88e9
Showing 1 changed file with 9 additions and 44 deletions.
53 changes: 9 additions & 44 deletions src/mixer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ static int RateConversion(T *b, int frac_pos)
return ((b[0] * ((1 << 16) - frac_pos)) + (b[1] * frac_pos)) >> 16;
}

template <typename T, uint TShift>
static void mix_int16(MixerChannel *sc, int16_t *buffer, uint samples, uint8_t effect_vol)
{
if (samples > sc->samples_left) samples = sc->samples_left;
sc->samples_left -= samples;
assert(samples > 0);

const int16_t *b = (const int16_t *)sc->memory + sc->pos;
const T *b = (const T *)sc->memory + sc->pos;
uint32_t frac_pos = sc->frac_pos;
uint32_t frac_speed = sc->frac_speed;
int volume_left = sc->volume_left * effect_vol / 255;
Expand All @@ -78,16 +79,16 @@ static void mix_int16(MixerChannel *sc, int16_t *buffer, uint samples, uint8_t e
if (frac_speed == 0x10000) {
/* Special case when frac_speed is 0x10000 */
do {
buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 16), -MAX_VOLUME, MAX_VOLUME);
buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 16), -MAX_VOLUME, MAX_VOLUME);
buffer[0] = Clamp(buffer[0] + (*b * volume_left >> TShift), -MAX_VOLUME, MAX_VOLUME);
buffer[1] = Clamp(buffer[1] + (*b * volume_right >> TShift), -MAX_VOLUME, MAX_VOLUME);
b++;
buffer += 2;
} while (--samples > 0);
} else {
do {
int data = RateConversion(b, frac_pos);
buffer[0] = Clamp(buffer[0] + (data * volume_left >> 16), -MAX_VOLUME, MAX_VOLUME);
buffer[1] = Clamp(buffer[1] + (data * volume_right >> 16), -MAX_VOLUME, MAX_VOLUME);
buffer[0] = Clamp(buffer[0] + (data * volume_left >> TShift), -MAX_VOLUME, MAX_VOLUME);
buffer[1] = Clamp(buffer[1] + (data * volume_right >> TShift), -MAX_VOLUME, MAX_VOLUME);
buffer += 2;
frac_pos += frac_speed;
b += frac_pos >> 16;
Expand All @@ -96,43 +97,7 @@ static void mix_int16(MixerChannel *sc, int16_t *buffer, uint samples, uint8_t e
}

sc->frac_pos = frac_pos;
sc->pos = b - (const int16_t *)sc->memory;
}

static void mix_int8_to_int16(MixerChannel *sc, int16_t *buffer, uint samples, uint8_t effect_vol)
{
if (samples > sc->samples_left) samples = sc->samples_left;
sc->samples_left -= samples;
assert(samples > 0);

const int8_t *b = sc->memory + sc->pos;
uint32_t frac_pos = sc->frac_pos;
uint32_t frac_speed = sc->frac_speed;
int volume_left = sc->volume_left * effect_vol / 255;
int volume_right = sc->volume_right * effect_vol / 255;

if (frac_speed == 0x10000) {
/* Special case when frac_speed is 0x10000 */
do {
buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME);
buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME);
b++;
buffer += 2;
} while (--samples > 0);
} else {
do {
int data = RateConversion(b, frac_pos);
buffer[0] = Clamp(buffer[0] + (data * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME);
buffer[1] = Clamp(buffer[1] + (data * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME);
buffer += 2;
frac_pos += frac_speed;
b += frac_pos >> 16;
frac_pos &= 0xffff;
} while (--samples > 0);
}

sc->frac_pos = frac_pos;
sc->pos = b - sc->memory;
sc->pos = b - (const T *)sc->memory;
}

static void MxCloseChannel(uint8_t channel_index)
Expand Down Expand Up @@ -172,9 +137,9 @@ void MxMixSamples(void *buffer, uint samples)
for (uint8_t idx : SetBitIterator(active)) {
MixerChannel *mc = &_channels[idx];
if (mc->is16bit) {
mix_int16(mc, (int16_t*)buffer, samples, effect_vol);
mix_int16<int16_t, 16>(mc, (int16_t*)buffer, samples, effect_vol);
} else {
mix_int8_to_int16(mc, (int16_t*)buffer, samples, effect_vol);
mix_int16<int8_t, 8>(mc, (int16_t*)buffer, samples, effect_vol);
}
if (mc->samples_left == 0) MxCloseChannel(idx);
}
Expand Down

0 comments on commit cfa88e9

Please sign in to comment.