2009-12-29 23:18:24 +00:00
|
|
|
/* 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.
|
2014-02-18 01:34:22 +00:00
|
|
|
*
|
2009-12-29 23:18:24 +00:00
|
|
|
* 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.
|
2014-02-18 01:34:22 +00:00
|
|
|
*
|
2009-12-29 23:18:24 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/debug.h"
|
|
|
|
#include "common/system.h"
|
2009-12-29 23:18:24 +00:00
|
|
|
#include "common/util.h"
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
2009-12-29 23:18:24 +00:00
|
|
|
|
2011-08-05 12:33:42 +00:00
|
|
|
#include "audio/midiparser.h"
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/musicplugin.h"
|
|
|
|
#include "audio/audiostream.h"
|
|
|
|
#include "audio/decoders/mp3.h"
|
|
|
|
#include "audio/decoders/raw.h"
|
|
|
|
#include "audio/decoders/wave.h"
|
2009-12-29 23:18:24 +00:00
|
|
|
|
2011-08-05 12:33:42 +00:00
|
|
|
#include "mohawk/sound.h"
|
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
namespace Mohawk {
|
|
|
|
|
|
|
|
Sound::Sound(MohawkEngine* vm) : _vm(vm) {
|
|
|
|
_midiDriver = NULL;
|
|
|
|
_midiParser = NULL;
|
2011-01-02 14:57:20 +00:00
|
|
|
_midiData = NULL;
|
2011-01-19 16:55:47 +00:00
|
|
|
_mystBackgroundSound.type = kFreeHandle;
|
2009-12-29 23:18:24 +00:00
|
|
|
initMidi();
|
|
|
|
}
|
|
|
|
|
|
|
|
Sound::~Sound() {
|
|
|
|
stopSound();
|
|
|
|
stopAllSLST();
|
2011-01-19 16:52:47 +00:00
|
|
|
stopBackgroundMyst();
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
if (_midiParser) {
|
|
|
|
_midiParser->unloadMusic();
|
|
|
|
delete _midiParser;
|
|
|
|
}
|
2011-01-02 14:23:18 +00:00
|
|
|
|
|
|
|
if (_midiDriver) {
|
|
|
|
_midiDriver->close();
|
|
|
|
delete _midiDriver;
|
|
|
|
}
|
2011-01-02 14:57:20 +00:00
|
|
|
|
|
|
|
if (_midiData)
|
|
|
|
delete[] _midiData;
|
2009-12-29 23:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::initMidi() {
|
|
|
|
if (!(_vm->getFeatures() & GF_HASMIDI))
|
|
|
|
return;
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
// Let's get our MIDI parser/driver
|
|
|
|
_midiParser = MidiParser::createParser_SMF();
|
2010-06-21 21:36:36 +00:00
|
|
|
_midiDriver = MidiDriver::createMidi(MidiDriver::detectDevice(MDT_ADLIB|MDT_MIDI));
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
// Set up everything!
|
|
|
|
_midiDriver->open();
|
|
|
|
_midiParser->setMidiDriver(_midiDriver);
|
|
|
|
_midiParser->setTimerRate(_midiDriver->getBaseTempo());
|
|
|
|
}
|
2009-12-30 07:30:04 +00:00
|
|
|
|
2011-01-19 16:36:34 +00:00
|
|
|
Audio::AudioStream *Sound::makeAudioStream(uint16 id, CueList *cueList) {
|
2010-01-22 04:24:04 +00:00
|
|
|
Audio::AudioStream *audStream = NULL;
|
2009-12-29 23:18:24 +00:00
|
|
|
|
|
|
|
switch (_vm->getGameType()) {
|
2009-12-30 07:14:09 +00:00
|
|
|
case GType_MYST:
|
2011-01-18 21:10:58 +00:00
|
|
|
if (_vm->getFeatures() & GF_ME)
|
|
|
|
audStream = Audio::makeWAVStream(_vm->getResource(ID_MSND, convertMystID(id)), DisposeAfterUse::YES);
|
|
|
|
else
|
2011-08-13 18:18:23 +00:00
|
|
|
audStream = makeMohawkWaveStream(_vm->getResource(ID_MSND, id), cueList);
|
2009-12-30 07:14:09 +00:00
|
|
|
break;
|
|
|
|
case GType_ZOOMBINI:
|
2010-11-20 23:53:14 +00:00
|
|
|
audStream = makeMohawkWaveStream(_vm->getResource(ID_SND, id));
|
2009-12-30 07:14:09 +00:00
|
|
|
break;
|
2010-01-22 03:43:57 +00:00
|
|
|
case GType_LIVINGBOOKSV1:
|
2011-03-23 01:16:27 +00:00
|
|
|
audStream = makeLivingBooksWaveStream_v1(_vm->getResource(ID_WAV, id));
|
2009-12-30 07:14:09 +00:00
|
|
|
break;
|
2011-02-13 21:11:22 +00:00
|
|
|
case GType_LIVINGBOOKSV2:
|
|
|
|
if (_vm->getPlatform() == Common::kPlatformMacintosh) {
|
2011-03-23 01:16:27 +00:00
|
|
|
audStream = makeLivingBooksWaveStream_v1(_vm->getResource(ID_WAV, id));
|
2011-02-13 21:11:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fall through
|
2009-12-30 07:14:09 +00:00
|
|
|
default:
|
2011-01-19 16:36:34 +00:00
|
|
|
audStream = makeMohawkWaveStream(_vm->getResource(ID_TWAV, id), cueList);
|
2009-12-29 23:18:24 +00:00
|
|
|
}
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2010-12-18 13:12:56 +00:00
|
|
|
return audStream;
|
|
|
|
}
|
|
|
|
|
2011-01-19 16:36:34 +00:00
|
|
|
Audio::SoundHandle *Sound::playSound(uint16 id, byte volume, bool loop, CueList *cueList) {
|
2010-12-18 13:12:56 +00:00
|
|
|
debug (0, "Playing sound %d", id);
|
|
|
|
|
2011-01-19 16:36:34 +00:00
|
|
|
Audio::AudioStream *audStream = makeAudioStream(id, cueList);
|
2010-12-18 13:12:56 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
if (audStream) {
|
2010-11-29 21:18:20 +00:00
|
|
|
SndHandle *handle = getHandle();
|
|
|
|
handle->type = kUsedHandle;
|
|
|
|
handle->id = id;
|
2011-01-19 16:36:34 +00:00
|
|
|
handle->samplesPerSecond = audStream->getRate();
|
2010-11-29 21:18:20 +00:00
|
|
|
|
2010-01-22 04:24:04 +00:00
|
|
|
// Set the stream to loop here if it's requested
|
|
|
|
if (loop)
|
|
|
|
audStream = Audio::makeLoopingAudioStream((Audio::RewindableAudioStream *)audStream, 0);
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2010-04-12 09:14:17 +00:00
|
|
|
_vm->_mixer->playStream(Audio::Mixer::kPlainSoundType, &handle->handle, audStream, -1, volume);
|
2009-12-29 23:18:24 +00:00
|
|
|
return &handle->handle;
|
|
|
|
}
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-01-18 21:10:58 +00:00
|
|
|
Audio::SoundHandle *Sound::replaceSoundMyst(uint16 id, byte volume, bool loop) {
|
2010-11-29 20:49:42 +00:00
|
|
|
debug (0, "Replacing sound %d", id);
|
|
|
|
|
2011-01-18 21:10:58 +00:00
|
|
|
// TODO: The original engine does fading
|
2010-11-29 20:49:42 +00:00
|
|
|
|
2011-01-18 21:10:58 +00:00
|
|
|
Common::String name = _vm->getResourceName(ID_MSND, convertMystID(id));
|
2010-11-29 20:49:42 +00:00
|
|
|
|
|
|
|
// Check if sound is already playing
|
2010-12-18 13:12:56 +00:00
|
|
|
for (uint32 i = 0; i < _handles.size(); i++)
|
|
|
|
if (_handles[i].type == kUsedHandle
|
|
|
|
&& _vm->_mixer->isSoundHandleActive(_handles[i].handle)
|
2011-01-18 21:10:58 +00:00
|
|
|
&& name.equals(_vm->getResourceName(ID_MSND, convertMystID(_handles[i].id))))
|
2010-11-29 20:49:42 +00:00
|
|
|
return &_handles[i].handle;
|
|
|
|
|
2011-07-02 19:47:33 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2010-11-29 20:49:42 +00:00
|
|
|
stopSound();
|
|
|
|
return playSound(id, volume, loop);
|
|
|
|
}
|
|
|
|
|
2010-09-08 20:50:56 +00:00
|
|
|
void Sound::playSoundBlocking(uint16 id, byte volume) {
|
|
|
|
Audio::SoundHandle *handle = playSound(id, volume);
|
|
|
|
|
|
|
|
while (_vm->_mixer->isSoundHandleActive(*handle))
|
|
|
|
_vm->_system->delayMillis(10);
|
|
|
|
}
|
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
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);
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
_midiParser->unloadMusic();
|
2011-01-02 14:57:20 +00:00
|
|
|
if (_midiData)
|
|
|
|
delete[] _midiData;
|
|
|
|
|
2010-11-20 23:53:14 +00:00
|
|
|
Common::SeekableReadStream *midi = _vm->getResource(ID_TMID, id);
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
idTag = midi->readUint32BE();
|
|
|
|
assert(idTag == ID_MHWK);
|
|
|
|
midi->readUint32BE(); // Skip size
|
|
|
|
idTag = midi->readUint32BE();
|
|
|
|
assert(idTag == ID_MIDI);
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2011-01-02 14:57:20 +00:00
|
|
|
_midiData = new byte[midi->size() - 12]; // Enough to cover MThd/Prg#/MTrk
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
// Read the MThd Data
|
2011-01-02 14:57:20 +00:00
|
|
|
midi->read(_midiData, 14);
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2010-01-24 23:39:27 +00:00
|
|
|
// TODO: Load patches from the Prg# section... skip it for now.
|
2009-12-29 23:18:24 +00:00
|
|
|
idTag = midi->readUint32BE();
|
|
|
|
assert(idTag == ID_PRG);
|
|
|
|
midi->skip(midi->readUint32BE());
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
// Read the MTrk Data
|
|
|
|
uint32 mtrkSize = midi->size() - midi->pos();
|
2011-01-02 14:57:20 +00:00
|
|
|
midi->read(_midiData + 14, mtrkSize);
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
delete midi;
|
|
|
|
|
|
|
|
// Now, play it :)
|
2011-01-02 14:57:20 +00:00
|
|
|
if (!_midiParser->loadMusic(_midiData, 14 + mtrkSize))
|
2009-12-29 23:18:24 +00:00
|
|
|
error ("Could not play MIDI music from tMID %04x\n", id);
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
_midiDriver->setTimerCallback(_midiParser, MidiParser::timerCallback);
|
|
|
|
}
|
|
|
|
|
2011-01-03 22:53:34 +00:00
|
|
|
void Sound::stopMidi() {
|
|
|
|
_midiParser->unloadMusic();
|
|
|
|
}
|
|
|
|
|
2010-09-08 20:50:56 +00:00
|
|
|
byte Sound::convertRivenVolume(uint16 volume) {
|
|
|
|
return (volume == 256) ? 255 : volume;
|
|
|
|
}
|
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
void Sound::playSLST(uint16 index, uint16 card) {
|
2010-11-20 23:53:14 +00:00
|
|
|
Common::SeekableReadStream *slstStream = _vm->getResource(ID_SLST, card);
|
2009-12-29 23:18:24 +00:00
|
|
|
SLSTRecord slstRecord;
|
|
|
|
uint16 recordCount = slstStream->readUint16BE();
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
for (uint16 i = 0; i < recordCount; i++) {
|
|
|
|
slstRecord.index = slstStream->readUint16BE();
|
|
|
|
slstRecord.sound_count = slstStream->readUint16BE();
|
|
|
|
slstRecord.sound_ids = new uint16[slstRecord.sound_count];
|
|
|
|
|
|
|
|
for (uint16 j = 0; j < slstRecord.sound_count; j++)
|
|
|
|
slstRecord.sound_ids[j] = slstStream->readUint16BE();
|
|
|
|
|
|
|
|
slstRecord.fade_flags = slstStream->readUint16BE();
|
|
|
|
slstRecord.loop = slstStream->readUint16BE();
|
|
|
|
slstRecord.global_volume = slstStream->readUint16BE();
|
|
|
|
slstRecord.u0 = slstStream->readUint16BE(); // Unknown
|
|
|
|
|
|
|
|
if (slstRecord.u0 > 1)
|
|
|
|
warning("slstRecord.u0: %d non-boolean", slstRecord.u0);
|
|
|
|
|
|
|
|
slstRecord.u1 = slstStream->readUint16BE(); // Unknown
|
|
|
|
|
|
|
|
if (slstRecord.u1 != 0)
|
|
|
|
warning("slstRecord.u1: %d non-zero", slstRecord.u1);
|
|
|
|
|
|
|
|
slstRecord.volumes = new uint16[slstRecord.sound_count];
|
|
|
|
slstRecord.balances = new int16[slstRecord.sound_count];
|
|
|
|
slstRecord.u2 = new uint16[slstRecord.sound_count];
|
|
|
|
|
|
|
|
for (uint16 j = 0; j < slstRecord.sound_count; j++)
|
|
|
|
slstRecord.volumes[j] = slstStream->readUint16BE();
|
|
|
|
|
|
|
|
for (uint16 j = 0; j < slstRecord.sound_count; j++)
|
|
|
|
slstRecord.balances[j] = slstStream->readSint16BE(); // negative = left, 0 = center, positive = right
|
|
|
|
|
|
|
|
for (uint16 j = 0; j < slstRecord.sound_count; j++) {
|
|
|
|
slstRecord.u2[j] = slstStream->readUint16BE(); // Unknown
|
|
|
|
|
|
|
|
if (slstRecord.u2[j] != 255 && slstRecord.u2[j] != 256)
|
|
|
|
warning("slstRecord.u2[%d]: %d not 255 or 256", j, slstRecord.u2[j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slstRecord.index == index) {
|
|
|
|
playSLST(slstRecord);
|
2010-07-09 16:53:50 +00:00
|
|
|
delete[] slstRecord.sound_ids;
|
|
|
|
delete[] slstRecord.volumes;
|
|
|
|
delete[] slstRecord.balances;
|
|
|
|
delete[] slstRecord.u2;
|
2009-12-29 23:18:24 +00:00
|
|
|
delete slstStream;
|
|
|
|
return;
|
|
|
|
}
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
delete[] slstRecord.sound_ids;
|
|
|
|
delete[] slstRecord.volumes;
|
|
|
|
delete[] slstRecord.balances;
|
|
|
|
delete[] slstRecord.u2;
|
|
|
|
}
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
delete slstStream;
|
2010-07-09 16:53:50 +00:00
|
|
|
|
2011-01-23 06:54:35 +00:00
|
|
|
// If we have no matching entries, we do nothing and just let
|
|
|
|
// the previous ambient sounds continue.
|
2009-12-29 23:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::playSLST(SLSTRecord slstRecord) {
|
|
|
|
// End old sounds
|
|
|
|
for (uint16 i = 0; i < _currentSLSTSounds.size(); i++) {
|
|
|
|
bool noLongerPlay = true;
|
|
|
|
for (uint16 j = 0; j < slstRecord.sound_count; j++)
|
|
|
|
if (_currentSLSTSounds[i].id == slstRecord.sound_ids[j])
|
|
|
|
noLongerPlay = false;
|
|
|
|
if (noLongerPlay)
|
|
|
|
stopSLSTSound(i, (slstRecord.fade_flags & 1) != 0);
|
|
|
|
}
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
// Start new sounds
|
|
|
|
for (uint16 i = 0; i < slstRecord.sound_count; i++) {
|
|
|
|
bool alreadyPlaying = false;
|
|
|
|
for (uint16 j = 0; j < _currentSLSTSounds.size(); j++) {
|
|
|
|
if (_currentSLSTSounds[j].id == slstRecord.sound_ids[i])
|
|
|
|
alreadyPlaying = true;
|
|
|
|
}
|
|
|
|
if (!alreadyPlaying) {
|
|
|
|
playSLSTSound(slstRecord.sound_ids[i],
|
|
|
|
(slstRecord.fade_flags & (1 << 1)) != 0,
|
|
|
|
slstRecord.loop != 0,
|
|
|
|
slstRecord.volumes[i],
|
|
|
|
slstRecord.balances[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-23 07:02:49 +00:00
|
|
|
void Sound::stopAllSLST(bool fade) {
|
2010-07-09 16:53:50 +00:00
|
|
|
for (uint16 i = 0; i < _currentSLSTSounds.size(); i++) {
|
2011-01-23 07:02:49 +00:00
|
|
|
// TODO: Fade out, if requested
|
2009-12-29 23:18:24 +00:00
|
|
|
_vm->_mixer->stopHandle(*_currentSLSTSounds[i].handle);
|
2010-07-09 16:53:50 +00:00
|
|
|
delete _currentSLSTSounds[i].handle;
|
|
|
|
}
|
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
_currentSLSTSounds.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int8 convertBalance(int16 balance) {
|
|
|
|
return (int8)(balance >> 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::playSLSTSound(uint16 id, bool fade, bool loop, uint16 volume, int16 balance) {
|
|
|
|
// WORKAROUND: Some Riven SLST entries have a volume of 0, so we just ignore them.
|
|
|
|
if (volume == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SLSTSndHandle sndHandle;
|
|
|
|
sndHandle.handle = new Audio::SoundHandle();
|
|
|
|
sndHandle.id = id;
|
|
|
|
_currentSLSTSounds.push_back(sndHandle);
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2010-11-20 23:53:14 +00:00
|
|
|
Audio::AudioStream *audStream = makeMohawkWaveStream(_vm->getResource(ID_TWAV, id));
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2010-01-22 04:24:04 +00:00
|
|
|
// Loop here if necessary
|
|
|
|
if (loop)
|
|
|
|
audStream = Audio::makeLoopingAudioStream((Audio::RewindableAudioStream *)audStream, 0);
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
// TODO: Handle fading, possibly just raise the volume of the channel in increments?
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2010-09-08 20:50:56 +00:00
|
|
|
_vm->_mixer->playStream(Audio::Mixer::kPlainSoundType, sndHandle.handle, audStream, -1, convertRivenVolume(volume), convertBalance(balance));
|
2009-12-29 23:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::stopSLSTSound(uint16 index, bool fade) {
|
2011-01-23 07:02:49 +00:00
|
|
|
// TODO: Fade out, if requested
|
2009-12-29 23:18:24 +00:00
|
|
|
_vm->_mixer->stopHandle(*_currentSLSTSounds[index].handle);
|
2010-07-09 16:53:50 +00:00
|
|
|
delete _currentSLSTSounds[index].handle;
|
2009-12-29 23:18:24 +00:00
|
|
|
_currentSLSTSounds.remove_at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::pauseSLST() {
|
|
|
|
for (uint16 i = 0; i < _currentSLSTSounds.size(); i++)
|
|
|
|
_vm->_mixer->pauseHandle(*_currentSLSTSounds[i].handle, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::resumeSLST() {
|
|
|
|
for (uint16 i = 0; i < _currentSLSTSounds.size(); i++)
|
|
|
|
_vm->_mixer->pauseHandle(*_currentSLSTSounds[i].handle, false);
|
|
|
|
}
|
|
|
|
|
2011-01-19 16:36:34 +00:00
|
|
|
Audio::AudioStream *Sound::makeMohawkWaveStream(Common::SeekableReadStream *stream, CueList *cueList) {
|
2009-12-29 23:18:24 +00:00
|
|
|
uint32 tag = 0;
|
2011-01-11 21:48:49 +00:00
|
|
|
ADPCMStatus adpcmStatus;
|
|
|
|
DataChunk dataChunk;
|
2010-02-26 08:11:45 +00:00
|
|
|
uint32 dataSize = 0;
|
2009-12-29 23:18:24 +00:00
|
|
|
|
2011-01-11 21:48:49 +00:00
|
|
|
memset(&dataChunk, 0, sizeof(DataChunk));
|
2010-01-28 09:37:50 +00:00
|
|
|
|
2010-02-26 08:11:45 +00:00
|
|
|
if (stream->readUint32BE() != ID_MHWK) // MHWK tag again
|
2011-01-11 21:48:49 +00:00
|
|
|
error ("Could not find tag 'MHWK'");
|
2009-12-29 23:18:24 +00:00
|
|
|
|
|
|
|
stream->readUint32BE(); // Skip size
|
|
|
|
|
2010-02-26 08:11:45 +00:00
|
|
|
if (stream->readUint32BE() != ID_WAVE)
|
2011-01-11 21:48:49 +00:00
|
|
|
error ("Could not find tag 'WAVE'");
|
2009-12-29 23:18:24 +00:00
|
|
|
|
2011-01-11 21:48:49 +00:00
|
|
|
while (!dataChunk.audioData) {
|
2009-12-29 23:18:24 +00:00
|
|
|
tag = stream->readUint32BE();
|
|
|
|
|
|
|
|
switch (tag) {
|
2009-12-30 07:14:09 +00:00
|
|
|
case ID_ADPC:
|
|
|
|
debug(2, "Found Tag ADPC");
|
2011-01-11 21:48:49 +00:00
|
|
|
// ADPCM Sound Only
|
2011-01-22 16:16:30 +00:00
|
|
|
//
|
|
|
|
// 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.
|
2010-01-24 23:39:27 +00:00
|
|
|
|
2011-01-11 21:48:49 +00:00
|
|
|
adpcmStatus.size = stream->readUint32BE();
|
|
|
|
adpcmStatus.itemCount = stream->readUint16BE();
|
|
|
|
adpcmStatus.channels = stream->readUint16BE();
|
|
|
|
adpcmStatus.statusItems = new ADPCMStatus::StatusItem[adpcmStatus.itemCount];
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2011-01-11 21:48:49 +00:00
|
|
|
assert(adpcmStatus.channels <= 2);
|
2010-01-24 23:39:27 +00:00
|
|
|
|
2011-01-11 21:48:49 +00:00
|
|
|
for (uint16 i = 0; i < adpcmStatus.itemCount; i++) {
|
|
|
|
adpcmStatus.statusItems[i].sampleFrame = stream->readUint32BE();
|
2010-01-24 23:39:27 +00:00
|
|
|
|
2011-01-22 16:16:30 +00:00
|
|
|
for (uint16 j = 0; j < adpcmStatus.channels; j++) {
|
|
|
|
adpcmStatus.statusItems[i].channelStatus[j].last = stream->readSint16BE();
|
|
|
|
adpcmStatus.statusItems[i].channelStatus[j].stepIndex = stream->readUint16BE();
|
|
|
|
}
|
2009-12-30 07:14:09 +00:00
|
|
|
}
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2011-01-22 16:16:30 +00:00
|
|
|
// TODO: Actually use this chunk. For now, just delete the status items...
|
2011-01-11 21:48:49 +00:00
|
|
|
delete[] adpcmStatus.statusItems;
|
2009-12-30 07:14:09 +00:00
|
|
|
break;
|
|
|
|
case ID_CUE:
|
|
|
|
debug(2, "Found Tag Cue#");
|
2011-01-11 21:48:49 +00:00
|
|
|
// Cues are used for animation sync. There are a couple in Myst and
|
|
|
|
// Riven but are not used there at all.
|
|
|
|
|
2011-01-19 16:36:34 +00:00
|
|
|
if (!cueList) {
|
|
|
|
uint32 size = stream->readUint32BE();
|
|
|
|
stream->skip(size);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
cueList->size = stream->readUint32BE();
|
|
|
|
cueList->pointCount = stream->readUint16BE();
|
2011-01-11 21:48:49 +00:00
|
|
|
|
2011-01-19 16:36:34 +00:00
|
|
|
if (cueList->pointCount == 0)
|
2011-01-11 21:48:49 +00:00
|
|
|
debug(2, "Cue# chunk found with no points!");
|
2009-12-30 07:14:09 +00:00
|
|
|
else
|
2011-01-19 16:36:34 +00:00
|
|
|
debug(2, "Cue# chunk found with %d point(s)!", cueList->pointCount);
|
2011-01-11 21:48:49 +00:00
|
|
|
|
2011-01-19 22:41:12 +00:00
|
|
|
cueList->points.resize(cueList->pointCount);
|
2011-01-19 16:36:34 +00:00
|
|
|
for (uint16 i = 0; i < cueList->pointCount; i++) {
|
|
|
|
cueList->points[i].sampleFrame = stream->readUint32BE();
|
2011-01-11 21:48:49 +00:00
|
|
|
|
|
|
|
byte nameLength = stream->readByte();
|
2011-01-19 16:36:34 +00:00
|
|
|
cueList->points[i].name.clear();
|
2011-01-11 21:48:49 +00:00
|
|
|
for (byte j = 0; j < nameLength; j++)
|
2011-01-19 16:36:34 +00:00
|
|
|
cueList->points[i].name += stream->readByte();
|
2011-01-11 21:48:49 +00:00
|
|
|
|
|
|
|
// Realign to an even boundary
|
|
|
|
if (!(nameLength & 1))
|
2009-12-30 07:14:09 +00:00
|
|
|
stream->readByte();
|
2011-01-11 21:48:49 +00:00
|
|
|
|
2011-01-19 16:36:34 +00:00
|
|
|
debug (3, "Cue# chunk point %d (frame %d): %s", i, cueList->points[i].sampleFrame, cueList->points[i].name.c_str());
|
2009-12-30 07:14:09 +00:00
|
|
|
}
|
|
|
|
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
|
2010-02-26 08:11:45 +00:00
|
|
|
dataSize = stream->readUint32BE() - 20;
|
2011-01-11 21:48:49 +00:00
|
|
|
dataChunk.sampleRate = stream->readUint16BE();
|
|
|
|
dataChunk.sampleCount = stream->readUint32BE();
|
|
|
|
dataChunk.bitsPerSample = stream->readByte();
|
|
|
|
dataChunk.channels = stream->readByte();
|
|
|
|
dataChunk.encoding = stream->readUint16BE();
|
2011-01-22 16:35:25 +00:00
|
|
|
dataChunk.loopCount = stream->readUint16BE();
|
2011-01-11 21:48:49 +00:00
|
|
|
dataChunk.loopStart = stream->readUint32BE();
|
|
|
|
dataChunk.loopEnd = stream->readUint32BE();
|
2009-12-30 07:14:09 +00:00
|
|
|
|
2011-01-22 16:35:25 +00:00
|
|
|
// 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.
|
2010-01-22 04:24:04 +00:00
|
|
|
|
2011-01-11 21:48:49 +00:00
|
|
|
dataChunk.audioData = stream->readStream(dataSize);
|
2009-12-30 07:14:09 +00:00
|
|
|
break;
|
|
|
|
default:
|
2011-01-11 21:48:49 +00:00
|
|
|
error ("Unknown tag found in 'tWAV' chunk -- '%s'", tag2str(tag));
|
2009-12-29 23:18:24 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
// 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
|
2011-01-11 21:48:49 +00:00
|
|
|
if (dataChunk.encoding == kCodecRaw) {
|
2010-01-19 22:30:33 +00:00
|
|
|
byte flags = Audio::FLAG_UNSIGNED;
|
2010-01-22 04:24:04 +00:00
|
|
|
|
2011-01-11 21:48:49 +00:00
|
|
|
if (dataChunk.channels == 2)
|
2010-01-19 22:30:33 +00:00
|
|
|
flags |= Audio::FLAG_STEREO;
|
2010-01-22 04:24:04 +00:00
|
|
|
|
2011-01-11 21:48:49 +00:00
|
|
|
return Audio::makeRawStream(dataChunk.audioData, dataChunk.sampleRate, flags);
|
|
|
|
} else if (dataChunk.encoding == kCodecADPCM) {
|
|
|
|
uint32 blockAlign = dataChunk.channels * dataChunk.bitsPerSample / 8;
|
2011-04-13 13:43:08 +00:00
|
|
|
return Audio::makeADPCMStream(dataChunk.audioData, DisposeAfterUse::YES, dataSize, Audio::kADPCMDVI, dataChunk.sampleRate, dataChunk.channels, blockAlign);
|
2011-01-11 21:48:49 +00:00
|
|
|
} else if (dataChunk.encoding == kCodecMPEG2) {
|
2009-12-29 23:18:24 +00:00
|
|
|
#ifdef USE_MAD
|
2011-01-11 21:48:49 +00:00
|
|
|
return Audio::makeMP3Stream(dataChunk.audioData, DisposeAfterUse::YES);
|
2009-12-29 23:18:24 +00:00
|
|
|
#else
|
|
|
|
warning ("MAD library not included - unable to play MP2 audio");
|
|
|
|
#endif
|
|
|
|
} else {
|
2011-01-11 21:48:49 +00:00
|
|
|
error ("Unknown Mohawk WAVE encoding %d", dataChunk.encoding);
|
2009-12-29 23:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-03-23 01:16:27 +00:00
|
|
|
Audio::AudioStream *Sound::makeLivingBooksWaveStream_v1(Common::SeekableReadStream *stream) {
|
2009-12-29 23:18:24 +00:00
|
|
|
uint16 header = stream->readUint16BE();
|
|
|
|
uint16 rate = 0;
|
|
|
|
uint32 size = 0;
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
if (header == 'Wv') { // Big Endian
|
|
|
|
rate = stream->readUint16BE();
|
2010-11-21 00:02:25 +00:00
|
|
|
stream->skip(10); // Unknown
|
2009-12-29 23:18:24 +00:00
|
|
|
size = stream->readUint32BE();
|
|
|
|
} else if (header == 'vW') { // Little Endian
|
2010-11-21 00:02:25 +00:00
|
|
|
stream->readUint16LE(); // Unknown
|
2009-12-29 23:18:24 +00:00
|
|
|
rate = stream->readUint16LE();
|
2010-11-21 00:02:25 +00:00
|
|
|
stream->skip(8); // Unknown
|
2009-12-29 23:18:24 +00:00
|
|
|
size = stream->readUint32LE();
|
|
|
|
} else
|
|
|
|
error("Could not find Old Mohawk Sound header");
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2010-02-26 08:11:45 +00:00
|
|
|
Common::SeekableReadStream *dataStream = stream->readStream(size);
|
2009-12-29 23:18:24 +00:00
|
|
|
delete stream;
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2010-02-26 08:11:45 +00:00
|
|
|
return Audio::makeRawStream(dataStream, rate, Audio::FLAG_UNSIGNED);
|
2009-12-29 23:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2010-11-27 21:36:04 +00:00
|
|
|
_handles[i].id = 0;
|
2009-12-29 23:18:24 +00:00
|
|
|
return &_handles[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's add a new sound handle!
|
|
|
|
SndHandle handle;
|
|
|
|
handle.handle = Audio::SoundHandle();
|
|
|
|
handle.type = kFreeHandle;
|
2010-11-27 21:36:04 +00:00
|
|
|
handle.id = 0;
|
2009-12-29 23:18:24 +00:00
|
|
|
_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;
|
2010-11-27 21:36:04 +00:00
|
|
|
_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;
|
2009-12-29 23:18:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::pauseSound() {
|
|
|
|
for (uint32 i = 0; i < _handles.size(); i++)
|
|
|
|
if (_handles[i].type == kUsedHandle)
|
|
|
|
_vm->_mixer->pauseHandle(_handles[i].handle, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::resumeSound() {
|
|
|
|
for (uint32 i = 0; i < _handles.size(); i++)
|
|
|
|
if (_handles[i].type == kUsedHandle)
|
|
|
|
_vm->_mixer->pauseHandle(_handles[i].handle, false);
|
|
|
|
}
|
|
|
|
|
2010-11-27 21:36:04 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-03-29 19:18:55 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-19 16:36:34 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-01-18 21:10:58 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Audio::SoundHandle *Sound::replaceBackgroundMyst(uint16 id, uint16 volume) {
|
2011-01-19 16:52:47 +00:00
|
|
|
debug(0, "Replacing background sound with %d", id);
|
2010-12-18 13:12:56 +00:00
|
|
|
|
2011-01-11 21:48:49 +00:00
|
|
|
// TODO: The original engine does fading
|
2010-12-18 13:12:56 +00:00
|
|
|
|
2011-01-18 21:10:58 +00:00
|
|
|
Common::String name = _vm->getResourceName(ID_MSND, convertMystID(id));
|
2010-12-18 13:12:56 +00:00
|
|
|
|
2011-05-14 17:53:41 +00:00
|
|
|
// 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;
|
|
|
|
|
2010-12-18 13:12:56 +00:00
|
|
|
// Check if sound is already playing
|
2011-01-19 16:52:47 +00:00
|
|
|
if (_mystBackgroundSound.type == kUsedHandle && _vm->_mixer->isSoundHandleActive(_mystBackgroundSound.handle)
|
2011-05-14 17:53:41 +00:00
|
|
|
&& _vm->getResourceName(ID_MSND, convertMystID(_mystBackgroundSound.id)).hasPrefix(prefix))
|
2011-01-19 16:52:47 +00:00
|
|
|
return &_mystBackgroundSound.handle;
|
2010-12-18 13:12:56 +00:00
|
|
|
|
|
|
|
// Stop old background sound
|
2011-01-18 21:10:58 +00:00
|
|
|
stopBackgroundMyst();
|
2010-12-18 13:12:56 +00:00
|
|
|
|
|
|
|
// Play new sound
|
|
|
|
Audio::AudioStream *audStream = makeAudioStream(id);
|
|
|
|
|
|
|
|
if (audStream) {
|
2011-01-19 16:52:47 +00:00
|
|
|
_mystBackgroundSound.type = kUsedHandle;
|
|
|
|
_mystBackgroundSound.id = id;
|
|
|
|
_mystBackgroundSound.samplesPerSecond = audStream->getRate();
|
2010-12-18 13:12:56 +00:00
|
|
|
|
|
|
|
// Set the stream to loop
|
|
|
|
audStream = Audio::makeLoopingAudioStream((Audio::RewindableAudioStream *)audStream, 0);
|
|
|
|
|
2011-01-19 16:52:47 +00:00
|
|
|
_vm->_mixer->playStream(Audio::Mixer::kPlainSoundType, &_mystBackgroundSound.handle, audStream, -1, volume >> 8);
|
|
|
|
return &_mystBackgroundSound.handle;
|
2010-12-18 13:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-01-18 21:10:58 +00:00
|
|
|
void Sound::stopBackgroundMyst() {
|
2011-01-19 16:52:47 +00:00
|
|
|
if (_mystBackgroundSound.type == kUsedHandle) {
|
|
|
|
_vm->_mixer->stopHandle(_mystBackgroundSound.handle);
|
|
|
|
_mystBackgroundSound.type = kFreeHandle;
|
|
|
|
_mystBackgroundSound.id = 0;
|
|
|
|
}
|
2010-12-18 13:12:56 +00:00
|
|
|
}
|
|
|
|
|
2011-01-18 21:10:58 +00:00
|
|
|
void Sound::pauseBackgroundMyst() {
|
2011-01-19 16:52:47 +00:00
|
|
|
if (_mystBackgroundSound.type == kUsedHandle)
|
|
|
|
_vm->_mixer->pauseHandle(_mystBackgroundSound.handle, true);
|
2010-12-18 13:12:56 +00:00
|
|
|
}
|
|
|
|
|
2011-01-18 21:10:58 +00:00
|
|
|
void Sound::resumeBackgroundMyst() {
|
2011-01-19 16:52:47 +00:00
|
|
|
if (_mystBackgroundSound.type == kUsedHandle)
|
|
|
|
_vm->_mixer->pauseHandle(_mystBackgroundSound.handle, false);
|
2010-12-18 13:12:56 +00:00
|
|
|
}
|
|
|
|
|
2011-01-18 21:10:58 +00:00
|
|
|
void Sound::changeBackgroundVolumeMyst(uint16 vol) {
|
2011-01-19 16:52:47 +00:00
|
|
|
if (_mystBackgroundSound.type == kUsedHandle)
|
|
|
|
_vm->_mixer->setChannelVolume(_mystBackgroundSound.handle, vol >> 8);
|
2010-12-18 13:12:56 +00:00
|
|
|
}
|
|
|
|
|
2009-12-29 23:18:24 +00:00
|
|
|
} // End of namespace Mohawk
|