forked from fuzzie/unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.cpp
338 lines (297 loc) · 8.85 KB
/
sound.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "sound.h"
#include "common/system.h"
#include "common/memstream.h"
#include "common/queue.h"
#include "audio/audiostream.h"
#include "audio/decoders/adpcm_intern.h"
namespace Unity {
class Unity_ADPCMStream : public ::Audio::Ima_ADPCMStream {
protected:
uint32 _loopPoint;
uint32 _currPos;
::Audio::ADPCMStream::ADPCMStatus _loopStatus;
Common::Queue<int16> _samplesBuffer;
public:
Unity_ADPCMStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse, uint32 size, int rate, int channels, uint32 loopPoint = 0)
: Ima_ADPCMStream(stream, disposeAfterUse, size, rate, channels, 0), _loopPoint(loopPoint), _currPos(0) {
memset(&_loopStatus, 0, sizeof(_loopStatus));
}
virtual bool endOfData() const;
virtual int readBuffer(int16 *buffer, const int numSamples);
virtual bool rewind();
};
bool Unity_ADPCMStream::endOfData() const {
return _samplesBuffer.empty() && ADPCMStream::endOfData();
}
int Unity_ADPCMStream::readBuffer(int16 *buffer, const int numSamples) {
bool stereo = _channels == 2;
int samples = 0;
// First, use anything left over from last time.
int16 *target = buffer;
while (!_samplesBuffer.empty() && samples < numSamples) {
*(target++) = _samplesBuffer.pop();
samples++;
}
for (; samples < numSamples && !_stream->eos() && _stream->pos() < _endpos; samples += 4) {
byte data;
int16 block[4];
data = _stream->readByte();
if (_currPos == _loopPoint)
memcpy(&_loopStatus.ima_ch[0], &_status.ima_ch[0], sizeof(_status.ima_ch[0]));
block[0] = decodeIMA(data & 0x0f);
block[stereo ? 2 : 1] = decodeIMA((data >> 4) & 0x0f);
data = _stream->readByte();
if (_currPos + (stereo ? 0 : 2) == _loopPoint)
memcpy(&_loopStatus.ima_ch[stereo ? 1 : 0], &_status.ima_ch[stereo ? 1 : 0], sizeof(_status.ima_ch[0]));
block[stereo ? 1 : 2] = decodeIMA(data & 0x0f, stereo ? 1 : 0);
block[3] = decodeIMA((data >> 4) & 0x0f, stereo ? 1 : 0);
// Put the result in the output buffer, storing anything left.
for (int i = 0; i < 4; i++) {
if (samples + i < numSamples) {
*(target++) = block[i];
} else {
_samplesBuffer.push(block[i]);
}
}
_currPos += 2;
}
if (samples > numSamples)
return numSamples;
return samples;
}
bool Unity_ADPCMStream::rewind() {
if (_stream->pos() < _startpos + (int)_loopPoint)
return true;
memcpy(&_status, &_loopStatus, sizeof(_status));
_stream->seek(_startpos + _loopPoint);
_currPos = _loopPoint;
return true;
}
Sound::Sound(UnityEngine *_engine) : _vm(_engine) {
_speechSoundHandle = NULL;
_sfxSoundHandle = NULL;
_musicSoundHandle = NULL;
}
Sound::~Sound() {
delete _speechSoundHandle;
delete _sfxSoundHandle;
delete _musicSoundHandle;
}
void Sound::init() {
_sfxSoundHandle = new Audio::SoundHandle();
_speechSoundHandle = new Audio::SoundHandle();
_musicSoundHandle = new Audio::SoundHandle();
}
void Sound::playSpeech(Common::String name) {
debug(1, "playing speech: %s", name.c_str());
stopSpeech();
Common::SeekableReadStream *audioFileStream = _vm->data.openFile(name);
Audio::AudioStream *sampleStream = new Unity_ADPCMStream(
audioFileStream, DisposeAfterUse::YES, audioFileStream->size(), 22050, 1);
if (!sampleStream) error("couldn't make sample stream");
_vm->_mixer->playStream(Audio::Mixer::kSpeechSoundType, _speechSoundHandle, sampleStream);
}
void Sound::playSfx(Common::String name) {
debug(1, "playing sfx: %s", name.c_str());
stopSfx();
Common::SeekableReadStream *audioFileStream = _vm->data.openFile(name);
Audio::AudioStream *sampleStream = new Unity_ADPCMStream(
audioFileStream, DisposeAfterUse::YES, audioFileStream->size(), 22050, 1);
if (!sampleStream) error("couldn't make sample stream");
_vm->_mixer->playStream(Audio::Mixer::kSFXSoundType, _sfxSoundHandle, sampleStream);
}
void Sound::playMusic(Common::String name, byte volume, int loopPos) {
debug(1, "playing music: %s, loop %d, vol %d", name.c_str(), loopPos, volume);
Common::SeekableReadStream *audioFileStream = _vm->data.openFile(name);
Audio::RewindableAudioStream *sampleStream = new Unity_ADPCMStream(
audioFileStream, DisposeAfterUse::YES, audioFileStream->size(), 22050, 2, loopPos);
if (!sampleStream) error("couldn't make sample stream");
Audio::AudioStream *audioStream = sampleStream;
if (loopPos != -1)
audioStream = makeLoopingAudioStream(sampleStream, 0);
_vm->_mixer->playStream(Audio::Mixer::kMusicSoundType, _musicSoundHandle, audioStream, -1, volume);
}
bool Sound::sfxPlaying() {
return _vm->_mixer->isSoundHandleActive(*_sfxSoundHandle);
}
bool Sound::musicPlaying() {
return _vm->_mixer->isSoundHandleActive(*_musicSoundHandle);
}
bool Sound::speechPlaying() {
return _vm->_mixer->isSoundHandleActive(*_speechSoundHandle);
}
void Sound::stopSfx() {
_vm->_mixer->stopHandle(*_sfxSoundHandle);
}
void Sound::stopMusic() {
_vm->_mixer->stopHandle(*_musicSoundHandle);
}
void Sound::stopSpeech() {
_vm->_mixer->stopHandle(*_speechSoundHandle);
}
void Sound::playAudioBuffer(unsigned int length, byte *data) {
Common::MemoryReadStream *audioFileStream = new Common::MemoryReadStream(data, length);
Audio::AudioStream *sampleStream = new Unity_ADPCMStream(
audioFileStream, DisposeAfterUse::YES, audioFileStream->size(), 22050, 1);
if (!sampleStream) error("couldn't make sample stream");
_vm->_mixer->playStream(Audio::Mixer::kSFXSoundType, _sfxSoundHandle, sampleStream);
}
void Sound::playIntroMusic() {
stopMusic();
// FIXME
}
void Sound::updateMusic() {
if (musicPlaying())
return;
// This is hard-coded, with loop offsets and volumes.
switch (_vm->data._currentScreen.world) {
case 2:
// Allanor
switch (_vm->data._currentScreen.screen) {
case 1:
case 2:
playMusic("adamb01.rac", 0x1f, 0xab40);
break;
case 3:
playMusic("adamb03.rac", 0x19, 0xae80);
break;
case 4:
case 5:
case 6:
case 7:
playMusic("adamb04.rac", 0x1f, 0xcbc0);
break;
case 8:
case 9:
case 10:
case 12:
playMusic("adamb08.rac", 0x19, 0xa1c0);
break;
default:
playMusic("adamb11.rac", 0x19, 0xab40);
break;
}
break;
case 3:
// Zoo World
switch (_vm->data._currentScreen.screen) {
case 3:
case 10:
playMusic("zootree.rac", 0x30, 0x548e);
break;
case 4:
playMusic("zoolab.rac", 0x30, 0x5b9c);
break;
case 6:
playMusic("zoodesrt.rac", 0x30, 0x8b82);
break;
case 7:
playMusic("zooswamp.rac", 0x30, 0x5c08);
break;
case 8:
playMusic("zoosulfr.rac", 0x30, 0x419c);
break;
case 16:
playMusic("powersta.rac", 0x30, 0xad80);
break;
default:
playMusic("zoobirds.rac", 0x30, 0x1b1ee);
break;
}
break;
case 4:
// Lab (Orbital Station)
switch (_vm->data._currentScreen.screen) {
case 2:
case 3:
case 4:
playMusic("lbbigfar.rac", 0x3f, 0x3160);
break;
case 6:
// Probe room - music depends on whether probe is still present.
if (_vm->data.getObject(objectID(4, 6, 3))->flags & OBJFLAG_ACTIVE)
playMusic("probroom.rac", 0x3f, 0x5a00);
else
playMusic("lbbignr.rac", 0x3f, 0x4ebc);
break;
case 10:
case 11:
playMusic("lbctnear.rac", 0x3f, 0x4896);
break;
default:
playMusic("lbctrfar.rac", 0x3f, 0x4e9c);
break;
}
break;
case 5:
// Frigis
switch (_vm->data._currentScreen.screen) {
case 1:
case 2:
playMusic("s5amb01.rac", 0x1f, 0xa7c0);
break;
case 3:
playMusic("s5amb03.rac", 0x1f, 0xeb80);
break;
case 4:
playMusic("s5amb04.rac", 0x1f, 0xb778);
break;
case 5:
playMusic("s5amb05.rac", 0x1f, 0x9a00);
break;
case 6:
playMusic("s5amb06.rac", 0x1f, 0xb0c0);
break;
case 7:
playMusic("s5amb07.rac", 0x1f, 0xb980);
break;
default:
playMusic("s5amb08.rac", 0x1f, 0x13388);
break;
}
break;
case 6:
// Unity Device
switch (_vm->data._currentScreen.screen) {
case 1:
playMusic("udamb01.rac", 0x1f, 0x4c00);
break;
case 2:
playMusic("udamb02.rac", 0x1f, 0x6c32);
break;
case 6:
playMusic("udamb06.rac", 0x1f, 0x7a40);
break;
case 9:
playMusic("udamb09.rac", 0x1f, 0x62c0);
break;
case 12:
playMusic("udamb12.rac", 0x1f, 0x5c80);
break;
default:
// default to Horst III music
playMusic("h3amb01.rac", 0x2f, 0x162b0);
break;
}
break;
case 7:
// Horst III
playMusic("h3amb01.rac", 0x2f, 0x162b0);
break;
}
}
}