tron@2186: /* $Id$ */ tron@2186: rubidium@10429: /** @file mixer.cpp Mixing of sound samples. */ belugas@6527: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1496: #include "mixer.h" rubidium@11137: #include "core/math_func.hpp" truelight@0: tron@1496: struct MixerChannel { truelight@0: bool active; truelight@0: belugas@6527: /* pointer to allocated buffer memory */ tron@337: int8 *memory; truelight@0: belugas@6527: /* 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: belugas@6527: /* Mixing volume */ rubidium@11137: int volume_left; rubidium@11137: int volume_right; truelight@0: truelight@0: uint flags; tron@1496: }; truelight@0: tron@2977: static MixerChannel _channels[8]; tron@2977: static uint32 _play_rate; truelight@0: rubidium@11137: /** rubidium@11137: * The theoretical maximum volume for a single sound sample. Multiple sound rubidium@11137: * samples should not exceed this limit as it will sound too loud. It also rubidium@11137: * stops overflowing when too many sounds are played at the same time, which rubidium@11137: * causes an even worse sound quality. rubidium@11137: */ rubidium@11137: static const int MAX_VOLUME = 128 * 128; rubidium@11137: 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; rubidium@11137: int volume_left; rubidium@11137: int 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) { belugas@6527: /* Special case when frac_speed is 0x10000 */ truelight@0: do { rubidium@11137: buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME); frosch@11148: buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME); truelight@0: b++; truelight@0: buffer += 2; tron@337: } while (--samples > 0); truelight@0: } else { truelight@0: do { rubidium@11137: buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME); frosch@11148: buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME); 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: tron@2977: void MxMixSamples(void *buffer, uint samples) truelight@0: { truelight@0: MixerChannel *mc; truelight@193: belugas@6527: /* Clear the buffer */ tron@337: memset(buffer, 0, sizeof(int16) * 2 * samples); truelight@193: belugas@6527: /* Mix each channel */ tron@2977: for (mc = _channels; mc != endof(_channels); mc++) { truelight@0: if (mc->active) { rubidium@5838: mix_int8_to_int16(mc, (int16*)buffer, samples); tron@337: if (mc->samples_left == 0) MxCloseChannel(mc); truelight@0: } truelight@0: } truelight@0: } truelight@0: rubidium@6573: MixerChannel *MxAllocateChannel() truelight@0: { truelight@0: MixerChannel *mc; tron@2977: for (mc = _channels; mc != endof(_channels); mc++) tron@337: if (mc->memory == NULL) { truelight@0: mc->active = false; truelight@0: return mc; truelight@0: } truelight@0: return NULL; truelight@0: } truelight@0: rubidium@10751: void MxSetChannelRawSrc(MixerChannel *mc, int8 *mem, size_t 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@2977: mc->frac_speed = (rate << 16) / _play_rate; truelight@0: belugas@6527: /* adjust the magnitude to prevent overflow */ rubidium@10751: while (size & ~0xFFFF) { tron@337: size >>= 1; tron@337: rate = (rate >> 1) + 1; tron@337: } truelight@193: rubidium@10751: mc->samples_left = (uint)size * _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: { tron@2977: _play_rate = rate; truelight@0: return true; truelight@0: }