author | bjarni |
Sun, 11 Mar 2007 16:31:18 +0000 | |
branch | cpp_gui |
changeset 6298 | c30fe89622df |
parent 6285 | 187e3ef04cc9 |
child 10429 | 1b99254f9607 |
child 11150 | 4e3726a46a72 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
6285
187e3ef04cc9
(svn r9004) [cpp_gui] -Sync with trunk (r8900..r9003)
KUDr
parents:
5838
diff
changeset
|
3 |
/** @file mixer.cpp*/ |
187e3ef04cc9
(svn r9004) [cpp_gui] -Sync with trunk (r8900..r9003)
KUDr
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" |
0 | 8 |
|
1496
3d0b86f5dcb8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
9 |
struct MixerChannel { |
0 | 10 |
bool active; |
11 |
||
6285
187e3ef04cc9
(svn r9004) [cpp_gui] -Sync with trunk (r8900..r9003)
KUDr
parents:
5838
diff
changeset
|
12 |
/* 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
|
13 |
int8 *memory; |
0 | 14 |
|
6285
187e3ef04cc9
(svn r9004) [cpp_gui] -Sync with trunk (r8900..r9003)
KUDr
parents:
5838
diff
changeset
|
15 |
/* current position in memory */ |
0 | 16 |
uint32 pos; |
17 |
uint32 frac_pos; |
|
18 |
uint32 frac_speed; |
|
19 |
uint32 samples_left; |
|
20 |
||
6285
187e3ef04cc9
(svn r9004) [cpp_gui] -Sync with trunk (r8900..r9003)
KUDr
parents:
5838
diff
changeset
|
21 |
/* Mixing volume */ |
0 | 22 |
uint volume_left; |
23 |
uint volume_right; |
|
24 |
||
25 |
uint flags; |
|
1496
3d0b86f5dcb8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
26 |
}; |
0 | 27 |
|
2977 | 28 |
static MixerChannel _channels[8]; |
29 |
static uint32 _play_rate; |
|
0 | 30 |
|
31 |
||
32 |
static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples) |
|
33 |
{ |
|
34 |
int8 *b; |
|
35 |
uint32 frac_pos; |
|
36 |
uint32 frac_speed; |
|
37 |
uint volume_left; |
|
38 |
uint volume_right; |
|
39 |
||
40 |
if (samples > sc->samples_left) samples = sc->samples_left; |
|
41 |
sc->samples_left -= samples; |
|
42 |
assert(samples > 0); |
|
43 |
||
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
44 |
b = sc->memory + sc->pos; |
0 | 45 |
frac_pos = sc->frac_pos; |
46 |
frac_speed = sc->frac_speed; |
|
47 |
volume_left = sc->volume_left; |
|
48 |
volume_right = sc->volume_right; |
|
49 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
50 |
if (frac_speed == 0x10000) { |
6285
187e3ef04cc9
(svn r9004) [cpp_gui] -Sync with trunk (r8900..r9003)
KUDr
parents:
5838
diff
changeset
|
51 |
/* Special case when frac_speed is 0x10000 */ |
0 | 52 |
do { |
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
53 |
buffer[0] += *b * volume_left >> 8; |
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
54 |
buffer[1] += *b * volume_right >> 8; |
0 | 55 |
b++; |
56 |
buffer += 2; |
|
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
57 |
} while (--samples > 0); |
0 | 58 |
} else { |
59 |
do { |
|
60 |
buffer[0] += *b * volume_left >> 8; |
|
61 |
buffer[1] += *b * volume_right >> 8; |
|
62 |
buffer += 2; |
|
63 |
frac_pos += frac_speed; |
|
64 |
b += frac_pos >> 16; |
|
65 |
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
|
66 |
} while (--samples > 0); |
0 | 67 |
} |
68 |
||
69 |
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
|
70 |
sc->pos = b - sc->memory; |
0 | 71 |
} |
72 |
||
73 |
static void MxCloseChannel(MixerChannel *mc) |
|
74 |
{ |
|
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
75 |
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
|
76 |
mc->active = false; |
0 | 77 |
mc->memory = NULL; |
78 |
} |
|
79 |
||
2977 | 80 |
void MxMixSamples(void *buffer, uint samples) |
0 | 81 |
{ |
82 |
MixerChannel *mc; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
83 |
|
6285
187e3ef04cc9
(svn r9004) [cpp_gui] -Sync with trunk (r8900..r9003)
KUDr
parents:
5838
diff
changeset
|
84 |
/* 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
|
85 |
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
|
86 |
|
6285
187e3ef04cc9
(svn r9004) [cpp_gui] -Sync with trunk (r8900..r9003)
KUDr
parents:
5838
diff
changeset
|
87 |
/* Mix each channel */ |
2977 | 88 |
for (mc = _channels; mc != endof(_channels); mc++) { |
0 | 89 |
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
|
90 |
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
|
91 |
if (mc->samples_left == 0) MxCloseChannel(mc); |
0 | 92 |
} |
93 |
} |
|
94 |
} |
|
95 |
||
6298
c30fe89622df
(svn r9119) [cpp_gui] -Sync with trunk (r9003:9100)
bjarni
parents:
6285
diff
changeset
|
96 |
MixerChannel *MxAllocateChannel() |
0 | 97 |
{ |
98 |
MixerChannel *mc; |
|
2977 | 99 |
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
|
100 |
if (mc->memory == NULL) { |
0 | 101 |
mc->active = false; |
102 |
return mc; |
|
103 |
} |
|
104 |
return NULL; |
|
105 |
} |
|
106 |
||
1496
3d0b86f5dcb8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
107 |
void MxSetChannelRawSrc(MixerChannel *mc, int8 *mem, uint size, uint rate, uint flags) |
0 | 108 |
{ |
109 |
mc->memory = mem; |
|
110 |
mc->flags = flags; |
|
111 |
mc->frac_pos = 0; |
|
112 |
mc->pos = 0; |
|
113 |
||
2977 | 114 |
mc->frac_speed = (rate << 16) / _play_rate; |
0 | 115 |
|
6285
187e3ef04cc9
(svn r9004) [cpp_gui] -Sync with trunk (r8900..r9003)
KUDr
parents:
5838
diff
changeset
|
116 |
/* adjust the magnitude to prevent overflow */ |
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
117 |
while (size & 0xFFFF0000) { |
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
118 |
size >>= 1; |
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
119 |
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
|
120 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
121 |
|
2977 | 122 |
mc->samples_left = size * _play_rate / rate; |
0 | 123 |
} |
124 |
||
1496
3d0b86f5dcb8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
125 |
void MxSetChannelVolume(MixerChannel *mc, uint left, uint right) |
0 | 126 |
{ |
127 |
mc->volume_left = left; |
|
128 |
mc->volume_right = right; |
|
129 |
} |
|
130 |
||
337
66647f97e7c0
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
131 |
|
1498
cbe7edba0316
(svn r2002) Rename MxActivate to MxActivateChannel, which is more appropriate
tron
parents:
1497
diff
changeset
|
132 |
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
|
133 |
{ |
3d0b86f5dcb8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
134 |
mc->active = true; |
0 | 135 |
} |
136 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
137 |
|
1496
3d0b86f5dcb8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
138 |
bool MxInitialize(uint rate) |
0 | 139 |
{ |
2977 | 140 |
_play_rate = rate; |
0 | 141 |
return true; |
142 |
} |