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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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.
*
*/
#include "common/debug.h"
#include "common/events.h"
#include "common/system.h"
#include "common/textconsole.h"
#include "audio/mididrv.h"
#include "audio/midiparser.h"
#include "audio/audiostream.h"
#include "audio/decoders/adpcm.h"
#include "audio/decoders/mp3.h"
#include "audio/decoders/raw.h"
#include "audio/decoders/wave.h"
#include "mohawk/sound.h"
namespace Mohawk {
Audio::RewindableAudioStream *makeMohawkWaveStream(Common::SeekableReadStream *stream, CueList *cueList) {
uint32 tag = 0;
ADPCMStatus adpcmStatus;
DataChunk dataChunk;
uint32 dataSize = 0;
memset(&dataChunk, 0, sizeof(DataChunk));
if (stream->readUint32BE() != ID_MHWK) // MHWK tag again
error ("Could not find tag 'MHWK'");
stream->readUint32BE(); // Skip size
if (stream->readUint32BE() != ID_WAVE)
error ("Could not find tag 'WAVE'");
while (!dataChunk.audioData) {
tag = stream->readUint32BE();
switch (tag) {
case ID_ADPC:
debug(2, "Found Tag ADPC");
// ADPCM Sound Only
//
// This is useful for seeking in the stream, and is actually quite brilliant
// considering some of the other things Broderbund did with the engine.
// Only Riven and CSTime are known to use ADPCM audio and only CSTime
// actually requires this for seeking. On the other hand, it may be interesting
// to look at that one Riven sample that uses the cue points.
//
// Basically, the sample frame from the cue list is looked up here and then
// sets the starting sample and step index at the point specified. Quite
// an elegant/efficient system, really.
adpcmStatus.size = stream->readUint32BE();
adpcmStatus.itemCount = stream->readUint16BE();
adpcmStatus.channels = stream->readUint16BE();
adpcmStatus.statusItems = new ADPCMStatus::StatusItem[adpcmStatus.itemCount];
assert(adpcmStatus.channels <= 2);
for (uint16 i = 0; i < adpcmStatus.itemCount; i++) {
adpcmStatus.statusItems[i].sampleFrame = stream->readUint32BE();
for (uint16 j = 0; j < adpcmStatus.channels; j++) {
adpcmStatus.statusItems[i].channelStatus[j].last = stream->readSint16BE();
adpcmStatus.statusItems[i].channelStatus[j].stepIndex = stream->readUint16BE();
}
}
// TODO: Actually use this chunk. For now, just delete the status items...
delete[] adpcmStatus.statusItems;
break;
case ID_CUE:
debug(2, "Found Tag Cue#");
// Cues are used for animation sync. There are a couple in Myst and
// Riven but are not used there at all.
if (!cueList) {
uint32 size = stream->readUint32BE();
stream->skip(size);
break;
}
cueList->size = stream->readUint32BE();
cueList->pointCount = stream->readUint16BE();
if (cueList->pointCount == 0)
debug(2, "Cue# chunk found with no points!");
else
debug(2, "Cue# chunk found with %d point(s)!", cueList->pointCount);
cueList->points.resize(cueList->pointCount);
for (uint16 i = 0; i < cueList->pointCount; i++) {
cueList->points[i].sampleFrame = stream->readUint32BE();
byte nameLength = stream->readByte();
cueList->points[i].name.clear();
for (byte j = 0; j < nameLength; j++)
cueList->points[i].name += stream->readByte();
// Realign to an even boundary
if (!(nameLength & 1))
stream->readByte();
debug (3, "Cue# chunk point %d (frame %d): %s", i, cueList->points[i].sampleFrame, cueList->points[i].name.c_str());
}
break;
case ID_DATA:
debug(2, "Found Tag DATA");
// We subtract 20 from the actual chunk size, which is the total size
// of the chunk's header
dataSize = stream->readUint32BE() - 20;
dataChunk.sampleRate = stream->readUint16BE();
dataChunk.sampleCount = stream->readUint32BE();
dataChunk.bitsPerSample = stream->readByte();
dataChunk.channels = stream->readByte();
dataChunk.encoding = stream->readUint16BE();
dataChunk.loopCount = stream->readUint16BE();
dataChunk.loopStart = stream->readUint32BE();
dataChunk.loopEnd = stream->readUint32BE();
// NOTE: We currently ignore all of the loop parameters here. Myst uses the
// loopCount variable but the loopStart and loopEnd are always 0 and the size of
// the sample. Myst ME doesn't use the Mohawk Sound format and just standard WAVE
// files and therefore does not contain any of this metadata and we have to specify
// whether or not to loop elsewhere.
dataChunk.audioData = stream->readStream(dataSize);
break;
default:
error ("Unknown tag found in 'tWAV' chunk -- '%s'", tag2str(tag));
}
}
// makeMohawkWaveStream always takes control of the original stream
delete stream;
// The sound in Myst uses raw unsigned 8-bit data
// The sound in the CD version of Riven is encoded in Intel DVI ADPCM
// The sound in the DVD version of Riven is encoded in MPEG-2 Layer II or Intel DVI ADPCM
if (dataChunk.encoding == kCodecRaw) {
byte flags = Audio::FLAG_UNSIGNED;
if (dataChunk.channels == 2)
flags |= Audio::FLAG_STEREO;
return Audio::makeRawStream(dataChunk.audioData, dataChunk.sampleRate, flags);
} else if (dataChunk.encoding == kCodecADPCM) {
uint32 blockAlign = dataChunk.channels * dataChunk.bitsPerSample / 8;
return Audio::makeADPCMStream(dataChunk.audioData, DisposeAfterUse::YES, dataSize, Audio::kADPCMDVI, dataChunk.sampleRate, dataChunk.channels, blockAlign);
} else if (dataChunk.encoding == kCodecMPEG2) {
#ifdef USE_MAD
return Audio::makeMP3Stream(dataChunk.audioData, DisposeAfterUse::YES);
#else
warning ("MAD library not included - unable to play MP2 audio");
#endif
} else {
error ("Unknown Mohawk WAVE encoding %d", dataChunk.encoding);
}
return nullptr;
}
Sound::Sound(MohawkEngine* vm) : _vm(vm) {
_midiDriver = NULL;
_midiParser = NULL;
_midiData = NULL;
_mystBackgroundSound.type = kFreeHandle;
initMidi();
}
Sound::~Sound() {
stopSound();
stopBackgroundMyst();
if (_midiParser) {
_midiParser->unloadMusic();
delete _midiParser;
}
if (_midiDriver) {
_midiDriver->close();
delete _midiDriver;
}
if (_midiData)
delete[] _midiData;
}
void Sound::initMidi() {
if (!(_vm->getFeatures() & GF_HASMIDI))
return;
// Let's get our MIDI parser/driver
_midiParser = MidiParser::createParser_SMF();
_midiDriver = MidiDriver::createMidi(MidiDriver::detectDevice(MDT_ADLIB|MDT_MIDI));
// Set up everything!
_midiDriver->open();
_midiParser->setMidiDriver(_midiDriver);
_midiParser->setTimerRate(_midiDriver->getBaseTempo());
}
Audio::RewindableAudioStream *Sound::makeAudioStream(uint16 id, CueList *cueList) {
Audio::RewindableAudioStream *audStream = NULL;
switch (_vm->getGameType()) {
case GType_MYST:
if (_vm->getFeatures() & GF_ME)
audStream = Audio::makeWAVStream(_vm->getResource(ID_MSND, convertMystID(id)), DisposeAfterUse::YES);
else
audStream = makeMohawkWaveStream(_vm->getResource(ID_MSND, id), cueList);
break;
case GType_ZOOMBINI:
audStream = makeMohawkWaveStream(_vm->getResource(ID_SND, id));
break;
case GType_LIVINGBOOKSV1:
audStream = makeLivingBooksWaveStream_v1(_vm->getResource(ID_WAV, id));
break;
case GType_LIVINGBOOKSV2:
if (_vm->getPlatform() == Common::kPlatformMacintosh) {
audStream = makeLivingBooksWaveStream_v1(_vm->getResource(ID_WAV, id));
break;
}
// fall through
default:
audStream = makeMohawkWaveStream(_vm->getResource(ID_TWAV, id), cueList);
}
return audStream;
}
Audio::SoundHandle *Sound::playSound(uint16 id, byte volume, bool loop, CueList *cueList) {
debug (0, "Playing sound %d", id);
Audio::RewindableAudioStream *rewindStream = makeAudioStream(id, cueList);
if (rewindStream) {
SndHandle *handle = getHandle();
handle->type = kUsedHandle;
handle->id = id;
handle->samplesPerSecond = rewindStream->getRate();
// Set the stream to loop here if it's requested
Audio::AudioStream *audStream = rewindStream;
if (loop)
audStream = Audio::makeLoopingAudioStream(rewindStream, 0);
_vm->_mixer->playStream(Audio::Mixer::kPlainSoundType, &handle->handle, audStream, -1, volume);
return &handle->handle;
}
return NULL;
}
Audio::SoundHandle *Sound::replaceSoundMyst(uint16 id, byte volume, bool loop) {
debug (0, "Replacing sound %d", id);
// The original engine also forces looping for those sounds
switch (id) {
case 2205:
case 2207:
case 5378:
case 7220:
case 9119: // Elevator engine sound in mechanical age is looping.
case 9120:
case 9327:
loop = true;
break;
}
stopSound();
return playSound(id, volume, loop);
}
void Sound::playSoundBlocking(uint16 id, byte volume) {
Audio::SoundHandle *handle = playSound(id, volume);
while (_vm->_mixer->isSoundHandleActive(*handle) && !_vm->shouldQuit()) {
Common::Event event;
while (_vm->_system->getEventManager()->pollEvent(event)) {
switch (event.type) {
case Common::EVENT_KEYDOWN:
switch (event.kbd.keycode) {
case Common::KEYCODE_SPACE:
_vm->pauseGame();
break;
default:
break;
}
default:
break;
}
}
// Cut down on CPU usage
_vm->_system->delayMillis(10);
}
}
void Sound::playMidi(uint16 id) {
uint32 idTag;
if (!(_vm->getFeatures() & GF_HASMIDI)) {
warning ("Attempting to play MIDI in a game without MIDI");
return;
}
assert(_midiDriver && _midiParser);
_midiParser->unloadMusic();
if (_midiData)
delete[] _midiData;
Common::SeekableReadStream *midi = _vm->getResource(ID_TMID, id);
idTag = midi->readUint32BE();
assert(idTag == ID_MHWK);
midi->readUint32BE(); // Skip size
idTag = midi->readUint32BE();
assert(idTag == ID_MIDI);
_midiData = new byte[midi->size() - 12]; // Enough to cover MThd/Prg#/MTrk
// Read the MThd Data
midi->read(_midiData, 14);
// TODO: Load patches from the Prg# section... skip it for now.
idTag = midi->readUint32BE();
assert(idTag == ID_PRG);
midi->skip(midi->readUint32BE());
// Read the MTrk Data
uint32 mtrkSize = midi->size() - midi->pos();
midi->read(_midiData + 14, mtrkSize);
delete midi;
// Now, play it :)
if (!_midiParser->loadMusic(_midiData, 14 + mtrkSize))
error ("Could not play MIDI music from tMID %04x\n", id);
_midiDriver->setTimerCallback(_midiParser, MidiParser::timerCallback);
}
void Sound::stopMidi() {
_midiParser->unloadMusic();
}
Audio::RewindableAudioStream *Sound::makeLivingBooksWaveStream_v1(Common::SeekableReadStream *stream) {
uint16 header = stream->readUint16BE();
uint16 rate = 0;
uint32 size = 0;
if (header == 'Wv') { // Big Endian
rate = stream->readUint16BE();
stream->skip(10); // Unknown
size = stream->readUint32BE();
} else if (header == 'vW') { // Little Endian
stream->readUint16LE(); // Unknown
rate = stream->readUint16LE();
stream->skip(8); // Unknown
size = stream->readUint32LE();
} else
error("Could not find Old Mohawk Sound header");
Common::SeekableReadStream *dataStream = stream->readStream(size);
delete stream;
return Audio::makeRawStream(dataStream, rate, Audio::FLAG_UNSIGNED);
}
SndHandle *Sound::getHandle() {
for (uint32 i = 0; i < _handles.size(); i++) {
if (_handles[i].type == kFreeHandle)
return &_handles[i];
if (!_vm->_mixer->isSoundHandleActive(_handles[i].handle)) {
_handles[i].type = kFreeHandle;
_handles[i].id = 0;
return &_handles[i];
}
}
// Let's add a new sound handle!
SndHandle handle;
handle.handle = Audio::SoundHandle();
handle.type = kFreeHandle;
handle.id = 0;
_handles.push_back(handle);
return &_handles[_handles.size() - 1];
}
void Sound::stopSound() {
for (uint32 i = 0; i < _handles.size(); i++)
if (_handles[i].type == kUsedHandle) {
_vm->_mixer->stopHandle(_handles[i].handle);
_handles[i].type = kFreeHandle;
_handles[i].id = 0;
}
}
void Sound::stopSound(uint16 id) {
for (uint32 i = 0; i < _handles.size(); i++)
if (_handles[i].type == kUsedHandle && _handles[i].id == id) {
_vm->_mixer->stopHandle(_handles[i].handle);
_handles[i].type = kFreeHandle;
_handles[i].id = 0;
}
}
bool Sound::isPlaying(uint16 id) {
for (uint32 i = 0; i < _handles.size(); i++)
if (_handles[i].type == kUsedHandle && _handles[i].id == id)
return _vm->_mixer->isSoundHandleActive(_handles[i].handle);
return false;
}
bool Sound::isPlaying() {
for (uint32 i = 0; i < _handles.size(); i++)
if (_handles[i].type == kUsedHandle)
if (_vm->_mixer->isSoundHandleActive(_handles[i].handle))
return true;
return false;
}
uint Sound::getNumSamplesPlayed(uint16 id) {
for (uint32 i = 0; i < _handles.size(); i++)
if (_handles[i].type == kUsedHandle && _handles[i].id == id) {
return (_vm->_mixer->getSoundElapsedTime(_handles[i].handle) * _handles[i].samplesPerSecond) / 1000;
}
return 0;
}
uint16 Sound::convertMystID(uint16 id) {
// Myst ME is a bit more efficient with sound storage than Myst
// Myst has lots of sounds repeated. To overcome this, Myst ME
// has MJMP resources which provide a link to the actual MSND
// resource we're looking for. This saves a lot of space from
// repeated data.
if (_vm->hasResource(ID_MJMP, id)) {
Common::SeekableReadStream *mjmpStream = _vm->getResource(ID_MJMP, id);
id = mjmpStream->readUint16LE();
delete mjmpStream;
}
return id;
}
void Sound::replaceBackgroundMyst(uint16 id, uint16 volume) {
debug(0, "Replacing background sound with %d", id);
// TODO: The original engine does fading
Common::String name = _vm->getResourceName(ID_MSND, convertMystID(id));
// Only the first eight characters need to be the same to have a match
Common::String prefix;
if (name.size() >= 8)
prefix = Common::String(name.c_str(), name.c_str() + 8);
else
prefix = name;
// Check if sound is already playing
if (_mystBackgroundSound.type == kUsedHandle && _vm->_mixer->isSoundHandleActive(_mystBackgroundSound.handle)
&& _vm->getResourceName(ID_MSND, convertMystID(_mystBackgroundSound.id)).hasPrefix(prefix)) {
// The sound is already playing, just change the volume
changeBackgroundVolumeMyst(volume);
return;
}
// Stop old background sound
stopBackgroundMyst();
// Play new sound
Audio::RewindableAudioStream *rewindStream = makeAudioStream(id);
if (rewindStream) {
_mystBackgroundSound.type = kUsedHandle;
_mystBackgroundSound.id = id;
_mystBackgroundSound.samplesPerSecond = rewindStream->getRate();
// Set the stream to loop
Audio::AudioStream *audStream = Audio::makeLoopingAudioStream(rewindStream, 0);
_vm->_mixer->playStream(Audio::Mixer::kPlainSoundType, &_mystBackgroundSound.handle, audStream, -1, volume >> 8);
}
}
void Sound::stopBackgroundMyst() {
if (_mystBackgroundSound.type == kUsedHandle) {
_vm->_mixer->stopHandle(_mystBackgroundSound.handle);
_mystBackgroundSound.type = kFreeHandle;
_mystBackgroundSound.id = 0;
}
}
void Sound::pauseBackgroundMyst() {
if (_mystBackgroundSound.type == kUsedHandle)
_vm->_mixer->pauseHandle(_mystBackgroundSound.handle, true);
}
void Sound::resumeBackgroundMyst() {
if (_mystBackgroundSound.type == kUsedHandle)
_vm->_mixer->pauseHandle(_mystBackgroundSound.handle, false);
}
void Sound::changeBackgroundVolumeMyst(uint16 vol) {
if (_mystBackgroundSound.type == kUsedHandle)
_vm->_mixer->setChannelVolume(_mystBackgroundSound.handle, vol >> 8);
}
} // End of namespace Mohawk
|