OpenTTD Source: src/mixer.cpp Source File

OpenTTD Source 13.1

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