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
|
/* Copyright (C) 1994-1998 Revolution Software Ltd.
* Copyright (C) 2003-2006 The ScummVM project
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*/
// ---------------------------------------------------------------------------
// BROKEN SWORD 2
//
// SOUND.CPP Contains the sound engine, fx & music functions
// Some very 'sound' code in here ;)
//
// (16Dec96 JEL)
//
// ---------------------------------------------------------------------------
#include "common/stdafx.h"
#include "common/file.h"
#include "common/system.h"
#include "sword2/sword2.h"
#include "sword2/defs.h"
#include "sword2/header.h"
#include "sword2/logic.h"
#include "sword2/resman.h"
#include "sword2/sound.h"
#include "sound/wave.h"
namespace Sword2 {
Sound::Sound(Sword2Engine *vm) {
int i;
_vm = vm;
for (i = 0; i < FXQ_LENGTH; i++)
_fxQueue[i].resource = 0;
for (i = 0; i < MAXMUS; i++) {
_music[i] = NULL;
_musicFile[i].idxTab = NULL;
_musicFile[i].idxLen = 0;
_musicFile[i].fileSize = 0;
_musicFile[i].fileType = 0;
_musicFile[i].inUse = false;
_speechFile[i].idxTab = NULL;
_speechFile[i].idxLen = 0;
_speechFile[i].fileSize = 0;
_speechFile[i].fileType = 0;
_speechFile[i].inUse = false;
}
_speechPaused = false;
_musicPaused = false;
_fxPaused = false;
_speechMuted = false;
_musicMuted = false;
_fxMuted = false;
_reverseStereo = false;
_loopingMusicId = 0;
_mixBuffer = NULL;
_mixBufferLen = 0;
_vm->_mixer->setupPremix(this, Audio::Mixer::kMusicSoundType);
}
Sound::~Sound() {
_vm->_mixer->setupPremix(0);
clearFxQueue();
stopMusic(true);
stopSpeech();
free(_mixBuffer);
for (int i = 0; i < MAXMUS; i++) {
if (_musicFile[i].file.isOpen())
_musicFile[i].file.close();
if (_speechFile[i].file.isOpen())
_speechFile[i].file.close();
free(_musicFile[i].idxTab);
free(_speechFile[i].idxTab);
}
}
void Sound::setReverseStereo(bool reverse) {
if (reverse != _reverseStereo) {
_reverseStereo = reverse;
for (int i = 0; i < FXQ_LENGTH; i++) {
if (!_fxQueue[i].resource)
continue;
_fxQueue[i].pan = -_fxQueue[i].pan;
_vm->_mixer->setChannelBalance(_fxQueue[i].handle, _fxQueue[i].pan);
}
}
}
/**
* Stop all sounds, close their resources and clear the FX queue.
*/
void Sound::clearFxQueue() {
for (int i = 0; i < FXQ_LENGTH; i++) {
if (_fxQueue[i].resource) {
stopFx(i);
}
}
}
/**
* Process the FX queue. This function is called once every game cycle.
*/
void Sound::processFxQueue() {
for (int i = 0; i < FXQ_LENGTH; i++) {
if (!_fxQueue[i].resource)
continue;
switch (_fxQueue[i].type) {
case FX_RANDOM:
// 1 in 'delay' chance of this fx occurring
if (_vm->_rnd.getRandomNumber(_fxQueue[i].delay) == 0)
playFx(&_fxQueue[i]);
break;
case FX_SPOT:
if (_fxQueue[i].delay)
_fxQueue[i].delay--;
else {
playFx(&_fxQueue[i]);
_fxQueue[i].type = FX_SPOT2;
}
break;
case FX_LOOP:
playFx(&_fxQueue[i]);
_fxQueue[i].type = FX_LOOPING;
break;
case FX_SPOT2:
// Once the FX has finished remove it from the queue.
if (!_vm->_mixer->isSoundHandleActive(_fxQueue[i].handle)) {
_vm->_resman->closeResource(_fxQueue[i].resource);
_fxQueue[i].resource = 0;
}
break;
case FX_LOOPING:
// Once the looped FX has started we can ignore it,
// but we can't close it since the WAV data is in use.
break;
}
}
}
/**
* Queue a sound effect for playing later.
* @param res the sound resource number
* @param type the type of sound effect
* @param delay when to play the sound effect
* @param volume the sound effect volume (0 through 16)
* @param pan the sound effect panning (-16 through 16)
*/
void Sound::queueFx(int32 res, int32 type, int32 delay, int32 volume, int32 pan) {
if (_vm->_wantSfxDebug) {
const char *typeStr;
switch (type) {
case FX_SPOT:
typeStr = "SPOT";
break;
case FX_LOOP:
typeStr = "LOOPED";
break;
case FX_RANDOM:
typeStr = "RANDOM";
break;
default:
typeStr = "INVALID";
break;
}
byte buf[NAME_LEN];
debug(0, "SFX (sample=\"%s\", vol=%d, pan=%d, delay=%d, type=%s)", _vm->_resman->fetchName(res, buf), volume, pan, delay, typeStr);
}
for (int i = 0; i < FXQ_LENGTH; i++) {
if (!_fxQueue[i].resource) {
byte *data = _vm->_resman->openResource(res);
assert(_vm->_resman->fetchType(data) == WAV_FILE);
uint32 len = _vm->_resman->fetchLen(res) - ResHeader::size();
if (type == FX_RANDOM) {
// For spot effects and loops the delay is the
// number of frames to wait. For random
// effects, however, it's the average number of
// seconds between playing the sound, so we
// have to multiply by the frame rate.
delay *= 12;
}
volume = (volume * Audio::Mixer::kMaxChannelVolume) / 16;
pan = (pan * 127) / 16;
if (isReverseStereo())
pan = -pan;
_fxQueue[i].resource = res;
_fxQueue[i].data = data + ResHeader::size();
_fxQueue[i].len = len;
_fxQueue[i].delay = delay;
_fxQueue[i].volume = volume;
_fxQueue[i].pan = pan;
_fxQueue[i].type = type;
// Keep track of the index in the loop so that
// fnStopFx() can be used later to kill this sound.
// Mainly for FX_LOOP and FX_RANDOM.
_vm->_logic->writeVar(RESULT, i);
return;
}
}
warning("No free slot in FX queue");
}
int32 Sound::playFx(FxQueueEntry *fx) {
return playFx(&fx->handle, fx->data, fx->len, fx->volume, fx->pan, (fx->type == FX_LOOP), Audio::Mixer::kSFXSoundType);
}
int32 Sound::playFx(Audio::SoundHandle *handle, byte *data, uint32 len, uint8 vol, int8 pan, bool loop, Audio::Mixer::SoundType soundType) {
if (_fxMuted)
return RD_OK;
if (_vm->_mixer->isSoundHandleActive(*handle))
return RDERR_FXALREADYOPEN;
Common::MemoryReadStream stream(data, len);
int rate, size;
byte flags;
if (!loadWAVFromStream(stream, size, rate, flags)) {
warning("playFX: Not a valid WAV file");
return RDERR_INVALIDWAV;
}
// The resource manager must have complete control over when resources
// are freed, or reference counting will break horribly. Besides, the
// data pointer is not valid for passing to free(). Why the hell is the
// AUTOFREE flag set by default anyway?
flags &= ~Audio::Mixer::FLAG_AUTOFREE;
if (isReverseStereo())
flags |= Audio::Mixer::FLAG_REVERSE_STEREO;
if (loop)
flags |= Audio::Mixer::FLAG_LOOP;
_vm->_mixer->playRaw(handle, data + stream.pos(), size, rate, flags, -1, vol, pan, 0, 0, soundType);
return RD_OK;
}
/**
* This function closes a sound effect which has been previously opened for
* playing. Sound effects must be closed when they are finished with, otherwise
* you will run out of sound effect buffers.
* @param i the index of the sound to close
*/
int32 Sound::stopFx(int32 i) {
if (!_fxQueue[i].resource)
return RDERR_FXNOTOPEN;
_vm->_mixer->stopHandle(_fxQueue[i].handle);
_vm->_resman->closeResource(_fxQueue[i].resource);
_fxQueue[i].resource = 0;
return RD_OK;
}
void Sound::pauseAllSound() {
pauseMusic();
pauseSpeech();
pauseFx();
}
void Sound::unpauseAllSound() {
unpauseMusic();
unpauseSpeech();
unpauseFx();
}
} // End of namespace Sword2
|