mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: Template input buffer type in audio mixer. (#11505)
This reduces code duplication.
This commit is contained in:
parent
e5aed24395
commit
3df7d1aff9
|
@ -63,13 +63,17 @@ static int RateConversion(T *b, int frac_pos)
|
||||||
return ((b[0] * ((1 << 16) - frac_pos)) + (b[1] * frac_pos)) >> 16;
|
return ((b[0] * ((1 << 16) - frac_pos)) + (b[1] * frac_pos)) >> 16;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
static void mix_int16(MixerChannel *sc, int16_t *buffer, uint samples, uint8_t effect_vol)
|
static void mix_int16(MixerChannel *sc, int16_t *buffer, uint samples, uint8_t effect_vol)
|
||||||
{
|
{
|
||||||
|
/* Shift required to get sample value into range for the data type. */
|
||||||
|
const uint SHIFT = sizeof(T) * CHAR_BIT;
|
||||||
|
|
||||||
if (samples > sc->samples_left) samples = sc->samples_left;
|
if (samples > sc->samples_left) samples = sc->samples_left;
|
||||||
sc->samples_left -= samples;
|
sc->samples_left -= samples;
|
||||||
assert(samples > 0);
|
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_pos = sc->frac_pos;
|
||||||
uint32_t frac_speed = sc->frac_speed;
|
uint32_t frac_speed = sc->frac_speed;
|
||||||
int volume_left = sc->volume_left * effect_vol / 255;
|
int volume_left = sc->volume_left * effect_vol / 255;
|
||||||
|
@ -78,16 +82,16 @@ static void mix_int16(MixerChannel *sc, int16_t *buffer, uint samples, uint8_t e
|
||||||
if (frac_speed == 0x10000) {
|
if (frac_speed == 0x10000) {
|
||||||
/* Special case when frac_speed is 0x10000 */
|
/* Special case when frac_speed is 0x10000 */
|
||||||
do {
|
do {
|
||||||
buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 16), -MAX_VOLUME, MAX_VOLUME);
|
buffer[0] = Clamp(buffer[0] + (*b * volume_left >> SHIFT), -MAX_VOLUME, MAX_VOLUME);
|
||||||
buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 16), -MAX_VOLUME, MAX_VOLUME);
|
buffer[1] = Clamp(buffer[1] + (*b * volume_right >> SHIFT), -MAX_VOLUME, MAX_VOLUME);
|
||||||
b++;
|
b++;
|
||||||
buffer += 2;
|
buffer += 2;
|
||||||
} while (--samples > 0);
|
} while (--samples > 0);
|
||||||
} else {
|
} else {
|
||||||
do {
|
do {
|
||||||
int data = RateConversion(b, frac_pos);
|
int data = RateConversion(b, frac_pos);
|
||||||
buffer[0] = Clamp(buffer[0] + (data * volume_left >> 16), -MAX_VOLUME, MAX_VOLUME);
|
buffer[0] = Clamp(buffer[0] + (data * volume_left >> SHIFT), -MAX_VOLUME, MAX_VOLUME);
|
||||||
buffer[1] = Clamp(buffer[1] + (data * volume_right >> 16), -MAX_VOLUME, MAX_VOLUME);
|
buffer[1] = Clamp(buffer[1] + (data * volume_right >> SHIFT), -MAX_VOLUME, MAX_VOLUME);
|
||||||
buffer += 2;
|
buffer += 2;
|
||||||
frac_pos += frac_speed;
|
frac_pos += frac_speed;
|
||||||
b += frac_pos >> 16;
|
b += frac_pos >> 16;
|
||||||
|
@ -96,43 +100,7 @@ static void mix_int16(MixerChannel *sc, int16_t *buffer, uint samples, uint8_t e
|
||||||
}
|
}
|
||||||
|
|
||||||
sc->frac_pos = frac_pos;
|
sc->frac_pos = frac_pos;
|
||||||
sc->pos = b - (const int16_t *)sc->memory;
|
sc->pos = b - (const 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void MxCloseChannel(uint8_t channel_index)
|
static void MxCloseChannel(uint8_t channel_index)
|
||||||
|
@ -172,9 +140,9 @@ void MxMixSamples(void *buffer, uint samples)
|
||||||
for (uint8_t idx : SetBitIterator(active)) {
|
for (uint8_t idx : SetBitIterator(active)) {
|
||||||
MixerChannel *mc = &_channels[idx];
|
MixerChannel *mc = &_channels[idx];
|
||||||
if (mc->is16bit) {
|
if (mc->is16bit) {
|
||||||
mix_int16(mc, (int16_t*)buffer, samples, effect_vol);
|
mix_int16<int16_t>(mc, (int16_t*)buffer, samples, effect_vol);
|
||||||
} else {
|
} else {
|
||||||
mix_int8_to_int16(mc, (int16_t*)buffer, samples, effect_vol);
|
mix_int16<int8_t>(mc, (int16_t*)buffer, samples, effect_vol);
|
||||||
}
|
}
|
||||||
if (mc->samples_left == 0) MxCloseChannel(idx);
|
if (mc->samples_left == 0) MxCloseChannel(idx);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue