OpenTTD Source: src/mixer.cpp Source File

OpenTTD Source 14.0-beta3

mixer.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #include "stdafx.h"
11 #include <mutex>
12 #include <atomic>
13 #include "core/math_func.hpp"
14 #include "framerate_type.h"
15 #include "mixer.h"
16 #include "settings_type.h"
17
18 #include "safeguards.h"
19
20 struct MixerChannel {
21 /* pointer to allocated buffer memory */
22 int8_t *memory;
23
24 /* current position in memory */
25 uint32_t pos;
26 uint32_t frac_pos;
27 uint32_t frac_speed;
28 uint32_t samples_left;
29
30 /* Mixing volume */
31 int volume_left;
32 int volume_right;
33
34 bool is16bit;
35 };
36
37 static std::atomic<uint8_t> _active_channels;
38 static MixerChannel _channels[8];
39 static uint32_t _play_rate = 11025;
40 static uint32_t _max_size = UINT_MAX;
41 static MxStreamCallback _music_stream = nullptr;
42 static std::mutex _music_stream_mutex;
43 static std::atomic<uint8_t> _effect_vol;
44
51 static const int MAX_VOLUME = 32767;
52
60 template <typename T>
61 static int RateConversion(T *b, int frac_pos)
62 {
63 return ((b[0] * ((1 << 16) - frac_pos)) + (b[1] * frac_pos)) >> 16;
64 }
65
66 template <typename T>
67 static void mix_int16(MixerChannel *sc, int16_t *buffer, uint samples, uint8_t effect_vol)
68 {
69 /* Shift required to get sample value into range for the data type. */
70 const uint SHIFT = sizeof(T) * CHAR_BIT;
71
72 if (samples > sc->samples_left) samples = sc->samples_left;
73 sc->samples_left -= samples;
74 assert(samples > 0);
75
76 const T *b = (const T *)sc->memory + sc->pos;
77 uint32_t frac_pos = sc->frac_pos;
78 uint32_t frac_speed = sc->frac_speed;
79 int volume_left = sc->volume_left * effect_vol / 255;
80 int volume_right = sc->volume_right * effect_vol / 255;
81
82 if (frac_speed == 0x10000) {
83 /* Special case when frac_speed is 0x10000 */
84 do {
85 buffer[0] = Clamp(buffer[0] + (*b * volume_left >> SHIFT), -MAX_VOLUME, MAX_VOLUME);
86 buffer[1] = Clamp(buffer[1] + (*b * volume_right >> SHIFT), -MAX_VOLUME, MAX_VOLUME);
87 b++;
88 buffer += 2;
89 } while (--samples > 0);
90 } else {
91 do {
92 int data = RateConversion(b, frac_pos);
93 buffer[0] = Clamp(buffer[0] + (data * volume_left >> SHIFT), -MAX_VOLUME, MAX_VOLUME);
94 buffer[1] = Clamp(buffer[1] + (data * volume_right >> SHIFT), -MAX_VOLUME, MAX_VOLUME);
95 buffer += 2;
96 frac_pos += frac_speed;
97 b += frac_pos >> 16;
98 frac_pos &= 0xffff;
99 } while (--samples > 0);
100 }
101
102 sc->frac_pos = frac_pos;
103 sc->pos = b - (const T *)sc->memory;
104 }
105
106 static void MxCloseChannel(uint8_t channel_index)
107 {
108 _active_channels.fetch_and(~(1 << channel_index), std::memory_order_release);
109 }
110
111 void MxMixSamples(void *buffer, uint samples)
112 {
113 PerformanceMeasurer framerate(PFE_SOUND);
114 static uint last_samples = 0;
115 if (samples != last_samples) {
116 framerate.SetExpectedRate((double)_play_rate / samples);
117 last_samples = samples;
118 }
119
120 /* Clear the buffer */
121 memset(buffer, 0, sizeof(int16_t) * 2 * samples);
122
123 {
124 std::lock_guard<std::mutex> lock{ _music_stream_mutex };
125 /* Fetch music if a sampled stream is available */
126 if (_music_stream) _music_stream((int16_t*)buffer, samples);
127 }
128
129 /* Apply simple x^3 scaling to master effect volume. This increases the
130 * perceived difference in loudness to better match expectations. effect_vol
131 * is expected to be in the range 0-127 hence the division by 127 * 127 to
132 * get back into range. */
133 uint8_t effect_vol_setting = _effect_vol.load(std::memory_order_relaxed);
134 uint8_t effect_vol = (effect_vol_setting *
135 effect_vol_setting *
136 effect_vol_setting) / (127 * 127);
137
138 /* Mix each channel */
139 uint8_t active = _active_channels.load(std::memory_order_acquire);
140 for (uint8_t idx : SetBitIterator(active)) {
141 MixerChannel *mc = &_channels[idx];
142 if (mc->is16bit) {
143 mix_int16<int16_t>(mc, (int16_t*)buffer, samples, effect_vol);
144 } else {
145 mix_int16<int8_t>(mc, (int16_t*)buffer, samples, effect_vol);
146 }
147 if (mc->samples_left == 0) MxCloseChannel(idx);
148 }
149 }
150
151 MixerChannel *MxAllocateChannel()
152 {
153 uint8_t currently_active = _active_channels.load(std::memory_order_acquire);
154 uint8_t available = ~currently_active;
155 if (available == 0) return nullptr;
156
157 uint8_t channel_index = FindFirstBit(available);
158
159 MixerChannel *mc = &_channels[channel_index];
160 free(mc->memory);
161 mc->memory = nullptr;
162 return mc;
163 }
164
165 void MxSetChannelRawSrc(MixerChannel *mc, int8_t *mem, size_t size, uint rate, bool is16bit)
166 {
167 mc->memory = mem;
168 mc->frac_pos = 0;
169 mc->pos = 0;
170
171 mc->frac_speed = (rate << 16) / _play_rate;
172
173 if (is16bit) size /= 2;
174
175 /* adjust the magnitude to prevent overflow */
176 while (size >= _max_size) {
177 size >>= 1;
178 rate = (rate >> 1) + 1;
179 }
180
181 mc->samples_left = (uint)size * _play_rate / rate;
182 mc->is16bit = is16bit;
183 }
184
191 void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan)
192 {
193 /* Use sinusoidal pan to maintain overall sound power level regardless
194 * of position. */
195 mc->volume_left = (uint)(sin((1.0 - pan) * M_PI / 2.0) * volume);
196 mc->volume_right = (uint)(sin(pan * M_PI / 2.0) * volume);
197 }
198
199
200 void MxActivateChannel(MixerChannel *mc)
201 {
202 uint8_t channel_index = mc - _channels;
203 _active_channels.fetch_or((1 << channel_index), std::memory_order_release);
204 }
205
211 uint32_t MxSetMusicSource(MxStreamCallback music_callback)
212 {
213 std::lock_guard<std::mutex> lock{ _music_stream_mutex };
214 _music_stream = music_callback;
215 return _play_rate;
216 }
217
218
219 bool MxInitialize(uint rate)
220 {
221 std::lock_guard<std::mutex> lock{ _music_stream_mutex };
222 _play_rate = rate;
223 _max_size = UINT_MAX / _play_rate;
224 _music_stream = nullptr; /* rate may have changed, any music source is now invalid */
225 return true;
226 }
227
228 void SetEffectVolume(uint8_t volume)
229 {
230 _effect_vol.store(volume, std::memory_order_relaxed);
231 }
MxStreamCallback
void(* MxStreamCallback)(int16_t *buffer, size_t samples)
Type of callback functions for supplying PCM music.
Definition: mixer.h:21
lock
std::mutex lock
synchronization for playback status fields
Definition: win32_m.cpp:35
math_func.hpp
PerformanceMeasurer
RAII class for measuring simple elements of performance.
Definition: framerate_type.h:92
SetBitIterator
Iterable ensemble of each set bit in a value.
Definition: bitmath_func.hpp:282
free
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:379
safeguards.h
settings_type.h
stdafx.h
PFE_SOUND
@ PFE_SOUND
Speed of mixing audio samples.
Definition: framerate_type.h:60
MxSetMusicSource
uint32_t MxSetMusicSource(MxStreamCallback music_callback)
Set source of PCM music.
Definition: mixer.cpp:211
MixerChannel
Definition: mixer.cpp:20
framerate_type.h
MAX_VOLUME
static const int MAX_VOLUME
The theoretical maximum volume for a single sound sample.
Definition: mixer.cpp:51
RateConversion
static int RateConversion(T *b, int frac_pos)
Perform the rate conversion between the input and output.
Definition: mixer.cpp:61
MxSetChannelVolume
void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan)
Set volume and pan parameters for a sound.
Definition: mixer.cpp:191
Clamp
constexpr T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:79
mixer.h
FindFirstBit
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
Definition: bitmath_func.hpp:194