OpenTTD Source  20240917-master-g9ab0a47812
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 std::atomic<uint8_t> _stop_channels;
39 static MixerChannel _channels[8];
40 static uint32_t _play_rate = 11025;
41 static uint32_t _max_size = UINT_MAX;
42 static MxStreamCallback _music_stream = nullptr;
43 static std::mutex _music_stream_mutex;
44 static std::atomic<uint8_t> _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 template <typename T>
68 static void mix_int16(MixerChannel *sc, int16_t *buffer, uint samples, uint8_t effect_vol)
69 {
70  /* Shift required to get sample value into range for the data type. */
71  const uint SHIFT = sizeof(T) * CHAR_BIT;
72 
73  if (samples > sc->samples_left) samples = sc->samples_left;
74  sc->samples_left -= samples;
75  assert(samples > 0);
76 
77  const T *b = (const T *)sc->memory + sc->pos;
78  uint32_t frac_pos = sc->frac_pos;
79  uint32_t frac_speed = sc->frac_speed;
80  int volume_left = sc->volume_left * effect_vol / 255;
81  int volume_right = sc->volume_right * effect_vol / 255;
82 
83  if (frac_speed == 0x10000) {
84  /* Special case when frac_speed is 0x10000 */
85  do {
86  buffer[0] = Clamp(buffer[0] + (*b * volume_left >> SHIFT), -MAX_VOLUME, MAX_VOLUME);
87  buffer[1] = Clamp(buffer[1] + (*b * volume_right >> SHIFT), -MAX_VOLUME, MAX_VOLUME);
88  b++;
89  buffer += 2;
90  } while (--samples > 0);
91  } else {
92  do {
93  int data = RateConversion(b, frac_pos);
94  buffer[0] = Clamp(buffer[0] + (data * volume_left >> SHIFT), -MAX_VOLUME, MAX_VOLUME);
95  buffer[1] = Clamp(buffer[1] + (data * volume_right >> SHIFT), -MAX_VOLUME, MAX_VOLUME);
96  buffer += 2;
97  frac_pos += frac_speed;
98  b += frac_pos >> 16;
99  frac_pos &= 0xffff;
100  } while (--samples > 0);
101  }
102 
103  sc->frac_pos = frac_pos;
104  sc->pos = b - (const T *)sc->memory;
105 }
106 
107 static void MxCloseChannel(uint8_t channel_index)
108 {
109  _active_channels.fetch_and(~(1 << channel_index), std::memory_order_release);
110 }
111 
118 {
119  _stop_channels.fetch_or(~0, std::memory_order_release);
120 }
121 
122 void MxMixSamples(void *buffer, uint samples)
123 {
124  PerformanceMeasurer framerate(PFE_SOUND);
125  static uint last_samples = 0;
126  if (samples != last_samples) {
127  framerate.SetExpectedRate((double)_play_rate / samples);
128  last_samples = samples;
129  }
130 
131  /* Clear the buffer */
132  memset(buffer, 0, sizeof(int16_t) * 2 * samples);
133 
134  {
135  std::lock_guard<std::mutex> lock{ _music_stream_mutex };
136  /* Fetch music if a sampled stream is available */
137  if (_music_stream) _music_stream((int16_t*)buffer, samples);
138  }
139 
140  /* Check if any channels should be stopped. */
141  uint8_t stop = _stop_channels.load(std::memory_order_acquire);
142  for (uint8_t idx : SetBitIterator(stop)) {
143  MxCloseChannel(idx);
144  }
145 
146  /* Apply simple x^3 scaling to master effect volume. This increases the
147  * perceived difference in loudness to better match expectations. effect_vol
148  * is expected to be in the range 0-127 hence the division by 127 * 127 to
149  * get back into range. */
150  uint8_t effect_vol_setting = _effect_vol.load(std::memory_order_relaxed);
151  uint8_t effect_vol = (effect_vol_setting *
152  effect_vol_setting *
153  effect_vol_setting) / (127 * 127);
154 
155  /* Mix each channel */
156  uint8_t active = _active_channels.load(std::memory_order_acquire);
157  for (uint8_t idx : SetBitIterator(active)) {
158  MixerChannel *mc = &_channels[idx];
159  if (mc->is16bit) {
160  mix_int16<int16_t>(mc, (int16_t*)buffer, samples, effect_vol);
161  } else {
162  mix_int16<int8_t>(mc, (int16_t*)buffer, samples, effect_vol);
163  }
164  if (mc->samples_left == 0) MxCloseChannel(idx);
165  }
166 }
167 
168 MixerChannel *MxAllocateChannel()
169 {
170  uint8_t currently_active = _active_channels.load(std::memory_order_acquire);
171  uint8_t available = ~currently_active;
172  if (available == 0) return nullptr;
173 
174  uint8_t channel_index = FindFirstBit(available);
175 
176  MixerChannel *mc = &_channels[channel_index];
177  free(mc->memory);
178  mc->memory = nullptr;
179  return mc;
180 }
181 
182 void MxSetChannelRawSrc(MixerChannel *mc, int8_t *mem, size_t size, uint rate, bool is16bit)
183 {
184  mc->memory = mem;
185  mc->frac_pos = 0;
186  mc->pos = 0;
187 
188  mc->frac_speed = (rate << 16) / _play_rate;
189 
190  if (is16bit) size /= 2;
191 
192  /* adjust the magnitude to prevent overflow */
193  while (size >= _max_size) {
194  size >>= 1;
195  rate = (rate >> 1) + 1;
196  }
197 
198  mc->samples_left = (uint)size * _play_rate / rate;
199  mc->is16bit = is16bit;
200 }
201 
208 void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan)
209 {
210  /* Use sinusoidal pan to maintain overall sound power level regardless
211  * of position. */
212  mc->volume_left = (uint)(sin((1.0 - pan) * M_PI / 2.0) * volume);
213  mc->volume_right = (uint)(sin(pan * M_PI / 2.0) * volume);
214 }
215 
216 
217 void MxActivateChannel(MixerChannel *mc)
218 {
219  uint8_t channel_index = mc - _channels;
220  _stop_channels.fetch_and(~(1 << channel_index), std::memory_order_release);
221  _active_channels.fetch_or((1 << channel_index), std::memory_order_release);
222 }
223 
229 uint32_t MxSetMusicSource(MxStreamCallback music_callback)
230 {
231  std::lock_guard<std::mutex> lock{ _music_stream_mutex };
232  _music_stream = music_callback;
233  return _play_rate;
234 }
235 
236 
237 bool MxInitialize(uint rate)
238 {
239  std::lock_guard<std::mutex> lock{ _music_stream_mutex };
240  _play_rate = rate;
241  _max_size = UINT_MAX / _play_rate;
242  _music_stream = nullptr; /* rate may have changed, any music source is now invalid */
243  return true;
244 }
245 
246 void SetEffectVolume(uint8_t volume)
247 {
248  _effect_vol.store(volume, std::memory_order_relaxed);
249 }
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:301
free
void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:334
safeguards.h
MxCloseAllChannels
void MxCloseAllChannels()
Close all mixer channels.
Definition: mixer.cpp:117
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:229
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: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:208
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:213