OpenTTD
mixer.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "stdafx.h"
13 #include <math.h>
14 #include "core/math_func.hpp"
15 #include "framerate_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 = NULL;
43 
50 static const int MAX_VOLUME = 128 * 128;
51 
59 template <typename T>
60 static int RateConversion(T *b, int frac_pos)
61 {
62  return ((b[0] * ((1 << 16) - frac_pos)) + (b[1] * frac_pos)) >> 16;
63 }
64 
65 static void mix_int16(MixerChannel *sc, int16 *buffer, uint samples)
66 {
67  if (samples > sc->samples_left) samples = sc->samples_left;
68  sc->samples_left -= samples;
69  assert(samples > 0);
70 
71  const int16 *b = (const int16 *)sc->memory + sc->pos;
72  uint32 frac_pos = sc->frac_pos;
73  uint32 frac_speed = sc->frac_speed;
74  int volume_left = sc->volume_left;
75  int volume_right = sc->volume_right;
76 
77  if (frac_speed == 0x10000) {
78  /* Special case when frac_speed is 0x10000 */
79  do {
80  buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 16), -MAX_VOLUME, MAX_VOLUME);
81  buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 16), -MAX_VOLUME, MAX_VOLUME);
82  b++;
83  buffer += 2;
84  } while (--samples > 0);
85  } else {
86  do {
87  int data = RateConversion(b, frac_pos);
88  buffer[0] = Clamp(buffer[0] + (data * volume_left >> 16), -MAX_VOLUME, MAX_VOLUME);
89  buffer[1] = Clamp(buffer[1] + (data * volume_right >> 16), -MAX_VOLUME, MAX_VOLUME);
90  buffer += 2;
91  frac_pos += frac_speed;
92  b += frac_pos >> 16;
93  frac_pos &= 0xffff;
94  } while (--samples > 0);
95  }
96 
97  sc->frac_pos = frac_pos;
98  sc->pos = b - (const int16 *)sc->memory;
99 }
100 
101 static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples)
102 {
103  if (samples > sc->samples_left) samples = sc->samples_left;
104  sc->samples_left -= samples;
105  assert(samples > 0);
106 
107  const int8 *b = sc->memory + sc->pos;
108  uint32 frac_pos = sc->frac_pos;
109  uint32 frac_speed = sc->frac_speed;
110  int volume_left = sc->volume_left;
111  int volume_right = sc->volume_right;
112 
113  if (frac_speed == 0x10000) {
114  /* Special case when frac_speed is 0x10000 */
115  do {
116  buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME);
117  buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME);
118  b++;
119  buffer += 2;
120  } while (--samples > 0);
121  } else {
122  do {
123  int data = RateConversion(b, frac_pos);
124  buffer[0] = Clamp(buffer[0] + (data * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME);
125  buffer[1] = Clamp(buffer[1] + (data * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME);
126  buffer += 2;
127  frac_pos += frac_speed;
128  b += frac_pos >> 16;
129  frac_pos &= 0xffff;
130  } while (--samples > 0);
131  }
132 
133  sc->frac_pos = frac_pos;
134  sc->pos = b - sc->memory;
135 }
136 
137 static void MxCloseChannel(MixerChannel *mc)
138 {
139  mc->active = false;
140 }
141 
142 void MxMixSamples(void *buffer, uint samples)
143 {
144  PerformanceMeasurer framerate(PFE_SOUND);
145  static uint last_samples = 0;
146  if (samples != last_samples) {
147  framerate.SetExpectedRate((double)_play_rate / samples);
148  last_samples = samples;
149  }
150 
151  MixerChannel *mc;
152 
153  /* Clear the buffer */
154  memset(buffer, 0, sizeof(int16) * 2 * samples);
155 
156  /* Fetch music if a sampled stream is available */
157  if (_music_stream) _music_stream((int16*)buffer, samples);
158 
159  /* Mix each channel */
160  for (mc = _channels; mc != endof(_channels); mc++) {
161  if (mc->active) {
162  if (mc->is16bit) {
163  mix_int16(mc, (int16*)buffer, samples);
164  } else {
165  mix_int8_to_int16(mc, (int16*)buffer, samples);
166  }
167  if (mc->samples_left == 0) MxCloseChannel(mc);
168  }
169  }
170 }
171 
172 MixerChannel *MxAllocateChannel()
173 {
174  MixerChannel *mc;
175  for (mc = _channels; mc != endof(_channels); mc++) {
176  if (!mc->active) {
177  free(mc->memory);
178  mc->memory = NULL;
179  return mc;
180  }
181  }
182  return NULL;
183 }
184 
185 void MxSetChannelRawSrc(MixerChannel *mc, int8 *mem, size_t size, uint rate, bool is16bit)
186 {
187  mc->memory = mem;
188  mc->frac_pos = 0;
189  mc->pos = 0;
190 
191  mc->frac_speed = (rate << 16) / _play_rate;
192 
193  if (is16bit) size /= 2;
194 
195  /* adjust the magnitude to prevent overflow */
196  while (size >= _max_size) {
197  size >>= 1;
198  rate = (rate >> 1) + 1;
199  }
200 
201  mc->samples_left = (uint)size * _play_rate / rate;
202  mc->is16bit = is16bit;
203 }
204 
211 void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan)
212 {
213  /* Use sinusoidal pan to maintain overall sound power level regardless
214  * of position. */
215  mc->volume_left = (uint)(sin((1.0 - pan) * M_PI / 2.0) * volume);
216  mc->volume_right = (uint)(sin(pan * M_PI / 2.0) * volume);
217 }
218 
219 
220 void MxActivateChannel(MixerChannel *mc)
221 {
222  mc->active = true;
223 }
224 
230 uint32 MxSetMusicSource(MxStreamCallback music_callback)
231 {
232  _music_stream = music_callback;
233  return _play_rate;
234 }
235 
236 
237 bool MxInitialize(uint rate)
238 {
239  _play_rate = rate;
240  _max_size = UINT_MAX / _play_rate;
241  _music_stream = NULL; /* rate may have changed, any music source is now invalid */
242  return true;
243 }
Types for recording game performance data.
void SetExpectedRate(double rate)
Set the rate of expected cycles per second of a performance element.
RAII class for measuring simple elements of performance.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan)
Set volume and pan parameters for a sound.
Definition: mixer.cpp:211
static int RateConversion(T *b, int frac_pos)
Perform the rate conversion between the input and output.
Definition: mixer.cpp:60
Integer math functions.
uint32 MxSetMusicSource(MxStreamCallback music_callback)
Set source of PCM music.
Definition: mixer.cpp:230
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:139
void(* MxStreamCallback)(int16 *buffer, size_t samples)
Type of callback functions for supplying PCM music.
Definition: mixer.h:23
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:412
Functions to mix sound samples.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
static const int MAX_VOLUME
The theoretical maximum volume for a single sound sample.
Definition: mixer.cpp:50
Speed of mixing audio samples.