Files @ r28630:fe3c5717ad2b
Branch filter:

Location: cpp/openttd-patchpack/source/src/mixer.cpp - annotation

Loïc Guilloux
Fix: [Script] Apply random deviation only at script start (#11944)
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r12768:980ae0491352
r9111:983de9c5a848
r6201:2e76eb9a1d7a
r5584:545d748cc681
r25851:c17ed4425175
r26535:bbf17c1357f1
r9632:ffa59673c0a8
r22959:8c769e762c2f
r27604:32b3e0a5692e
r25118:c8e90e7c7a37
r5584:545d748cc681
r21383:942c32fb8b0e
r21383:942c32fb8b0e
r5584:545d748cc681
r6201:2e76eb9a1d7a
r27737:728d55b97775
r5584:545d748cc681
r6201:2e76eb9a1d7a
r27737:728d55b97775
r27737:728d55b97775
r27737:728d55b97775
r27737:728d55b97775
r5584:545d748cc681
r6201:2e76eb9a1d7a
r9632:ffa59673c0a8
r9632:ffa59673c0a8
r12675:08178fc9d621
r12675:08178fc9d621
r5584:545d748cc681
r5584:545d748cc681
r27737:728d55b97775
r5584:545d748cc681
r27737:728d55b97775
r27737:728d55b97775
r23607:36c15679007d
r25851:c17ed4425175
r27737:728d55b97775
r5584:545d748cc681
r9632:ffa59673c0a8
r9632:ffa59673c0a8
r9632:ffa59673c0a8
r9632:ffa59673c0a8
r9632:ffa59673c0a8
r9632:ffa59673c0a8
r25125:6fc9a3029944
r9632:ffa59673c0a8
r12679:1015c7a9aebf
r12679:1015c7a9aebf
r12679:1015c7a9aebf
r12679:1015c7a9aebf
r12679:1015c7a9aebf
r12679:1015c7a9aebf
r12679:1015c7a9aebf
r12679:1015c7a9aebf
r12679:1015c7a9aebf
r12679:1015c7a9aebf
r12679:1015c7a9aebf
r12679:1015c7a9aebf
r12679:1015c7a9aebf
r28190:29617a715136
r27737:728d55b97775
r12675:08178fc9d621
r28190:29617a715136
r28190:29617a715136
r28190:29617a715136
r12675:08178fc9d621
r12675:08178fc9d621
r12675:08178fc9d621
r12675:08178fc9d621
r28190:29617a715136
r27737:728d55b97775
r27737:728d55b97775
r25118:c8e90e7c7a37
r25118:c8e90e7c7a37
r12675:08178fc9d621
r12675:08178fc9d621
r12675:08178fc9d621
r12675:08178fc9d621
r28190:29617a715136
r28190:29617a715136
r12675:08178fc9d621
r12675:08178fc9d621
r12675:08178fc9d621
r12675:08178fc9d621
r12675:08178fc9d621
r12679:1015c7a9aebf
r28190:29617a715136
r28190:29617a715136
r12675:08178fc9d621
r12675:08178fc9d621
r12675:08178fc9d621
r12675:08178fc9d621
r12675:08178fc9d621
r12675:08178fc9d621
r12675:08178fc9d621
r12675:08178fc9d621
r28190:29617a715136
r5584:545d748cc681
r5584:545d748cc681
r27737:728d55b97775
r5584:545d748cc681
r26535:bbf17c1357f1
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r22959:8c769e762c2f
r22959:8c769e762c2f
r22959:8c769e762c2f
r22959:8c769e762c2f
r22959:8c769e762c2f
r22959:8c769e762c2f
r22959:8c769e762c2f
r6201:2e76eb9a1d7a
r27737:728d55b97775
r5584:545d748cc681
r25851:c17ed4425175
r25851:c17ed4425175
r25851:c17ed4425175
r27737:728d55b97775
r25851:c17ed4425175
r23101:10b102b8ce3d
r25124:fb2557a5732d
r25124:fb2557a5732d
r25124:fb2557a5732d
r25124:fb2557a5732d
r27737:728d55b97775
r27737:728d55b97775
r26536:a1a59110a08b
r26536:a1a59110a08b
r25118:c8e90e7c7a37
r6201:2e76eb9a1d7a
r27737:728d55b97775
r27737:728d55b97775
r26535:bbf17c1357f1
r26535:bbf17c1357f1
r28190:29617a715136
r26535:bbf17c1357f1
r28190:29617a715136
r5584:545d748cc681
r26535:bbf17c1357f1
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r6247:96e840dbefcc
r5584:545d748cc681
r27737:728d55b97775
r27737:728d55b97775
r26535:bbf17c1357f1
r26535:bbf17c1357f1
r27737:728d55b97775
r26535:bbf17c1357f1
r26535:bbf17c1357f1
r26535:bbf17c1357f1
r26535:bbf17c1357f1
r26535:bbf17c1357f1
r5584:545d748cc681
r5584:545d748cc681
r27737:728d55b97775
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r14273:79f9c8f3fcad
r14273:79f9c8f3fcad
r6201:2e76eb9a1d7a
r14273:79f9c8f3fcad
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r14273:79f9c8f3fcad
r12675:08178fc9d621
r5584:545d748cc681
r5584:545d748cc681
r14734:58c3f99dc69c
r14734:58c3f99dc69c
r14734:58c3f99dc69c
r14734:58c3f99dc69c
r14734:58c3f99dc69c
r14734:58c3f99dc69c
r14734:58c3f99dc69c
r5584:545d748cc681
r14734:58c3f99dc69c
r14734:58c3f99dc69c
r14898:8e3ea8ec8eab
r14898:8e3ea8ec8eab
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r10647:62911ec68e89
r5584:545d748cc681
r27737:728d55b97775
r26535:bbf17c1357f1
r25851:c17ed4425175
r25851:c17ed4425175
r25851:c17ed4425175
r25851:c17ed4425175
r25851:c17ed4425175
r25851:c17ed4425175
r25851:c17ed4425175
r27737:728d55b97775
r25851:c17ed4425175
r25851:c17ed4425175
r25851:c17ed4425175
r25851:c17ed4425175
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r5584:545d748cc681
r25851:c17ed4425175
r5584:545d748cc681
r14273:79f9c8f3fcad
r23607:36c15679007d
r5584:545d748cc681
r5584:545d748cc681
r26536:a1a59110a08b
r27737:728d55b97775
r26536:a1a59110a08b
r26536:a1a59110a08b
r26536:a1a59110a08b
/*
 * 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/>.
 */

/** @file mixer.cpp Mixing of sound samples. */

#include "stdafx.h"
#include <mutex>
#include <atomic>
#include "core/math_func.hpp"
#include "framerate_type.h"
#include "mixer.h"
#include "settings_type.h"

#include "safeguards.h"

struct MixerChannel {
	/* pointer to allocated buffer memory */
	int8_t *memory;

	/* current position in memory */
	uint32_t pos;
	uint32_t frac_pos;
	uint32_t frac_speed;
	uint32_t samples_left;

	/* Mixing volume */
	int volume_left;
	int volume_right;

	bool is16bit;
};

static std::atomic<uint8_t> _active_channels;
static MixerChannel _channels[8];
static uint32_t _play_rate = 11025;
static uint32_t _max_size = UINT_MAX;
static MxStreamCallback _music_stream = nullptr;
static std::mutex _music_stream_mutex;
static std::atomic<uint8_t> _effect_vol;

/**
 * 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.
 */
static const int MAX_VOLUME = 32767;

/**
 * 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;
}

template <typename T>
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;
	sc->samples_left -= samples;
	assert(samples > 0);

	const T *b = (const T *)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  >> SHIFT), -MAX_VOLUME, MAX_VOLUME);
			buffer[1] = Clamp(buffer[1] + (*b * volume_right >> SHIFT), -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  >> SHIFT), -MAX_VOLUME, MAX_VOLUME);
			buffer[1] = Clamp(buffer[1] + (data * volume_right >> SHIFT), -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 - (const T *)sc->memory;
}

static void MxCloseChannel(uint8_t channel_index)
{
	_active_channels.fetch_and(~(1 << channel_index), std::memory_order_release);
}

void MxMixSamples(void *buffer, uint samples)
{
	PerformanceMeasurer framerate(PFE_SOUND);
	static uint last_samples = 0;
	if (samples != last_samples) {
		framerate.SetExpectedRate((double)_play_rate / samples);
		last_samples = samples;
	}

	/* Clear the buffer */
	memset(buffer, 0, sizeof(int16_t) * 2 * samples);

	{
		std::lock_guard<std::mutex> lock{ _music_stream_mutex };
		/* Fetch music if a sampled stream is available */
		if (_music_stream) _music_stream((int16_t*)buffer, samples);
	}

	/* 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_t effect_vol_setting = _effect_vol.load(std::memory_order_relaxed);
	uint8_t effect_vol = (effect_vol_setting *
	                    effect_vol_setting *
	                    effect_vol_setting) / (127 * 127);

	/* Mix each channel */
	uint8_t active = _active_channels.load(std::memory_order_acquire);
	for (uint8_t idx : SetBitIterator(active)) {
		MixerChannel *mc = &_channels[idx];
		if (mc->is16bit) {
			mix_int16<int16_t>(mc, (int16_t*)buffer, samples, effect_vol);
		} else {
			mix_int16<int8_t>(mc, (int16_t*)buffer, samples, effect_vol);
		}
		if (mc->samples_left == 0) MxCloseChannel(idx);
	}
}

