tron@1496: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1496: #include "mixer.h" tron@1496: tron@1496: struct MixerChannel { tron@1496: // Mixer tron@1496: Mixer *mx; tron@1496: bool active; tron@1496: tron@1496: // pointer to allocated buffer memory tron@1496: int8 *memory; tron@1496: tron@1496: // current position in memory tron@1496: uint32 pos; tron@1496: uint32 frac_pos; tron@1496: uint32 frac_speed; tron@1496: uint32 samples_left; tron@1496: tron@1496: // Mixing volume tron@1496: uint volume_left; tron@1496: uint volume_right; tron@1496: tron@1496: uint flags; tron@1496: }; tron@1496: tron@1496: struct Mixer { tron@1496: uint32 play_rate; tron@1496: MixerChannel channels[8]; tron@1496: }; tron@1496: tron@1496: tron@1496: static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples) tron@1496: { tron@1496: int8 *b; tron@1496: uint32 frac_pos; tron@1496: uint32 frac_speed; tron@1496: uint volume_left; tron@1496: uint volume_right; tron@1496: tron@1496: if (samples > sc->samples_left) samples = sc->samples_left; tron@1496: sc->samples_left -= samples; tron@1496: assert(samples > 0); tron@1496: tron@1496: b = sc->memory + sc->pos; tron@1496: frac_pos = sc->frac_pos; tron@1496: frac_speed = sc->frac_speed; tron@1496: volume_left = sc->volume_left; tron@1496: volume_right = sc->volume_right; tron@1496: tron@1496: if (frac_speed == 0x10000) { tron@1496: // Special case when frac_speed is 0x10000 tron@1496: do { tron@1496: buffer[0] += *b * volume_left >> 8; tron@1496: buffer[1] += *b * volume_right >> 8; tron@1496: b++; tron@1496: buffer += 2; tron@1496: } while (--samples > 0); tron@1496: } else { tron@1496: do { tron@1496: buffer[0] += *b * volume_left >> 8; tron@1496: buffer[1] += *b * volume_right >> 8; tron@1496: buffer += 2; tron@1496: frac_pos += frac_speed; tron@1496: b += frac_pos >> 16; tron@1496: frac_pos &= 0xffff; tron@1496: } while (--samples > 0); tron@1496: } tron@1496: tron@1496: sc->frac_pos = frac_pos; tron@1496: sc->pos = b - sc->memory; tron@1496: } tron@1496: tron@1496: static void MxCloseChannel(MixerChannel *mc) tron@1496: { tron@1496: if (mc->flags & MX_AUTOFREE) free(mc->memory); tron@1497: mc->active = false; tron@1496: mc->memory = NULL; tron@1496: } tron@1496: tron@1496: void MxMixSamples(Mixer *mx, void *buffer, uint samples) tron@1496: { tron@1496: MixerChannel *mc; tron@1496: tron@1496: // Clear the buffer tron@1496: memset(buffer, 0, sizeof(int16) * 2 * samples); tron@1496: tron@1496: // Mix each channel tron@1496: for (mc = mx->channels; mc != endof(mx->channels); mc++) { tron@1496: if (mc->active) { tron@1496: mix_int8_to_int16(mc, buffer, samples); tron@1496: if (mc->samples_left == 0) MxCloseChannel(mc); tron@1496: } tron@1496: } tron@1496: tron@1496: #if 0 tron@1496: { tron@1496: static FILE *out = NULL; tron@1496: if (out == NULL) tron@1496: out = fopen("d:\\dump.raw", "wb"); tron@1496: fwrite(buffer, samples * 4, 1, out); tron@1496: } tron@1496: #endif tron@1496: } tron@1496: tron@1496: MixerChannel *MxAllocateChannel(Mixer *mx) tron@1496: { tron@1496: MixerChannel *mc; tron@1496: for (mc = mx->channels; mc != endof(mx->channels); mc++) tron@1496: if (mc->memory == NULL) { tron@1496: mc->active = false; tron@1496: mc->mx = mx; tron@1496: return mc; tron@1496: } tron@1496: return NULL; tron@1496: } tron@1496: tron@1496: void MxSetChannelRawSrc(MixerChannel *mc, int8 *mem, uint size, uint rate, uint flags) tron@1496: { tron@1496: mc->memory = mem; tron@1496: mc->flags = flags; tron@1496: mc->frac_pos = 0; tron@1496: mc->pos = 0; tron@1496: tron@1496: mc->frac_speed = (rate << 16) / mc->mx->play_rate; tron@1496: tron@1496: // adjust the magnitude to prevent overflow tron@1496: while (size & 0xFFFF0000) { tron@1496: size >>= 1; tron@1496: rate = (rate >> 1) + 1; tron@1496: } tron@1496: tron@1496: mc->samples_left = size * mc->mx->play_rate / rate; tron@1496: } tron@1496: tron@1496: void MxSetChannelVolume(MixerChannel *mc, uint left, uint right) tron@1496: { tron@1496: mc->volume_left = left; tron@1496: mc->volume_right = right; tron@1496: } tron@1496: tron@1496: tron@1498: void MxActivateChannel(MixerChannel* mc) tron@1496: { tron@1496: mc->active = true; tron@1496: } tron@1496: tron@1496: tron@1496: bool MxInitialize(uint rate) tron@1496: { tron@1496: static Mixer mx; tron@1496: _mixer = &mx; tron@1496: mx.play_rate = rate; tron@1496: return true; tron@1496: }