From bca3cac978f247d7df9a4b6d0d38796b4cffa5be Mon Sep 17 00:00:00 2001 From: Joel Teichroeb Date: Fri, 17 Feb 2012 16:53:08 -0800 Subject: [PATCH] EMI: Add basic support for sound effects --- engines/grim/emi/lua_v2_sound.cpp | 46 ++++++++++++++++++++-- engines/grim/emi/sound/aifftrack.cpp | 59 ++++++++++++++++++++++++++++ engines/grim/emi/sound/aifftrack.h | 47 ++++++++++++++++++++++ engines/grim/module.mk | 1 + 4 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 engines/grim/emi/sound/aifftrack.cpp create mode 100644 engines/grim/emi/sound/aifftrack.h diff --git a/engines/grim/emi/lua_v2_sound.cpp b/engines/grim/emi/lua_v2_sound.cpp index d6dc63c0fe2..985c4401425 100644 --- a/engines/grim/emi/lua_v2_sound.cpp +++ b/engines/grim/emi/lua_v2_sound.cpp @@ -26,11 +26,18 @@ #define FORBIDDEN_SYMBOL_EXCEPTION_getwd #define FORBIDDEN_SYMBOL_EXCEPTION_mkdir +#include "audio/mixer.h" +#include "audio/audiostream.h" +#include "common/system.h" + +#include "engines/grim/emi/sound/aifftrack.h" #include "engines/grim/emi/lua_v2.h" #include "engines/grim/lua/lua.h" #include "engines/grim/sound.h" #include "engines/grim/grim.h" +#include "engines/grim/resource.h" +#include "audio/decoders/aiff.h" namespace Grim { @@ -172,6 +179,18 @@ void Lua_V2::ImFlushStack() { warning("Lua_V2::ImFlushStack: implement opcode"); } +class PoolSound : public PoolObject{ +public: + PoolSound(const Common::String &filename); + AIFFTrack *track; +}; + +PoolSound::PoolSound(const Common::String &filename) { + track = new AIFFTrack(Audio::Mixer::kSFXSoundType); + Common::SeekableReadStream *stream = g_resourceloader->openNewStreamFile(filename); + track->openSound(filename, stream); +} + void Lua_V2::LoadSound() { lua_Object strObj = lua_getparam(1); @@ -179,12 +198,23 @@ void Lua_V2::LoadSound() { return; const char *str = lua_getstring(strObj); - // FIXME: implement code - warning("Lua_V2::LoadSound: implement opcode, wants to load %s", str); + + Common::String filename = str; + filename += ".aif"; + + PoolSound *sound = new PoolSound(filename); + lua_pushusertag(sound->getId(), MKTAG('A', 'I', 'F', 'F')); } void Lua_V2::FreeSound() { - warning("Lua_V2::FreeSound: implement opcode"); + lua_Object idObj = lua_getparam(1); + if (!lua_isuserdata(idObj) || lua_tag(idObj) != MKTAG('A', 'I', 'F', 'F')) + return; + PoolSound *sound = PoolSound::getPool().getObject(lua_getuserdata(idObj)); + if (sound) { + sound->track->stop(); + delete sound; + } } void Lua_V2::PlayLoadedSound() { @@ -193,7 +223,15 @@ void Lua_V2::PlayLoadedSound() { lua_Object volumeObj = lua_getparam(3); lua_Object bool2Obj = lua_getparam(4); - warning("Lua_V2::PlayLoadedSound: implement opcode"); + + if (!lua_isuserdata(idObj) || lua_tag(idObj) != MKTAG('A', 'I', 'F', 'F')) + return; + + bool looping = !lua_isnil(bool1Obj); + + PoolSound *sound = PoolSound::getPool().getObject(lua_getuserdata(idObj)); + sound->track->setLooping(looping); + sound->track->play(); } void Lua_V2::ImSetMusicVol() { diff --git a/engines/grim/emi/sound/aifftrack.cpp b/engines/grim/emi/sound/aifftrack.cpp new file mode 100644 index 00000000000..db3d71e8fe6 --- /dev/null +++ b/engines/grim/emi/sound/aifftrack.cpp @@ -0,0 +1,59 @@ +/* ResidualVM - A 3D game interpreter + * + * ResidualVM 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 library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + + * This library 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 + * Lesser General Public License for more details. + + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + * + */ + +#include "common/mutex.h" +#include "common/textconsole.h" +#include "audio/mixer.h" +#include "audio/audiostream.h" +#include "audio/decoders/aiff.h" +#include "engines/grim/resource.h" +#include "engines/grim/emi/sound/aifftrack.h" + +namespace Grim { + +AIFFTrack::AIFFTrack(Audio::Mixer::SoundType soundType) { + _soundType = soundType; +} + +AIFFTrack::~AIFFTrack() { + stop(); + delete _handle; +} + +bool AIFFTrack::openSound(Common::String soundName, Common::SeekableReadStream *file) { + if (!file) { + warning("Stream for %s not open", soundName.c_str()); + //return false; + } + _soundName = soundName; + _stream = Audio::makeAIFFStream(file, DisposeAfterUse::YES); + _handle = new Audio::SoundHandle(); + return true; +} + +void AIFFTrack::setLooping(bool looping) { + if (looping) { + _stream = Audio::makeLoopingAudioStream(dynamic_cast(_stream), 0); + } +} + +} // end of namespace Grim diff --git a/engines/grim/emi/sound/aifftrack.h b/engines/grim/emi/sound/aifftrack.h new file mode 100644 index 00000000000..9b1106e2ef8 --- /dev/null +++ b/engines/grim/emi/sound/aifftrack.h @@ -0,0 +1,47 @@ +/* ResidualVM - A 3D game interpreter + * + * ResidualVM 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 library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + + * This library 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 + * Lesser General Public License for more details. + + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + * + */ + +#ifndef GRIM_AIFFTRACK_H +#define GRIM_AIFFTRACK_H + +#include "common/str.h" +#include "common/stream.h" +#include "engines/grim/emi/sound/track.h" + +namespace Audio { + class AudioStream; + class SoundHandle; +} + +namespace Grim { + +class AIFFTrack : public SoundTrack { +public: + AIFFTrack(Audio::Mixer::SoundType soundType); + ~AIFFTrack(); + bool openSound(Common::String soundName, Common::SeekableReadStream *file); + bool isPlaying() { return true; } + void setLooping(bool looping); +}; + +} +#endif diff --git a/engines/grim/module.mk b/engines/grim/module.mk index 0c1f1c29de0..cb7994a6f44 100644 --- a/engines/grim/module.mk +++ b/engines/grim/module.mk @@ -18,6 +18,7 @@ MODULE_OBJS := \ emi/costume/emimesh_component.o \ emi/costume/emiskel_component.o \ emi/costume/emisprite_component.o \ + emi/sound/aifftrack.o \ emi/sound/mp3track.o \ emi/sound/scxtrack.o \ emi/sound/vimatrack.o \