From 18db41db506b1e18e050e8dded44dc8501b9bc8f Mon Sep 17 00:00:00 2001 From: Sven Hesse Date: Thu, 8 May 2008 15:51:02 +0000 Subject: Implemented the background "music" / atmospheric sounds in Woodruff svn-id: r31949 --- engines/gob/sound/bgatmosphere.cpp | 126 +++++++++++++++++++++++++++++++++++++ engines/gob/sound/bgatmosphere.h | 75 ++++++++++++++++++++++ engines/gob/sound/sound.cpp | 64 ++++++++++++++++++- engines/gob/sound/sound.h | 14 ++++- engines/gob/sound/soundblaster.cpp | 2 +- 5 files changed, 278 insertions(+), 3 deletions(-) create mode 100644 engines/gob/sound/bgatmosphere.cpp create mode 100644 engines/gob/sound/bgatmosphere.h (limited to 'engines/gob/sound') diff --git a/engines/gob/sound/bgatmosphere.cpp b/engines/gob/sound/bgatmosphere.cpp new file mode 100644 index 0000000000..3c61f4f76a --- /dev/null +++ b/engines/gob/sound/bgatmosphere.cpp @@ -0,0 +1,126 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +#include "common/system.h" +#include "common/events.h" + +#include "gob/sound/bgatmosphere.h" + +namespace Gob { + +BackgroundAtmosphere::BackgroundAtmosphere(Audio::Mixer &mixer) : SoundMixer(mixer) { + _playMode = kPlayModeLinear; + _queuePos = -1; + _shaded = false; + + g_system->getEventManager()->registerRandomSource(_rnd, "gobBA"); +} + +BackgroundAtmosphere::~BackgroundAtmosphere() { + queueClear(); +} + +void BackgroundAtmosphere::play() { + Common::StackLock slock(_mutex); + + _queuePos = -1; + getNextQueuePos(); + + if (_queuePos == -1) + return; + + SoundMixer::play(*_queue[_queuePos], 1, 0); +} + +void BackgroundAtmosphere::stop() { + SoundMixer::stop(0); +} + +void BackgroundAtmosphere::setPlayMode(PlayMode mode) { + _playMode = mode; +} + +void BackgroundAtmosphere::queueSample(SoundDesc &sndDesc) { + Common::StackLock slock(_mutex); + + _queue.push_back(&sndDesc); +} + +void BackgroundAtmosphere::queueClear() { + Common::StackLock slock(_mutex); + + SoundMixer::stop(0); + for (uint i = 0; i < _queue.size(); i++) + delete _queue[i]; + + _queue.clear(); + _queuePos = -1; +} + +void BackgroundAtmosphere::checkEndSample() { + Common::StackLock slock(_mutex); + + getNextQueuePos(); + + if (_queuePos == -1) { + _end = true; + _playingSound = 0; + } else { + SoundMixer::setSample(*_queue[_queuePos], 1, 0, 0); + if (_shaded) + _fadeVol = 20000; + } +} + +void BackgroundAtmosphere::getNextQueuePos() { + if (_queue.size() == 0) { + _queuePos = -1; + return; + } + + switch (_playMode) { + + case kPlayModeLinear: + _queuePos = (_queuePos + 1) % _queue.size(); + break; + + case kPlayModeRandom: + _queuePos = _rnd.getRandomNumber(_queue.size() - 1); + break; + + } +} + +void BackgroundAtmosphere::shade() { + _shaded = true; + _fadeVol = 32768; +} + +void BackgroundAtmosphere::unshade() { + _shaded = false; + _fadeVol = 65536; +} + +} // End of namespace Gob diff --git a/engines/gob/sound/bgatmosphere.h b/engines/gob/sound/bgatmosphere.h new file mode 100644 index 0000000000..72b5614282 --- /dev/null +++ b/engines/gob/sound/bgatmosphere.h @@ -0,0 +1,75 @@ +/* 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. + * + * $URL$ + * $Id$ + * + */ + +#ifndef GOB_SOUND_BGATMOSPHERE_H +#define GOB_SOUND_BGATMOSPHERE_H + +#include "sound/mixer.h" +#include "common/mutex.h" + +#include "gob/sound/sounddesc.h" +#include "gob/sound/soundmixer.h" + +namespace Gob { + +class BackgroundAtmosphere : private SoundMixer { +public: + enum PlayMode { + kPlayModeLinear, + kPlayModeRandom + }; + + BackgroundAtmosphere(Audio::Mixer &mixer); + ~BackgroundAtmosphere(); + + void play(); + void stop(); + + void setPlayMode(PlayMode mode); + + void queueSample(SoundDesc &sndDesc); + void queueClear(); + + void shade(); + void unshade(); + +private: + PlayMode _playMode; + + Common::Array _queue; + int _queuePos; + bool _shaded; + + Common::Mutex _mutex; + + Common::RandomSource _rnd; + + void checkEndSample(); + void getNextQueuePos(); +}; + +} // End of namespace Gob + +#endif // GOB_SOUND_BGATMOSPHERE_H diff --git a/engines/gob/sound/sound.cpp b/engines/gob/sound/sound.cpp index 5c375c260a..63b74ca36f 100644 --- a/engines/gob/sound/sound.cpp +++ b/engines/gob/sound/sound.cpp @@ -39,6 +39,7 @@ Sound::Sound(GobEngine *vm) : _vm(vm) { _adlib = 0; _infogrames = 0; _cdrom = 0; + _bgatmos = 0; if (!_vm->_noMusic && _vm->hasAdlib()) _adlib = new AdLib(*_vm->_mixer); @@ -46,6 +47,8 @@ Sound::Sound(GobEngine *vm) : _vm(vm) { _infogrames = new Infogrames(*_vm->_mixer); if (_vm->isCD()) _cdrom = new CDROM; + if (_vm->getGameType() == kGameTypeWoodruff) + _bgatmos = new BackgroundAtmosphere(*_vm->_mixer); } Sound::~Sound() { @@ -85,10 +88,18 @@ int Sound::sampleGetNextFreeSlot() const { return -1; } -bool Sound::sampleLoad(SoundDesc *sndDesc, const char *fileName) { +bool Sound::sampleLoad(SoundDesc *sndDesc, const char *fileName, bool tryExist) { if (!sndDesc) return false; + int16 handle = _vm->_dataIO->openData(fileName); + if (handle < 0) { + warning("Can't open sample file \"%s\"", fileName); + return false; + } + + _vm->_dataIO->closeData(handle); + byte *data; uint32 size; @@ -458,4 +469,55 @@ void Sound::cdTest(int trySubst, const char *label) { _cdrom->testCD(trySubst, label); } +void Sound::bgPlay(const char *base, int count) { + if (!_bgatmos) + return; + + _bgatmos->stop(); + _bgatmos->queueClear(); + + int length = strlen(base) + 7; + char *fileName = new char[length]; + SoundDesc *sndDesc; + + for (int i = 1; i <= count; i++) { + snprintf(fileName, length, "%s%02d.SND", base, i); + + sndDesc = new SoundDesc; + if (sampleLoad(sndDesc, fileName)) + _bgatmos->queueSample(*sndDesc); + } + + _bgatmos->play(); +} + +void Sound::bgStop() { + if (!_bgatmos) + return; + + _bgatmos->stop(); + _bgatmos->queueClear(); +} + +void Sound::bgSetPlayMode(BackgroundAtmosphere::PlayMode mode) { + if (!_bgatmos) + return; + + _bgatmos->setPlayMode(mode); +} + +void Sound::bgShade() { + if (!_bgatmos) + return; + + _bgatmos->shade(); +} + +void Sound::bgUnshade() { + if (!_bgatmos) + return; + + _bgatmos->unshade(); +} + } // End of namespace Gob diff --git a/engines/gob/sound/sound.h b/engines/gob/sound/sound.h index 81b48688c4..b59510e4bb 100644 --- a/engines/gob/sound/sound.h +++ b/engines/gob/sound/sound.h @@ -32,6 +32,7 @@ #include "gob/sound/adlib.h" #include "gob/sound/infogrames.h" #include "gob/sound/cdrom.h" +#include "gob/sound/bgatmosphere.h" namespace Gob { @@ -49,7 +50,7 @@ public: const SoundDesc *sampleGetBySlot(int slot) const; int sampleGetNextFreeSlot() const; - bool sampleLoad(SoundDesc *sndDesc, const char *fileName); + bool sampleLoad(SoundDesc *sndDesc, const char *fileName, bool tryExist = true); void sampleFree(SoundDesc *sndDesc, bool noteAdlib = false, int index = -1); @@ -118,6 +119,16 @@ public: void cdTest(int trySubst, const char *label); + + // Background Atmosphere + void bgPlay(const char *base, int count); + void bgStop(); + + void bgSetPlayMode(BackgroundAtmosphere::PlayMode mode); + + void bgShade(); + void bgUnshade(); + private: GobEngine *_vm; @@ -128,6 +139,7 @@ private: AdLib *_adlib; Infogrames *_infogrames; CDROM *_cdrom; + BackgroundAtmosphere *_bgatmos; }; } // End of namespace Gob diff --git a/engines/gob/sound/soundblaster.cpp b/engines/gob/sound/soundblaster.cpp index bacb16a6d3..cfa11b6ff9 100644 --- a/engines/gob/sound/soundblaster.cpp +++ b/engines/gob/sound/soundblaster.cpp @@ -23,7 +23,7 @@ * */ -#include "gob/sound/sound.h" +#include "gob/sound/soundblaster.h" namespace Gob { -- cgit v1.2.3