MixerChannel *MxAllocateChannel()
{
	uint8_t currently_active = _active_channels.load(std::memory_order_acquire);
	uint8_t available = ~currently_active;
	if (available == 0) return nullptr;

	uint8_t channel_index = FindFirstBit(available);

	MixerChannel *mc = &_channels[channel_index];
	free(mc->memory);
	mc->memory = nullptr;
	return mc;
}

void MxSetChannelRawSrc(MixerChannel *mc, int8_t *mem, size_t size, uint rate, bool is16bit)
{
	mc->memory = mem;
	mc->frac_pos = 0;
	mc->pos = 0;

	mc->frac_speed = (rate << 16) / _play_rate;

	if (is16bit) size /= 2;

	/* adjust the magnitude to prevent overflow */
	while (size >= _max_size) {
		size >>= 1;
		rate = (rate >> 1) + 1;
	}

	mc->samples_left = (uint)size * _play_rate / rate;
	mc->is16bit = is16bit;
}

/**
 * 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)
{
	/* Use sinusoidal pan to maintain overall sound power level regardless
	 * of position. */
	mc->volume_left = (uint)(sin((1.0 - pan) * M_PI / 2.0) * volume);
	mc->volume_right = (uint)(sin(pan * M_PI / 2.0) * volume);
}


void MxActivateChannel(MixerChannel *mc)
{
	uint8_t channel_index = mc - _channels;
	_active_channels.fetch_or((1 << channel_index), std::memory_order_release);
}

/**
 * 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_t MxSetMusicSource(MxStreamCallback music_callback)
{
	std::lock_guard<std::mutex> lock{ _music_stream_mutex };
	_music_stream = music_callback;
	return _play_rate;
}


bool MxInitialize(uint rate)
{
	std::lock_guard<std::mutex> lock{ _music_stream_mutex };
	_play_rate = rate;
	_max_size  = UINT_MAX / _play_rate;
	_music_stream = nullptr; /* rate may have changed, any music source is now invalid */
	return true;
}

void SetEffectVolume(uint8_t volume)
{
	_effect_vol.store(volume, std::memory_order_relaxed);
}