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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
/* ScummVM - Scumm Interpreter
* Copyright (C) 2001-2003 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
#include "stdafx.h"
#include "common/file.h"
#include "common/util.h"
#include "simon/sound.h"
#include "sound/voc.h"
namespace Simon {
#define SOUND_BIG_ENDIAN true
class BaseSound {
protected:
File *_file;
uint32 *_offsets;
SoundMixer *_mixer;
public:
BaseSound(SoundMixer *mixer, File *file, uint32 base = 0, bool bigendian = false);
BaseSound(SoundMixer *mixer, File *file, uint32 *offsets, bool bigendian = false);
virtual ~BaseSound();
virtual void playSound(uint sound, PlayingSoundHandle *handle, byte flags) = 0;
};
class WavSound : public BaseSound {
public:
WavSound(SoundMixer *mixer, File *file, uint32 base = 0, bool bigendian = false) : BaseSound(mixer, file, base, bigendian) {};
WavSound(SoundMixer *mixer, File *file, uint32 *offsets) : BaseSound(mixer, file, offsets) {};
void playSound(uint sound, PlayingSoundHandle *handle, byte flags);
};
class VocSound : public BaseSound {
public:
VocSound(SoundMixer *mixer, File *file, uint32 base = 0, bool bigendian = false) : BaseSound(mixer, file, base, bigendian) {};
void playSound(uint sound, PlayingSoundHandle *handle, byte flags);
};
class RawSound : public BaseSound {
public:
RawSound(SoundMixer *mixer, File *file, uint32 base = 0, bool bigendian = false) : BaseSound(mixer, file, base, bigendian) {};
void playSound(uint sound, PlayingSoundHandle *handle, byte flags);
};
BaseSound::BaseSound(SoundMixer *mixer, File *file, uint32 base, bool bigendian) {
_mixer = mixer;
_file = file;
uint res = 0;
uint32 size;
_file->seek(base + sizeof(uint32), SEEK_SET);
if (bigendian)
size = _file->readUint32BE();
else
size = _file->readUint32LE();
res = size / sizeof(uint32);
_offsets = (uint32 *)malloc(size + sizeof(uint32));
_file->seek(base, SEEK_SET);
if (_file->read(_offsets, size) != size)
error("Can't read offsets");
for (uint i = 0; i < res; i++) {
#if defined(SCUMM_BIG_ENDIAN)
if (!(bigendian))
_offsets[i] = FROM_LE_32(_offsets[i]);
#endif
if (bigendian)
_offsets[i] = TO_BE_32(_offsets[i]);
_offsets[i] += base;
}
// only needed for mp3
_file->seek(0, SEEK_END);
_offsets[res] = _file->pos();
}
BaseSound::BaseSound(SoundMixer *mixer, File *file, uint32 *offsets, bool bigendian) {
_mixer = mixer;
_file = file;
_offsets = offsets;
}
BaseSound::~BaseSound() {
free(_offsets);
delete _file;
}
#if !defined(__GNUC__)
#pragma START_PACK_STRUCTS
#endif
struct WaveHeader {
uint32 riff;
uint32 unk;
uint32 wave;
uint32 fmt;
uint32 size;
uint16 format_tag;
uint16 channels;
uint32 samples_per_sec;
uint32 avg_bytes;
uint16 block_align;
uint16 bits_per_sample;
} GCC_PACK;
#if !defined(__GNUC__)
#pragma END_PACK_STRUCTS
#endif
void WavSound::playSound(uint sound, PlayingSoundHandle *handle, byte flags) {
if (_offsets == NULL)
return;
WaveHeader wave_hdr;
uint32 data[2];
flags |= SoundMixer::FLAG_AUTOFREE;
_file->seek(_offsets[sound], SEEK_SET);
if (_file->read(&wave_hdr, sizeof(wave_hdr)) != sizeof(wave_hdr) ||
wave_hdr.riff != MKID('RIFF') || wave_hdr.wave != MKID('WAVE')
|| wave_hdr.fmt != MKID('fmt ') || READ_LE_UINT16(&wave_hdr.format_tag) != 1
|| READ_LE_UINT16(&wave_hdr.channels) != 1
|| READ_LE_UINT16(&wave_hdr.bits_per_sample) != 8) {
error("playWav(%d): can't read RIFF header", sound);
}
_file->seek(FROM_LE_32(wave_hdr.size) - sizeof(wave_hdr) + 20, SEEK_CUR);
data[0] = _file->readUint32LE();
data[1] = _file->readUint32LE();
if (//fread(data, sizeof(data), 1, sound_file) != 1 ||
data[0] != 'atad') {
error("playWav(%d): can't read data header", sound);
}
byte *buffer = (byte *)malloc(data[1]);
_file->read(buffer, data[1]);
_mixer->playRaw(handle, buffer, data[1], FROM_LE_32(wave_hdr.samples_per_sec), flags);
}
void VocSound::playSound(uint sound, PlayingSoundHandle *handle, byte flags) {
if (_offsets == NULL)
return;
VocHeader voc_hdr;
VocBlockHeader voc_block_hdr;
uint32 size;
flags |= SoundMixer::FLAG_AUTOFREE;
_file->seek(_offsets[sound], SEEK_SET);
if (_file->read(&voc_hdr, sizeof(voc_hdr)) != sizeof(voc_hdr) ||
strncmp((char *)voc_hdr.desc, "Creative Voice File\x1A", 10) != 0) {
error("playVoc(%d): can't read voc header", sound);
}
_file->read(&voc_block_hdr, sizeof(voc_block_hdr));
size = voc_block_hdr.size[0] + (voc_block_hdr.size[1] << 8) + (voc_block_hdr.size[2] << 16) - 2;
uint32 samples_per_sec;
/* workaround for voc weakness */
samples_per_sec = getSampleRateFromVOCRate(voc_block_hdr.sr);
byte *buffer = (byte *)malloc(size);
_file->read(buffer, size);
_mixer->playRaw(handle, buffer, size, samples_per_sec, flags);
}
void RawSound::playSound(uint sound, PlayingSoundHandle *handle, byte flags) {
if (_offsets == NULL)
return;
flags |= SoundMixer::FLAG_AUTOFREE;
_file->seek(_offsets[sound], SEEK_SET);
uint size = _file->readUint32BE();
byte *buffer = (byte *)malloc(size);
_file->read(buffer, size);
_mixer->playRaw(handle, buffer, size, 22050, flags);
}
#ifdef USE_MAD
class MP3Sound : public BaseSound {
public:
MP3Sound(SoundMixer *mixer, File *file, uint32 base = 0) : BaseSound(mixer, file, base) {};
void playSound(uint sound, PlayingSoundHandle *handle, byte flags);
};
void MP3Sound::playSound(uint sound, PlayingSoundHandle *handle, byte flags)
{
if (_offsets == NULL)
return;
_file->seek(_offsets[sound], SEEK_SET);
uint32 size = _offsets[sound+1] - _offsets[sound];
_mixer->playMP3(handle, _file, size);
}
#endif
#ifdef USE_VORBIS
class VorbisSound : public BaseSound {
public:
VorbisSound(SoundMixer *mixer, File *file, uint32 base = 0) : BaseSound(mixer, file, base) {};
void playSound(uint sound, PlayingSoundHandle *handle, byte flags);
};
void VorbisSound::playSound(uint sound, PlayingSoundHandle *handle, byte flags)
{
if (_offsets == NULL)
return;
_file->seek(_offsets[sound], SEEK_SET);
uint32 size = _offsets[sound+1] - _offsets[sound];
_mixer->playVorbis(handle, _file, size);
}
#endif
SimonSound::SimonSound(const byte game, const GameSpecificSettings *gss, const Common::String &gameDataPath, SoundMixer *mixer)
: _game(game), _gameDataPath(gameDataPath), _mixer(mixer) {
_voice = 0;
_effects = 0;
_effects_paused = false;
_ambient_paused = false;
_filenums = 0;
_last_voice_file = 0;
_offsets = 0;
_voice_handle = 0;
_effects_handle = 0;
_ambient_handle = 0;
_voice_file = false;
_ambient_playing = 0;
File *file = new File();
const char *s;
#ifdef USE_MAD
if (!_voice && gss->mp3_filename && gss->mp3_filename[0]) {
file->open(gss->mp3_filename, gameDataPath);
if (file->isOpen()) {
_voice_file = true;
_voice = new MP3Sound(_mixer, file);
}
}
#endif
#ifdef USE_VORBIS
if (!_voice && gss->vorbis_filename && gss->vorbis_filename[0]) {
file->open(gss->vorbis_filename, gameDataPath);
if (file->isOpen()) {
_voice_file = true;
_voice = new VorbisSound(_mixer, file);
}
}
#endif
if (!_voice) {
// for simon2 mac/amiga, only read index file
if (_game == GAME_SIMON2MAC) {
file->open("voices.idx", gameDataPath);
if (file->isOpen() == false) {
warning("Can't open voice index file 'voices.idx'");
} else {
file->seek(0, SEEK_END);
int end = file->pos();
file->seek(0, SEEK_SET);
_filenums = (uint16 *)malloc(end / 3 + 1);
_offsets = (uint32 *)malloc((end / 6) * 4 + 1);
for (int i = 1; i <= end / 6; i++) {
_filenums[i] = file->readUint16BE();
_offsets[i] = file->readUint32BE();
}
_voice_file = true;
}
delete file;
} else if (_game & GF_WIN) {
s = gss->wav_filename;
file->open(s, gameDataPath);
if (file->isOpen() == false) {
warning("Can't open voice file %s", s);
delete file;
} else {
_voice_file = true;
_voice = new WavSound(_mixer, file);
}
} else if (_game == GAME_SIMON1CD32) {
// simon1cd32 uses separate voice files
return;
} else if (_game & GF_TALKIE) {
s = gss->voc_filename;
file->open(s, gameDataPath);
if (file->isOpen() == false) {
warning("Can't open voice file %s", s);
delete file;
} else {
_voice_file = true;
_voice = new VocSound(_mixer, file);
}
}
}
if (_game == GAME_SIMON1ACORN || _game == GAME_SIMON1TALKIE) {
file = new File();
#ifdef USE_MAD
if (!_effects && gss->mp3_effects_filename && gss->mp3_effects_filename[0]) {
file->open(gss->mp3_effects_filename, gameDataPath);
if (file->isOpen()) {
_effects = new MP3Sound(_mixer, file);
}
}
#endif
#ifdef USE_VORBIS
if (!_effects && gss->vorbis_effects_filename && gss->vorbis_effects_filename[0]) {
file->open(gss->vorbis_effects_filename, gameDataPath);
if (file->isOpen()) {
_effects = new VorbisSound(_mixer, file);
}
}
#endif
if (!_effects) {
s = gss->voc_effects_filename;
file->open(s, gameDataPath);
if (file->isOpen() == false) {
warning("Can't open effects file %s", s);
} else {
_effects = new VocSound(_mixer, file);
}
}
}
}
SimonSound::~SimonSound() {
delete _voice;
delete _effects;
free(_filenums);
free(_offsets);
}
void SimonSound::readSfxFile(const char *filename, const Common::String &gameDataPath) {
stopAll();
File *file = new File();
file->open(filename, gameDataPath);
if (file->isOpen() == false) {
char *filename2;
filename2 = (char *)malloc(strlen(filename) + 2);
strcpy(filename2, filename);
strcat(filename2, ".");
file->open(filename2, gameDataPath);
free(filename2);
if (file->isOpen() == false) {
if (atoi(filename + 6) != 1 && atoi(filename + 6) != 30)
warning("readSfxFile: Can't load sfx file %s", filename);
return;
}
}
delete _effects;
if (_game == GAME_SIMON1CD32) {
_effects = new VocSound(_mixer, file, 0, SOUND_BIG_ENDIAN);
} else
_effects = new WavSound(_mixer, file);
}
void SimonSound::loadSfxTable(File *gameFile, uint32 base) {
stopAll();
if (_game & GF_WIN)
_effects = new WavSound(_mixer, gameFile, base);
else
_effects = new VocSound(_mixer, gameFile, base);
}
void SimonSound::readVoiceFile(const char *filename, const Common::String &gameDataPath) {
stopAll();
File *file = new File();
file->open(filename, gameDataPath);
if (file->isOpen() == false) {
char *filename2;
filename2 = (char *)malloc(strlen(filename) + 2);
strcpy(filename2, filename);
strcat(filename2, ".");
file->open(filename2, gameDataPath);
free(filename2);
if (file->isOpen() == false) {
warning("readVoiceFile: Can't load voice file %s", filename);
return;
}
}
delete _voice;
_voice = new RawSound(_mixer, file, 0, SOUND_BIG_ENDIAN);
}
void SimonSound::playVoice(uint sound) {
if (_game == GAME_SIMON2MAC && _filenums) {
if (_last_voice_file != _filenums[sound]) {
stopAll();
char filename[16];
_last_voice_file = _filenums[sound];
sprintf(filename, "voices%d.dat", _filenums[sound]);
File *file = new File();
file->open(filename, _gameDataPath);
if (file->isOpen() == false) {
warning("playVoice: Can't load voice file %s", filename);
return;
}
// FIXME freeing voice at this point causes frequent game crashes
// Maybe related to sound effects and speech using same sound format ?
// In any case, this means we currently leak.
// delete _voice;
_voice = new WavSound(_mixer, file, _offsets);
}
}
if (!_voice)
return;
_mixer->stopHandle(_voice_handle);
_voice->playSound(sound, &_voice_handle, (_game == GAME_SIMON1CD32) ? 0 : SoundMixer::FLAG_UNSIGNED);
}
void SimonSound::playEffects(uint sound) {
if (!_effects)
return;
if (_effects_paused)
return;
_effects->playSound(sound, &_effects_handle, (_game == GAME_SIMON1CD32) ? 0 : SoundMixer::FLAG_UNSIGNED);
}
void SimonSound::playAmbient(uint sound) {
if (!_effects)
return;
if (sound == _ambient_playing)
return;
_ambient_playing = sound;
if (_ambient_paused)
return;
_mixer->stopHandle(_ambient_handle);
_effects->playSound(sound, &_ambient_handle, SoundMixer::FLAG_LOOP|SoundMixer::FLAG_UNSIGNED);
}
bool SimonSound::hasVoice() {
return _voice_file;
}
void SimonSound::stopVoice() {
_mixer->stopHandle(_voice_handle);
}
void SimonSound::stopAll() {
_mixer->stopAll();
_ambient_playing = 0;
}
void SimonSound::effectsPause(bool b) {
_effects_paused = b;
}
void SimonSound::ambientPause(bool b) {
_ambient_paused = b;
if (_ambient_paused && _ambient_playing) {
_mixer->stopHandle(_ambient_handle);
} else if (_ambient_playing) {
uint tmp = _ambient_playing;
_ambient_playing = 0;
playAmbient(tmp);
}
}
} // End of namespace Simon
|