OpenTTD Source  1.11.2
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 "core/math_func.hpp"
13 #include "framerate_type.h"
14 #include "settings_type.h"
15 
16 #include "safeguards.h"
17 #include "mixer.h"
18 
19 struct MixerChannel {
20  bool active;
21 
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 MixerChannel _channels[8];
39 static uint32 _play_rate = 11025;
40 static uint32 _max_size = UINT_MAX;
41 static MxStreamCallback _music_stream = nullptr;
42 
49 static const int MAX_VOLUME = 32767;
50 
58 template <typename T>
59 static int RateConversion(T *b, int frac_pos)
60 {
61  return ((b[0] * ((1 << 16) - frac_pos)) + (b[1] * frac_pos)) >> 16;
62 }
63 
64 static void mix_int16(MixerChannel *sc, int16 *buffer, uint samples, uint8 effect_vol)
65 {
66  if (samples > sc->samples_left) samples = sc->samples_left;
67  sc->samples_left -= samples;
68  assert(samples > 0);
69 
70  const int16 *b = (const int16 *)sc->memory + sc->pos;
71  uint32 frac_pos = sc->frac_pos;
72  uint32 frac_speed = sc->frac_speed;
73  int volume_left = sc->volume_left * effect_vol / 255;
74  int volume_right = sc->volume_right * effect_vol / 255;
75 
76  if (frac_speed == 0x10000) {
77  /* Special case when frac_speed is 0x10000 */
78  do {
79  buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 16), -MAX_VOLUME, MAX_VOLUME);
80  buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 16), -MAX_VOLUME, MAX_VOLUME);
81  b++;
82  buffer += 2;
83  } while (--samples > 0);
84  } else {
85  do {
86  int data = RateConversion(b, frac_pos);
87  buffer[0] = Clamp(buffer[0] + (data * volume_left >> 16), -MAX_VOLUME, MAX_VOLUME);
88  buffer[1] = Clamp(buffer[1] + (data * volume_right >> 16), -MAX_VOLUME, MAX_VOLUME);
89  buffer += 2;
90  frac_pos += frac_speed;
91  b += frac_pos >> 16;
92  frac_pos &= 0xffff;
93  } while (--samples > 0);
94  }
95 
96  sc->frac_pos = frac_pos;
97  sc->pos = b - (const int16 *)sc->memory;
98 }
99 
100 static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples, uint8 effect_vol)
101 {
102  if (samples > sc->samples_left) samples = sc->samples_left;
103  sc->samples_left -= samples;
104  assert(samples > 0);
105 
106  const int8 *b = sc->memory + sc->pos;
107  uint32 frac_pos = sc->frac_pos;
108  uint32 frac_speed = sc->frac_speed;
109  int volume_left = sc->volume_left * effect_vol / 255;
110  int volume_right = sc->volume_right * effect_vol / 255;
111 
112  if (frac_speed == 0x10000) {
113  /* Special case when frac_speed is 0x10000 */
114  do {
115  buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME);
116  buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME);
117  b++;
118  buffer += 2;
119  } while (--samples > 0);
120  } else {
121  do {
122  int data = RateConversion(b, frac_pos);
123  buffer[0] = Clamp(buffer[0] + (data * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME);
124  buffer[1] = Clamp(buffer[1] + (data * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME);
125  buffer += 2;
126  frac_pos += frac_speed;
127  b += frac_pos >> 16;
128  frac_pos &= 0xffff;
129  } while (--samples > 0);
130  }
131 
132  sc->frac_pos = frac_pos;
133  sc->pos = b - sc->memory;
134 }
135 
136 static void MxCloseChannel(MixerChannel *mc)
137 {
138  mc->active = false;
139 }
140 
141 void MxMixSamples(void *buffer, uint samples)
142 {
143  PerformanceMeasurer framerate(PFE_SOUND);
144  static uint last_samples = 0;
145  if (samples != last_samples) {
146  framerate.SetExpectedRate((double)_play_rate / samples);
147  last_samples = samples;
148  }
149 
150  MixerChannel *mc;
151 
152  /* Clear the buffer */
153  memset(buffer, 0, sizeof(int16) * 2 * samples);
154 
155  /* Fetch music if a sampled stream is available */
156  if (_music_stream) _music_stream((int16*)buffer, samples);
157 
158  /* Apply simple x^3 scaling to master effect volume. This increases the
159  * perceived difference in loudness to better match expectations. effect_vol
160  * is expected to be in the range 0-127 hence the division by 127 * 127 to
161  * get back into range. */
162  uint8 effect_vol = (_settings_client.music.effect_vol *
164  _settings_client.music.effect_vol) / (127 * 127);
165 
166  /* Mix each channel */
167  for (mc = _channels; mc != endof(_channels); mc++) {
168  if (mc->active) {
169  if (mc->is16bit) {
170  mix_int16(mc, (int16*)buffer, samples, effect_vol);
171  } else {
172  mix_int8_to_int16(mc, (int16*)buffer, samples, effect_vol);
173  }
174  if (mc->samples_left == 0) MxCloseChannel(mc);
175  }
176  }
177 }
178 
179 MixerChannel *MxAllocateChannel()
180 {
181  MixerChannel *mc;
182  for (mc = _channels; mc != endof(_channels); mc++) {
183  if (!mc->active) {
184  free(mc->memory);
185  mc->memory = nullptr;
186  return mc;
187  }
188  }
189  return nullptr;
190 }
191 
192 void MxSetChannelRawSrc(MixerChannel *mc, int8 *mem, size_t size, uint rate, bool is16bit)
193 {
194  mc->memory = mem;
195  mc->frac_pos = 0;
196  mc->pos = 0;
197 
198  mc->frac_speed = (rate << 16) / _play_rate;
199 
200  if (is16bit) size /= 2;
201 
202  /* adjust the magnitude to prevent overflow */
203  while (size >= _max_size) {
204  size >>= 1;
205  rate = (rate >> 1) + 1;
206  }
207 
208  mc->samples_left = (uint)size * _play_rate / rate;
209  mc->is16bit = is16bit;
210 }
211 
218 void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan)
219 {
220  /* Use sinusoidal pan to maintain overall sound power level regardless
221  * of position. */
222  mc->volume_left = (uint)(sin((1.0 - pan) * M_PI / 2.0) * volume);
223  mc->volume_right = (uint)(sin(pan * M_PI / 2.0) * volume);
224 }
225 
226 
227 void MxActivateChannel(MixerChannel *mc)
228 {
229  mc->active = true;
230 }
231 
237 uint32 MxSetMusicSource(MxStreamCallback music_callback)
238 {
239  _music_stream = music_callback;
240  return _play_rate;
241 }
242 
243 
244 bool MxInitialize(uint rate)
245 {
246  _play_rate = rate;
247  _max_size = UINT_MAX / _play_rate;
248  _music_stream = nullptr; /* rate may have changed, any music source is now invalid */
249  return true;
250 }
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:79
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:19
endof
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:377
framerate_type.h
MAX_VOLUME
static const int MAX_VOLUME
The theoretical maximum volume for a single sound sample.
Definition: mixer.cpp:49
RateConversion
static int RateConversion(T *b, int frac_pos)
Perform the rate conversion between the input and output.
Definition: mixer.cpp:59
MxSetChannelVolume
void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan)
Set volume and pan parameters for a sound.
Definition: mixer.cpp:218
MxSetMusicSource
uint32 MxSetMusicSource(MxStreamCallback music_callback)
Set source of PCM music.
Definition: mixer.cpp:237
MusicSettings::effect_vol
byte effect_vol
The requested effects volume.
Definition: settings_type.h:208
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:456
ClientSettings::music
MusicSettings music
settings related to music/sound
Definition: settings_type.h:585
mixer.h