From 8723afd6f4a6c5d7163d509b0fa22cd562de3233 Mon Sep 17 00:00:00 2001 From: Nicola Mettifogo Date: Sat, 14 Mar 2009 15:09:31 +0000 Subject: * Added a generic sound manager that hides engine-specific managers and allows accessing them through a simplified command/parameter interface. * Updated client code to use the new manager. * Moved Nippon Safes sound code from sound.cpp to sound_ns.cpp. svn-id: r39394 --- engines/parallaction/callables_ns.cpp | 20 +- engines/parallaction/gui_ns.cpp | 12 +- engines/parallaction/module.mk | 2 +- engines/parallaction/parallaction.cpp | 13 +- engines/parallaction/parallaction.h | 7 +- engines/parallaction/parallaction_br.cpp | 8 +- engines/parallaction/parallaction_ns.cpp | 24 +- engines/parallaction/parser_ns.cpp | 2 +- engines/parallaction/sound.cpp | 484 ---------------------------- engines/parallaction/sound.h | 90 ++++-- engines/parallaction/sound_ns.cpp | 520 +++++++++++++++++++++++++++++++ 11 files changed, 634 insertions(+), 548 deletions(-) delete mode 100644 engines/parallaction/sound.cpp create mode 100644 engines/parallaction/sound_ns.cpp (limited to 'engines') diff --git a/engines/parallaction/callables_ns.cpp b/engines/parallaction/callables_ns.cpp index c3e8c8e6a4..c31d49a259 100644 --- a/engines/parallaction/callables_ns.cpp +++ b/engines/parallaction/callables_ns.cpp @@ -151,8 +151,8 @@ void Parallaction_ns::_c_play_boogie(void *parm) { return; flag = 0; - _soundMan->setMusicFile("boogie2"); - _soundMan->playMusic(); + _soundManI->setMusicFile("boogie2"); + _soundManI->playMusic(); return; } @@ -418,18 +418,18 @@ void Parallaction_ns::_c_testResult(void *parm) { } void Parallaction_ns::_c_offSound(void*) { - _soundMan->stopSfx(0); - _soundMan->stopSfx(1); - _soundMan->stopSfx(2); - _soundMan->stopSfx(3); + _soundManI->stopSfx(0); + _soundManI->stopSfx(1); + _soundManI->stopSfx(2); + _soundManI->stopSfx(3); } void Parallaction_ns::_c_startMusic(void*) { - _soundMan->playMusic(); + _soundManI->playMusic(); } void Parallaction_ns::_c_closeMusic(void*) { - _soundMan->stopMusic(); + _soundManI->stopMusic(); } /* @@ -440,8 +440,8 @@ void Parallaction_ns::_c_startIntro(void *parm) { _rightHandAnim = _location.findAnimation("righthand"); if (getPlatform() == Common::kPlatformPC) { - _soundMan->setMusicFile("intro"); - _soundMan->playMusic(); + _soundManI->setMusicFile("intro"); + _soundManI->playMusic(); } _input->setMouseState(MOUSE_DISABLED); diff --git a/engines/parallaction/gui_ns.cpp b/engines/parallaction/gui_ns.cpp index dd46e04317..efa1c40309 100644 --- a/engines/parallaction/gui_ns.cpp +++ b/engines/parallaction/gui_ns.cpp @@ -371,7 +371,7 @@ class SelectCharacterInputState_NS : public MenuInputState { static const Common::Rect codeSelectBlocks[9]; static const Common::Rect codeTrueBlocks[9]; - Parallaction *_vm; + Parallaction_ns *_vm; int guiGetSelectedBlock(const Common::Point &p) { @@ -424,7 +424,7 @@ class SelectCharacterInputState_NS : public MenuInputState { public: - SelectCharacterInputState_NS(Parallaction *vm, MenuInputHelper *helper) : MenuInputState("selectcharacter", helper), _vm(vm) { + SelectCharacterInputState_NS(Parallaction_ns *vm, MenuInputHelper *helper) : MenuInputState("selectcharacter", helper), _vm(vm) { _keys = (_vm->getPlatform() == Common::kPlatformAmiga && (_vm->getFeatures() & GF_LANG_MULT)) ? _amigaKeys : _pcKeys; _block.create(BLOCK_WIDTH, BLOCK_HEIGHT, 1); } @@ -541,7 +541,7 @@ public: } virtual void enter() { - _vm->_soundMan->stopMusic(); + _vm->_soundManI->stopMusic(); _vm->showSlide("password"); _emptySlots.create(BLOCK_WIDTH * 8, BLOCK_HEIGHT, 1); @@ -683,11 +683,11 @@ const ShowCreditsInputState_NS::Credit ShowCreditsInputState_NS::_credits[6] = { }; class EndIntroInputState_NS : public MenuInputState { - Parallaction *_vm; + Parallaction_ns *_vm; bool _isDemo; public: - EndIntroInputState_NS(Parallaction *vm, MenuInputHelper *helper) : MenuInputState("endintro", helper), _vm(vm) { + EndIntroInputState_NS(Parallaction_ns *vm, MenuInputHelper *helper) : MenuInputState("endintro", helper), _vm(vm) { _isDemo = (_vm->getFeatures() & GF_DEMO) != 0; } @@ -711,7 +711,7 @@ public: _vm->_input->setMouseState(MOUSE_DISABLED); if (!_isDemo) { - _vm->_soundMan->stopMusic(); + _vm->_soundManI->stopMusic(); int label = _vm->_gfx->createLabel(_vm->_menuFont, "CLICK MOUSE BUTTON TO START", 1); _vm->_gfx->showLabel(label, CENTER_LABEL_HORIZONTAL, 80); } diff --git a/engines/parallaction/module.mk b/engines/parallaction/module.mk index 1cca5856e1..29e4863fec 100644 --- a/engines/parallaction/module.mk +++ b/engines/parallaction/module.mk @@ -28,7 +28,7 @@ MODULE_OBJS := \ parser_br.o \ parser_ns.o \ saveload.o \ - sound.o \ + sound_ns.o \ staticres.o \ walk.o diff --git a/engines/parallaction/parallaction.cpp b/engines/parallaction/parallaction.cpp index 16a29044dc..a295aa950e 100644 --- a/engines/parallaction/parallaction.cpp +++ b/engines/parallaction/parallaction.cpp @@ -142,7 +142,7 @@ Common::Error Parallaction::init() { void Parallaction::pauseEngineIntern(bool pause) { if (_soundMan) { - _soundMan->pause(pause); + _soundMan->execute(SC_PAUSE, (SoundManCommandParameter)pause); } } @@ -635,7 +635,10 @@ void Parallaction::runZone(ZonePtr z) { break; case kZoneHear: - _soundMan->playSfx(z->u.hear->_name, z->u.hear->_channel, (z->_flags & kFlagsLooping) == kFlagsLooping, 60); + _soundMan->execute(SC_SETSFXCHANNEL, (SoundManCommandParameter)z->u.hear->_channel); + _soundMan->execute(SC_SETSFXLOOPING, (SoundManCommandParameter)((z->_flags & kFlagsLooping) == kFlagsLooping)); + _soundMan->execute(SC_SETSFXVOLUME, (SoundManCommandParameter)60); + _soundMan->execute(SC_PLAYSFX, (SoundManCommandParameter)z->u.hear->_name); break; case kZoneSpeak: @@ -980,9 +983,11 @@ bool CharacterName::dummy() const { return _dummy; } - void Parallaction::beep() { - _soundMan->playSfx("beep", 3, false); + _soundMan->execute(SC_SETSFXCHANNEL, (SoundManCommandParameter)3); + _soundMan->execute(SC_SETSFXVOLUME, (SoundManCommandParameter)127); + _soundMan->execute(SC_SETSFXLOOPING, (SoundManCommandParameter)false); + _soundMan->execute(SC_PLAYSFX, (SoundManCommandParameter)"beep"); } void Parallaction::scheduleLocationSwitch(const char *location) { diff --git a/engines/parallaction/parallaction.h b/engines/parallaction/parallaction.h index 9bfd633a8b..4aaa3dd0c6 100644 --- a/engines/parallaction/parallaction.h +++ b/engines/parallaction/parallaction.h @@ -116,7 +116,6 @@ extern const char *_minidrkiName; class Debugger; class Gfx; -class SoundMan; class Input; class DialogueManager; class MenuInputHelper; @@ -124,6 +123,8 @@ class PathWalker_NS; class PathWalker_BR; class CommandExec; class ProgramExec; +class SoundMan; +class SoundMan_ns; struct Location { @@ -273,11 +274,11 @@ public: Gfx *_gfx; Disk *_disk; Input *_input; - SoundMan *_soundMan; Debugger *_debugger; SaveLoad *_saveLoad; MenuInputHelper *_menuHelper; Common::RandomSource _rnd; + SoundMan *_soundMan; // fonts Font *_labelFont; @@ -392,6 +393,8 @@ public: virtual Common::Error init(); virtual Common::Error go(); + SoundMan_ns* _soundManI; + public: virtual void parseLocation(const char *filename); virtual void changeLocation(); diff --git a/engines/parallaction/parallaction_br.cpp b/engines/parallaction/parallaction_br.cpp index 24fbc1018e..5f2ced4d1f 100644 --- a/engines/parallaction/parallaction_br.cpp +++ b/engines/parallaction/parallaction_br.cpp @@ -53,6 +53,8 @@ Common::Error Parallaction_br::init() { _screenWidth = 640; _screenHeight = 400; + SoundManImpl* _soundManI = 0; + if (getPlatform() == Common::kPlatformPC) { if (getFeatures() & GF_DEMO) { _disk = new DosDemoDisk_br(this); @@ -60,15 +62,15 @@ Common::Error Parallaction_br::init() { _disk = new DosDisk_br(this); } _disk->setLanguage(2); // NOTE: language is now hardcoded to English. Original used command-line parameters. - _soundMan = new DummySoundMan(this); + _soundManI = new DummySoundMan(); } else { _disk = new AmigaDisk_br(this); _disk->setLanguage(2); // NOTE: language is now hardcoded to English. Original used command-line parameters. - _soundMan = new AmigaSoundMan(this); + _soundManI = new DummySoundMan(); } _disk->init(); - + _soundMan = new SoundMan(_soundManI); initResources(); initFonts(); diff --git a/engines/parallaction/parallaction_ns.cpp b/engines/parallaction/parallaction_ns.cpp index 8d0317f1f2..211f6e573c 100644 --- a/engines/parallaction/parallaction_ns.cpp +++ b/engines/parallaction/parallaction_ns.cpp @@ -168,11 +168,13 @@ Common::Error Parallaction_ns::init() { if (getPlatform() == Common::kPlatformPC) { int midiDriver = MidiDriver::detectMusicDriver(MDT_MIDI | MDT_ADLIB | MDT_PREFER_MIDI); MidiDriver *driver = MidiDriver::createMidi(midiDriver); - _soundMan = new DosSoundMan(this, driver); - _soundMan->setMusicVolume(ConfMan.getInt("music_volume")); + _soundManI = new DosSoundMan_ns(this, driver); + _soundManI->setMusicVolume(ConfMan.getInt("music_volume")); } else { - _soundMan = new AmigaSoundMan(this); + _soundManI = new AmigaSoundMan_ns(this); } + + _soundMan = new SoundMan(_soundManI); initResources(); initFonts(); @@ -302,7 +304,7 @@ void Parallaction_ns::changeLocation() { MouseTriState oldMouseState = _input->getMouseState(); _input->setMouseState(MOUSE_DISABLED); - _soundMan->playLocationMusic(location); + _soundManI->playLocationMusic(location); _input->stopHovering(); _gfx->freeLabels(); @@ -369,7 +371,7 @@ void Parallaction_ns::changeLocation() { _cmdExec->run(_location._aCommands); if (_location._hasSound) - _soundMan->playSfx(_location._soundFile, 0, true); + _soundManI->playSfx(_location._soundFile, 0, true); if (!_intro) { _input->setMouseState(oldMouseState); @@ -428,7 +430,7 @@ void Parallaction_ns::changeCharacter(const char *name) { _objects = _disk->loadObjects(_char.getBaseName()); _objectsNames = _disk->loadTable(_char.getBaseName()); - _soundMan->playCharacterMusic(_char.getBaseName()); + _soundManI->playCharacterMusic(_char.getBaseName()); // The original engine used to reload 'common' only on loadgames. We are reloading here since 'common' // contains character specific stuff. This causes crashes like bug #1816899, because parseLocation tries @@ -466,10 +468,10 @@ void Parallaction_ns::freeCharacter() { void Parallaction_ns::freeLocation(bool removeAll) { debugC(2, kDebugExec, "freeLocation"); - _soundMan->stopSfx(0); - _soundMan->stopSfx(1); - _soundMan->stopSfx(2); - _soundMan->stopSfx(3); + _soundManI->stopSfx(0); + _soundManI->stopSfx(1); + _soundManI->stopSfx(2); + _soundManI->stopSfx(3); _localFlagNames->clear(); @@ -481,7 +483,7 @@ void Parallaction_ns::freeLocation(bool removeAll) { } void Parallaction_ns::cleanupGame() { - _soundMan->stopMusic(); + _soundManI->stopMusic(); _inTestResult = false; _engineFlags &= ~kEngineTransformedDonna; diff --git a/engines/parallaction/parser_ns.cpp b/engines/parallaction/parser_ns.cpp index a84c821e24..b08de2e31a 100644 --- a/engines/parallaction/parser_ns.cpp +++ b/engines/parallaction/parser_ns.cpp @@ -1089,7 +1089,7 @@ DECLARE_LOCATION_PARSER(music) { debugC(7, kDebugParser, "LOCATION_PARSER(music) "); if (_vm->getPlatform() == Common::kPlatformAmiga) - _vm->_soundMan->setMusicFile(_tokens[1]); + _vm->_soundMan->execute(SC_SETMUSICFILE, (SoundManCommandParameter)_tokens[1]); } void LocationParser_ns::parse(Script *script) { diff --git a/engines/parallaction/sound.cpp b/engines/parallaction/sound.cpp deleted file mode 100644 index 503f8b3303..0000000000 --- a/engines/parallaction/sound.cpp +++ /dev/null @@ -1,484 +0,0 @@ -/* 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/file.h" - -#include "common/stream.h" - -#include "sound/mixer.h" -#include "sound/midiparser.h" -#include "sound/mods/protracker.h" - -#include "parallaction/sound.h" -#include "parallaction/parallaction.h" - - -namespace Parallaction { - -class MidiPlayer : public MidiDriver { -public: - - enum { - NUM_CHANNELS = 16 - }; - - MidiPlayer(MidiDriver *driver); - ~MidiPlayer(); - - void play(Common::SeekableReadStream *stream); - void stop(); - void pause(bool p); - void updateTimer(); - void adjustVolume(int diff); - void setVolume(int volume); - int getVolume() const { return _masterVolume; } - void setLooping(bool loop) { _isLooping = loop; } - - // MidiDriver interface - int open(); - void close(); - void send(uint32 b); - void metaEvent(byte type, byte *data, uint16 length); - void setTimerCallback(void *timerParam, void (*timerProc)(void *)) { } - uint32 getBaseTempo() { return _driver ? _driver->getBaseTempo() : 0; } - MidiChannel *allocateChannel() { return 0; } - MidiChannel *getPercussionChannel() { return 0; } - -private: - - static void timerCallback(void *p); - - MidiDriver *_driver; - MidiParser *_parser; - uint8 *_midiData; - bool _isLooping; - bool _isPlaying; - bool _paused; - int _masterVolume; - MidiChannel *_channelsTable[NUM_CHANNELS]; - uint8 _channelsVolume[NUM_CHANNELS]; - Common::Mutex _mutex; -}; - -MidiPlayer::MidiPlayer(MidiDriver *driver) - : _driver(driver), _parser(0), _midiData(0), _isLooping(false), _isPlaying(false), _paused(false), _masterVolume(0) { - assert(_driver); - memset(_channelsTable, 0, sizeof(_channelsTable)); - for (int i = 0; i < NUM_CHANNELS; i++) { - _channelsVolume[i] = 127; - } - - open(); -} - -MidiPlayer::~MidiPlayer() { - close(); -} - -void MidiPlayer::play(Common::SeekableReadStream *stream) { - if (!stream) { - stop(); - return; - } - - int size = stream->size(); - - _midiData = (uint8 *)malloc(size); - if (_midiData) { - stream->read(_midiData, size); - delete stream; - _mutex.lock(); - _parser->loadMusic(_midiData, size); - _parser->setTrack(0); - _isLooping = true; - _isPlaying = true; - _mutex.unlock(); - } -} - -void MidiPlayer::stop() { - _mutex.lock(); - if (_isPlaying) { - _isPlaying = false; - _parser->unloadMusic(); - free(_midiData); - _midiData = 0; - } - _mutex.unlock(); -} - -void MidiPlayer::pause(bool p) { - _paused = p; - - for (int i = 0; i < NUM_CHANNELS; ++i) { - if (_channelsTable[i]) { - _channelsTable[i]->volume(_paused ? 0 : _channelsVolume[i] * _masterVolume / 255); - } - } -} - -void MidiPlayer::updateTimer() { - if (_paused) { - return; - } - - _mutex.lock(); - if (_isPlaying) { - _parser->onTimer(); - } - _mutex.unlock(); -} - -void MidiPlayer::adjustVolume(int diff) { - setVolume(_masterVolume + diff); -} - -void MidiPlayer::setVolume(int volume) { - _masterVolume = CLIP(volume, 0, 255); - _mutex.lock(); - for (int i = 0; i < NUM_CHANNELS; ++i) { - if (_channelsTable[i]) { - _channelsTable[i]->volume(_channelsVolume[i] * _masterVolume / 255); - } - } - _mutex.unlock(); -} - -int MidiPlayer::open() { - int ret = _driver->open(); - if (ret == 0) { - _parser = MidiParser::createParser_SMF(); - _parser->setMidiDriver(this); - _parser->setTimerRate(_driver->getBaseTempo()); - _driver->setTimerCallback(this, &timerCallback); - } - return ret; -} - -void MidiPlayer::close() { - stop(); - _mutex.lock(); - _driver->setTimerCallback(NULL, NULL); - _driver->close(); - delete _driver; - _driver = 0; - _parser->setMidiDriver(NULL); - delete _parser; - _mutex.unlock(); -} - -void MidiPlayer::send(uint32 b) { - byte volume, ch = (byte)(b & 0xF); - switch (b & 0xFFF0) { - case 0x07B0: // volume change - volume = (byte)((b >> 16) & 0x7F); - _channelsVolume[ch] = volume; - volume = volume * _masterVolume / 255; - b = (b & 0xFF00FFFF) | (volume << 16); - break; - case 0x7BB0: // all notes off - if (!_channelsTable[ch]) { - // channel not yet allocated, no need to send the event - return; - } - break; - } - - if (!_channelsTable[ch]) { - _channelsTable[ch] = (ch == 9) ? _driver->getPercussionChannel() : _driver->allocateChannel(); - } - if (_channelsTable[ch]) { - _channelsTable[ch]->send(b); - } -} - -void MidiPlayer::metaEvent(byte type, byte *data, uint16 length) { - switch (type) { - case 0x2F: // end of Track - if (_isLooping) { - _parser->jumpToTick(0); - } else { - stop(); - } - break; - default: -// warning("Unhandled meta event: %02x", type); - break; - } -} - -void MidiPlayer::timerCallback(void *p) { - MidiPlayer *player = (MidiPlayer *)p; - - player->updateTimer(); -} - -DosSoundMan::DosSoundMan(Parallaction *vm, MidiDriver *midiDriver) : SoundMan(vm), _musicData1(0) { - _midiPlayer = new MidiPlayer(midiDriver); -} - -DosSoundMan::~DosSoundMan() { - debugC(1, kDebugAudio, "DosSoundMan::playMusic()"); - - delete _midiPlayer; -} - -bool DosSoundMan::isLocationSilent(const char *locationName) { - - // these are the prefixes for location names with no background midi music - const char *noMusicPrefix[] = { "museo", "intgrottadopo", "caveau", "estgrotta", "plaza1", "endtgz", "common", 0 }; - Common::String s(locationName); - - for (int i = 0; noMusicPrefix[i]; i++) { - if (s.hasPrefix(noMusicPrefix[i])) { - return true; - } - } - - return false; -} - -void DosSoundMan::playMusic() { - debugC(1, kDebugAudio, "DosSoundMan::playMusic()"); - - if (isLocationSilent(_vm->_location._name)) { - // just stop the music if this location is silent - _midiPlayer->stop(); - return; - } - - Common::SeekableReadStream *stream = _vm->_disk->loadMusic(_musicFile); - _midiPlayer->play(stream); - _midiPlayer->setVolume(255); -} - -void DosSoundMan::stopMusic() { - _midiPlayer->stop(); -} - -void DosSoundMan::pause(bool p) { - SoundMan::pause(p); - _midiPlayer->pause(p); -} - -void DosSoundMan::playCharacterMusic(const char *character) { - if (character == NULL) { - return; - } - - if (!scumm_stricmp(_vm->_location._name, "night") || - !scumm_stricmp(_vm->_location._name, "intsushi")) { - return; - } - - char *name = const_cast(character); - - if (!scumm_stricmp(name, _dinoName)) { - setMusicFile("dino"); - } else - if (!scumm_stricmp(name, _donnaName)) { - setMusicFile("donna"); - } else - if (!scumm_stricmp(name, _doughName)) { - setMusicFile("nuts"); - } else { - warning("unknown character '%s' in DosSoundMan::playCharacterMusic", character); - return; - } - - playMusic(); -} - -void DosSoundMan::playLocationMusic(const char *location) { - if (_musicData1 != 0) { - playCharacterMusic(_vm->_char.getBaseName()); - _musicData1 = 0; - debugC(2, kDebugExec, "changeLocation: started character specific music"); - } - - if (!scumm_stricmp(location, "night") || !scumm_stricmp(location, "intsushi")) { - setMusicFile("nuts"); - playMusic(); - - debugC(2, kDebugExec, "changeLocation: started music 'soft'"); - } - - if (isLocationSilent(location)) { - stopMusic(); - _musicData1 = 1; - - debugC(2, kDebugExec, "changeLocation: music stopped"); - } -} - -AmigaSoundMan::AmigaSoundMan(Parallaction *vm) : SoundMan(vm) { - _musicStream = 0; - _channels[0].data = 0; - _channels[0].dispose = false; - _channels[1].data = 0; - _channels[1].dispose = false; - _channels[2].data = 0; - _channels[2].dispose = false; - _channels[3].data = 0; - _channels[3].dispose = false; -} - -AmigaSoundMan::~AmigaSoundMan() { - stopMusic(); - stopSfx(0); - stopSfx(1); - stopSfx(2); - stopSfx(3); -} - -#define AMIGABEEP_SIZE 16 -#define NUM_REPEATS 60 - -static int8 res_amigaBeep[AMIGABEEP_SIZE] = { - 0, 20, 40, 60, 80, 60, 40, 20, 0, -20, -40, -60, -80, -60, -40, -20 -}; - - -void AmigaSoundMan::loadChannelData(const char *filename, Channel *ch) { - if (!scumm_stricmp("beep", filename)) { - ch->header.oneShotHiSamples = 0; - ch->header.repeatHiSamples = 0; - ch->header.samplesPerHiCycle = 0; - ch->header.samplesPerSec = 11934; - ch->header.volume = 160; - ch->data = (int8*)malloc(AMIGABEEP_SIZE * NUM_REPEATS); - int8* odata = ch->data; - for (uint i = 0; i < NUM_REPEATS; i++) { - memcpy(odata, res_amigaBeep, AMIGABEEP_SIZE); - odata += AMIGABEEP_SIZE; - } - ch->dataSize = AMIGABEEP_SIZE * NUM_REPEATS; - ch->dispose = true; - return; - } - - Common::ReadStream *stream = _vm->_disk->loadSound(filename); - Audio::A8SVXDecoder decoder(*stream, ch->header, ch->data, ch->dataSize); - decoder.decode(); - ch->dispose = true; - delete stream; -} - -void AmigaSoundMan::playSfx(const char *filename, uint channel, bool looping, int volume, int rate) { - if (channel >= NUM_AMIGA_CHANNELS) { - warning("unknown sfx channel"); - return; - } - - stopSfx(channel); - - debugC(1, kDebugAudio, "AmigaSoundMan::playSfx(%s, %i)", filename, channel); - - Channel *ch = &_channels[channel]; - loadChannelData(filename, ch); - - uint32 loopStart, loopEnd, flags; - if (looping) { - // the standard way to loop 8SVX audio implies use of the oneShotHiSamples and - // repeatHiSamples fields, but Nippon Safes handles loops according to flags - // set in its location scripts and always operates on the whole data. - loopStart = 0; - loopEnd = ch->header.oneShotHiSamples + ch->header.repeatHiSamples; - flags = Audio::Mixer::FLAG_LOOP; - } else { - loopStart = loopEnd = 0; - flags = 0; - } - - if (volume == -1) { - volume = ch->header.volume; - } - - if (rate == -1) { - rate = ch->header.samplesPerSec; - } - - _mixer->playRaw(Audio::Mixer::kSFXSoundType, &ch->handle, ch->data, ch->dataSize, rate, flags, -1, volume, 0, loopStart, loopEnd); -} - -void AmigaSoundMan::stopSfx(uint channel) { - if (channel >= NUM_AMIGA_CHANNELS) { - warning("unknown sfx channel"); - return; - } - - if (_channels[channel].dispose) { - debugC(1, kDebugAudio, "AmigaSoundMan::stopSfx(%i)", channel); - _mixer->stopHandle(_channels[channel].handle); - free(_channels[channel].data); - _channels[channel].data = 0; - } -} - -void AmigaSoundMan::playMusic() { - stopMusic(); - - debugC(1, kDebugAudio, "AmigaSoundMan::playMusic()"); - - Common::SeekableReadStream *stream = _vm->_disk->loadMusic(_musicFile); - _musicStream = Audio::makeProtrackerStream(stream); - delete stream; - - debugC(3, kDebugAudio, "AmigaSoundMan::playMusic(): created new music stream"); - - _mixer->playInputStream(Audio::Mixer::kMusicSoundType, &_musicHandle, _musicStream, -1, 255, 0, false, false); -} - -void AmigaSoundMan::stopMusic() { - debugC(1, kDebugAudio, "AmigaSoundMan::stopMusic()"); - - if (_mixer->isSoundHandleActive(_musicHandle)) { - _mixer->stopHandle(_musicHandle); - delete _musicStream; - _musicStream = 0; - } -} - -void AmigaSoundMan::playCharacterMusic(const char *character) { -} - -void AmigaSoundMan::playLocationMusic(const char *location) { -} - - -SoundMan::SoundMan(Parallaction *vm) : _vm(vm) { - _mixer = _vm->_mixer; -} - -void SoundMan::setMusicVolume(int value) { - _mixer->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, value); -} - -void SoundMan::setMusicFile(const char *filename) { - strcpy(_musicFile, filename); -} - - -} // namespace Parallaction diff --git a/engines/parallaction/sound.h b/engines/parallaction/sound.h index 2baa9aaa63..e3eac1ccff 100644 --- a/engines/parallaction/sound.h +++ b/engines/parallaction/sound.h @@ -40,21 +40,69 @@ class MidiParser; namespace Parallaction { -class Parallaction; +class Parallaction_ns; class MidiPlayer; +typedef void* SoundManCommandParameter; + +class SoundManImpl { +public: + virtual void execute(int command, SoundManCommandParameter parm) = 0; + virtual ~SoundManImpl() { } +}; + class SoundMan { + SoundManImpl *_impl; +public: + SoundMan(SoundManImpl *impl) : _impl(impl) { } + virtual ~SoundMan() { delete _impl; } + void execute(int command, SoundManCommandParameter parm = 0) { + if (_impl) { + _impl->execute(command, parm); + } + } +}; + +enum { + // soundMan commands + SC_PLAYMUSIC, + SC_STOPMUSIC, + SC_SETMUSICTYPE, + SC_SETMUSICFILE, + SC_PLAYSFX, + SC_STOPSFX, + SC_SETSFXCHANNEL, + SC_SETSFXLOOPING, + SC_SETSFXVOLUME, + SC_SETSFXRATE, + SC_PAUSE +}; + +class SoundMan_ns : public SoundManImpl { +public: + enum { + MUSIC_ANY, + MUSIC_CHARACTER, + MUSIC_LOCATION + }; protected: - Parallaction *_vm; + Parallaction_ns *_vm; Audio::Mixer *_mixer; char _musicFile[PATH_LEN]; + bool _sfxLooping; + int _sfxVolume; + int _sfxRate; + uint _sfxChannel; + + int _musicType; + public: - SoundMan(Parallaction *vm); - virtual ~SoundMan() {} + SoundMan_ns(Parallaction_ns *vm); + virtual ~SoundMan_ns() {} - virtual void playSfx(const char *filename, uint channel, bool looping, int volume = -1, int rate = -1) { } + virtual void playSfx(const char *filename, uint channel, bool looping, int volume = -1) { } virtual void stopSfx(uint channel) { } void setMusicFile(const char *filename); @@ -63,11 +111,12 @@ public: virtual void playCharacterMusic(const char *character) = 0; virtual void playLocationMusic(const char *location) = 0; virtual void pause(bool p) { } + virtual void execute(int command, SoundManCommandParameter parm = 0); void setMusicVolume(int value); }; -class DosSoundMan : public SoundMan { +class DosSoundMan_ns : public SoundMan_ns { MidiPlayer *_midiPlayer; int _musicData1; @@ -75,8 +124,8 @@ class DosSoundMan : public SoundMan { bool isLocationSilent(const char *locationName); public: - DosSoundMan(Parallaction *vm, MidiDriver *midiDriver); - ~DosSoundMan(); + DosSoundMan_ns(Parallaction_ns *vm, MidiDriver *midiDriver); + ~DosSoundMan_ns(); void playMusic(); void stopMusic(); @@ -88,7 +137,7 @@ public: #define NUM_AMIGA_CHANNELS 4 -class AmigaSoundMan : public SoundMan { +class AmigaSoundMan_ns : public SoundMan_ns { Audio::AudioStream *_musicStream; Audio::SoundHandle _musicHandle; @@ -105,32 +154,21 @@ class AmigaSoundMan : public SoundMan { void loadChannelData(const char *filename, Channel *ch); public: - AmigaSoundMan(Parallaction *vm); - ~AmigaSoundMan(); + AmigaSoundMan_ns(Parallaction_ns *vm); + ~AmigaSoundMan_ns(); void playMusic(); void stopMusic(); - void playSfx(const char *filename, uint channel, bool looping, int volume, int rate); + void playSfx(const char *filename, uint channel, bool looping, int volume); void stopSfx(uint channel); void playCharacterMusic(const char *character); void playLocationMusic(const char *location); }; -class DummySoundMan : public SoundMan { - -public: - DummySoundMan(Parallaction *vm) : SoundMan(vm) { } - ~DummySoundMan() { } - void playMusic() { } - void stopMusic() { } - - void playSfx(const char *filename, uint channel, bool looping, int volume, int rate) { } - void stopSfx(uint channel) { } - - void playCharacterMusic(const char *character) { } - void playLocationMusic(const char *location) { } - +class DummySoundMan : public SoundManImpl { +public: + void execute(int command, SoundManCommandParameter parm) { } }; } // namespace Parallaction diff --git a/engines/parallaction/sound_ns.cpp b/engines/parallaction/sound_ns.cpp new file mode 100644 index 0000000000..20dc7dcd25 --- /dev/null +++ b/engines/parallaction/sound_ns.cpp @@ -0,0 +1,520 @@ +/* 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/file.h" + +#include "common/stream.h" + +#include "sound/mixer.h" +#include "sound/midiparser.h" +#include "sound/mods/protracker.h" + +#include "parallaction/sound.h" +#include "parallaction/parallaction.h" + + +namespace Parallaction { + +class MidiPlayer : public MidiDriver { +public: + + enum { + NUM_CHANNELS = 16 + }; + + MidiPlayer(MidiDriver *driver); + ~MidiPlayer(); + + void play(Common::SeekableReadStream *stream); + void stop(); + void pause(bool p); + void updateTimer(); + void adjustVolume(int diff); + void setVolume(int volume); + int getVolume() const { return _masterVolume; } + void setLooping(bool loop) { _isLooping = loop; } + + // MidiDriver interface + int open(); + void close(); + void send(uint32 b); + void metaEvent(byte type, byte *data, uint16 length); + void setTimerCallback(void *timerParam, void (*timerProc)(void *)) { } + uint32 getBaseTempo() { return _driver ? _driver->getBaseTempo() : 0; } + MidiChannel *allocateChannel() { return 0; } + MidiChannel *getPercussionChannel() { return 0; } + +private: + + static void timerCallback(void *p); + + MidiDriver *_driver; + MidiParser *_parser; + uint8 *_midiData; + bool _isLooping; + bool _isPlaying; + bool _paused; + int _masterVolume; + MidiChannel *_channelsTable[NUM_CHANNELS]; + uint8 _channelsVolume[NUM_CHANNELS]; + Common::Mutex _mutex; +}; + +MidiPlayer::MidiPlayer(MidiDriver *driver) + : _driver(driver), _parser(0), _midiData(0), _isLooping(false), _isPlaying(false), _paused(false), _masterVolume(0) { + assert(_driver); + memset(_channelsTable, 0, sizeof(_channelsTable)); + for (int i = 0; i < NUM_CHANNELS; i++) { + _channelsVolume[i] = 127; + } + + open(); +} + +MidiPlayer::~MidiPlayer() { + close(); +} + +void MidiPlayer::play(Common::SeekableReadStream *stream) { + if (!stream) { + stop(); + return; + } + + int size = stream->size(); + + _midiData = (uint8 *)malloc(size); + if (_midiData) { + stream->read(_midiData, size); + delete stream; + _mutex.lock(); + _parser->loadMusic(_midiData, size); + _parser->setTrack(0); + _isLooping = true; + _isPlaying = true; + _mutex.unlock(); + } +} + +void MidiPlayer::stop() { + _mutex.lock(); + if (_isPlaying) { + _isPlaying = false; + _parser->unloadMusic(); + free(_midiData); + _midiData = 0; + } + _mutex.unlock(); +} + +void MidiPlayer::pause(bool p) { + _paused = p; + + for (int i = 0; i < NUM_CHANNELS; ++i) { + if (_channelsTable[i]) { + _channelsTable[i]->volume(_paused ? 0 : _channelsVolume[i] * _masterVolume / 255); + } + } +} + +void MidiPlayer::updateTimer() { + if (_paused) { + return; + } + + _mutex.lock(); + if (_isPlaying) { + _parser->onTimer(); + } + _mutex.unlock(); +} + +void MidiPlayer::adjustVolume(int diff) { + setVolume(_masterVolume + diff); +} + +void MidiPlayer::setVolume(int volume) { + _masterVolume = CLIP(volume, 0, 255); + _mutex.lock(); + for (int i = 0; i < NUM_CHANNELS; ++i) { + if (_channelsTable[i]) { + _channelsTable[i]->volume(_channelsVolume[i] * _masterVolume / 255); + } + } + _mutex.unlock(); +} + +int MidiPlayer::open() { + int ret = _driver->open(); + if (ret == 0) { + _parser = MidiParser::createParser_SMF(); + _parser->setMidiDriver(this); + _parser->setTimerRate(_driver->getBaseTempo()); + _driver->setTimerCallback(this, &timerCallback); + } + return ret; +} + +void MidiPlayer::close() { + stop(); + _mutex.lock(); + _driver->setTimerCallback(NULL, NULL); + _driver->close(); + delete _driver; + _driver = 0; + _parser->setMidiDriver(NULL); + delete _parser; + _mutex.unlock(); +} + +void MidiPlayer::send(uint32 b) { + byte volume, ch = (byte)(b & 0xF); + switch (b & 0xFFF0) { + case 0x07B0: // volume change + volume = (byte)((b >> 16) & 0x7F); + _channelsVolume[ch] = volume; + volume = volume * _masterVolume / 255; + b = (b & 0xFF00FFFF) | (volume << 16); + break; + case 0x7BB0: // all notes off + if (!_channelsTable[ch]) { + // channel not yet allocated, no need to send the event + return; + } + break; + } + + if (!_channelsTable[ch]) { + _channelsTable[ch] = (ch == 9) ? _driver->getPercussionChannel() : _driver->allocateChannel(); + } + if (_channelsTable[ch]) { + _channelsTable[ch]->send(b); + } +} + +void MidiPlayer::metaEvent(byte type, byte *data, uint16 length) { + switch (type) { + case 0x2F: // end of Track + if (_isLooping) { + _parser->jumpToTick(0); + } else { + stop(); + } + break; + default: +// warning("Unhandled meta event: %02x", type); + break; + } +} + +void MidiPlayer::timerCallback(void *p) { + MidiPlayer *player = (MidiPlayer *)p; + + player->updateTimer(); +} + +DosSoundMan_ns::DosSoundMan_ns(Parallaction_ns *vm, MidiDriver *midiDriver) : SoundMan_ns(vm), _musicData1(0) { + _midiPlayer = new MidiPlayer(midiDriver); +} + +DosSoundMan_ns::~DosSoundMan_ns() { + debugC(1, kDebugAudio, "DosSoundMan_ns_ns::playMusic()"); + + delete _midiPlayer; +} + +bool DosSoundMan_ns::isLocationSilent(const char *locationName) { + + // these are the prefixes for location names with no background midi music + const char *noMusicPrefix[] = { "museo", "intgrottadopo", "caveau", "estgrotta", "plaza1", "endtgz", "common", 0 }; + Common::String s(locationName); + + for (int i = 0; noMusicPrefix[i]; i++) { + if (s.hasPrefix(noMusicPrefix[i])) { + return true; + } + } + + return false; +} + +void DosSoundMan_ns::playMusic() { + debugC(1, kDebugAudio, "DosSoundMan_ns_ns::playMusic()"); + + if (isLocationSilent(_vm->_location._name)) { + // just stop the music if this location is silent + _midiPlayer->stop(); + return; + } + + Common::SeekableReadStream *stream = _vm->_disk->loadMusic(_musicFile); + _midiPlayer->play(stream); + _midiPlayer->setVolume(255); +} + +void DosSoundMan_ns::stopMusic() { + _midiPlayer->stop(); +} + +void DosSoundMan_ns::pause(bool p) { + SoundMan_ns::pause(p); + _midiPlayer->pause(p); +} + +void DosSoundMan_ns::playCharacterMusic(const char *character) { + if (character == NULL) { + return; + } + + if (!scumm_stricmp(_vm->_location._name, "night") || + !scumm_stricmp(_vm->_location._name, "intsushi")) { + return; + } + + char *name = const_cast(character); + + if (!scumm_stricmp(name, _dinoName)) { + setMusicFile("dino"); + } else + if (!scumm_stricmp(name, _donnaName)) { + setMusicFile("donna"); + } else + if (!scumm_stricmp(name, _doughName)) { + setMusicFile("nuts"); + } else { + warning("unknown character '%s' in DosSoundMan_ns_ns::playCharacterMusic", character); + return; + } + + playMusic(); +} + +void DosSoundMan_ns::playLocationMusic(const char *location) { + if (_musicData1 != 0) { + playCharacterMusic(_vm->_char.getBaseName()); + _musicData1 = 0; + debugC(2, kDebugExec, "changeLocation: started character specific music"); + } + + if (!scumm_stricmp(location, "night") || !scumm_stricmp(location, "intsushi")) { + setMusicFile("nuts"); + playMusic(); + + debugC(2, kDebugExec, "changeLocation: started music 'soft'"); + } + + if (isLocationSilent(location)) { + stopMusic(); + _musicData1 = 1; + + debugC(2, kDebugExec, "changeLocation: music stopped"); + } +} + +AmigaSoundMan_ns::AmigaSoundMan_ns(Parallaction_ns *vm) : SoundMan_ns(vm) { + _musicStream = 0; + _channels[0].data = 0; + _channels[0].dispose = false; + _channels[1].data = 0; + _channels[1].dispose = false; + _channels[2].data = 0; + _channels[2].dispose = false; + _channels[3].data = 0; + _channels[3].dispose = false; +} + +AmigaSoundMan_ns::~AmigaSoundMan_ns() { + stopMusic(); + stopSfx(0); + stopSfx(1); + stopSfx(2); + stopSfx(3); +} + +#define AMIGABEEP_SIZE 16 +#define NUM_REPEATS 60 + +static int8 res_amigaBeep[AMIGABEEP_SIZE] = { + 0, 20, 40, 60, 80, 60, 40, 20, 0, -20, -40, -60, -80, -60, -40, -20 +}; + + +void AmigaSoundMan_ns::loadChannelData(const char *filename, Channel *ch) { + if (!scumm_stricmp("beep", filename)) { + ch->header.oneShotHiSamples = 0; + ch->header.repeatHiSamples = 0; + ch->header.samplesPerHiCycle = 0; + ch->header.samplesPerSec = 11934; + ch->header.volume = 160; + ch->data = (int8*)malloc(AMIGABEEP_SIZE * NUM_REPEATS); + int8* odata = ch->data; + for (uint i = 0; i < NUM_REPEATS; i++) { + memcpy(odata, res_amigaBeep, AMIGABEEP_SIZE); + odata += AMIGABEEP_SIZE; + } + ch->dataSize = AMIGABEEP_SIZE * NUM_REPEATS; + ch->dispose = true; + return; + } + + Common::ReadStream *stream = _vm->_disk->loadSound(filename); + Audio::A8SVXDecoder decoder(*stream, ch->header, ch->data, ch->dataSize); + decoder.decode(); + ch->dispose = true; + delete stream; +} + +void AmigaSoundMan_ns::playSfx(const char *filename, uint channel, bool looping, int volume) { + if (channel >= NUM_AMIGA_CHANNELS) { + warning("unknown sfx channel"); + return; + } + + stopSfx(channel); + + debugC(1, kDebugAudio, "AmigaSoundMan_ns::playSfx(%s, %i)", filename, channel); + + Channel *ch = &_channels[channel]; + loadChannelData(filename, ch); + + uint32 loopStart, loopEnd, flags; + if (looping) { + // the standard way to loop 8SVX audio implies use of the oneShotHiSamples and + // repeatHiSamples fields, but Nippon Safes handles loops according to flags + // set in its location scripts and always operates on the whole data. + loopStart = 0; + loopEnd = ch->header.oneShotHiSamples + ch->header.repeatHiSamples; + flags = Audio::Mixer::FLAG_LOOP; + } else { + loopStart = loopEnd = 0; + flags = 0; + } + + if (volume == -1) { + volume = ch->header.volume; + } + + _mixer->playRaw(Audio::Mixer::kSFXSoundType, &ch->handle, ch->data, ch->dataSize, + ch->header.samplesPerSec, flags, -1, volume, 0, loopStart, loopEnd); +} + +void AmigaSoundMan_ns::stopSfx(uint channel) { + if (channel >= NUM_AMIGA_CHANNELS) { + warning("unknown sfx channel"); + return; + } + + if (_channels[channel].dispose) { + debugC(1, kDebugAudio, "AmigaSoundMan_ns::stopSfx(%i)", channel); + _mixer->stopHandle(_channels[channel].handle); + free(_channels[channel].data); + _channels[channel].data = 0; + } +} + +void AmigaSoundMan_ns::playMusic() { + stopMusic(); + + debugC(1, kDebugAudio, "AmigaSoundMan_ns::playMusic()"); + + Common::SeekableReadStream *stream = _vm->_disk->loadMusic(_musicFile); + _musicStream = Audio::makeProtrackerStream(stream); + delete stream; + + debugC(3, kDebugAudio, "AmigaSoundMan_ns::playMusic(): created new music stream"); + + _mixer->playInputStream(Audio::Mixer::kMusicSoundType, &_musicHandle, _musicStream, -1, 255, 0, false, false); +} + +void AmigaSoundMan_ns::stopMusic() { + debugC(1, kDebugAudio, "AmigaSoundMan_ns::stopMusic()"); + + if (_mixer->isSoundHandleActive(_musicHandle)) { + _mixer->stopHandle(_musicHandle); + delete _musicStream; + _musicStream = 0; + } +} + +void AmigaSoundMan_ns::playCharacterMusic(const char *character) { +} + +void AmigaSoundMan_ns::playLocationMusic(const char *location) { +} + + +SoundMan_ns::SoundMan_ns(Parallaction_ns *vm) : _vm(vm) { + _mixer = _vm->_mixer; +} + +void SoundMan_ns::setMusicVolume(int value) { + _mixer->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, value); +} + +void SoundMan_ns::setMusicFile(const char *filename) { + strcpy(_musicFile, filename); +} + +void SoundMan_ns::execute(int command, SoundManCommandParameter parm) { + switch (command) { + case SC_PLAYMUSIC: + if (_musicType == MUSIC_CHARACTER) playCharacterMusic((const char*)parm); + else if (_musicType == MUSIC_LOCATION) playLocationMusic((const char*)parm); + else playMusic(); + break; + case SC_STOPMUSIC: + stopMusic(); + break; + case SC_SETMUSICTYPE: + _musicType = (int)parm; + break; + case SC_SETMUSICFILE: + setMusicFile((const char*)parm); + break; + + case SC_PLAYSFX: + playSfx((const char*)parm, _sfxChannel, _sfxLooping, _sfxVolume); + break; + case SC_STOPSFX: + stopSfx((int)parm); + break; + + case SC_SETSFXCHANNEL: + _sfxChannel = (uint)parm; + break; + case SC_SETSFXLOOPING: + _sfxLooping = (bool)parm; + break; + case SC_SETSFXVOLUME: + _sfxVolume = (int)parm; + break; + + case SC_PAUSE: + pause((bool)parm); + break; + } +} + +} // namespace Parallaction -- cgit v1.2.3