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