-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixer.cpp
165 lines (139 loc) · 4.62 KB
/
mixer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* Raw - Another World Interpreter
* Copyright (C) 2004 Gregory Montoir
*
* This program 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; either version 2
* of the License, or (at your option) any later version.
* This program 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. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "mixer.h"
#include "serializer.h"
#include "sys.h"
static int8 addclamp(int a, int b) {
int add = a + b;
if (add < -128) {
add = -128;
}
else if (add > 127) {
add = 127;
}
return (int8)add;
}
Mixer::Mixer(System *stub)
: sys(stub) {
}
void Mixer::init() {
memset(_channels, 0, sizeof(_channels));
_mutex = sys->createMutex();
sys->startAudio(Mixer::mixCallback, this);
}
void Mixer::free() {
stopAll();
sys->stopAudio();
sys->destroyMutex(_mutex);
}
void Mixer::playChannel(uint8 channel, const MixerChunk *mc, uint16 freq, uint8 volume) {
debug(DBG_SND, "Mixer::playChannel(%d, %d, %d)", channel, freq, volume);
assert(channel < AUDIO_NUM_CHANNELS);
// The mutex is acquired in the constructor
MutexStack(sys, _mutex);
MixerChannel *ch = &_channels[channel];
ch->active = true;
ch->volume = volume;
ch->chunk = *mc;
ch->chunkPos = 0;
ch->chunkInc = (freq << 8) / sys->getOutputSampleRate();
//At the end of the scope the MutexStack destructor is called and the mutex is released.
}
void Mixer::stopChannel(uint8 channel) {
debug(DBG_SND, "Mixer::stopChannel(%d)", channel);
assert(channel < AUDIO_NUM_CHANNELS);
MutexStack(sys, _mutex);
_channels[channel].active = false;
}
void Mixer::setChannelVolume(uint8 channel, uint8 volume) {
debug(DBG_SND, "Mixer::setChannelVolume(%d, %d)", channel, volume);
assert(channel < AUDIO_NUM_CHANNELS);
MutexStack(sys, _mutex);
_channels[channel].volume = volume;
}
void Mixer::stopAll() {
debug(DBG_SND, "Mixer::stopAll()");
MutexStack(sys, _mutex);
for (uint8 i = 0; i < AUDIO_NUM_CHANNELS; ++i) {
_channels[i].active = false;
}
}
// This is SDL callback. Called in order to populate the buf with len bytes.
// The mixer iterates through all active channels and combine all sounds.
// Since there is no way to know when SDL will ask for a buffer fill, we need
// to synchronize with a mutex so the channels remain stable during the execution
// of this method.
void Mixer::mix(int8 *buf, int len) {
MutexStack(sys, _mutex);
//Clear the buffer since nothing garanty we are receiving clean memory.
memset(buf, 0, len);
for (uint8 i = 0; i < AUDIO_NUM_CHANNELS; ++i) {
MixerChannel *ch = &_channels[i];
if (!ch->active)
continue;
int8 *pBuf = buf;
for (int j = 0; j < len; ++j, ++pBuf) {
uint16 p1, p2;
uint16 ilc = (ch->chunkPos & 0xFF);
p1 = ch->chunkPos >> 8;
ch->chunkPos += ch->chunkInc;
if (ch->chunk.loopLen != 0) {
if (p1 == ch->chunk.loopPos + ch->chunk.loopLen - 1) {
debug(DBG_SND, "Looping sample on channel %d", i);
ch->chunkPos = p2 = ch->chunk.loopPos;
} else {
p2 = p1 + 1;
}
} else {
if (p1 == ch->chunk.len - 1) {
debug(DBG_SND, "Stopping sample on channel %d", i);
ch->active = false;
break;
} else {
p2 = p1 + 1;
}
}
// interpolate
int8 b1 = *(int8 *)(ch->chunk.data + p1);
int8 b2 = *(int8 *)(ch->chunk.data + p2);
int8 b = (int8)((b1 * (0xFF - ilc) + b2 * ilc) >> 8);
// set volume and clamp
*pBuf = addclamp(*pBuf, (int)b * ch->volume / 0x40); //0x40=64
}
}
}
void Mixer::mixCallback(void *param, uint8 *buf, int len) {
((Mixer *)param)->mix((int8 *)buf, len);
}
void Mixer::saveOrLoad(Serializer &ser) {
sys->lockMutex(_mutex);
for (int i = 0; i < AUDIO_NUM_CHANNELS; ++i) {
MixerChannel *ch = &_channels[i];
Serializer::Entry entries[] = {
SE_INT(&ch->active, Serializer::SES_BOOL, VER(2)),
SE_INT(&ch->volume, Serializer::SES_INT8, VER(2)),
SE_INT(&ch->chunkPos, Serializer::SES_INT32, VER(2)),
SE_INT(&ch->chunkInc, Serializer::SES_INT32, VER(2)),
SE_PTR(&ch->chunk.data, VER(2)),
SE_INT(&ch->chunk.len, Serializer::SES_INT16, VER(2)),
SE_INT(&ch->chunk.loopPos, Serializer::SES_INT16, VER(2)),
SE_INT(&ch->chunk.loopLen, Serializer::SES_INT16, VER(2)),
SE_END()
};
ser.saveOrLoadEntries(entries);
}
sys->unlockMutex(_mutex);
};