author | Tero Marttila <terom@fixme.fi> |
Tue, 22 Jul 2008 23:20:33 +0300 | |
changeset 11184 | 88c967f1422b |
parent 11148 | f0e0245a05c3 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
10429
1b99254f9607
(svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents:
6573
diff
changeset
|
3 |
/** @file mixer.cpp Mixing of sound samples. */ |
6527
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5838
diff
changeset
|
4 |
|
0 | 5 |
#include "stdafx.h" |
1891
92a3b0aa0946
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1498
diff
changeset
|
6 |
#include "openttd.h" |
1496
3d0b86f5dcb8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
7 |
#include "mixer.h" |
11137
16d0ea29e638
(svn r13695) -Fix [FS#2120]: in some cases the (sound) mixer could overflow causing artefacts in the sound.
rubidium
parents:
10751
diff
changeset
|
8 |
#include "core/math_func.hpp" |
0 | 9 |
|
1496
3d0b86f5dcb8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
10 |
struct MixerChannel { |
0 | 11 |
bool active; |
12 |
||
6527
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5838
diff
changeset
|
13 |
/* pointer to allocated buffer memory */ |
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
14 |
int8 *memory; |
0 | 15 |
|
6527
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5838
diff
changeset
|
16 |
/* current position in memory */ |
0 | 17 |
uint32 pos; |
18 |
uint32 frac_pos; |
|
19 |
uint32 frac_speed; |
|
20 |
uint32 samples_left; |
|
21 |
||
6527
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5838
diff
changeset
|
22 |
/* Mixing volume */ |
11137
16d0ea29e638
(svn r13695) -Fix [FS#2120]: in some cases the (sound) mixer could overflow causing artefacts in the sound.
rubidium
parents:
10751
diff
changeset
|
23 |
int volume_left; |
16d0ea29e638
(svn r13695) -Fix [FS#2120]: in some cases the (sound) mixer could overflow causing artefacts in the sound.
rubidium
parents:
10751
diff
changeset
|
24 |
int volume_right; |
0 | 25 |
|
26 |
uint flags; |
|
1496
3d0b86f5dcb8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
27 |
}; |
0 | 28 |
|
2977 | 29 |
static MixerChannel _channels[8]; |
30 |
static uint32 _play_rate; |
|
0 | 31 |
|
11137
16d0ea29e638
(svn r13695) -Fix [FS#2120]: in some cases the (sound) mixer could overflow causing artefacts in the sound.
rubidium
parents:
10751
diff
changeset
|
32 |
/** |
16d0ea29e638
(svn r13695) -Fix [FS#2120]: in some cases the (sound) mixer could overflow causing artefacts in the sound.
rubidium
parents:
10751
diff
changeset
|
33 |
* The theoretical maximum volume for a single sound sample. Multiple sound |
16d0ea29e638
(svn r13695) -Fix [FS#2120]: in some cases the (sound) mixer could overflow causing artefacts in the sound.
rubidium
parents:
10751
diff
changeset
|
34 |
* samples should not exceed this limit as it will sound too loud. It also |
16d0ea29e638
(svn r13695) -Fix [FS#2120]: in some cases the (sound) mixer could overflow causing artefacts in the sound.
rubidium
parents:
10751
diff
changeset
|
35 |
* stops overflowing when too many sounds are played at the same time, which |
16d0ea29e638
(svn r13695) -Fix [FS#2120]: in some cases the (sound) mixer could overflow causing artefacts in the sound.
rubidium
parents:
10751
diff
changeset
|
36 |
* causes an even worse sound quality. |
16d0ea29e638
(svn r13695) -Fix [FS#2120]: in some cases the (sound) mixer could overflow causing artefacts in the sound.
rubidium
parents:
10751
diff
changeset
|
37 |
*/ |
16d0ea29e638
(svn r13695) -Fix [FS#2120]: in some cases the (sound) mixer could overflow causing artefacts in the sound.
rubidium
parents:
10751
diff
changeset
|
38 |
static const int MAX_VOLUME = 128 * 128; |
16d0ea29e638
(svn r13695) -Fix [FS#2120]: in some cases the (sound) mixer could overflow causing artefacts in the sound.
rubidium
parents:
10751
diff
changeset
|
39 |
|
0 | 40 |
|
41 |
static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples) |
|
42 |
{ |
|
43 |
int8 *b; |
|
44 |
uint32 frac_pos; |
|
45 |
uint32 frac_speed; |
|
11137
16d0ea29e638
(svn r13695) -Fix [FS#2120]: in some cases the (sound) mixer could overflow causing artefacts in the sound.
rubidium
parents:
10751
diff
changeset
|
46 |
int volume_left; |
16d0ea29e638
(svn r13695) -Fix [FS#2120]: in some cases the (sound) mixer could overflow causing artefacts in the sound.
rubidium
parents:
10751
diff
changeset
|
47 |
int volume_right; |
0 | 48 |
|
49 |
if (samples > sc->samples_left) samples = sc->samples_left; |
|
50 |
sc->samples_left -= samples; |
|
51 |
assert(samples > 0); |
|
52 |
||
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
53 |
b = sc->memory + sc->pos; |
0 | 54 |
frac_pos = sc->frac_pos; |
55 |
frac_speed = sc->frac_speed; |
|
56 |
volume_left = sc->volume_left; |
|
57 |
volume_right = sc->volume_right; |
|
58 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
59 |
if (frac_speed == 0x10000) { |
6527
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5838
diff
changeset
|
60 |
/* Special case when frac_speed is 0x10000 */ |
0 | 61 |
do { |
11137
16d0ea29e638
(svn r13695) -Fix [FS#2120]: in some cases the (sound) mixer could overflow causing artefacts in the sound.
rubidium
parents:
10751
diff
changeset
|
62 |
buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME); |
11148 | 63 |
buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME); |
0 | 64 |
b++; |
65 |
buffer += 2; |
|
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
66 |
} while (--samples > 0); |
0 | 67 |
} else { |
68 |
do { |
|
11137
16d0ea29e638
(svn r13695) -Fix [FS#2120]: in some cases the (sound) mixer could overflow causing artefacts in the sound.
rubidium
parents:
10751
diff
changeset
|
69 |
buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME); |
11148 | 70 |
buffer[1] = Clamp(buffer[1] + (*b * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME); |
0 | 71 |
buffer += 2; |
72 |
frac_pos += frac_speed; |
|
73 |
b += frac_pos >> 16; |
|
74 |
frac_pos &= 0xffff; |
|
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
75 |
} while (--samples > 0); |
0 | 76 |
} |
77 |
||
78 |
sc->frac_pos = frac_pos; |
|
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
79 |
sc->pos = b - sc->memory; |
0 | 80 |
} |
81 |
||
82 |
static void MxCloseChannel(MixerChannel *mc) |
|
83 |
{ |
|
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
84 |
if (mc->flags & MX_AUTOFREE) free(mc->memory); |
1497
698be1f59f4b
(svn r2001) Resolve a race condition which could lead to dropped a sound and a memory leak
tron
parents:
1496
diff
changeset
|
85 |
mc->active = false; |
0 | 86 |
mc->memory = NULL; |
87 |
} |
|
88 |
||
2977 | 89 |
void MxMixSamples(void *buffer, uint samples) |
0 | 90 |
{ |
91 |
MixerChannel *mc; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
92 |
|
6527
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5838
diff
changeset
|
93 |
/* Clear the buffer */ |
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
94 |
memset(buffer, 0, sizeof(int16) * 2 * samples); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
95 |
|
6527
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5838
diff
changeset
|
96 |
/* Mix each channel */ |
2977 | 97 |
for (mc = _channels; mc != endof(_channels); mc++) { |
0 | 98 |
if (mc->active) { |
5838
9c3129cb019b
(svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents:
5835
diff
changeset
|
99 |
mix_int8_to_int16(mc, (int16*)buffer, samples); |
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
100 |
if (mc->samples_left == 0) MxCloseChannel(mc); |
0 | 101 |
} |
102 |
} |
|
103 |
} |
|
104 |
||
6573 | 105 |
MixerChannel *MxAllocateChannel() |
0 | 106 |
{ |
107 |
MixerChannel *mc; |
|
2977 | 108 |
for (mc = _channels; mc != endof(_channels); mc++) |
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
109 |
if (mc->memory == NULL) { |
0 | 110 |
mc->active = false; |
111 |
return mc; |
|
112 |
} |
|
113 |
return NULL; |
|
114 |
} |
|
115 |
||
10751
ebd94f2d6385
(svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents:
10429
diff
changeset
|
116 |
void MxSetChannelRawSrc(MixerChannel *mc, int8 *mem, size_t size, uint rate, uint flags) |
0 | 117 |
{ |
118 |
mc->memory = mem; |
|
119 |
mc->flags = flags; |
|
120 |
mc->frac_pos = 0; |
|
121 |
mc->pos = 0; |
|
122 |
||
2977 | 123 |
mc->frac_speed = (rate << 16) / _play_rate; |
0 | 124 |
|
6527
f584ab6d87f8
(svn r8987) -Cleanup: doxygen changes. Again. Mostly (still) @files missing tags and (more than just) a few comments style.
belugas
parents:
5838
diff
changeset
|
125 |
/* adjust the magnitude to prevent overflow */ |
10751
ebd94f2d6385
(svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents:
10429
diff
changeset
|
126 |
while (size & ~0xFFFF) { |
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
127 |
size >>= 1; |
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
128 |
rate = (rate >> 1) + 1; |
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
129 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
130 |
|
10751
ebd94f2d6385
(svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents:
10429
diff
changeset
|
131 |
mc->samples_left = (uint)size * _play_rate / rate; |
0 | 132 |
} |
133 |
||
1496
3d0b86f5dcb8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
134 |
void MxSetChannelVolume(MixerChannel *mc, uint left, uint right) |
0 | 135 |
{ |
136 |
mc->volume_left = left; |
|
137 |
mc->volume_right = right; |
|
138 |
} |
|
139 |
||
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
140 |
|
1498
cbe7edba0316
(svn r2002) Rename MxActivate to MxActivateChannel, which is more appropriate
tron
parents:
1497
diff
changeset
|
141 |
void MxActivateChannel(MixerChannel* mc) |
1496
3d0b86f5dcb8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
142 |
{ |
3d0b86f5dcb8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
143 |
mc->active = true; |
0 | 144 |
} |
145 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
146 |
|
1496
3d0b86f5dcb8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
147 |
bool MxInitialize(uint rate) |
0 | 148 |
{ |
2977 | 149 |
_play_rate = rate; |
0 | 150 |
return true; |
151 |
} |