From 3d5384e2bb350d42f0331fa77f59e3d5c804bb97 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Thu, 29 Mar 2012 11:28:33 -0700 Subject: [PATCH 01/18] Remove some unneeded casts --- apps/openmw/mwsound/soundmanager.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/openmw/mwsound/soundmanager.cpp b/apps/openmw/mwsound/soundmanager.cpp index ad9e47f72..f072e084d 100644 --- a/apps/openmw/mwsound/soundmanager.cpp +++ b/apps/openmw/mwsound/soundmanager.cpp @@ -108,7 +108,7 @@ namespace MWSound max = std::max(min, max); } - return std::string("Sound/")+snd->sound; + return "Sound/"+snd->sound; } @@ -182,7 +182,7 @@ namespace MWSound { // The range values are not tested float basevol = 1.0f; /* TODO: volume settings */ - std::string filePath = std::string("Sound/")+filename; + std::string filePath = "Sound/"+filename; const ESM::Position &pos = ptr.getCellRef().pos; SoundPtr sound = mOutput->playSound3D(filePath, pos.pos, basevol, 1.0f, From de102cd27465c22f32a6e16277c45a850d13e34d Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 30 Mar 2012 06:28:40 -0700 Subject: [PATCH 02/18] Simplify broken OpenAL workaround --- apps/openmw/mwsound/openal_output.cpp | 30 +++++---------------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp index 041759683..13ea49481 100644 --- a/apps/openmw/mwsound/openal_output.cpp +++ b/apps/openmw/mwsound/openal_output.cpp @@ -442,33 +442,13 @@ void OpenAL_Output::init(const std::string &devname) { ALCuint maxtotal = std::min(maxmono+maxstereo, 256); if (maxtotal == 0) // workaround for broken implementations - { maxtotal = 256; - bool stop = false; - for(size_t i = 0;i < maxtotal && !stop;i++) // generate source until error returned - { - ALuint src = 0; - alGenSources(1, &src); - ALenum err = alGetError(); - if(err != AL_NO_ERROR) - { - stop = true; - } - else - { - mFreeSources.push_back(src); - } - } - } - else // normal case + for(size_t i = 0;i < maxtotal;i++) { - for(size_t i = 0;i < maxtotal;i++) - { - ALuint src = 0; - alGenSources(1, &src); - throwALerror(); - mFreeSources.push_back(src); - } + ALuint src = 0; + alGenSources(1, &src); + throwALerror(); + mFreeSources.push_back(src); } } catch(std::exception &e) From 71d9d7e94331df0ab05709cb00230b23fb836357 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 30 Mar 2012 06:41:06 -0700 Subject: [PATCH 03/18] Store the current sound position with the Sound object --- apps/openmw/mwsound/sound.hpp | 6 +++++- apps/openmw/mwsound/soundmanager.cpp | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/apps/openmw/mwsound/sound.hpp b/apps/openmw/mwsound/sound.hpp index 2cbd48d96..f1b09875c 100644 --- a/apps/openmw/mwsound/sound.hpp +++ b/apps/openmw/mwsound/sound.hpp @@ -1,6 +1,8 @@ #ifndef GAME_SOUND_SOUND_H #define GAME_SOUND_SOUND_H +#include + namespace MWSound { class Sound @@ -11,6 +13,7 @@ namespace MWSound Sound(const Sound &rhs); protected: + Ogre::Vector3 mPos; float mVolume; /* NOTE: Real volume = mVolume*mBaseVolume */ float mBaseVolume; float mMinDistance; @@ -21,7 +24,8 @@ namespace MWSound virtual bool isPlaying() = 0; virtual void setVolume(float volume) = 0; - Sound() : mVolume(1.0f) + Sound() : mPos(0.0f, 0.0f, 0.0f) + , mVolume(1.0f) , mBaseVolume(1.0f) , mMinDistance(20.0f) /* 1 * min_range_scale */ , mMaxDistance(12750.0f) /* 255 * max_range_scale */ diff --git a/apps/openmw/mwsound/soundmanager.cpp b/apps/openmw/mwsound/soundmanager.cpp index f072e084d..d2e52043a 100644 --- a/apps/openmw/mwsound/soundmanager.cpp +++ b/apps/openmw/mwsound/soundmanager.cpp @@ -187,6 +187,7 @@ namespace MWSound SoundPtr sound = mOutput->playSound3D(filePath, pos.pos, basevol, 1.0f, 20.0f, 12750.0f, false); + sound->mPos = Ogre::Vector3(pos.pos[0], pos.pos[1], pos.pos[2]); sound->mBaseVolume = basevol; mActiveSounds[sound] = std::make_pair(ptr, std::string("_say_sound")); @@ -241,6 +242,7 @@ namespace MWSound const ESM::Position &pos = ptr.getCellRef().pos; sound = mOutput->playSound3D(file, pos.pos, volume*basevol, pitch, min, max, loop); + sound->mPos = Ogre::Vector3(pos.pos[0], pos.pos[1], pos.pos[2]); sound->mVolume = volume; sound->mBaseVolume = basevol; sound->mMinDistance = min; @@ -330,7 +332,10 @@ namespace MWSound while(snditer != mActiveSounds.end()) { if(snditer->second.first == ptr) + { snditer->first->update(pos.pos); + snditer->first->mPos = Ogre::Vector3(pos.pos[0], pos.pos[1], pos.pos[2]); + } snditer++; } } From 479df78ea185bd56b8fe4f214beb1c1a8bc034cd Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 30 Mar 2012 07:01:37 -0700 Subject: [PATCH 04/18] Update the actual sound position after the listener --- apps/openmw/mwsound/openal_output.cpp | 12 ++++++------ apps/openmw/mwsound/sound.hpp | 2 +- apps/openmw/mwsound/soundmanager.cpp | 9 +++++---- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp index 13ea49481..ad0f7603b 100644 --- a/apps/openmw/mwsound/openal_output.cpp +++ b/apps/openmw/mwsound/openal_output.cpp @@ -90,7 +90,7 @@ public: virtual void stop(); virtual bool isPlaying(); virtual void setVolume(float volume); - virtual void update(const float *pos); + virtual void update(); void play(); bool process(); @@ -262,9 +262,9 @@ void OpenAL_SoundStream::setVolume(float volume) mVolume = volume; } -void OpenAL_SoundStream::update(const float *pos) +void OpenAL_SoundStream::update() { - alSource3f(mSource, AL_POSITION, pos[0], pos[2], -pos[1]); + alSource3f(mSource, AL_POSITION, mPos[0], mPos[2], -mPos[1]); alSource3f(mSource, AL_DIRECTION, 0.0f, 0.0f, 0.0f); alSource3f(mSource, AL_VELOCITY, 0.0f, 0.0f, 0.0f); throwALerror(); @@ -340,7 +340,7 @@ public: virtual void stop(); virtual bool isPlaying(); virtual void setVolume(float volume); - virtual void update(const float *pos); + virtual void update(); }; OpenAL_Sound::OpenAL_Sound(OpenAL_Output &output, ALuint src, ALuint buf) @@ -379,9 +379,9 @@ void OpenAL_Sound::setVolume(float volume) mVolume = volume; } -void OpenAL_Sound::update(const float *pos) +void OpenAL_Sound::update() { - alSource3f(mSource, AL_POSITION, pos[0], pos[2], -pos[1]); + alSource3f(mSource, AL_POSITION, mPos[0], mPos[2], -mPos[1]); alSource3f(mSource, AL_DIRECTION, 0.0f, 0.0f, 0.0f); alSource3f(mSource, AL_VELOCITY, 0.0f, 0.0f, 0.0f); throwALerror(); diff --git a/apps/openmw/mwsound/sound.hpp b/apps/openmw/mwsound/sound.hpp index f1b09875c..521d8dbca 100644 --- a/apps/openmw/mwsound/sound.hpp +++ b/apps/openmw/mwsound/sound.hpp @@ -7,7 +7,7 @@ namespace MWSound { class Sound { - virtual void update(const float *pos) = 0; + virtual void update() = 0; Sound& operator=(const Sound &rhs); Sound(const Sound &rhs); diff --git a/apps/openmw/mwsound/soundmanager.cpp b/apps/openmw/mwsound/soundmanager.cpp index d2e52043a..da7cb9d3b 100644 --- a/apps/openmw/mwsound/soundmanager.cpp +++ b/apps/openmw/mwsound/soundmanager.cpp @@ -328,14 +328,12 @@ namespace MWSound void SoundManager::updateObject(MWWorld::Ptr ptr) { const ESM::Position &pos = ptr.getCellRef().pos; + const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]); SoundMap::iterator snditer = mActiveSounds.begin(); while(snditer != mActiveSounds.end()) { if(snditer->second.first == ptr) - { - snditer->first->update(pos.pos); - snditer->first->mPos = Ogre::Vector3(pos.pos[0], pos.pos[1], pos.pos[2]); - } + snditer->first->mPos = objpos; snditer++; } } @@ -428,7 +426,10 @@ namespace MWSound if(!snditer->first->isPlaying()) mActiveSounds.erase(snditer++); else + { + snditer->first->update(); snditer++; + } } } From fad27d99e66c6892f6e09d03691246ce833b3346 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 30 Mar 2012 07:10:34 -0700 Subject: [PATCH 05/18] Update the actual sound volume with the position --- apps/openmw/mwsound/openal_output.cpp | 18 ++---------------- apps/openmw/mwsound/sound.hpp | 2 +- 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp index ad0f7603b..d8945cf52 100644 --- a/apps/openmw/mwsound/openal_output.cpp +++ b/apps/openmw/mwsound/openal_output.cpp @@ -89,7 +89,6 @@ public: virtual void stop(); virtual bool isPlaying(); - virtual void setVolume(float volume); virtual void update(); void play(); @@ -255,15 +254,9 @@ bool OpenAL_SoundStream::isPlaying() return !mIsFinished; } -void OpenAL_SoundStream::setVolume(float volume) -{ - alSourcef(mSource, AL_GAIN, volume*mBaseVolume); - throwALerror(); - mVolume = volume; -} - void OpenAL_SoundStream::update() { + alSourcef(mSource, AL_GAIN, mVolume*mBaseVolume); alSource3f(mSource, AL_POSITION, mPos[0], mPos[2], -mPos[1]); alSource3f(mSource, AL_DIRECTION, 0.0f, 0.0f, 0.0f); alSource3f(mSource, AL_VELOCITY, 0.0f, 0.0f, 0.0f); @@ -339,7 +332,6 @@ public: virtual void stop(); virtual bool isPlaying(); - virtual void setVolume(float volume); virtual void update(); }; @@ -372,15 +364,9 @@ bool OpenAL_Sound::isPlaying() return state==AL_PLAYING; } -void OpenAL_Sound::setVolume(float volume) -{ - alSourcef(mSource, AL_GAIN, volume*mBaseVolume); - throwALerror(); - mVolume = volume; -} - void OpenAL_Sound::update() { + alSourcef(mSource, AL_GAIN, mVolume*mBaseVolume); alSource3f(mSource, AL_POSITION, mPos[0], mPos[2], -mPos[1]); alSource3f(mSource, AL_DIRECTION, 0.0f, 0.0f, 0.0f); alSource3f(mSource, AL_VELOCITY, 0.0f, 0.0f, 0.0f); diff --git a/apps/openmw/mwsound/sound.hpp b/apps/openmw/mwsound/sound.hpp index 521d8dbca..d2c2d5209 100644 --- a/apps/openmw/mwsound/sound.hpp +++ b/apps/openmw/mwsound/sound.hpp @@ -22,7 +22,7 @@ namespace MWSound public: virtual void stop() = 0; virtual bool isPlaying() = 0; - virtual void setVolume(float volume) = 0; + void setVolume(float volume) { mVolume = volume; } Sound() : mPos(0.0f, 0.0f, 0.0f) , mVolume(1.0f) From 1ee8b963d06a17f58f4d28fac02ac3fefcab79bb Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 30 Mar 2012 07:30:17 -0700 Subject: [PATCH 06/18] Store the sound listener position as well --- apps/openmw/mwsound/openal_output.cpp | 3 ++- apps/openmw/mwsound/sound_output.hpp | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp index d8945cf52..8d574b2f2 100644 --- a/apps/openmw/mwsound/openal_output.cpp +++ b/apps/openmw/mwsound/openal_output.cpp @@ -760,8 +760,9 @@ void OpenAL_Output::updateListener(const float *pos, const float *atdir, const f atdir[0], atdir[2], -atdir[1], updir[0], updir[2], -updir[1] }; + mPos = Ogre::Vector3(pos[0], pos[1], pos[2]); - alListener3f(AL_POSITION, pos[0], pos[2], -pos[1]); + alListener3f(AL_POSITION, mPos[0], mPos[2], -mPos[1]); alListenerfv(AL_ORIENTATION, orient); throwALerror(); } diff --git a/apps/openmw/mwsound/sound_output.hpp b/apps/openmw/mwsound/sound_output.hpp index 794383591..3c1a3d312 100644 --- a/apps/openmw/mwsound/sound_output.hpp +++ b/apps/openmw/mwsound/sound_output.hpp @@ -4,6 +4,8 @@ #include #include +#include + #include "soundmanager.hpp" #include "../mwworld/ptr.hpp" @@ -34,7 +36,13 @@ namespace MWSound Sound_Output& operator=(const Sound_Output &rhs); Sound_Output(const Sound_Output &rhs); - Sound_Output(SoundManager &mgr) : mManager(mgr) { } + protected: + Ogre::Vector3 mPos; + + Sound_Output(SoundManager &mgr) + : mManager(mgr) + , mPos(0.0f, 0.0f, 0.0f) + { } public: virtual ~Sound_Output() { } From fefc8f86ab05f48cd5253a4939d96e9354c3fbd3 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 30 Mar 2012 11:11:07 -0700 Subject: [PATCH 07/18] Remove the unused streamSound3D method --- apps/openmw/mwsound/openal_output.cpp | 44 --------------------------- apps/openmw/mwsound/openal_output.hpp | 3 -- apps/openmw/mwsound/sound_output.hpp | 2 -- 3 files changed, 49 deletions(-) diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp index 8d574b2f2..da1de3d14 100644 --- a/apps/openmw/mwsound/openal_output.cpp +++ b/apps/openmw/mwsound/openal_output.cpp @@ -709,50 +709,6 @@ SoundPtr OpenAL_Output::streamSound(const std::string &fname, float volume, floa return sound; } -SoundPtr OpenAL_Output::streamSound3D(const std::string &fname, const float *pos, float volume, float pitch, - float min, float max) -{ - throwALerror(); - - boost::shared_ptr sound; - ALuint src; - - if(mFreeSources.empty()) - fail("No free sources"); - src = mFreeSources.front(); - mFreeSources.pop_front(); - - try - { - DecoderPtr decoder = mManager.getDecoder(); - decoder->open(fname); - sound.reset(new OpenAL_SoundStream(*this, src, decoder)); - } - catch(std::exception &e) - { - mFreeSources.push_back(src); - throw; - } - - alSource3f(src, AL_POSITION, pos[0], pos[2], -pos[1]); - alSource3f(src, AL_DIRECTION, 0.0f, 0.0f, 0.0f); - alSource3f(src, AL_VELOCITY, 0.0f, 0.0f, 0.0f); - - alSourcef(src, AL_REFERENCE_DISTANCE, min); - alSourcef(src, AL_MAX_DISTANCE, max); - alSourcef(src, AL_ROLLOFF_FACTOR, 1.0f); - - alSourcef(src, AL_GAIN, volume); - alSourcef(src, AL_PITCH, pitch); - - alSourcei(src, AL_SOURCE_RELATIVE, AL_FALSE); - alSourcei(src, AL_LOOPING, AL_FALSE); - throwALerror(); - - sound->play(); - return sound; -} - void OpenAL_Output::updateListener(const float *pos, const float *atdir, const float *updir) { diff --git a/apps/openmw/mwsound/openal_output.hpp b/apps/openmw/mwsound/openal_output.hpp index d288a62f3..f37f72cab 100644 --- a/apps/openmw/mwsound/openal_output.hpp +++ b/apps/openmw/mwsound/openal_output.hpp @@ -43,10 +43,7 @@ namespace MWSound virtual SoundPtr playSound(const std::string &fname, float volume, float pitch, bool loop); virtual SoundPtr playSound3D(const std::string &fname, const float *pos, float volume, float pitch, float min, float max, bool loop); - virtual SoundPtr streamSound(const std::string &fname, float volume, float pitch); - virtual SoundPtr streamSound3D(const std::string &fname, const float *pos, float volume, float pitch, - float min, float max); virtual void updateListener(const float *pos, const float *atdir, const float *updir); diff --git a/apps/openmw/mwsound/sound_output.hpp b/apps/openmw/mwsound/sound_output.hpp index 3c1a3d312..e54f9016d 100644 --- a/apps/openmw/mwsound/sound_output.hpp +++ b/apps/openmw/mwsound/sound_output.hpp @@ -28,8 +28,6 @@ namespace MWSound virtual SoundPtr playSound3D(const std::string &fname, const float *pos, float volume, float pitch, float min, float max, bool loop) = 0; virtual SoundPtr streamSound(const std::string &fname, float volume, float pitch) = 0; - virtual SoundPtr streamSound3D(const std::string &fname, const float *pos, float volume, float pitch, - float min, float max) = 0; virtual void updateListener(const float *pos, const float *atdir, const float *updir) = 0; From fc167dbc83b6312433d1469ec8222f22ac47e10b Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Fri, 30 Mar 2012 11:42:11 -0700 Subject: [PATCH 08/18] Pass Ogre Vector3s to playSound3D and updateListener --- apps/openmw/mwsound/openal_output.cpp | 16 ++++++++-------- apps/openmw/mwsound/openal_output.hpp | 6 +++--- apps/openmw/mwsound/sound_output.hpp | 6 +++--- apps/openmw/mwsound/soundmanager.cpp | 16 +++++++++------- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp index da1de3d14..9fda1470c 100644 --- a/apps/openmw/mwsound/openal_output.cpp +++ b/apps/openmw/mwsound/openal_output.cpp @@ -616,7 +616,7 @@ SoundPtr OpenAL_Output::playSound(const std::string &fname, float volume, float return sound; } -SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const float *pos, float volume, float pitch, +SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const Ogre::Vector3 &pos, float volume, float pitch, float min, float max, bool loop) { throwALerror(); @@ -643,7 +643,7 @@ SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const float *pos, throw; } - alSource3f(src, AL_POSITION, pos[0], pos[2], -pos[1]); + alSource3f(src, AL_POSITION, pos.x, pos.z, -pos.y); alSource3f(src, AL_DIRECTION, 0.0f, 0.0f, 0.0f); alSource3f(src, AL_VELOCITY, 0.0f, 0.0f, 0.0f); @@ -710,15 +710,15 @@ SoundPtr OpenAL_Output::streamSound(const std::string &fname, float volume, floa } -void OpenAL_Output::updateListener(const float *pos, const float *atdir, const float *updir) +void OpenAL_Output::updateListener(const Ogre::Vector3 &pos, const Ogre::Vector3 &atdir, const Ogre::Vector3 &updir) { - float orient[6] = { - atdir[0], atdir[2], -atdir[1], - updir[0], updir[2], -updir[1] + ALfloat orient[6] = { + atdir.x, atdir.z, -atdir.y, + updir.x, updir.z, -updir.y }; - mPos = Ogre::Vector3(pos[0], pos[1], pos[2]); + mPos = pos; - alListener3f(AL_POSITION, mPos[0], mPos[2], -mPos[1]); + alListener3f(AL_POSITION, mPos.x, mPos.z, -mPos.y); alListenerfv(AL_ORIENTATION, orient); throwALerror(); } diff --git a/apps/openmw/mwsound/openal_output.hpp b/apps/openmw/mwsound/openal_output.hpp index f37f72cab..f4d4e90af 100644 --- a/apps/openmw/mwsound/openal_output.hpp +++ b/apps/openmw/mwsound/openal_output.hpp @@ -41,11 +41,11 @@ namespace MWSound virtual void deinit(); virtual SoundPtr playSound(const std::string &fname, float volume, float pitch, bool loop); - virtual SoundPtr playSound3D(const std::string &fname, const float *pos, float volume, float pitch, - float min, float max, bool loop); + virtual SoundPtr playSound3D(const std::string &fname, const Ogre::Vector3 &pos, + float volume, float pitch, float min, float max, bool loop); virtual SoundPtr streamSound(const std::string &fname, float volume, float pitch); - virtual void updateListener(const float *pos, const float *atdir, const float *updir); + virtual void updateListener(const Ogre::Vector3 &pos, const Ogre::Vector3 &atdir, const Ogre::Vector3 &updir); OpenAL_Output& operator=(const OpenAL_Output &rhs); OpenAL_Output(const OpenAL_Output &rhs); diff --git a/apps/openmw/mwsound/sound_output.hpp b/apps/openmw/mwsound/sound_output.hpp index e54f9016d..ed3dda2f3 100644 --- a/apps/openmw/mwsound/sound_output.hpp +++ b/apps/openmw/mwsound/sound_output.hpp @@ -25,11 +25,11 @@ namespace MWSound virtual void deinit() = 0; virtual SoundPtr playSound(const std::string &fname, float volume, float pitch, bool loop) = 0; - virtual SoundPtr playSound3D(const std::string &fname, const float *pos, float volume, float pitch, - float min, float max, bool loop) = 0; + virtual SoundPtr playSound3D(const std::string &fname, const Ogre::Vector3 &pos, + float volume, float pitch, float min, float max, bool loop) = 0; virtual SoundPtr streamSound(const std::string &fname, float volume, float pitch) = 0; - virtual void updateListener(const float *pos, const float *atdir, const float *updir) = 0; + virtual void updateListener(const Ogre::Vector3 &pos, const Ogre::Vector3 &atdir, const Ogre::Vector3 &updir) = 0; Sound_Output& operator=(const Sound_Output &rhs); Sound_Output(const Sound_Output &rhs); diff --git a/apps/openmw/mwsound/soundmanager.cpp b/apps/openmw/mwsound/soundmanager.cpp index da7cb9d3b..005df3e7e 100644 --- a/apps/openmw/mwsound/soundmanager.cpp +++ b/apps/openmw/mwsound/soundmanager.cpp @@ -184,10 +184,11 @@ namespace MWSound float basevol = 1.0f; /* TODO: volume settings */ std::string filePath = "Sound/"+filename; const ESM::Position &pos = ptr.getCellRef().pos; + const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]); - SoundPtr sound = mOutput->playSound3D(filePath, pos.pos, basevol, 1.0f, + SoundPtr sound = mOutput->playSound3D(filePath, objpos, basevol, 1.0f, 20.0f, 12750.0f, false); - sound->mPos = Ogre::Vector3(pos.pos[0], pos.pos[1], pos.pos[2]); + sound->mPos = objpos; sound->mBaseVolume = basevol; mActiveSounds[sound] = std::make_pair(ptr, std::string("_say_sound")); @@ -240,9 +241,10 @@ namespace MWSound float min, max; std::string file = lookup(soundId, basevol, min, max); const ESM::Position &pos = ptr.getCellRef().pos; + const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]); - sound = mOutput->playSound3D(file, pos.pos, volume*basevol, pitch, min, max, loop); - sound->mPos = Ogre::Vector3(pos.pos[0], pos.pos[1], pos.pos[2]); + sound = mOutput->playSound3D(file, objpos, volume*basevol, pitch, min, max, loop); + sound->mPos = objpos; sound->mVolume = volume; sound->mBaseVolume = basevol; sound->mMinDistance = min; @@ -414,9 +416,9 @@ namespace MWSound // The output handler is expecting vectors oriented like the game // (that is, -Z goes down, +Y goes forward), but that's not what we // get from Ogre's camera, so we have to convert. - float pos[3] = { nPos[0], -nPos[2], nPos[1] }; - float at[3] = { nDir[0], -nDir[2], nDir[1] }; - float up[3] = { nUp[0], -nUp[2], nUp[1] }; + const Ogre::Vector3 pos(nPos[0], -nPos[2], nPos[1]); + const Ogre::Vector3 at(nDir[0], -nDir[2], nDir[1]); + const Ogre::Vector3 up(nUp[0], -nUp[2], nUp[1]); mOutput->updateListener(pos, at, up); // Check if any sounds are finished playing, and trash them From 5adeee20fd893f1c7b04206365b6b06a8bacaa9c Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 31 Mar 2012 00:57:29 -0700 Subject: [PATCH 09/18] Mute sounds that go outside the max distance --- apps/openmw/mwsound/openal_output.cpp | 34 +++++++++++++++++---------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp index 9fda1470c..1b90c62fa 100644 --- a/apps/openmw/mwsound/openal_output.cpp +++ b/apps/openmw/mwsound/openal_output.cpp @@ -77,6 +77,7 @@ class OpenAL_SoundStream : public Sound ALuint mBufferSize; DecoderPtr mDecoder; + bool mIs3D; volatile bool mIsFinished; @@ -84,7 +85,7 @@ class OpenAL_SoundStream : public Sound OpenAL_SoundStream& operator=(const OpenAL_SoundStream &rhs); public: - OpenAL_SoundStream(OpenAL_Output &output, ALuint src, DecoderPtr decoder); + OpenAL_SoundStream(OpenAL_Output &output, ALuint src, DecoderPtr decoder, bool is3D); virtual ~OpenAL_SoundStream(); virtual void stop(); @@ -164,8 +165,8 @@ private: }; -OpenAL_SoundStream::OpenAL_SoundStream(OpenAL_Output &output, ALuint src, DecoderPtr decoder) - : mOutput(output), mSource(src), mDecoder(decoder), mIsFinished(true) +OpenAL_SoundStream::OpenAL_SoundStream(OpenAL_Output &output, ALuint src, DecoderPtr decoder, bool is3D) + : mOutput(output), mSource(src), mDecoder(decoder), mIs3D(is3D), mIsFinished(true) { throwALerror(); @@ -256,7 +257,10 @@ bool OpenAL_SoundStream::isPlaying() void OpenAL_SoundStream::update() { - alSourcef(mSource, AL_GAIN, mVolume*mBaseVolume); + if(mIs3D && mPos.squaredDistance(mOutput.mPos) > mMaxDistance*mMaxDistance) + alSourcef(mSource, AL_GAIN, 0.0f); + else + alSourcef(mSource, AL_GAIN, mVolume*mBaseVolume); alSource3f(mSource, AL_POSITION, mPos[0], mPos[2], -mPos[1]); alSource3f(mSource, AL_DIRECTION, 0.0f, 0.0f, 0.0f); alSource3f(mSource, AL_VELOCITY, 0.0f, 0.0f, 0.0f); @@ -323,11 +327,13 @@ class OpenAL_Sound : public Sound ALuint mSource; ALuint mBuffer; + bool mIs3D; + OpenAL_Sound(const OpenAL_Sound &rhs); OpenAL_Sound& operator=(const OpenAL_Sound &rhs); public: - OpenAL_Sound(OpenAL_Output &output, ALuint src, ALuint buf); + OpenAL_Sound(OpenAL_Output &output, ALuint src, ALuint buf, bool is3D); virtual ~OpenAL_Sound(); virtual void stop(); @@ -335,8 +341,8 @@ public: virtual void update(); }; -OpenAL_Sound::OpenAL_Sound(OpenAL_Output &output, ALuint src, ALuint buf) - : mOutput(output), mSource(src), mBuffer(buf) +OpenAL_Sound::OpenAL_Sound(OpenAL_Output &output, ALuint src, ALuint buf, bool is3D) + : mOutput(output), mSource(src), mBuffer(buf), mIs3D(is3D) { } OpenAL_Sound::~OpenAL_Sound() @@ -366,7 +372,10 @@ bool OpenAL_Sound::isPlaying() void OpenAL_Sound::update() { - alSourcef(mSource, AL_GAIN, mVolume*mBaseVolume); + if(mIs3D && mPos.squaredDistance(mOutput.mPos) > mMaxDistance*mMaxDistance) + alSourcef(mSource, AL_GAIN, 0.0f); + else + alSourcef(mSource, AL_GAIN, mVolume*mBaseVolume); alSource3f(mSource, AL_POSITION, mPos[0], mPos[2], -mPos[1]); alSource3f(mSource, AL_DIRECTION, 0.0f, 0.0f, 0.0f); alSource3f(mSource, AL_VELOCITY, 0.0f, 0.0f, 0.0f); @@ -583,7 +592,7 @@ SoundPtr OpenAL_Output::playSound(const std::string &fname, float volume, float try { buf = getBuffer(fname); - sound.reset(new OpenAL_Sound(*this, src, buf)); + sound.reset(new OpenAL_Sound(*this, src, buf, false)); } catch(std::exception &e) { @@ -632,7 +641,7 @@ SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const Ogre::Vector try { buf = getBuffer(fname); - sound.reset(new OpenAL_Sound(*this, src, buf)); + sound.reset(new OpenAL_Sound(*this, src, buf, true)); } catch(std::exception &e) { @@ -651,7 +660,8 @@ SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const Ogre::Vector alSourcef(src, AL_MAX_DISTANCE, max); alSourcef(src, AL_ROLLOFF_FACTOR, 1.0f); - alSourcef(src, AL_GAIN, volume); + alSourcef(src, AL_GAIN, (pos.squaredDistance(mPos) > max*max) ? + 0.0f : volume); alSourcef(src, AL_PITCH, pitch); alSourcei(src, AL_SOURCE_RELATIVE, AL_FALSE); @@ -682,7 +692,7 @@ SoundPtr OpenAL_Output::streamSound(const std::string &fname, float volume, floa { DecoderPtr decoder = mManager.getDecoder(); decoder->open(fname); - sound.reset(new OpenAL_SoundStream(*this, src, decoder)); + sound.reset(new OpenAL_SoundStream(*this, src, decoder, false)); } catch(std::exception &e) { From b01289128bb50e6216799cbb0e6427bb0df369d9 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 31 Mar 2012 01:15:27 -0700 Subject: [PATCH 10/18] Split OpenAL_Sound into separate classes for 2D and 3D sounds --- apps/openmw/mwsound/openal_output.cpp | 57 ++++++++++++++++++--------- apps/openmw/mwsound/openal_output.hpp | 1 + 2 files changed, 40 insertions(+), 18 deletions(-) diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp index 1b90c62fa..3ed86cf2b 100644 --- a/apps/openmw/mwsound/openal_output.cpp +++ b/apps/openmw/mwsound/openal_output.cpp @@ -77,7 +77,6 @@ class OpenAL_SoundStream : public Sound ALuint mBufferSize; DecoderPtr mDecoder; - bool mIs3D; volatile bool mIsFinished; @@ -85,7 +84,7 @@ class OpenAL_SoundStream : public Sound OpenAL_SoundStream& operator=(const OpenAL_SoundStream &rhs); public: - OpenAL_SoundStream(OpenAL_Output &output, ALuint src, DecoderPtr decoder, bool is3D); + OpenAL_SoundStream(OpenAL_Output &output, ALuint src, DecoderPtr decoder); virtual ~OpenAL_SoundStream(); virtual void stop(); @@ -165,8 +164,8 @@ private: }; -OpenAL_SoundStream::OpenAL_SoundStream(OpenAL_Output &output, ALuint src, DecoderPtr decoder, bool is3D) - : mOutput(output), mSource(src), mDecoder(decoder), mIs3D(is3D), mIsFinished(true) +OpenAL_SoundStream::OpenAL_SoundStream(OpenAL_Output &output, ALuint src, DecoderPtr decoder) + : mOutput(output), mSource(src), mDecoder(decoder), mIsFinished(true) { throwALerror(); @@ -257,10 +256,7 @@ bool OpenAL_SoundStream::isPlaying() void OpenAL_SoundStream::update() { - if(mIs3D && mPos.squaredDistance(mOutput.mPos) > mMaxDistance*mMaxDistance) - alSourcef(mSource, AL_GAIN, 0.0f); - else - alSourcef(mSource, AL_GAIN, mVolume*mBaseVolume); + alSourcef(mSource, AL_GAIN, mVolume*mBaseVolume); alSource3f(mSource, AL_POSITION, mPos[0], mPos[2], -mPos[1]); alSource3f(mSource, AL_DIRECTION, 0.0f, 0.0f, 0.0f); alSource3f(mSource, AL_VELOCITY, 0.0f, 0.0f, 0.0f); @@ -318,22 +314,22 @@ bool OpenAL_SoundStream::process() } // -// A regular OpenAL sound +// A regular 2D OpenAL sound // class OpenAL_Sound : public Sound { +protected: OpenAL_Output &mOutput; ALuint mSource; ALuint mBuffer; - bool mIs3D; - +private: OpenAL_Sound(const OpenAL_Sound &rhs); OpenAL_Sound& operator=(const OpenAL_Sound &rhs); public: - OpenAL_Sound(OpenAL_Output &output, ALuint src, ALuint buf, bool is3D); + OpenAL_Sound(OpenAL_Output &output, ALuint src, ALuint buf); virtual ~OpenAL_Sound(); virtual void stop(); @@ -341,8 +337,24 @@ public: virtual void update(); }; -OpenAL_Sound::OpenAL_Sound(OpenAL_Output &output, ALuint src, ALuint buf, bool is3D) - : mOutput(output), mSource(src), mBuffer(buf), mIs3D(is3D) +// +// A regular 3D OpenAL sound +// +class OpenAL_Sound3D : public OpenAL_Sound +{ + OpenAL_Sound3D(const OpenAL_Sound &rhs); + OpenAL_Sound3D& operator=(const OpenAL_Sound &rhs); + +public: + OpenAL_Sound3D(OpenAL_Output &output, ALuint src, ALuint buf) + : OpenAL_Sound(output, src, buf) + { } + + virtual void update(); +}; + +OpenAL_Sound::OpenAL_Sound(OpenAL_Output &output, ALuint src, ALuint buf) + : mOutput(output), mSource(src), mBuffer(buf) { } OpenAL_Sound::~OpenAL_Sound() @@ -372,7 +384,16 @@ bool OpenAL_Sound::isPlaying() void OpenAL_Sound::update() { - if(mIs3D && mPos.squaredDistance(mOutput.mPos) > mMaxDistance*mMaxDistance) + alSourcef(mSource, AL_GAIN, mVolume*mBaseVolume); + alSource3f(mSource, AL_POSITION, mPos[0], mPos[2], -mPos[1]); + alSource3f(mSource, AL_DIRECTION, 0.0f, 0.0f, 0.0f); + alSource3f(mSource, AL_VELOCITY, 0.0f, 0.0f, 0.0f); + throwALerror(); +} + +void OpenAL_Sound3D::update() +{ + if(mPos.squaredDistance(mOutput.mPos) > mMaxDistance*mMaxDistance) alSourcef(mSource, AL_GAIN, 0.0f); else alSourcef(mSource, AL_GAIN, mVolume*mBaseVolume); @@ -592,7 +613,7 @@ SoundPtr OpenAL_Output::playSound(const std::string &fname, float volume, float try { buf = getBuffer(fname); - sound.reset(new OpenAL_Sound(*this, src, buf, false)); + sound.reset(new OpenAL_Sound(*this, src, buf)); } catch(std::exception &e) { @@ -641,7 +662,7 @@ SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const Ogre::Vector try { buf = getBuffer(fname); - sound.reset(new OpenAL_Sound(*this, src, buf, true)); + sound.reset(new OpenAL_Sound3D(*this, src, buf)); } catch(std::exception &e) { @@ -692,7 +713,7 @@ SoundPtr OpenAL_Output::streamSound(const std::string &fname, float volume, floa { DecoderPtr decoder = mManager.getDecoder(); decoder->open(fname); - sound.reset(new OpenAL_SoundStream(*this, src, decoder, false)); + sound.reset(new OpenAL_SoundStream(*this, src, decoder)); } catch(std::exception &e) { diff --git a/apps/openmw/mwsound/openal_output.hpp b/apps/openmw/mwsound/openal_output.hpp index f4d4e90af..35966cc29 100644 --- a/apps/openmw/mwsound/openal_output.hpp +++ b/apps/openmw/mwsound/openal_output.hpp @@ -57,6 +57,7 @@ namespace MWSound std::auto_ptr mStreamThread; friend class OpenAL_Sound; + friend class OpenAL_Sound3D; friend class OpenAL_SoundStream; friend class SoundManager; }; From 4e908aa0954801549c5d0f8c30bad057e65c5c9e Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 31 Mar 2012 01:34:26 -0700 Subject: [PATCH 11/18] Add a method to set the sound's position --- apps/openmw/mwsound/sound.hpp | 1 + apps/openmw/mwsound/soundmanager.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/openmw/mwsound/sound.hpp b/apps/openmw/mwsound/sound.hpp index d2c2d5209..e23830cdb 100644 --- a/apps/openmw/mwsound/sound.hpp +++ b/apps/openmw/mwsound/sound.hpp @@ -22,6 +22,7 @@ namespace MWSound public: virtual void stop() = 0; virtual bool isPlaying() = 0; + void setPosition(const Ogre::Vector3 &pos) { mPos = pos; } void setVolume(float volume) { mVolume = volume; } Sound() : mPos(0.0f, 0.0f, 0.0f) diff --git a/apps/openmw/mwsound/soundmanager.cpp b/apps/openmw/mwsound/soundmanager.cpp index 005df3e7e..9c26f0084 100644 --- a/apps/openmw/mwsound/soundmanager.cpp +++ b/apps/openmw/mwsound/soundmanager.cpp @@ -335,7 +335,7 @@ namespace MWSound while(snditer != mActiveSounds.end()) { if(snditer->second.first == ptr) - snditer->first->mPos = objpos; + snditer->first->setPosition(objpos); snditer++; } } From 28378c063b0b7601e2846d33963834473f35dad3 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 31 Mar 2012 02:22:23 -0700 Subject: [PATCH 12/18] Minor OpenAL_SoundStream cleanups --- apps/openmw/mwsound/openal_output.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp index 3ed86cf2b..c40b340c5 100644 --- a/apps/openmw/mwsound/openal_output.cpp +++ b/apps/openmw/mwsound/openal_output.cpp @@ -65,7 +65,7 @@ static ALenum getALFormat(ChannelConfig chans, SampleType type) class OpenAL_SoundStream : public Sound { static const ALuint sNumBuffers = 6; - static const ALfloat sBufferLength; + static const ALfloat sBufferLength = 0.125f; OpenAL_Output &mOutput; @@ -95,7 +95,6 @@ public: bool process(); }; -const ALfloat OpenAL_SoundStream::sBufferLength = 0.125f; // // A background streaming thread (keeps active streams processed) @@ -186,7 +185,6 @@ OpenAL_SoundStream::OpenAL_SoundStream(OpenAL_Output &output, ALuint src, Decode } catch(std::exception &e) { - mOutput.mFreeSources.push_back(mSource); alDeleteBuffers(sNumBuffers, mBuffers); alGetError(); throw; From 7541e08909ee3be2655831bdf0a680ea45112a82 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 31 Mar 2012 02:48:14 -0700 Subject: [PATCH 13/18] Don't reset the sound Output device if init fails --- apps/openmw/mwsound/soundmanager.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/openmw/mwsound/soundmanager.cpp b/apps/openmw/mwsound/soundmanager.cpp index 9c26f0084..d537a2bbd 100644 --- a/apps/openmw/mwsound/soundmanager.cpp +++ b/apps/openmw/mwsound/soundmanager.cpp @@ -62,7 +62,6 @@ namespace MWSound catch(std::exception &e) { std::cout <<"Sound init failed: "< Date: Sat, 31 Mar 2012 03:31:41 -0700 Subject: [PATCH 14/18] Finally "fix" --nosound Expect degraded performance with it. Looping sounds are constantly checked to see if they're playing, and try to play it again when it's not. --- apps/openmw/mwsound/openal_output.cpp | 41 ++++++++++++++++----------- apps/openmw/mwsound/soundmanager.cpp | 5 ++-- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp index c40b340c5..6a4e3fe57 100644 --- a/apps/openmw/mwsound/openal_output.cpp +++ b/apps/openmw/mwsound/openal_output.cpp @@ -25,14 +25,20 @@ static void throwALCerror(ALCdevice *device) { ALCenum err = alcGetError(device); if(err != ALC_NO_ERROR) - fail(alcGetString(device, err)); + { + const ALCchar *errstring = alcGetString(device, err); + fail(errstring ? errstring : ""); + } } static void throwALerror() { ALenum err = alGetError(); if(err != AL_NO_ERROR) - fail(alGetString(err)); + { + const ALchar *errstring = alGetString(err); + fail(errstring ? errstring : ""); + } } @@ -424,8 +430,7 @@ std::vector OpenAL_Output::enumerate() void OpenAL_Output::init(const std::string &devname) { - if(mDevice || mContext) - fail("Device already open"); + deinit(); mDevice = alcOpenDevice(devname.c_str()); if(!mDevice) @@ -442,7 +447,12 @@ void OpenAL_Output::init(const std::string &devname) mContext = alcCreateContext(mDevice, NULL); if(!mContext || alcMakeContextCurrent(mContext) == ALC_FALSE) + { + if(mContext) + alcDestroyContext(mContext); + mContext = 0; fail(std::string("Failed to setup context: ")+alcGetString(mDevice, alcGetError(mDevice))); + } alDistanceModel(AL_LINEAR_DISTANCE_CLAMPED); throwALerror(); @@ -598,8 +608,6 @@ void OpenAL_Output::bufferFinished(ALuint buf) SoundPtr OpenAL_Output::playSound(const std::string &fname, float volume, float pitch, bool loop) { - throwALerror(); - boost::shared_ptr sound; ALuint src=0, buf=0; @@ -647,8 +655,6 @@ SoundPtr OpenAL_Output::playSound(const std::string &fname, float volume, float SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const Ogre::Vector3 &pos, float volume, float pitch, float min, float max, bool loop) { - throwALerror(); - boost::shared_ptr sound; ALuint src=0, buf=0; @@ -697,8 +703,6 @@ SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const Ogre::Vector SoundPtr OpenAL_Output::streamSound(const std::string &fname, float volume, float pitch) { - throwALerror(); - boost::shared_ptr sound; ALuint src; @@ -741,15 +745,18 @@ SoundPtr OpenAL_Output::streamSound(const std::string &fname, float volume, floa void OpenAL_Output::updateListener(const Ogre::Vector3 &pos, const Ogre::Vector3 &atdir, const Ogre::Vector3 &updir) { - ALfloat orient[6] = { - atdir.x, atdir.z, -atdir.y, - updir.x, updir.z, -updir.y - }; mPos = pos; - alListener3f(AL_POSITION, mPos.x, mPos.z, -mPos.y); - alListenerfv(AL_ORIENTATION, orient); - throwALerror(); + if(mContext) + { + ALfloat orient[6] = { + atdir.x, atdir.z, -atdir.y, + updir.x, updir.z, -updir.y + }; + alListener3f(AL_POSITION, mPos.x, mPos.z, -mPos.y); + alListenerfv(AL_ORIENTATION, orient); + throwALerror(); + } } diff --git a/apps/openmw/mwsound/soundmanager.cpp b/apps/openmw/mwsound/soundmanager.cpp index d537a2bbd..534b5b6de 100644 --- a/apps/openmw/mwsound/soundmanager.cpp +++ b/apps/openmw/mwsound/soundmanager.cpp @@ -41,6 +41,8 @@ namespace MWSound SoundManager::SoundManager(bool useSound, MWWorld::Environment& environment) : mResourceMgr(Ogre::ResourceGroupManager::getSingleton()) , mEnvironment(environment) + , mOutput(new DEFAULT_OUTPUT(*this)) + { if(!useSound) return; @@ -50,8 +52,6 @@ namespace MWSound try { - mOutput.reset(new DEFAULT_OUTPUT(*this)); - std::vector names = mOutput->enumerate(); std::cout <<"Enumerated output devices:"<< std::endl; for(size_t i = 0;i < names.size();i++) @@ -62,7 +62,6 @@ namespace MWSound catch(std::exception &e) { std::cout <<"Sound init failed: "< Date: Sat, 31 Mar 2012 05:57:03 -0700 Subject: [PATCH 15/18] Only allow one instance of a given tracked soundid The untracked flag should probably be broken up and combined with the loop boolean into a set of flags. --- apps/openmw/mwsound/soundmanager.cpp | 42 +++++++++++++++++++++++++--- apps/openmw/mwsound/soundmanager.hpp | 3 ++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/apps/openmw/mwsound/soundmanager.cpp b/apps/openmw/mwsound/soundmanager.cpp index 534b5b6de..9a71ef421 100644 --- a/apps/openmw/mwsound/soundmanager.cpp +++ b/apps/openmw/mwsound/soundmanager.cpp @@ -67,6 +67,7 @@ namespace MWSound SoundManager::~SoundManager() { + mSingleSounds.clear(); mActiveSounds.clear(); mMusic.reset(); mOutput.reset(); @@ -231,15 +232,29 @@ namespace MWSound float volume, float pitch, bool loop, bool untracked) { + const ESM::Position &pos = ptr.getCellRef().pos; + const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]); SoundPtr sound; + + if(!untracked) + { + IDSoundMap::iterator inviter = mSingleSounds.find(soundId); + if(inviter != mSingleSounds.end()) + { + if(inviter->second->mPos.squaredDistance(mOutput->mPos) < + objpos.squaredDistance(mOutput->mPos)) + return sound; + inviter->second->stop(); + mSingleSounds.erase(inviter); + } + } + try { // Look up the sound in the ESM data float basevol = 1.0f; /* TODO: volume settings */ float min, max; std::string file = lookup(soundId, basevol, min, max); - const ESM::Position &pos = ptr.getCellRef().pos; - const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]); sound = mOutput->playSound3D(file, objpos, volume*basevol, pitch, min, max, loop); sound->mPos = objpos; @@ -248,8 +263,13 @@ namespace MWSound sound->mMinDistance = min; sound->mMaxDistance = max; - mActiveSounds[sound] = (!untracked ? std::make_pair(ptr, soundId) : - std::make_pair(MWWorld::Ptr(), soundId)); + if(untracked) + mActiveSounds[sound] = std::make_pair(MWWorld::Ptr(), soundId); + else + { + mActiveSounds[sound] = std::make_pair(ptr, soundId); + mSingleSounds[soundId] = sound; + } } catch(std::exception &e) { @@ -265,6 +285,9 @@ namespace MWSound { if(snditer->second.first == ptr && snditer->second.second == soundId) { + IDSoundMap::iterator inviter = mSingleSounds.find(snditer->second.second); + if(inviter != mSingleSounds.end() && inviter->second == snditer->first) + mSingleSounds.erase(inviter); snditer->first->stop(); mActiveSounds.erase(snditer++); } @@ -280,6 +303,9 @@ namespace MWSound { if(snditer->second.first == ptr) { + IDSoundMap::iterator inviter = mSingleSounds.find(snditer->second.second); + if(inviter != mSingleSounds.end() && inviter->second == snditer->first) + mSingleSounds.erase(inviter); snditer->first->stop(); mActiveSounds.erase(snditer++); } @@ -296,6 +322,9 @@ namespace MWSound if(snditer->second.first != MWWorld::Ptr() && snditer->second.first.getCell() == cell) { + IDSoundMap::iterator inviter = mSingleSounds.find(snditer->second.second); + if(inviter != mSingleSounds.end() && inviter->second == snditer->first) + mSingleSounds.erase(inviter); snditer->first->stop(); mActiveSounds.erase(snditer++); } @@ -424,7 +453,12 @@ namespace MWSound while(snditer != mActiveSounds.end()) { if(!snditer->first->isPlaying()) + { + IDSoundMap::iterator inviter = mSingleSounds.find(snditer->second.second); + if(inviter != mSingleSounds.end() && inviter->second == snditer->first) + mSingleSounds.erase(inviter); mActiveSounds.erase(snditer++); + } else { snditer->first->update(); diff --git a/apps/openmw/mwsound/soundmanager.hpp b/apps/openmw/mwsound/soundmanager.hpp index 3ab1e881c..3ab2b569e 100644 --- a/apps/openmw/mwsound/soundmanager.hpp +++ b/apps/openmw/mwsound/soundmanager.hpp @@ -45,6 +45,9 @@ namespace MWSound typedef std::map SoundMap; SoundMap mActiveSounds; + typedef std::map IDSoundMap; + IDSoundMap mSingleSounds; + std::string lookup(const std::string &soundId, float &volume, float &min, float &max); void streamMusicFull(const std::string& filename); From ae308b9b5fc4c73859167d4390cb61a2b264bc2f Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 31 Mar 2012 07:31:55 -0700 Subject: [PATCH 16/18] Use a set of flags instead of separate booleans --- apps/openmw/mwclass/apparatus.cpp | 2 +- apps/openmw/mwclass/armor.cpp | 2 +- apps/openmw/mwclass/book.cpp | 2 +- apps/openmw/mwclass/clothing.cpp | 2 +- apps/openmw/mwclass/container.cpp | 4 ++-- apps/openmw/mwclass/door.cpp | 6 +++--- apps/openmw/mwclass/ingredient.cpp | 2 +- apps/openmw/mwclass/light.cpp | 4 ++-- apps/openmw/mwclass/lockpick.cpp | 2 +- apps/openmw/mwclass/misc.cpp | 2 +- apps/openmw/mwclass/potion.cpp | 2 +- apps/openmw/mwclass/probe.cpp | 2 +- apps/openmw/mwclass/repair.cpp | 2 +- apps/openmw/mwclass/weapon.cpp | 2 +- apps/openmw/mwscript/soundextensions.cpp | 4 ++-- apps/openmw/mwsound/soundmanager.cpp | 13 ++++++------- apps/openmw/mwsound/soundmanager.hpp | 21 ++++++++++++++++++--- 17 files changed, 44 insertions(+), 30 deletions(-) diff --git a/apps/openmw/mwclass/apparatus.cpp b/apps/openmw/mwclass/apparatus.cpp index e95fb572f..d27d0bc71 100644 --- a/apps/openmw/mwclass/apparatus.cpp +++ b/apps/openmw/mwclass/apparatus.cpp @@ -56,7 +56,7 @@ namespace MWClass boost::shared_ptr Apparatus::activate (const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const { - environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr), 1.0, 1.0, false, true); + environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr), 1.0, 1.0, MWSound::Play_NoTrack); return boost::shared_ptr ( new MWWorld::ActionTake (ptr)); diff --git a/apps/openmw/mwclass/armor.cpp b/apps/openmw/mwclass/armor.cpp index e1c2734f0..9956a56fb 100644 --- a/apps/openmw/mwclass/armor.cpp +++ b/apps/openmw/mwclass/armor.cpp @@ -60,7 +60,7 @@ namespace MWClass boost::shared_ptr Armor::activate (const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const { - environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); + environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack); return boost::shared_ptr ( new MWWorld::ActionTake (ptr)); diff --git a/apps/openmw/mwclass/book.cpp b/apps/openmw/mwclass/book.cpp index 0a81ebafb..76370dc5c 100644 --- a/apps/openmw/mwclass/book.cpp +++ b/apps/openmw/mwclass/book.cpp @@ -58,7 +58,7 @@ namespace MWClass { // TODO implement reading - environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); + environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack); return boost::shared_ptr ( new MWWorld::ActionTake (ptr)); diff --git a/apps/openmw/mwclass/clothing.cpp b/apps/openmw/mwclass/clothing.cpp index 4fe19ada4..2357851d7 100644 --- a/apps/openmw/mwclass/clothing.cpp +++ b/apps/openmw/mwclass/clothing.cpp @@ -57,7 +57,7 @@ namespace MWClass boost::shared_ptr Clothing::activate (const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const { - environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); + environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack); return boost::shared_ptr ( new MWWorld::ActionTake (ptr)); diff --git a/apps/openmw/mwclass/container.cpp b/apps/openmw/mwclass/container.cpp index c58a25c03..29b3331ba 100644 --- a/apps/openmw/mwclass/container.cpp +++ b/apps/openmw/mwclass/container.cpp @@ -85,7 +85,7 @@ namespace MWClass { // TODO check for key std::cout << "Locked container" << std::endl; - environment.mSoundManager->playSound3D (ptr, lockedSound, 1.0, 1.0, false); + environment.mSoundManager->playSound3D (ptr, lockedSound, 1.0, 1.0); return boost::shared_ptr (new MWWorld::NullAction); } else @@ -100,7 +100,7 @@ namespace MWClass { // Trap activation goes here std::cout << "Activated trap: " << ptr.getCellRef().trap << std::endl; - environment.mSoundManager->playSound3D (ptr, trapActivationSound, 1.0, 1.0, false); + environment.mSoundManager->playSound3D (ptr, trapActivationSound, 1.0, 1.0); ptr.getCellRef().trap = ""; return boost::shared_ptr (new MWWorld::NullAction); } diff --git a/apps/openmw/mwclass/door.cpp b/apps/openmw/mwclass/door.cpp index 5654dff69..9d6c6a78d 100644 --- a/apps/openmw/mwclass/door.cpp +++ b/apps/openmw/mwclass/door.cpp @@ -73,7 +73,7 @@ namespace MWClass // TODO check for key // TODO report failure to player (message, sound?). Look up behaviour of original MW. std::cout << "Locked!" << std::endl; - environment.mSoundManager->playSound3D (ptr, lockedSound, 1.0, 1.0, false); + environment.mSoundManager->playSound3D (ptr, lockedSound, 1.0, 1.0); return boost::shared_ptr (new MWWorld::NullAction); } @@ -81,7 +81,7 @@ namespace MWClass { // Trap activation std::cout << "Activated trap: " << ptr.getCellRef().trap << std::endl; - environment.mSoundManager->playSound3D(ptr, trapActivationSound, 1.0, 1.0, false); + environment.mSoundManager->playSound3D(ptr, trapActivationSound, 1.0, 1.0); ptr.getCellRef().trap = ""; return boost::shared_ptr (new MWWorld::NullAction); } @@ -110,7 +110,7 @@ namespace MWClass // TODO return action for rotating the door // This is a little pointless, but helps with testing - environment.mSoundManager->playSound3D (ptr, openSound, 1.0, 1.0, false); + environment.mSoundManager->playSound3D (ptr, openSound, 1.0, 1.0); return boost::shared_ptr (new MWWorld::NullAction); } } diff --git a/apps/openmw/mwclass/ingredient.cpp b/apps/openmw/mwclass/ingredient.cpp index 1a7edf632..cbe153ba3 100644 --- a/apps/openmw/mwclass/ingredient.cpp +++ b/apps/openmw/mwclass/ingredient.cpp @@ -54,7 +54,7 @@ namespace MWClass boost::shared_ptr Ingredient::activate (const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const { - environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); + environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack); return boost::shared_ptr ( new MWWorld::ActionTake (ptr)); diff --git a/apps/openmw/mwclass/light.cpp b/apps/openmw/mwclass/light.cpp index e2e63a89b..71e477591 100644 --- a/apps/openmw/mwclass/light.cpp +++ b/apps/openmw/mwclass/light.cpp @@ -59,7 +59,7 @@ namespace MWClass if (!ref->base->sound.empty()) { - environment.mSoundManager->playSound3D (ptr, ref->base->sound, 1.0, 1.0, true); + environment.mSoundManager->playSound3D (ptr, ref->base->sound, 1.0, 1.0, MWSound::Play_Loop); } } @@ -83,7 +83,7 @@ namespace MWClass if (!(ref->base->data.flags & ESM::Light::Carry)) return boost::shared_ptr (new MWWorld::NullAction); - environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); + environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack); return boost::shared_ptr ( new MWWorld::ActionTake (ptr)); diff --git a/apps/openmw/mwclass/lockpick.cpp b/apps/openmw/mwclass/lockpick.cpp index 3dda2f4af..1eef0db8b 100644 --- a/apps/openmw/mwclass/lockpick.cpp +++ b/apps/openmw/mwclass/lockpick.cpp @@ -58,7 +58,7 @@ namespace MWClass boost::shared_ptr Lockpick::activate (const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const { - environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); + environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack); return boost::shared_ptr ( new MWWorld::ActionTake (ptr)); diff --git a/apps/openmw/mwclass/misc.cpp b/apps/openmw/mwclass/misc.cpp index 864fc1e38..def1a90a8 100644 --- a/apps/openmw/mwclass/misc.cpp +++ b/apps/openmw/mwclass/misc.cpp @@ -56,7 +56,7 @@ namespace MWClass boost::shared_ptr Miscellaneous::activate (const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const { - environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); + environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack); return boost::shared_ptr ( new MWWorld::ActionTake (ptr)); diff --git a/apps/openmw/mwclass/potion.cpp b/apps/openmw/mwclass/potion.cpp index 4ab374590..ed1733e2d 100644 --- a/apps/openmw/mwclass/potion.cpp +++ b/apps/openmw/mwclass/potion.cpp @@ -56,7 +56,7 @@ namespace MWClass boost::shared_ptr Potion::activate (const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const { - environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); + environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack); return boost::shared_ptr ( new MWWorld::ActionTake (ptr)); diff --git a/apps/openmw/mwclass/probe.cpp b/apps/openmw/mwclass/probe.cpp index 4b4d79a73..8013e2e80 100644 --- a/apps/openmw/mwclass/probe.cpp +++ b/apps/openmw/mwclass/probe.cpp @@ -57,7 +57,7 @@ namespace MWClass boost::shared_ptr Probe::activate (const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const { - environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); + environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack); return boost::shared_ptr ( new MWWorld::ActionTake (ptr)); diff --git a/apps/openmw/mwclass/repair.cpp b/apps/openmw/mwclass/repair.cpp index 758bf4079..d49979861 100644 --- a/apps/openmw/mwclass/repair.cpp +++ b/apps/openmw/mwclass/repair.cpp @@ -56,7 +56,7 @@ namespace MWClass boost::shared_ptr Repair::activate (const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const { - environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); + environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack); return boost::shared_ptr ( new MWWorld::ActionTake (ptr)); diff --git a/apps/openmw/mwclass/weapon.cpp b/apps/openmw/mwclass/weapon.cpp index 20db0cf38..e36e9202f 100644 --- a/apps/openmw/mwclass/weapon.cpp +++ b/apps/openmw/mwclass/weapon.cpp @@ -57,7 +57,7 @@ namespace MWClass boost::shared_ptr Weapon::activate (const MWWorld::Ptr& ptr, const MWWorld::Ptr& actor, const MWWorld::Environment& environment) const { - environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, false, true); + environment.mSoundManager->playSound3D (ptr, getUpSoundId(ptr, environment), 1.0, 1.0, MWSound::Play_NoTrack); return boost::shared_ptr ( new MWWorld::ActionTake (ptr)); diff --git a/apps/openmw/mwscript/soundextensions.cpp b/apps/openmw/mwscript/soundextensions.cpp index d5cc41b76..7ae109075 100644 --- a/apps/openmw/mwscript/soundextensions.cpp +++ b/apps/openmw/mwscript/soundextensions.cpp @@ -130,7 +130,7 @@ namespace MWScript std::string sound = runtime.getStringLiteral (runtime[0].mInteger); runtime.pop(); - context.getSoundManager().playSound3D (ptr, sound, 1.0, 1.0, mLoop); + context.getSoundManager().playSound3D (ptr, sound, 1.0, 1.0, MWSound::Play_Single | (mLoop ? MWSound::Play_Loop : 0)); } }; @@ -159,7 +159,7 @@ namespace MWScript Interpreter::Type_Float pitch = runtime[0].mFloat; runtime.pop(); - context.getSoundManager().playSound3D (ptr, sound, volume, pitch, mLoop); + context.getSoundManager().playSound3D (ptr, sound, volume, pitch, MWSound::Play_Single | (mLoop ? MWSound::Play_Loop : 0)); } }; diff --git a/apps/openmw/mwsound/soundmanager.cpp b/apps/openmw/mwsound/soundmanager.cpp index 9a71ef421..2d743d9cb 100644 --- a/apps/openmw/mwsound/soundmanager.cpp +++ b/apps/openmw/mwsound/soundmanager.cpp @@ -204,7 +204,7 @@ namespace MWSound } - SoundPtr SoundManager::playSound(const std::string& soundId, float volume, float pitch, bool loop) + SoundPtr SoundManager::playSound(const std::string& soundId, float volume, float pitch, int mode) { SoundPtr sound; try @@ -213,7 +213,7 @@ namespace MWSound float min, max; std::string file = lookup(soundId, basevol, min, max); - sound = mOutput->playSound(file, volume*basevol, pitch, loop); + sound = mOutput->playSound(file, volume*basevol, pitch, mode&Play_Loop); sound->mVolume = volume; sound->mBaseVolume = basevol; sound->mMinDistance = min; @@ -229,14 +229,13 @@ namespace MWSound } SoundPtr SoundManager::playSound3D(MWWorld::Ptr ptr, const std::string& soundId, - float volume, float pitch, bool loop, - bool untracked) + float volume, float pitch, int mode) { const ESM::Position &pos = ptr.getCellRef().pos; const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]); SoundPtr sound; - if(!untracked) + if((mode&Play_Single)) { IDSoundMap::iterator inviter = mSingleSounds.find(soundId); if(inviter != mSingleSounds.end()) @@ -256,14 +255,14 @@ namespace MWSound float min, max; std::string file = lookup(soundId, basevol, min, max); - sound = mOutput->playSound3D(file, objpos, volume*basevol, pitch, min, max, loop); + sound = mOutput->playSound3D(file, objpos, volume*basevol, pitch, min, max, mode&Play_Loop); sound->mPos = objpos; sound->mVolume = volume; sound->mBaseVolume = basevol; sound->mMinDistance = min; sound->mMaxDistance = max; - if(untracked) + if((mode&Play_NoTrack)) mActiveSounds[sound] = std::make_pair(MWWorld::Ptr(), soundId); else { diff --git a/apps/openmw/mwsound/soundmanager.hpp b/apps/openmw/mwsound/soundmanager.hpp index 3ab2b569e..03cacca23 100644 --- a/apps/openmw/mwsound/soundmanager.hpp +++ b/apps/openmw/mwsound/soundmanager.hpp @@ -30,6 +30,22 @@ namespace MWSound typedef boost::shared_ptr DecoderPtr; typedef boost::shared_ptr SoundPtr; + enum PlayMode { + Play_Normal = 0, /* tracked, non-looping, multi-instance, environment */ + Play_Loop = 1<<0, /* Sound will continually loop until explicitly stopped */ + Play_NoEnv = 1<<1, /* Do not apply environment effects (eg, underwater filters) */ + Play_NoTrack = 1<<2, /* (3D only) Play the sound at the given object's position + * but do not keep it updated (the sound will not move with + * the object and will not stop when the object is deleted. */ + Play_Single = 1<<3, /* (3D only) Play only a single instance of the given sound id. + * Sounds not marked as Single will not count, and all but the + * closest to the listener's position will be stopped. */ + }; + static inline int operator|(const PlayMode &a, const PlayMode &b) + { return (int)a | (int)b; } + static inline int operator&(const PlayMode &a, const PlayMode &b) + { return (int)a & (int)b; } + class SoundManager { Ogre::ResourceGroupManager& mResourceMgr; @@ -90,12 +106,11 @@ namespace MWSound bool sayDone(MWWorld::Ptr reference) const; ///< Is actor not speaking? - SoundPtr playSound(const std::string& soundId, float volume, float pitch, bool loop=false); + SoundPtr playSound(const std::string& soundId, float volume, float pitch, int mode=Play_Normal); ///< Play a sound, independently of 3D-position SoundPtr playSound3D(MWWorld::Ptr reference, const std::string& soundId, - float volume, float pitch, bool loop, - bool untracked=false); + float volume, float pitch, int mode=Play_Normal); ///< Play a sound from an object void stopSound3D(MWWorld::Ptr reference, const std::string& soundId); From 977e7ac9a3c49a4771c2f8ede2193429399a5ee0 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 31 Mar 2012 07:41:26 -0700 Subject: [PATCH 17/18] Remove the Play_Single flag. It's not correct. --- apps/openmw/mwscript/soundextensions.cpp | 4 +-- apps/openmw/mwsound/soundmanager.cpp | 36 ++---------------------- apps/openmw/mwsound/soundmanager.hpp | 6 ---- 3 files changed, 4 insertions(+), 42 deletions(-) diff --git a/apps/openmw/mwscript/soundextensions.cpp b/apps/openmw/mwscript/soundextensions.cpp index 7ae109075..b4386a8a0 100644 --- a/apps/openmw/mwscript/soundextensions.cpp +++ b/apps/openmw/mwscript/soundextensions.cpp @@ -130,7 +130,7 @@ namespace MWScript std::string sound = runtime.getStringLiteral (runtime[0].mInteger); runtime.pop(); - context.getSoundManager().playSound3D (ptr, sound, 1.0, 1.0, MWSound::Play_Single | (mLoop ? MWSound::Play_Loop : 0)); + context.getSoundManager().playSound3D (ptr, sound, 1.0, 1.0, mLoop ? MWSound::Play_Loop : 0); } }; @@ -159,7 +159,7 @@ namespace MWScript Interpreter::Type_Float pitch = runtime[0].mFloat; runtime.pop(); - context.getSoundManager().playSound3D (ptr, sound, volume, pitch, MWSound::Play_Single | (mLoop ? MWSound::Play_Loop : 0)); + context.getSoundManager().playSound3D (ptr, sound, volume, pitch, mLoop ? MWSound::Play_Loop : 0); } }; diff --git a/apps/openmw/mwsound/soundmanager.cpp b/apps/openmw/mwsound/soundmanager.cpp index 2d743d9cb..6249c4e4d 100644 --- a/apps/openmw/mwsound/soundmanager.cpp +++ b/apps/openmw/mwsound/soundmanager.cpp @@ -67,7 +67,6 @@ namespace MWSound SoundManager::~SoundManager() { - mSingleSounds.clear(); mActiveSounds.clear(); mMusic.reset(); mOutput.reset(); @@ -231,29 +230,15 @@ namespace MWSound SoundPtr SoundManager::playSound3D(MWWorld::Ptr ptr, const std::string& soundId, float volume, float pitch, int mode) { - const ESM::Position &pos = ptr.getCellRef().pos; - const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]); SoundPtr sound; - - if((mode&Play_Single)) - { - IDSoundMap::iterator inviter = mSingleSounds.find(soundId); - if(inviter != mSingleSounds.end()) - { - if(inviter->second->mPos.squaredDistance(mOutput->mPos) < - objpos.squaredDistance(mOutput->mPos)) - return sound; - inviter->second->stop(); - mSingleSounds.erase(inviter); - } - } - try { // Look up the sound in the ESM data float basevol = 1.0f; /* TODO: volume settings */ float min, max; std::string file = lookup(soundId, basevol, min, max); + const ESM::Position &pos = ptr.getCellRef().pos; + const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]); sound = mOutput->playSound3D(file, objpos, volume*basevol, pitch, min, max, mode&Play_Loop); sound->mPos = objpos; @@ -265,10 +250,7 @@ namespace MWSound if((mode&Play_NoTrack)) mActiveSounds[sound] = std::make_pair(MWWorld::Ptr(), soundId); else - { mActiveSounds[sound] = std::make_pair(ptr, soundId); - mSingleSounds[soundId] = sound; - } } catch(std::exception &e) { @@ -284,9 +266,6 @@ namespace MWSound { if(snditer->second.first == ptr && snditer->second.second == soundId) { - IDSoundMap::iterator inviter = mSingleSounds.find(snditer->second.second); - if(inviter != mSingleSounds.end() && inviter->second == snditer->first) - mSingleSounds.erase(inviter); snditer->first->stop(); mActiveSounds.erase(snditer++); } @@ -302,9 +281,6 @@ namespace MWSound { if(snditer->second.first == ptr) { - IDSoundMap::iterator inviter = mSingleSounds.find(snditer->second.second); - if(inviter != mSingleSounds.end() && inviter->second == snditer->first) - mSingleSounds.erase(inviter); snditer->first->stop(); mActiveSounds.erase(snditer++); } @@ -321,9 +297,6 @@ namespace MWSound if(snditer->second.first != MWWorld::Ptr() && snditer->second.first.getCell() == cell) { - IDSoundMap::iterator inviter = mSingleSounds.find(snditer->second.second); - if(inviter != mSingleSounds.end() && inviter->second == snditer->first) - mSingleSounds.erase(inviter); snditer->first->stop(); mActiveSounds.erase(snditer++); } @@ -452,12 +425,7 @@ namespace MWSound while(snditer != mActiveSounds.end()) { if(!snditer->first->isPlaying()) - { - IDSoundMap::iterator inviter = mSingleSounds.find(snditer->second.second); - if(inviter != mSingleSounds.end() && inviter->second == snditer->first) - mSingleSounds.erase(inviter); mActiveSounds.erase(snditer++); - } else { snditer->first->update(); diff --git a/apps/openmw/mwsound/soundmanager.hpp b/apps/openmw/mwsound/soundmanager.hpp index 03cacca23..de5cca839 100644 --- a/apps/openmw/mwsound/soundmanager.hpp +++ b/apps/openmw/mwsound/soundmanager.hpp @@ -37,9 +37,6 @@ namespace MWSound Play_NoTrack = 1<<2, /* (3D only) Play the sound at the given object's position * but do not keep it updated (the sound will not move with * the object and will not stop when the object is deleted. */ - Play_Single = 1<<3, /* (3D only) Play only a single instance of the given sound id. - * Sounds not marked as Single will not count, and all but the - * closest to the listener's position will be stopped. */ }; static inline int operator|(const PlayMode &a, const PlayMode &b) { return (int)a | (int)b; } @@ -61,9 +58,6 @@ namespace MWSound typedef std::map SoundMap; SoundMap mActiveSounds; - typedef std::map IDSoundMap; - IDSoundMap mSingleSounds; - std::string lookup(const std::string &soundId, float &volume, float &min, float &max); void streamMusicFull(const std::string& filename); From 575474ff69459e52e0d7bba14fadc86da4fd8f84 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sat, 31 Mar 2012 08:14:39 -0700 Subject: [PATCH 18/18] Pass the mode flags to the sound output play methods --- apps/openmw/mwsound/openal_output.cpp | 8 ++++---- apps/openmw/mwsound/openal_output.hpp | 4 ++-- apps/openmw/mwsound/sound.hpp | 2 ++ apps/openmw/mwsound/sound_output.hpp | 4 ++-- apps/openmw/mwsound/soundmanager.cpp | 8 +++++--- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp index 6a4e3fe57..ddf4df705 100644 --- a/apps/openmw/mwsound/openal_output.cpp +++ b/apps/openmw/mwsound/openal_output.cpp @@ -606,7 +606,7 @@ void OpenAL_Output::bufferFinished(ALuint buf) } -SoundPtr OpenAL_Output::playSound(const std::string &fname, float volume, float pitch, bool loop) +SoundPtr OpenAL_Output::playSound(const std::string &fname, float volume, float pitch, int flags) { boost::shared_ptr sound; ALuint src=0, buf=0; @@ -642,7 +642,7 @@ SoundPtr OpenAL_Output::playSound(const std::string &fname, float volume, float alSourcef(src, AL_PITCH, pitch); alSourcei(src, AL_SOURCE_RELATIVE, AL_TRUE); - alSourcei(src, AL_LOOPING, (loop?AL_TRUE:AL_FALSE)); + alSourcei(src, AL_LOOPING, (flags&Play_Loop) ? AL_TRUE : AL_FALSE); throwALerror(); alSourcei(src, AL_BUFFER, buf); @@ -653,7 +653,7 @@ SoundPtr OpenAL_Output::playSound(const std::string &fname, float volume, float } SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const Ogre::Vector3 &pos, float volume, float pitch, - float min, float max, bool loop) + float min, float max, int flags) { boost::shared_ptr sound; ALuint src=0, buf=0; @@ -690,7 +690,7 @@ SoundPtr OpenAL_Output::playSound3D(const std::string &fname, const Ogre::Vector alSourcef(src, AL_PITCH, pitch); alSourcei(src, AL_SOURCE_RELATIVE, AL_FALSE); - alSourcei(src, AL_LOOPING, (loop?AL_TRUE:AL_FALSE)); + alSourcei(src, AL_LOOPING, (flags&Play_Loop) ? AL_TRUE : AL_FALSE); throwALerror(); alSourcei(src, AL_BUFFER, buf); diff --git a/apps/openmw/mwsound/openal_output.hpp b/apps/openmw/mwsound/openal_output.hpp index 35966cc29..a709576ba 100644 --- a/apps/openmw/mwsound/openal_output.hpp +++ b/apps/openmw/mwsound/openal_output.hpp @@ -40,9 +40,9 @@ namespace MWSound virtual void init(const std::string &devname=""); virtual void deinit(); - virtual SoundPtr playSound(const std::string &fname, float volume, float pitch, bool loop); + virtual SoundPtr playSound(const std::string &fname, float volume, float pitch, int flags); virtual SoundPtr playSound3D(const std::string &fname, const Ogre::Vector3 &pos, - float volume, float pitch, float min, float max, bool loop); + float volume, float pitch, float min, float max, int flags); virtual SoundPtr streamSound(const std::string &fname, float volume, float pitch); virtual void updateListener(const Ogre::Vector3 &pos, const Ogre::Vector3 &atdir, const Ogre::Vector3 &updir); diff --git a/apps/openmw/mwsound/sound.hpp b/apps/openmw/mwsound/sound.hpp index e23830cdb..ca12ec557 100644 --- a/apps/openmw/mwsound/sound.hpp +++ b/apps/openmw/mwsound/sound.hpp @@ -18,6 +18,7 @@ namespace MWSound float mBaseVolume; float mMinDistance; float mMaxDistance; + int mFlags; public: virtual void stop() = 0; @@ -30,6 +31,7 @@ namespace MWSound , mBaseVolume(1.0f) , mMinDistance(20.0f) /* 1 * min_range_scale */ , mMaxDistance(12750.0f) /* 255 * max_range_scale */ + , mFlags(Play_Normal) { } virtual ~Sound() { } diff --git a/apps/openmw/mwsound/sound_output.hpp b/apps/openmw/mwsound/sound_output.hpp index ed3dda2f3..1507e1847 100644 --- a/apps/openmw/mwsound/sound_output.hpp +++ b/apps/openmw/mwsound/sound_output.hpp @@ -24,9 +24,9 @@ namespace MWSound virtual void init(const std::string &devname="") = 0; virtual void deinit() = 0; - virtual SoundPtr playSound(const std::string &fname, float volume, float pitch, bool loop) = 0; + virtual SoundPtr playSound(const std::string &fname, float volume, float pitch, int flags) = 0; virtual SoundPtr playSound3D(const std::string &fname, const Ogre::Vector3 &pos, - float volume, float pitch, float min, float max, bool loop) = 0; + float volume, float pitch, float min, float max, int flags) = 0; virtual SoundPtr streamSound(const std::string &fname, float volume, float pitch) = 0; virtual void updateListener(const Ogre::Vector3 &pos, const Ogre::Vector3 &atdir, const Ogre::Vector3 &updir) = 0; diff --git a/apps/openmw/mwsound/soundmanager.cpp b/apps/openmw/mwsound/soundmanager.cpp index 6249c4e4d..2c2e6e9f9 100644 --- a/apps/openmw/mwsound/soundmanager.cpp +++ b/apps/openmw/mwsound/soundmanager.cpp @@ -185,7 +185,7 @@ namespace MWSound const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]); SoundPtr sound = mOutput->playSound3D(filePath, objpos, basevol, 1.0f, - 20.0f, 12750.0f, false); + 20.0f, 12750.0f, Play_Normal); sound->mPos = objpos; sound->mBaseVolume = basevol; @@ -212,11 +212,12 @@ namespace MWSound float min, max; std::string file = lookup(soundId, basevol, min, max); - sound = mOutput->playSound(file, volume*basevol, pitch, mode&Play_Loop); + sound = mOutput->playSound(file, volume*basevol, pitch, mode); sound->mVolume = volume; sound->mBaseVolume = basevol; sound->mMinDistance = min; sound->mMaxDistance = max; + sound->mFlags = mode; mActiveSounds[sound] = std::make_pair(MWWorld::Ptr(), soundId); } @@ -240,12 +241,13 @@ namespace MWSound const ESM::Position &pos = ptr.getCellRef().pos; const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]); - sound = mOutput->playSound3D(file, objpos, volume*basevol, pitch, min, max, mode&Play_Loop); + sound = mOutput->playSound3D(file, objpos, volume*basevol, pitch, min, max, mode); sound->mPos = objpos; sound->mVolume = volume; sound->mBaseVolume = basevol; sound->mMinDistance = min; sound->mMaxDistance = max; + sound->mFlags = mode; if((mode&Play_NoTrack)) mActiveSounds[sound] = std::make_pair(MWWorld::Ptr(), soundId);