2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file mixer.cpp Mixing of sound samples. */
|
2007-03-03 04:04:22 +00:00
|
|
|
|
2005-03-12 09:38:03 +00:00
|
|
|
#include "stdafx.h"
|
2010-03-06 11:08:31 +00:00
|
|
|
#include <math.h>
|
2008-07-12 22:30:10 +00:00
|
|
|
#include "core/math_func.hpp"
|
2018-07-19 19:17:07 +00:00
|
|
|
#include "framerate_type.h"
|
2021-04-05 10:18:59 +00:00
|
|
|
#include "settings_type.h"
|
2005-03-12 09:38:03 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
2018-11-04 11:58:42 +00:00
|
|
|
#include "mixer.h"
|
2014-04-23 20:13:33 +00:00
|
|
|
|
2005-03-12 09:38:03 +00:00
|
|
|
struct MixerChannel {
|
|
|
|
bool active;
|
|
|
|
|
2007-03-03 04:04:22 +00:00
|
|
|
/* pointer to allocated buffer memory */
|
2005-03-12 09:38:03 +00:00
|
|
|
int8 *memory;
|
|
|
|
|
2007-03-03 04:04:22 +00:00
|
|
|
/* current position in memory */
|
2005-03-12 09:38:03 +00:00
|
|
|
uint32 pos;
|
|
|
|
uint32 frac_pos;
|
|
|
|
uint32 frac_speed;
|
|
|
|
uint32 samples_left;
|
|
|
|
|
2007-03-03 04:04:22 +00:00
|
|
|
/* Mixing volume */
|
2008-07-12 22:30:10 +00:00
|
|
|
int volume_left;
|
|
|
|
int volume_right;
|
2009-08-09 23:04:08 +00:00
|
|
|
|
|
|
|
bool is16bit;
|
2005-03-12 09:38:03 +00:00
|
|
|
};
|
|
|
|
|
2006-02-05 09:56:04 +00:00
|
|
|
static MixerChannel _channels[8];
|
2009-07-10 18:22:04 +00:00
|
|
|
static uint32 _play_rate = 11025;
|
2010-01-16 22:15:02 +00:00
|
|
|
static uint32 _max_size = UINT_MAX;
|
2019-04-10 21:07:06 +00:00
|
|
|
static MxStreamCallback _music_stream = nullptr;
|
2005-03-12 09:38:03 +00:00
|
|
|
|
2008-07-12 22:30:10 +00:00
|
|
|
/**
|
|
|
|
* The theoretical maximum volume for a single sound sample. Multiple sound
|
|
|
|
* samples should not exceed this limit as it will sound too loud. It also
|
|
|
|
* stops overflowing when too many sounds are played at the same time, which
|
|
|
|
* causes an even worse sound quality.
|
|
|
|
*/
|
2021-04-04 18:13:08 +00:00
|
|
|
static const int MAX_VOLUME = 32767;
|
2008-07-12 22:30:10 +00:00
|
|
|
|
2009-08-10 17:46:44 +00:00
|
|
|
/**
|
|
|
|
* Perform the rate conversion between the input and output.
|
|
|
|
* @param b the buffer to read the data from
|
|
|
|
* @param frac_pos the position from the begin of the buffer till the next element
|
|
|
|
* @tparam T the size of the buffer (8 or 16 bits)
|
|
|
|
* @return the converted value.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static int RateConversion(T *b, int frac_pos)
|
|
|
|
{
|
|
|
|
return ((b[0] * ((1 << 16) - frac_pos)) + (b[1] * frac_pos)) >> 16;
|
|
|
|
}
|
|
|
|
|
2021-04-05 10:18:59 +00:00
|
|
|
static void mix_int16(MixerChannel *sc, int16 *buffer, uint samples, uint8 effect_vol)
|
2009-08-09 23:04:08 +00:00
|
|
|
{
|
|
|
|
if (samples > sc->samples_left) samples = sc->samples_left;
|
|
|
|
sc->samples_left -= samples;
|
|
|
|
assert(samples > 0);
|
|
|
|
|
2010-01-16 20:59:58 +00:00
|
|
|
const int16 *b = (const int16 *)sc->memory + sc->pos;
|
|
|
|
uint32 frac_pos = sc->frac_pos;
|
|
|
|
uint32 frac_speed = sc->frac_speed;
|
2021-04-05 10:18:59 +00:00
|
|
|
int volume_left = sc->volume_left * effect_vol / 255;
|
|
|
|
int volume_right = sc->volume_right * effect_vol / 255;
|
2009-08-09 23:04:08 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
b++;
|
|
|
|
buffer += 2;
|
|
|
|
} while (--samples > 0);
|
|
|
|
} else {
|
|
|
|
do {
|
2009-08-10 17:46:44 +00:00
|
|
|
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);
|
2009-08-09 23:04:08 +00:00
|
|
|
buffer += 2;
|
|
|
|
frac_pos += frac_speed;
|
|
|
|
b += frac_pos >> 16;
|
|
|
|
frac_pos &= 0xffff;
|
|
|
|
} while (--samples > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
sc->frac_pos = frac_pos;
|
2010-01-16 20:59:58 +00:00
|
|
|
sc->pos = b - (const int16 *)sc->memory;
|
2009-08-09 23:04:08 +00:00
|
|
|
}
|
2005-03-12 09:38:03 +00:00
|
|
|
|
2021-04-05 10:18:59 +00:00
|
|
|
static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples, uint8 effect_vol)
|
2005-03-12 09:38:03 +00:00
|
|
|
{
|
|
|
|
if (samples > sc->samples_left) samples = sc->samples_left;
|
|
|
|
sc->samples_left -= samples;
|
|
|
|
assert(samples > 0);
|
|
|
|
|
2010-01-16 20:59:58 +00:00
|
|
|
const int8 *b = sc->memory + sc->pos;
|
|
|
|
uint32 frac_pos = sc->frac_pos;
|
|
|
|
uint32 frac_speed = sc->frac_speed;
|
2021-04-05 10:18:59 +00:00
|
|
|
int volume_left = sc->volume_left * effect_vol / 255;
|
|
|
|
int volume_right = sc->volume_right * effect_vol / 255;
|
2005-03-12 09:38:03 +00:00
|
|
|
|
|
|
|
if (frac_speed == 0x10000) {
|
2007-03-03 04:04:22 +00:00
|
|
|
/* Special case when frac_speed is 0x10000 */
|
2005-03-12 09:38:03 +00:00
|
|
|
do {
|
2008-07-12 22:30:10 +00:00
|
|
|
buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME);
|
2008-07-15 17:13:50 +00:00
|
|
|
buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME);
|
2005-03-12 09:38:03 +00:00
|
|
|
b++;
|
|
|
|
buffer += 2;
|
|
|
|
} while (--samples > 0);
|
|
|
|
} else {
|
|
|
|
do {
|
2009-08-10 17:46:44 +00:00
|
|
|
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);
|
2005-03-12 09:38:03 +00:00
|
|
|
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(MixerChannel *mc)
|
|
|
|
{
|
|
|
|
mc->active = false;
|
|
|
|
}
|
|
|
|
|
2006-02-05 09:56:04 +00:00
|
|
|
void MxMixSamples(void *buffer, uint samples)
|
2005-03-12 09:38:03 +00:00
|
|
|
{
|
2018-07-19 19:17:07 +00:00
|
|
|
PerformanceMeasurer framerate(PFE_SOUND);
|
|
|
|
static uint last_samples = 0;
|
|
|
|
if (samples != last_samples) {
|
|
|
|
framerate.SetExpectedRate((double)_play_rate / samples);
|
|
|
|
last_samples = samples;
|
|
|
|
}
|
|
|
|
|
2005-03-12 09:38:03 +00:00
|
|
|
MixerChannel *mc;
|
|
|
|
|
2007-03-03 04:04:22 +00:00
|
|
|
/* Clear the buffer */
|
2005-03-12 09:38:03 +00:00
|
|
|
memset(buffer, 0, sizeof(int16) * 2 * samples);
|
|
|
|
|
2018-11-04 11:58:42 +00:00
|
|
|
/* Fetch music if a sampled stream is available */
|
|
|
|
if (_music_stream) _music_stream((int16*)buffer, samples);
|
|
|
|
|
2021-04-04 14:58:06 +00:00
|
|
|
/* Apply simple x^3 scaling to master effect volume. This increases the
|
|
|
|
* perceived difference in loudness to better match expectations. effect_vol
|
|
|
|
* is expected to be in the range 0-127 hence the division by 127 * 127 to
|
|
|
|
* get back into range. */
|
|
|
|
uint8 effect_vol = (_settings_client.music.effect_vol *
|
|
|
|
_settings_client.music.effect_vol *
|
|
|
|
_settings_client.music.effect_vol) / (127 * 127);
|
2021-04-05 10:18:59 +00:00
|
|
|
|
2007-03-03 04:04:22 +00:00
|
|
|
/* Mix each channel */
|
2006-02-05 09:56:04 +00:00
|
|
|
for (mc = _channels; mc != endof(_channels); mc++) {
|
2005-03-12 09:38:03 +00:00
|
|
|
if (mc->active) {
|
2009-08-09 23:04:08 +00:00
|
|
|
if (mc->is16bit) {
|
2021-04-05 10:18:59 +00:00
|
|
|
mix_int16(mc, (int16*)buffer, samples, effect_vol);
|
2009-08-09 23:04:08 +00:00
|
|
|
} else {
|
2021-04-05 10:18:59 +00:00
|
|
|
mix_int8_to_int16(mc, (int16*)buffer, samples, effect_vol);
|
2009-08-09 23:04:08 +00:00
|
|
|
}
|
2005-03-12 09:38:03 +00:00
|
|
|
if (mc->samples_left == 0) MxCloseChannel(mc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
MixerChannel *MxAllocateChannel()
|
2005-03-12 09:38:03 +00:00
|
|
|
{
|
|
|
|
MixerChannel *mc;
|
2010-07-24 10:14:39 +00:00
|
|
|
for (mc = _channels; mc != endof(_channels); mc++) {
|
2010-01-16 17:47:26 +00:00
|
|
|
if (!mc->active) {
|
|
|
|
free(mc->memory);
|
2019-04-10 21:07:06 +00:00
|
|
|
mc->memory = nullptr;
|
2005-03-12 09:38:03 +00:00
|
|
|
return mc;
|
|
|
|
}
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2005-03-12 09:38:03 +00:00
|
|
|
}
|
|
|
|
|
2009-08-09 23:04:08 +00:00
|
|
|
void MxSetChannelRawSrc(MixerChannel *mc, int8 *mem, size_t size, uint rate, bool is16bit)
|
2005-03-12 09:38:03 +00:00
|
|
|
{
|
|
|
|
mc->memory = mem;
|
|
|
|
mc->frac_pos = 0;
|
|
|
|
mc->pos = 0;
|
|
|
|
|
2006-02-05 09:56:04 +00:00
|
|
|
mc->frac_speed = (rate << 16) / _play_rate;
|
2005-03-12 09:38:03 +00:00
|
|
|
|
2010-01-16 22:15:02 +00:00
|
|
|
if (is16bit) size /= 2;
|
|
|
|
|
2007-03-03 04:04:22 +00:00
|
|
|
/* adjust the magnitude to prevent overflow */
|
2010-01-16 22:15:02 +00:00
|
|
|
while (size >= _max_size) {
|
2005-03-12 09:38:03 +00:00
|
|
|
size >>= 1;
|
|
|
|
rate = (rate >> 1) + 1;
|
|
|
|
}
|
|
|
|
|
2010-01-16 22:15:02 +00:00
|
|
|
mc->samples_left = (uint)size * _play_rate / rate;
|
2009-08-09 23:04:08 +00:00
|
|
|
mc->is16bit = is16bit;
|
2005-03-12 09:38:03 +00:00
|
|
|
}
|
|
|
|
|
2010-03-06 11:08:31 +00:00
|
|
|
/**
|
|
|
|
* Set volume and pan parameters for a sound.
|
|
|
|
* @param mc MixerChannel to set
|
|
|
|
* @param volume Volume level for sound, range is 0..16384
|
|
|
|
* @param pan Pan position for sound, range is 0..1
|
|
|
|
*/
|
|
|
|
void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan)
|
2005-03-12 09:38:03 +00:00
|
|
|
{
|
2010-03-06 11:08:31 +00:00
|
|
|
/* Use sinusoidal pan to maintain overall sound power level regardless
|
|
|
|
* of position. */
|
2010-03-23 19:48:24 +00:00
|
|
|
mc->volume_left = (uint)(sin((1.0 - pan) * M_PI / 2.0) * volume);
|
|
|
|
mc->volume_right = (uint)(sin(pan * M_PI / 2.0) * volume);
|
2005-03-12 09:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-10 00:31:47 +00:00
|
|
|
void MxActivateChannel(MixerChannel *mc)
|
2005-03-12 09:38:03 +00:00
|
|
|
{
|
|
|
|
mc->active = true;
|
2018-11-04 11:58:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set source of PCM music
|
|
|
|
* @param music_callback Function that will be called to fill sample buffers with music data.
|
|
|
|
* @return Sample rate of mixer, which the buffers supplied to the callback must be rendered at.
|
|
|
|
*/
|
|
|
|
uint32 MxSetMusicSource(MxStreamCallback music_callback)
|
|
|
|
{
|
|
|
|
_music_stream = music_callback;
|
|
|
|
return _play_rate;
|
2005-03-12 09:38:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool MxInitialize(uint rate)
|
|
|
|
{
|
2006-02-05 09:56:04 +00:00
|
|
|
_play_rate = rate;
|
2010-01-16 22:15:02 +00:00
|
|
|
_max_size = UINT_MAX / _play_rate;
|
2019-04-10 21:07:06 +00:00
|
|
|
_music_stream = nullptr; /* rate may have changed, any music source is now invalid */
|
2005-03-12 09:38:03 +00:00
|
|
|
return true;
|
|
|
|
}
|