OpenTTD Source  12.0-beta2
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 "core/math_func.hpp"
14 #include "framerate_type.h"
15 #include "settings_type.h"
16 
17 #include "safeguards.h"
18 #include "mixer.h"
19 
20 struct MixerChannel {
21  bool active;
22 
23  /* pointer to allocated buffer memory */
24  int8 *memory;
25 
26  /* current position in memory */
27  uint32 pos;
28  uint32 frac_pos;
29  uint32 frac_speed;
30  uint32 samples_left;
31 
32  /* Mixing volume */
33  int volume_left;
34  int volume_right;
35 
36  bool is16bit;
37 };
38 
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 
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 static void mix_int16(MixerChannel *sc, int16 *buffer, uint samples, uint8 effect_vol)
67 {
68  if (samples > sc->samples_left) samples = sc->samples_left;
69  sc->samples_left -= samples;
70  assert(samples > 0);
71 
72  const int16 *b = (const int16 *)sc->memory + sc->pos;
73  uint32 frac_pos = sc->frac_pos;
74  uint32 frac_speed = sc->frac_speed;
75  int volume_left = sc->volume_left * effect_vol / 255;
76  int volume_right = sc->volume_right * effect_vol / 255;
77 
78  if (frac_speed == 0x10000) {
79  /* Special case when frac_speed is 0x10000 */
80  do {
81  buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 16), -MAX_VOLUME, MAX_VOLUME);
82  buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 16), -MAX_VOLUME, MAX_VOLUME);
83  b++;
84  buffer += 2;
85  } while (--samples > 0);
86  } else {
87  do {
88  int data = RateConversion(b, frac_pos);
89  buffer[0] = Clamp(buffer[0] + (data * volume_left >> 16), -MAX_VOLUME, MAX_VOLUME);
90  buffer[1] = Clamp(buffer[1] + (data * volume_right >> 16), -MAX_VOLUME, MAX_VOLUME);
91  buffer += 2;
92  frac_pos += frac_speed;
93  b += frac_pos >> 16;
94  frac_pos &= 0xffff;
95  } while (--samples > 0);
96  }
97 
98  sc->frac_pos = frac_pos;
99  sc->pos = b - (const int16 *)sc->memory;
100 }
101 
102 static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples, uint8 effect_vol)
103 {
104  if (samples > sc->samples_left) samples = sc->samples_left;
105  sc->samples_left -= samples;
106  assert(samples > 0);
107 
108  const int8 *b = sc->memory + sc->pos;
109  uint32 frac_pos = sc->frac_pos;
110  uint32 frac_speed = sc->frac_speed;
111  int volume_left = sc->volume_left * effect_vol / 255;
112  int volume_right = sc->volume_right * effect_vol / 255;
113 
114  if (frac_speed == 0x10000) {
115  /* Special case when frac_speed is 0x10000 */
116  do {
117  buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME);
118  buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME);
119  b++;
120  buffer += 2;
121  } while (--samples > 0);
122  } else {
123  do {
124  int data = RateConversion(b, frac_pos);
125  buffer[0] = Clamp(buffer[0] + (data * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME);
126  buffer[1] = Clamp(buffer[1] + (data * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME);
127  buffer += 2;
128  frac_pos += frac_speed;
129  b += frac_pos >> 16;
130  frac_pos &= 0xffff;
131  } while (--samples > 0);
132  }
133 
134  sc->frac_pos = frac_pos;
135  sc->pos = b - sc->memory;
136 }
137 
138 static void MxCloseChannel(MixerChannel *mc)
139 {
140  mc->active = false;
141 }
142 
143 void MxMixSamples(void *buffer, uint samples)
144 {
145  PerformanceMeasurer framerate(PFE_SOUND);
146  static uint last_samples = 0;
147  if (samples != last_samples) {
148  framerate.SetExpectedRate((double)_play_rate / samples);
149  last_samples = samples;
150  }
151 
152  MixerChannel *mc;
153 
154  /* Clear the buffer */
155  memset(buffer, 0, sizeof(int16) * 2 * samples);
156 
157  {
158  std::lock_guard<std::mutex> lock{ _music_stream_mutex };
159  /* Fetch music if a sampled stream is available */
160  if (_music_stream) _music_stream((int16*)buffer, samples);
161  }
162 
163  /* Apply simple x^3 scaling to master effect volume. This increases the
164  * perceived difference in loudness to better match expectations. effect_vol
165  * is expected to be in the range 0-127 hence the division by 127 * 127 to
166  * get back into range. */
167  uint8 effect_vol = (_settings_client.music.effect_vol *
169  _settings_client.music.effect_vol) / (127 * 127);
170 
171  /* Mix each channel */
172  for (mc = _channels; mc != endof(_channels); mc++) {
173  if (mc->active) {
174  if (mc->is16bit) {
175  mix_int16(mc, (int16*)buffer, samples, effect_vol);
176  } else {
177  mix_int8_to_int16(mc, (int16*)buffer, samples, effect_vol);
178  }
179  if (mc->samples_left == 0) MxCloseChannel(mc);
180  }
181  }
182 }
183 
184 MixerChannel *MxAllocateChannel()
185 {
186  MixerChannel *mc;
187  for (mc = _channels; mc != endof(_channels); mc++) {
188  if (!mc->active) {
189  free(mc->memory);
190  mc->memory = nullptr;
191  return mc;
192  }
193  }
194  return nullptr;
195 }
196 
197 void MxSetChannelRawSrc(MixerChannel *mc, int8 *mem, size_t size, uint rate, bool is16bit)
198 {
199  mc->memory = mem;
200  mc->frac_pos = 0;
201  mc->pos = 0;
202 
203  mc->frac_speed = (rate << 16) / _play_rate;
204 
205  if (is16bit) size /= 2;
206 
207  /* adjust the magnitude to prevent overflow */
208  while (size >= _max_size) {
209  size >>= 1;
210  rate = (rate >> 1) + 1;
211  }
212 
213  mc->samples_left = (uint)size * _play_rate / rate;
214  mc->is16bit = is16bit;
215 }
216 
223 void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan)
224 {
225  /* Use sinusoidal pan to maintain overall sound power level regardless
226  * of position. */
227  mc->volume_left = (uint)(sin((1.0 - pan) * M_PI / 2.0) * volume);
228  mc->volume_right = (uint)(sin(pan * M_PI / 2.0) * volume);
229 }
230 
231 
232 void MxActivateChannel(MixerChannel *mc)
233 {
234  mc->active = true;
235 }
236 
242 uint32 MxSetMusicSource(MxStreamCallback music_callback)
243 {
244  std::lock_guard<std::mutex> lock{ _music_stream_mutex };
245  _music_stream = music_callback;
246  return _play_rate;
247 }
248 
249 
250 bool MxInitialize(uint rate)
251 {
252  std::lock_guard<std::mutex> lock{ _music_stream_mutex };
253  _play_rate = rate;
254  _max_size = UINT_MAX / _play_rate;
255  _music_stream = nullptr; /* rate may have changed, any music source is now invalid */
256  return true;
257 }
lock
std::mutex lock
synchronization for playback status fields
Definition: win32_m.cpp:34
math_func.hpp
PerformanceMeasurer
RAII class for measuring simple elements of performance.
Definition: framerate_type.h:92
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:52
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:20
endof
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:386
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:223
MxSetMusicSource
uint32 MxSetMusicSource(MxStreamCallback music_callback)
Set source of PCM music.
Definition: mixer.cpp:242
MusicSettings::effect_vol
byte effect_vol
The requested effects volume.
Definition: settings_type.h:219
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:460
ClientSettings::music
MusicSettings music
settings related to music/sound
Definition: settings_type.h:597
mixer.h