diff options
Diffstat (limited to 'engines/dreamweb/sound.cpp')
-rw-r--r-- | engines/dreamweb/sound.cpp | 144 |
1 files changed, 78 insertions, 66 deletions
diff --git a/engines/dreamweb/sound.cpp b/engines/dreamweb/sound.cpp index b3d5db9e0d..76c734e932 100644 --- a/engines/dreamweb/sound.cpp +++ b/engines/dreamweb/sound.cpp @@ -20,28 +20,53 @@ * */ -#include "dreamweb/dreamweb.h" - -#include "audio/mixer.h" #include "audio/decoders/raw.h" - #include "common/config-manager.h" +#include "common/debug.h" +#include "common/file.h" + +#include "dreamweb/dreamweb.h" +#include "dreamweb/sound.h" namespace DreamWeb { -bool DreamWebEngine::loadSpeech(byte type1, int idx1, byte type2, int idx2) { +DreamWebSound::DreamWebSound(DreamWebEngine *vm) : _vm(vm) { + _vm->_mixer->setVolumeForSoundType(Audio::Mixer::kSFXSoundType, ConfMan.getInt("sfx_volume")); + _vm->_mixer->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, ConfMan.getInt("music_volume")); + _vm->_mixer->setVolumeForSoundType(Audio::Mixer::kSpeechSoundType, ConfMan.getInt("speech_volume")); + + _channel0 = 0; + _channel1 = 0; + + _currentSample = 0xff; + _channel0Playing = 0; + _channel0Repeat = 0; + _channel0NewSound = false; + _channel1Playing = 255; + + _volume = 0; + _volumeTo = 0; + _volumeDirection = 0; + _volumeCount = 0; +} + +DreamWebSound::~DreamWebSound() { +} + +bool DreamWebSound::loadSpeech(byte type1, int idx1, byte type2, int idx2) { cancelCh1(); Common::String name = Common::String::format("%c%02d%c%04d.RAW", type1, idx1, type2, idx2); - //debug("name = %s", name.c_str()); - bool result = loadSpeech(name); - - _speechLoaded = result; - return result; + debug(2, "loadSpeech() name:%s", name.c_str()); + return loadSpeech(name); } +void DreamWebSound::volumeChange(uint8 value, int8 direction) { + _volumeTo = value; + _volumeDirection = direction; +} -void DreamWebEngine::volumeAdjust() { +void DreamWebSound::volumeAdjust() { if (_volumeDirection == 0) return; if (_volume != _volumeTo) { @@ -54,39 +79,41 @@ void DreamWebEngine::volumeAdjust() { } } -void DreamWebEngine::playChannel0(uint8 index, uint8 repeat) { +void DreamWebSound::playChannel0(uint8 index, uint8 repeat) { debug(1, "playChannel0(index:%d, repeat:%d)", index, repeat); - _channel0Playing = index; - if (index >= 12) - index -= 12; + if (index == _channel0Playing) { + warning("playChannel0(index: %d) already playing! Forcing restart...", index); + _channel0NewSound = true; + } + + _channel0Playing = index; _channel0Repeat = repeat; } -void DreamWebEngine::playChannel1(uint8 index) { +void DreamWebSound::playChannel1(uint8 index) { + debug(1, "playChannel1(index:%d)", index); if (_channel1Playing == 7) return; _channel1Playing = index; - if (index >= 12) - index -= 12; } -void DreamWebEngine::cancelCh0() { +void DreamWebSound::cancelCh0() { debug(1, "cancelCh0()"); - _channel0Repeat = 0; _channel0Playing = 255; + _channel0Repeat = 0; stopSound(0); } -void DreamWebEngine::cancelCh1() { +void DreamWebSound::cancelCh1() { + debug(1, "cancelCh1()"); _channel1Playing = 255; stopSound(1); } -void DreamWebEngine::loadRoomsSample() { - debug(1, "loadRoomsSample() _roomsSample:%d", _roomsSample); - uint8 sample = _roomsSample; +void DreamWebSound::loadRoomsSample(uint8 sample) { + debug(1, "loadRoomsSample(sample:%d)", sample); if (sample == 255 || _currentSample == sample) return; // loaded already @@ -104,13 +131,8 @@ void DreamWebEngine::loadRoomsSample() { loadSounds(1, sampleSuffix.c_str()); } -} // End of namespace DreamWeb - - -namespace DreamWeb { - -void DreamWebEngine::playSound(uint8 channel, uint8 id, uint8 loops) { - debug(1, "playSound(%u, %u, %u)", channel, id, loops); +void DreamWebSound::playSound(uint8 channel, uint8 id, uint8 loops) { + debug(1, "playSound(channel:%u, id:%u, loops:%u)", channel, id, loops); int bank = 0; bool speech = false; @@ -140,47 +162,42 @@ void DreamWebEngine::playSound(uint8 channel, uint8 id, uint8 loops) { error("out of memory: cannot allocate memory for sound(%u bytes)", sample.size); memcpy(buffer, data.data.begin() + sample.offset, sample.size); - raw = Audio::makeRawStream( - buffer, - sample.size, 22050, Audio::FLAG_UNSIGNED); + raw = Audio::makeRawStream(buffer, sample.size, 22050, Audio::FLAG_UNSIGNED); } else { uint8 *buffer = (uint8 *)malloc(_speechData.size()); if (!buffer) error("out of memory: cannot allocate memory for sound(%u bytes)", _speechData.size()); memcpy(buffer, _speechData.begin(), _speechData.size()); - raw = Audio::makeRawStream( - buffer, - _speechData.size(), 22050, Audio::FLAG_UNSIGNED); - + raw = Audio::makeRawStream(buffer, _speechData.size(), 22050, Audio::FLAG_UNSIGNED); } Audio::AudioStream *stream; if (loops > 1) { - stream = new Audio::LoopingAudioStream(raw, loops < 255? loops: 0); + stream = new Audio::LoopingAudioStream(raw, (loops < 255) ? loops : 0); } else stream = raw; - if (_mixer->isSoundHandleActive(_channelHandle[channel])) - _mixer->stopHandle(_channelHandle[channel]); - _mixer->playStream(type, &_channelHandle[channel], stream); + if (_vm->_mixer->isSoundHandleActive(_channelHandle[channel])) + _vm->_mixer->stopHandle(_channelHandle[channel]); + _vm->_mixer->playStream(type, &_channelHandle[channel], stream); } -void DreamWebEngine::stopSound(uint8 channel) { +void DreamWebSound::stopSound(uint8 channel) { debug(1, "stopSound(%u)", channel); assert(channel == 0 || channel == 1); - _mixer->stopHandle(_channelHandle[channel]); + _vm->_mixer->stopHandle(_channelHandle[channel]); if (channel == 0) _channel0 = 0; else _channel1 = 0; } -bool DreamWebEngine::loadSpeech(const Common::String &filename) { - if (!hasSpeech()) +bool DreamWebSound::loadSpeech(const Common::String &filename) { + if (!_vm->hasSpeech()) return false; Common::File file; - if (!file.open(_speechDirName + "/" + filename)) + if (!file.open(_vm->getSpeechDirName() + "/" + filename)) return false; debug(1, "loadSpeech(%s)", filename.c_str()); @@ -192,13 +209,8 @@ bool DreamWebEngine::loadSpeech(const Common::String &filename) { return true; } -void DreamWebEngine::soundHandler() { - static uint8 volumeOld = 0, channel0Old = 0, channel0PlayingOld = 0; - if (_volume != volumeOld || _channel0 != channel0Old || _channel0Playing != channel0PlayingOld) - debug(1, "soundHandler() _volume: %d _channel0: %d _channel0Playing: %d", _volume, _channel0, _channel0Playing); - volumeOld = _volume, channel0Old = _channel0, channel0PlayingOld = _channel0Playing; - - _subtitles = ConfMan.getBool("subtitles"); +void DreamWebSound::soundHandler() { + _vm->_subtitles = ConfMan.getBool("subtitles"); volumeAdjust(); uint volume = _volume; @@ -215,7 +227,7 @@ void DreamWebEngine::soundHandler() { if (volume >= 8) volume = 7; volume = (8 - volume) * Audio::Mixer::kMaxChannelVolume / 8; - _mixer->setChannelVolume(_channelHandle[0], volume); + _vm->_mixer->setChannelVolume(_channelHandle[0], volume); uint8 ch0 = _channel0Playing; if (ch0 == 255) @@ -225,8 +237,9 @@ void DreamWebEngine::soundHandler() { ch1 = 0; uint8 ch0loop = _channel0Repeat; - if (_channel0 != ch0) { + if (_channel0 != ch0 || _channel0NewSound) { _channel0 = ch0; + _channel0NewSound = false; if (ch0) { playSound(0, ch0, ch0loop); } @@ -237,20 +250,19 @@ void DreamWebEngine::soundHandler() { playSound(1, ch1, 1); } } - if (!_mixer->isSoundHandleActive(_channelHandle[0])) { - if (_channel0Playing != 255 && _channel0 != 0) - debug(1, "!_mixer->isSoundHandleActive _channelHandle[0] _channel0Playing:%d _channel0:%d", _channel0Playing, _channel0); + + if (!_vm->_mixer->isSoundHandleActive(_channelHandle[0])) { _channel0Playing = 255; _channel0 = 0; } - if (!_mixer->isSoundHandleActive(_channelHandle[1])) { + if (!_vm->_mixer->isSoundHandleActive(_channelHandle[1])) { _channel1Playing = 255; _channel1 = 0; } } -void DreamWebEngine::loadSounds(uint bank, const Common::String &suffix) { - Common::String filename = getDatafilePrefix() + suffix; +void DreamWebSound::loadSounds(uint bank, const Common::String &suffix) { + Common::String filename = _vm->getDatafilePrefix() + suffix; debug(1, "loadSounds(%u, %s)", bank, filename.c_str()); Common::File file; if (!file.open(filename)) { @@ -258,9 +270,9 @@ void DreamWebEngine::loadSounds(uint bank, const Common::String &suffix) { return; } - uint8 header[0x60]; + uint8 header[96]; file.read(header, sizeof(header)); - uint tablesize = READ_LE_UINT16(header + 0x32); + uint tablesize = READ_LE_UINT16(header + 50); debug(1, "table size = %u", tablesize); SoundData &soundData = _soundData[bank]; @@ -270,8 +282,8 @@ void DreamWebEngine::loadSounds(uint bank, const Common::String &suffix) { uint8 entry[6]; Sample &sample = soundData.samples[i]; file.read(entry, sizeof(entry)); - sample.offset = entry[0] * 0x4000 + READ_LE_UINT16(entry + 1); - sample.size = READ_LE_UINT16(entry + 3) * 0x800; + sample.offset = entry[0] * 16384 + READ_LE_UINT16(entry + 1); + sample.size = READ_LE_UINT16(entry + 3) * 2048; total += sample.size; debug(1, "offset: %08x, size: %u", sample.offset, sample.size); } |