2012-08-09 14:33:21 +02:00
|
|
|
#include "soundmanagerimp.hpp"
|
2010-07-03 15:04:00 +02:00
|
|
|
|
2010-08-14 14:28:17 +02:00
|
|
|
#include <iostream>
|
2011-10-09 09:28:36 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
2017-08-25 16:08:49 -04:00
|
|
|
#include <numeric>
|
2011-10-09 09:28:36 +02:00
|
|
|
|
2015-12-10 17:48:45 -08:00
|
|
|
#include <osg/Matrixf>
|
|
|
|
|
2015-04-22 17:58:55 +02:00
|
|
|
#include <components/misc/rng.hpp>
|
2015-03-15 14:07:47 +13:00
|
|
|
|
2015-04-13 22:48:37 +02:00
|
|
|
#include <components/vfs/manager.hpp>
|
|
|
|
|
2012-04-23 15:27:03 +02:00
|
|
|
#include "../mwbase/environment.hpp"
|
2012-07-03 12:30:50 +02:00
|
|
|
#include "../mwbase/world.hpp"
|
2014-01-21 14:50:58 +01:00
|
|
|
#include "../mwbase/statemanager.hpp"
|
2012-04-23 15:27:03 +02:00
|
|
|
|
2013-08-27 16:04:19 -07:00
|
|
|
#include "../mwworld/esmstore.hpp"
|
2014-02-23 20:11:05 +01:00
|
|
|
#include "../mwworld/cellstore.hpp"
|
2010-08-14 14:28:17 +02:00
|
|
|
|
2015-08-21 21:12:39 +12:00
|
|
|
#include "../mwmechanics/actorutil.hpp"
|
|
|
|
|
2012-03-16 23:40:07 -07:00
|
|
|
#include "sound_output.hpp"
|
2015-11-22 04:53:24 -08:00
|
|
|
#include "sound_buffer.hpp"
|
2012-03-16 23:18:15 -07:00
|
|
|
#include "sound_decoder.hpp"
|
2012-03-17 02:45:18 -07:00
|
|
|
#include "sound.hpp"
|
2012-03-16 22:12:17 -07:00
|
|
|
|
|
|
|
#include "openal_output.hpp"
|
2012-03-17 22:45:28 -07:00
|
|
|
#include "ffmpeg_decoder.hpp"
|
2010-08-12 16:13:54 +02:00
|
|
|
|
|
|
|
|
2010-07-03 15:04:00 +02:00
|
|
|
namespace MWSound
|
|
|
|
{
|
2017-09-15 01:03:41 -07:00
|
|
|
// For combining PlayMode and Type flags
|
|
|
|
inline int operator|(PlayMode a, Type b) { return static_cast<int>(a) | static_cast<int>(b); }
|
|
|
|
|
2017-08-06 20:10:56 +02:00
|
|
|
SoundManager::SoundManager(const VFS::Manager* vfs, const std::map<std::string, std::string>& fallbackMap, bool useSound)
|
2015-04-13 22:48:37 +02:00
|
|
|
: mVFS(vfs)
|
2016-11-27 21:19:52 +01:00
|
|
|
, mFallback(fallbackMap)
|
2012-03-31 03:31:41 -07:00
|
|
|
, mOutput(new DEFAULT_OUTPUT(*this))
|
2012-04-07 16:00:30 -07:00
|
|
|
, mMasterVolume(1.0f)
|
|
|
|
, mSFXVolume(1.0f)
|
|
|
|
, mMusicVolume(1.0f)
|
2012-05-24 04:34:53 +02:00
|
|
|
, mVoiceVolume(1.0f)
|
2015-04-30 19:24:27 -05:00
|
|
|
, mFootstepsVolume(1.0f)
|
2015-11-30 17:42:25 +01:00
|
|
|
, mSoundBuffers(new SoundBufferList::element_type())
|
2015-11-23 06:52:37 -08:00
|
|
|
, mBufferCacheSize(0)
|
2017-09-11 21:33:18 -07:00
|
|
|
, mSounds(new std::deque<Sound>())
|
|
|
|
, mStreams(new std::deque<Stream>())
|
|
|
|
, mMusic(nullptr)
|
2015-04-30 19:24:27 -05:00
|
|
|
, mListenerUnderwater(false)
|
2013-07-30 23:24:18 +02:00
|
|
|
, mListenerPos(0,0,0)
|
|
|
|
, mListenerDir(1,0,0)
|
|
|
|
, mListenerUp(0,0,1)
|
2015-04-30 19:24:27 -05:00
|
|
|
, mPausedSoundTypes(0)
|
2017-09-11 21:33:18 -07:00
|
|
|
, mUnderwaterSound(nullptr)
|
|
|
|
, mNearWaterSound(nullptr)
|
2010-08-12 16:13:54 +02:00
|
|
|
{
|
2012-04-07 16:00:30 -07:00
|
|
|
mMasterVolume = Settings::Manager::getFloat("master volume", "Sound");
|
|
|
|
mMasterVolume = std::min(std::max(mMasterVolume, 0.0f), 1.0f);
|
|
|
|
mSFXVolume = Settings::Manager::getFloat("sfx volume", "Sound");
|
|
|
|
mSFXVolume = std::min(std::max(mSFXVolume, 0.0f), 1.0f);
|
|
|
|
mMusicVolume = Settings::Manager::getFloat("music volume", "Sound");
|
|
|
|
mMusicVolume = std::min(std::max(mMusicVolume, 0.0f), 1.0f);
|
2012-05-24 04:37:41 +02:00
|
|
|
mVoiceVolume = Settings::Manager::getFloat("voice volume", "Sound");
|
2012-05-24 08:56:45 +02:00
|
|
|
mVoiceVolume = std::min(std::max(mVoiceVolume, 0.0f), 1.0f);
|
2012-05-24 04:37:41 +02:00
|
|
|
mFootstepsVolume = Settings::Manager::getFloat("footsteps volume", "Sound");
|
2012-05-24 08:56:45 +02:00
|
|
|
mFootstepsVolume = std::min(std::max(mFootstepsVolume, 0.0f), 1.0f);
|
2012-04-07 16:00:30 -07:00
|
|
|
|
2016-11-27 21:19:52 +01:00
|
|
|
mNearWaterRadius = mFallback.getFallbackInt("Water_NearWaterRadius");
|
|
|
|
mNearWaterPoints = mFallback.getFallbackInt("Water_NearWaterPoints");
|
|
|
|
mNearWaterIndoorTolerance = mFallback.getFallbackFloat("Water_NearWaterIndoorTolerance");
|
|
|
|
mNearWaterOutdoorTolerance = mFallback.getFallbackFloat("Water_NearWaterOutdoorTolerance");
|
2017-09-12 02:48:10 -07:00
|
|
|
mNearWaterIndoorID = Misc::StringUtils::lowerCase(mFallback.getFallbackString("Water_NearWaterIndoorID"));
|
|
|
|
mNearWaterOutdoorID = Misc::StringUtils::lowerCase(mFallback.getFallbackString("Water_NearWaterOutdoorID"));
|
2016-11-27 21:19:52 +01:00
|
|
|
|
2015-11-26 02:13:37 -08:00
|
|
|
mBufferCacheMin = std::max(Settings::Manager::getInt("buffer cache min", "Sound"), 1);
|
|
|
|
mBufferCacheMax = std::max(Settings::Manager::getInt("buffer cache max", "Sound"), 1);
|
|
|
|
mBufferCacheMax *= 1024*1024;
|
|
|
|
mBufferCacheMin = std::min(mBufferCacheMin*1024*1024, mBufferCacheMax);
|
|
|
|
|
2015-12-08 21:12:05 +01:00
|
|
|
if(!useSound)
|
2017-09-15 21:21:49 -07:00
|
|
|
{
|
|
|
|
std::cout<< "Sound disabled." <<std::endl;
|
2015-12-08 21:12:05 +01:00
|
|
|
return;
|
2017-09-15 21:21:49 -07:00
|
|
|
}
|
2015-12-08 21:12:05 +01:00
|
|
|
|
2015-12-04 12:54:41 -08:00
|
|
|
std::string hrtfname = Settings::Manager::getString("hrtf", "Sound");
|
|
|
|
int hrtfstate = Settings::Manager::getInt("hrtf enable", "Sound");
|
2017-09-14 04:48:12 -07:00
|
|
|
HrtfMode hrtfmode = hrtfstate < 0 ? HrtfMode::Auto :
|
|
|
|
hrtfstate > 0 ? HrtfMode::Enable : HrtfMode::Disable;
|
2015-12-04 12:54:41 -08:00
|
|
|
|
2017-09-15 21:21:49 -07:00
|
|
|
std::string devname = Settings::Manager::getString("device", "Sound");
|
|
|
|
if(!mOutput->init(devname, hrtfname, hrtfmode))
|
|
|
|
{
|
|
|
|
std::cerr<< "Failed to initialize audio output, sound disabled" <<std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
std::vector<std::string> names = mOutput->enumerate();
|
|
|
|
std::cout <<"Enumerated output devices:\n";
|
2017-09-16 01:56:58 -07:00
|
|
|
for(const std::string &name : names)
|
|
|
|
std::cout <<" "<<name<<"\n";
|
2017-09-14 03:41:11 -07:00
|
|
|
std::cout.flush();
|
2015-12-04 12:54:41 -08:00
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
names = mOutput->enumerateHrtf();
|
|
|
|
if(!names.empty())
|
|
|
|
{
|
|
|
|
std::cout<< "Enumerated HRTF names:\n";
|
2017-09-16 01:56:58 -07:00
|
|
|
for(const std::string &name : names)
|
|
|
|
std::cout <<" "<<name<<"\n";
|
2017-09-14 03:41:11 -07:00
|
|
|
std::cout.flush();
|
2012-03-16 22:12:17 -07:00
|
|
|
}
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
2010-08-14 14:28:17 +02:00
|
|
|
|
2012-03-06 01:21:00 +02:00
|
|
|
SoundManager::~SoundManager()
|
2010-09-07 15:21:38 +01:00
|
|
|
{
|
2015-11-23 06:35:01 -08:00
|
|
|
clear();
|
2017-09-16 01:56:58 -07:00
|
|
|
for(Sound_Buffer &sfx : *mSoundBuffers)
|
2015-11-23 01:01:20 -08:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
if(sfx.mHandle)
|
|
|
|
mOutput->unloadSound(sfx.mHandle);
|
|
|
|
sfx.mHandle = 0;
|
2015-11-23 01:01:20 -08:00
|
|
|
}
|
2015-12-04 12:54:41 -08:00
|
|
|
mUnusedBuffers.clear();
|
2012-03-18 11:17:45 -07:00
|
|
|
mOutput.reset();
|
2010-09-07 15:21:38 +01:00
|
|
|
}
|
|
|
|
|
2012-03-17 23:30:43 -07:00
|
|
|
// Return a new decoder instance, used as needed by the output implementations
|
|
|
|
DecoderPtr SoundManager::getDecoder()
|
|
|
|
{
|
2015-04-13 22:48:37 +02:00
|
|
|
return DecoderPtr(new DEFAULT_DECODER (mVFS));
|
2012-03-17 23:30:43 -07:00
|
|
|
}
|
|
|
|
|
2015-11-26 01:26:33 -08:00
|
|
|
Sound_Buffer *SoundManager::insertSound(const std::string &soundId, const ESM::Sound *sound)
|
2010-07-03 15:04:00 +02:00
|
|
|
{
|
2015-11-24 01:40:14 -08:00
|
|
|
MWBase::World* world = MWBase::Environment::get().getWorld();
|
|
|
|
static const float fAudioDefaultMinDistance = world->getStore().get<ESM::GameSetting>().find("fAudioDefaultMinDistance")->getFloat();
|
|
|
|
static const float fAudioDefaultMaxDistance = world->getStore().get<ESM::GameSetting>().find("fAudioDefaultMaxDistance")->getFloat();
|
|
|
|
static const float fAudioMinDistanceMult = world->getStore().get<ESM::GameSetting>().find("fAudioMinDistanceMult")->getFloat();
|
|
|
|
static const float fAudioMaxDistanceMult = world->getStore().get<ESM::GameSetting>().find("fAudioMaxDistanceMult")->getFloat();
|
|
|
|
float volume, min, max;
|
|
|
|
|
|
|
|
volume = static_cast<float>(pow(10.0, (sound->mData.mVolume / 255.0*3348.0 - 3348.0) / 2000.0));
|
|
|
|
if(sound->mData.mMinRange == 0 && sound->mData.mMaxRange == 0)
|
|
|
|
{
|
|
|
|
min = fAudioDefaultMinDistance;
|
|
|
|
max = fAudioDefaultMaxDistance;
|
|
|
|
}
|
2015-11-24 00:03:54 -08:00
|
|
|
else
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
2015-11-24 01:40:14 -08:00
|
|
|
min = sound->mData.mMinRange;
|
|
|
|
max = sound->mData.mMaxRange;
|
|
|
|
}
|
2012-03-16 17:08:13 -07:00
|
|
|
|
2015-11-24 01:40:14 -08:00
|
|
|
min *= fAudioMinDistanceMult;
|
|
|
|
max *= fAudioMaxDistanceMult;
|
|
|
|
min = std::max(min, 1.0f);
|
|
|
|
max = std::max(min, max);
|
2014-12-08 17:24:45 +01:00
|
|
|
|
2015-11-30 17:42:25 +01:00
|
|
|
Sound_Buffer *sfx = &*mSoundBuffers->insert(mSoundBuffers->end(),
|
2015-11-30 17:38:46 +01:00
|
|
|
Sound_Buffer("Sound/"+sound->mSound, volume, min, max)
|
2015-11-26 01:26:33 -08:00
|
|
|
);
|
2015-11-24 01:40:14 -08:00
|
|
|
mVFS->normalizeFilename(sfx->mResourceName);
|
2015-11-24 03:18:23 -08:00
|
|
|
|
2015-11-26 01:26:33 -08:00
|
|
|
mBufferNameMap.insert(std::make_pair(soundId, sfx));
|
2015-11-23 07:50:46 -08:00
|
|
|
|
2015-11-26 01:26:33 -08:00
|
|
|
return sfx;
|
2015-11-24 03:18:23 -08:00
|
|
|
}
|
|
|
|
|
2015-11-26 01:26:33 -08:00
|
|
|
// Lookup a soundId for its sound data (resource name, local volume,
|
|
|
|
// minRange, and maxRange)
|
|
|
|
Sound_Buffer *SoundManager::lookupSound(const std::string &soundId) const
|
2015-11-24 03:18:23 -08:00
|
|
|
{
|
2015-11-26 01:26:33 -08:00
|
|
|
NameBufferMap::const_iterator snd = mBufferNameMap.find(soundId);
|
2017-09-14 03:41:11 -07:00
|
|
|
if(snd != mBufferNameMap.end())
|
|
|
|
{
|
|
|
|
Sound_Buffer *sfx = snd->second;
|
|
|
|
if(sfx->mHandle) return sfx;
|
|
|
|
}
|
|
|
|
return nullptr;
|
2015-11-24 03:18:23 -08:00
|
|
|
}
|
|
|
|
|
2015-11-26 01:26:33 -08:00
|
|
|
// Lookup a soundId for its sound data (resource name, local volume,
|
|
|
|
// minRange, and maxRange), and ensure it's ready for use.
|
|
|
|
Sound_Buffer *SoundManager::loadSound(const std::string &soundId)
|
2015-11-24 03:18:23 -08:00
|
|
|
{
|
2017-09-16 03:48:41 -07:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#define LIKELY(x) __builtin_expect((bool)(x), true)
|
|
|
|
#define UNLIKELY(x) __builtin_expect((bool)(x), false)
|
|
|
|
#else
|
|
|
|
#define LIKELY(x) (bool)(x)
|
|
|
|
#define UNLIKELY(x) (bool)(x)
|
|
|
|
#endif
|
|
|
|
if(UNLIKELY(mBufferNameMap.empty()))
|
|
|
|
{
|
|
|
|
MWBase::World *world = MWBase::Environment::get().getWorld();
|
|
|
|
for(const ESM::Sound &sound : world->getStore().get<ESM::Sound>())
|
|
|
|
insertSound(Misc::StringUtils::lowerCase(sound.mId), &sound);
|
|
|
|
}
|
|
|
|
|
2015-11-26 01:26:33 -08:00
|
|
|
Sound_Buffer *sfx;
|
|
|
|
NameBufferMap::const_iterator snd = mBufferNameMap.find(soundId);
|
2017-09-16 03:48:41 -07:00
|
|
|
if(LIKELY(snd != mBufferNameMap.end()))
|
2015-11-26 01:26:33 -08:00
|
|
|
sfx = snd->second;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MWBase::World *world = MWBase::Environment::get().getWorld();
|
2017-09-14 03:41:11 -07:00
|
|
|
const ESM::Sound *sound = world->getStore().get<ESM::Sound>().search(soundId);
|
|
|
|
if(!sound) return nullptr;
|
2015-11-26 01:26:33 -08:00
|
|
|
sfx = insertSound(soundId, sound);
|
|
|
|
}
|
2017-09-16 03:48:41 -07:00
|
|
|
#undef LIKELY
|
|
|
|
#undef UNLIKELY
|
2015-11-24 01:40:14 -08:00
|
|
|
|
2015-11-24 00:03:54 -08:00
|
|
|
if(!sfx->mHandle)
|
2015-11-23 06:52:37 -08:00
|
|
|
{
|
2017-09-16 04:24:01 -07:00
|
|
|
size_t size;
|
|
|
|
std::tie(sfx->mHandle, size) = mOutput->loadSound(sfx->mResourceName);
|
2017-09-14 03:41:11 -07:00
|
|
|
if(!sfx->mHandle) return nullptr;
|
2015-11-24 00:03:54 -08:00
|
|
|
|
2017-09-16 04:24:01 -07:00
|
|
|
mBufferCacheSize += size;
|
2015-11-26 02:13:37 -08:00
|
|
|
if(mBufferCacheSize > mBufferCacheMax)
|
2015-11-23 06:52:37 -08:00
|
|
|
{
|
2015-11-26 02:13:37 -08:00
|
|
|
do {
|
|
|
|
if(mUnusedBuffers.empty())
|
|
|
|
{
|
|
|
|
std::cerr<< "No unused sound buffers to free, using "<<mBufferCacheSize<<" bytes!" <<std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Sound_Buffer *unused = mUnusedBuffers.back();
|
|
|
|
|
2017-09-16 04:24:01 -07:00
|
|
|
size = mOutput->unloadSound(unused->mHandle);
|
|
|
|
mBufferCacheSize -= size;
|
2015-11-26 02:13:37 -08:00
|
|
|
unused->mHandle = 0;
|
|
|
|
|
|
|
|
mUnusedBuffers.pop_back();
|
|
|
|
} while(mBufferCacheSize > mBufferCacheMin);
|
2015-11-23 06:52:37 -08:00
|
|
|
}
|
2015-11-26 01:26:33 -08:00
|
|
|
mUnusedBuffers.push_front(sfx);
|
2015-11-23 06:52:37 -08:00
|
|
|
}
|
2015-11-23 01:01:20 -08:00
|
|
|
|
2015-11-23 07:50:46 -08:00
|
|
|
return sfx;
|
2010-07-03 15:04:00 +02:00
|
|
|
}
|
2010-08-14 09:26:00 +02:00
|
|
|
|
2016-06-27 20:54:42 +02:00
|
|
|
DecoderPtr SoundManager::loadVoice(const std::string &voicefile)
|
2015-11-23 01:33:59 -08:00
|
|
|
{
|
2015-11-23 20:13:24 -08:00
|
|
|
DecoderPtr decoder = getDecoder();
|
|
|
|
// Workaround: Bethesda at some point converted some of the files to mp3, but the references were kept as .wav.
|
2015-12-10 15:37:39 -08:00
|
|
|
if(mVFS->exists(voicefile))
|
2015-11-23 20:13:24 -08:00
|
|
|
decoder->open(voicefile);
|
|
|
|
else
|
2015-11-23 01:33:59 -08:00
|
|
|
{
|
2015-11-23 20:13:24 -08:00
|
|
|
std::string file = voicefile;
|
|
|
|
std::string::size_type pos = file.rfind('.');
|
|
|
|
if(pos != std::string::npos)
|
|
|
|
file = file.substr(0, pos)+".mp3";
|
|
|
|
decoder->open(file);
|
|
|
|
}
|
2015-11-23 01:33:59 -08:00
|
|
|
|
2015-11-24 04:00:17 -08:00
|
|
|
return decoder;
|
2015-11-23 01:33:59 -08:00
|
|
|
}
|
|
|
|
|
2017-09-11 21:33:18 -07:00
|
|
|
Sound *SoundManager::getSoundRef()
|
|
|
|
{
|
|
|
|
Sound *ret;
|
|
|
|
if(!mUnusedSounds.empty())
|
|
|
|
{
|
|
|
|
ret = mUnusedSounds.back();
|
|
|
|
mUnusedSounds.pop_back();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mSounds->emplace_back();
|
|
|
|
ret = &mSounds->back();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream *SoundManager::getStreamRef()
|
|
|
|
{
|
|
|
|
Stream *ret;
|
|
|
|
if(!mUnusedStreams.empty())
|
|
|
|
{
|
|
|
|
ret = mUnusedStreams.back();
|
|
|
|
mUnusedStreams.pop_back();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mStreams->emplace_back();
|
|
|
|
ret = &mStreams->back();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
Stream *SoundManager::playVoice(DecoderPtr decoder, const osg::Vec3f &pos, bool playlocal)
|
2015-11-27 09:47:14 -08:00
|
|
|
{
|
|
|
|
MWBase::World* world = MWBase::Environment::get().getWorld();
|
|
|
|
static const float fAudioMinDistanceMult = world->getStore().get<ESM::GameSetting>().find("fAudioMinDistanceMult")->getFloat();
|
|
|
|
static const float fAudioMaxDistanceMult = world->getStore().get<ESM::GameSetting>().find("fAudioMaxDistanceMult")->getFloat();
|
|
|
|
static const float fAudioVoiceDefaultMinDistance = world->getStore().get<ESM::GameSetting>().find("fAudioVoiceDefaultMinDistance")->getFloat();
|
|
|
|
static const float fAudioVoiceDefaultMaxDistance = world->getStore().get<ESM::GameSetting>().find("fAudioVoiceDefaultMaxDistance")->getFloat();
|
|
|
|
static float minDistance = std::max(fAudioVoiceDefaultMinDistance * fAudioMinDistanceMult, 1.0f);
|
|
|
|
static float maxDistance = std::max(fAudioVoiceDefaultMaxDistance * fAudioMaxDistanceMult, minDistance);
|
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
bool played;
|
2017-09-15 01:03:41 -07:00
|
|
|
float basevol = volumeFromType(Type::Voice);
|
2017-09-11 21:33:18 -07:00
|
|
|
Stream *sound = getStreamRef();
|
2015-11-27 09:47:14 -08:00
|
|
|
if(playlocal)
|
2015-12-02 08:04:30 -08:00
|
|
|
{
|
2017-09-15 01:03:41 -07:00
|
|
|
sound->init(1.0f, basevol, 1.0f, PlayMode::NoEnv|Type::Voice|Play_2D);
|
2017-09-14 03:41:11 -07:00
|
|
|
played = mOutput->streamSound(decoder, sound);
|
2015-12-02 08:04:30 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-09-11 21:33:18 -07:00
|
|
|
sound->init(pos, 1.0f, basevol, 1.0f, minDistance, maxDistance,
|
2017-09-15 01:03:41 -07:00
|
|
|
PlayMode::Normal|Type::Voice|Play_3D);
|
2017-09-14 03:41:11 -07:00
|
|
|
played = mOutput->streamSound3D(decoder, sound, true);
|
|
|
|
}
|
|
|
|
if(!played)
|
|
|
|
{
|
|
|
|
mUnusedStreams.push_back(sound);
|
|
|
|
return nullptr;
|
2015-12-02 08:04:30 -08:00
|
|
|
}
|
|
|
|
return sound;
|
2015-11-27 09:47:14 -08:00
|
|
|
}
|
|
|
|
|
2012-12-18 02:01:04 -08:00
|
|
|
// Gets the combined volume settings for the given sound type
|
2017-09-15 01:03:41 -07:00
|
|
|
float SoundManager::volumeFromType(Type type) const
|
2012-12-18 02:01:04 -08:00
|
|
|
{
|
|
|
|
float volume = mMasterVolume;
|
|
|
|
switch(type)
|
|
|
|
{
|
2017-09-15 01:03:41 -07:00
|
|
|
case Type::Sfx:
|
2012-12-18 02:01:04 -08:00
|
|
|
volume *= mSFXVolume;
|
|
|
|
break;
|
2017-09-15 01:03:41 -07:00
|
|
|
case Type::Voice:
|
2012-12-18 02:01:04 -08:00
|
|
|
volume *= mVoiceVolume;
|
|
|
|
break;
|
2017-09-15 01:03:41 -07:00
|
|
|
case Type::Foot:
|
2013-07-18 19:01:13 -07:00
|
|
|
volume *= mFootstepsVolume;
|
|
|
|
break;
|
2017-09-15 01:03:41 -07:00
|
|
|
case Type::Music:
|
2012-12-18 02:01:04 -08:00
|
|
|
volume *= mMusicVolume;
|
|
|
|
break;
|
2017-09-15 01:03:41 -07:00
|
|
|
case Type::Movie:
|
|
|
|
case Type::Mask:
|
2014-01-14 03:08:37 +01:00
|
|
|
break;
|
2012-12-18 02:01:04 -08:00
|
|
|
}
|
|
|
|
return volume;
|
|
|
|
}
|
2010-07-03 15:04:00 +02:00
|
|
|
|
2012-03-09 03:56:29 +02:00
|
|
|
void SoundManager::stopMusic()
|
|
|
|
{
|
2012-03-17 02:45:18 -07:00
|
|
|
if(mMusic)
|
2017-09-11 21:33:18 -07:00
|
|
|
{
|
2015-12-11 15:13:14 -08:00
|
|
|
mOutput->finishStream(mMusic);
|
2017-09-11 21:33:18 -07:00
|
|
|
mUnusedStreams.push_back(mMusic);
|
|
|
|
mMusic = nullptr;
|
|
|
|
}
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
2011-06-12 16:36:18 -04:00
|
|
|
|
2012-03-20 12:39:49 -07:00
|
|
|
void SoundManager::streamMusicFull(const std::string& filename)
|
2011-06-15 22:33:31 +02:00
|
|
|
{
|
2012-04-06 10:43:14 -07:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
2012-03-20 11:31:13 -07:00
|
|
|
std::cout <<"Playing "<<filename<< std::endl;
|
2014-06-11 18:10:58 +02:00
|
|
|
mLastPlayedMusic = filename;
|
2012-12-12 22:32:02 -08:00
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
stopMusic();
|
2012-12-12 22:32:02 -08:00
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
DecoderPtr decoder = getDecoder();
|
|
|
|
decoder->open(filename);
|
|
|
|
|
|
|
|
mMusic = getStreamRef();
|
2017-09-15 01:03:41 -07:00
|
|
|
mMusic->init(1.0f, volumeFromType(Type::Music), 1.0f,
|
|
|
|
PlayMode::NoEnv|Type::Music|Play_2D);
|
2017-09-14 03:41:11 -07:00
|
|
|
mOutput->streamSound(decoder, mMusic);
|
2011-01-05 22:18:21 +01:00
|
|
|
}
|
2010-11-11 19:47:26 -05:00
|
|
|
|
2017-08-06 20:10:56 +02:00
|
|
|
void SoundManager::advanceMusic(const std::string& filename)
|
|
|
|
{
|
|
|
|
if (!isMusicPlaying())
|
|
|
|
{
|
|
|
|
streamMusicFull(filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mNextMusic = filename;
|
|
|
|
|
2017-08-08 03:17:40 +02:00
|
|
|
mMusic->setFadeout(0.5f);
|
2017-08-06 20:10:56 +02:00
|
|
|
}
|
|
|
|
|
2012-03-16 17:08:13 -07:00
|
|
|
void SoundManager::startRandomTitle()
|
2011-01-05 22:18:21 +01:00
|
|
|
{
|
2018-04-21 22:20:07 +03:00
|
|
|
const std::vector<std::string> &filelist = mMusicFiles[mCurrentPlaylist];
|
2017-08-25 16:08:49 -04:00
|
|
|
auto &tracklist = mMusicToPlay[mCurrentPlaylist];
|
2012-03-20 11:31:13 -07:00
|
|
|
|
2017-08-25 16:08:49 -04:00
|
|
|
// Do a Fisher-Yates shuffle
|
2017-08-20 20:20:27 +00:00
|
|
|
|
2017-08-25 16:08:49 -04:00
|
|
|
// Repopulate if playlist is empty
|
|
|
|
if(tracklist.empty())
|
2014-06-11 18:10:58 +02:00
|
|
|
{
|
2017-08-25 16:08:49 -04:00
|
|
|
tracklist.resize(filelist.size());
|
|
|
|
std::iota(tracklist.begin(), tracklist.end(), 0);
|
2014-06-11 18:10:58 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 16:08:49 -04:00
|
|
|
int i = Misc::Rng::rollDice(tracklist.size());
|
|
|
|
|
|
|
|
// Reshuffle if last played music is the same after a repopulation
|
|
|
|
if(filelist[tracklist[i]] == mLastPlayedMusic)
|
|
|
|
i = (i+1) % tracklist.size();
|
|
|
|
|
|
|
|
// Remove music from list after advancing music
|
|
|
|
advanceMusic(filelist[tracklist[i]]);
|
|
|
|
tracklist[i] = tracklist.back();
|
|
|
|
tracklist.pop_back();
|
2011-01-05 22:18:21 +01:00
|
|
|
}
|
2010-11-11 19:47:26 -05:00
|
|
|
|
2017-09-17 21:44:16 -07:00
|
|
|
|
|
|
|
void SoundManager::streamMusic(const std::string& filename)
|
|
|
|
{
|
|
|
|
advanceMusic("Music/"+filename);
|
|
|
|
}
|
|
|
|
|
2010-10-31 12:23:03 -04:00
|
|
|
bool SoundManager::isMusicPlaying()
|
2011-01-01 22:33:08 +01:00
|
|
|
{
|
2015-11-30 05:42:51 -08:00
|
|
|
return mMusic && mOutput->isStreamPlaying(mMusic);
|
2011-01-01 22:33:08 +01:00
|
|
|
}
|
2010-10-31 12:23:03 -04:00
|
|
|
|
2012-03-20 11:31:13 -07:00
|
|
|
void SoundManager::playPlaylist(const std::string &playlist)
|
2012-03-07 17:46:51 +02:00
|
|
|
{
|
2018-04-21 22:20:07 +03:00
|
|
|
if (mCurrentPlaylist == playlist)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mMusicFiles.find(playlist) == mMusicFiles.end())
|
|
|
|
{
|
|
|
|
std::vector<std::string> filelist;
|
|
|
|
const std::map<std::string, VFS::File*>& index = mVFS->getIndex();
|
|
|
|
|
|
|
|
std::string pattern = "Music/" + playlist;
|
|
|
|
mVFS->normalizeFilename(pattern);
|
|
|
|
|
|
|
|
std::map<std::string, VFS::File*>::const_iterator found = index.lower_bound(pattern);
|
|
|
|
while (found != index.end())
|
|
|
|
{
|
|
|
|
if (found->first.size() >= pattern.size() && found->first.substr(0, pattern.size()) == pattern)
|
|
|
|
filelist.push_back(found->first);
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
++found;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMusicFiles[playlist] = filelist;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mMusicFiles[playlist].empty())
|
|
|
|
return;
|
|
|
|
|
2012-03-20 11:31:13 -07:00
|
|
|
mCurrentPlaylist = playlist;
|
|
|
|
startRandomTitle();
|
2012-03-07 17:46:51 +02:00
|
|
|
}
|
|
|
|
|
2015-11-23 04:00:10 -08:00
|
|
|
|
2015-12-18 18:11:30 +01:00
|
|
|
void SoundManager::say(const MWWorld::ConstPtr &ptr, const std::string &filename)
|
2012-01-29 20:27:03 +01:00
|
|
|
{
|
2012-04-06 10:43:14 -07:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
2012-03-28 04:58:47 -07:00
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
std::string voicefile = "Sound/"+filename;
|
2015-11-23 20:13:24 -08:00
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
mVFS->normalizeFilename(voicefile);
|
|
|
|
DecoderPtr decoder = loadVoice(voicefile);
|
2015-12-15 15:12:48 +01:00
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
MWBase::World *world = MWBase::Environment::get().getWorld();
|
|
|
|
const osg::Vec3f pos = world->getActorHeadTransform(ptr).getTrans();
|
|
|
|
|
|
|
|
stopSay(ptr);
|
|
|
|
Stream *sound = playVoice(decoder, pos, (ptr == MWMechanics::getPlayer()));
|
|
|
|
if(!sound) return;
|
|
|
|
|
|
|
|
mActiveSaySounds.insert(std::make_pair(ptr, sound));
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
|
|
|
|
2015-12-18 18:11:30 +01:00
|
|
|
float SoundManager::getSaySoundLoudness(const MWWorld::ConstPtr &ptr) const
|
2014-07-29 00:26:26 +02:00
|
|
|
{
|
2015-11-23 04:00:10 -08:00
|
|
|
SaySoundMap::const_iterator snditer = mActiveSaySounds.find(ptr);
|
|
|
|
if(snditer != mActiveSaySounds.end())
|
2014-07-29 00:26:26 +02:00
|
|
|
{
|
2017-09-11 21:33:18 -07:00
|
|
|
Stream *sound = snditer->second;
|
2016-06-27 20:54:42 +02:00
|
|
|
return mOutput->getStreamLoudness(sound);
|
2014-07-29 00:26:26 +02:00
|
|
|
}
|
|
|
|
|
2015-11-23 02:08:27 -08:00
|
|
|
return 0.0f;
|
2014-07-29 00:26:26 +02:00
|
|
|
}
|
|
|
|
|
2012-05-01 20:30:31 -07:00
|
|
|
void SoundManager::say(const std::string& filename)
|
|
|
|
{
|
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
std::string voicefile = "Sound/"+filename;
|
2015-11-23 20:13:24 -08:00
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
mVFS->normalizeFilename(voicefile);
|
|
|
|
DecoderPtr decoder = loadVoice(voicefile);
|
|
|
|
|
|
|
|
stopSay(MWWorld::ConstPtr());
|
|
|
|
Stream *sound = playVoice(decoder, osg::Vec3f(), true);
|
|
|
|
if(!sound) return;
|
|
|
|
|
|
|
|
mActiveSaySounds.insert(std::make_pair(MWWorld::ConstPtr(), sound));
|
2012-05-01 20:30:31 -07:00
|
|
|
}
|
|
|
|
|
2015-12-18 18:11:30 +01:00
|
|
|
bool SoundManager::sayDone(const MWWorld::ConstPtr &ptr) const
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
2015-11-23 04:00:10 -08:00
|
|
|
SaySoundMap::const_iterator snditer = mActiveSaySounds.find(ptr);
|
|
|
|
if(snditer != mActiveSaySounds.end())
|
|
|
|
{
|
2016-06-27 20:54:42 +02:00
|
|
|
if(mOutput->isStreamPlaying(snditer->second))
|
2015-11-23 04:00:10 -08:00
|
|
|
return false;
|
2015-11-27 09:47:14 -08:00
|
|
|
return true;
|
2015-11-23 04:00:10 -08:00
|
|
|
}
|
2016-06-27 20:54:42 +02:00
|
|
|
return true;
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
|
|
|
|
2015-12-18 18:11:30 +01:00
|
|
|
void SoundManager::stopSay(const MWWorld::ConstPtr &ptr)
|
2012-05-01 20:30:31 -07:00
|
|
|
{
|
2015-11-23 04:00:10 -08:00
|
|
|
SaySoundMap::iterator snditer = mActiveSaySounds.find(ptr);
|
|
|
|
if(snditer != mActiveSaySounds.end())
|
2012-05-01 20:30:31 -07:00
|
|
|
{
|
2016-06-27 20:54:42 +02:00
|
|
|
mOutput->finishStream(snditer->second);
|
2017-09-11 21:33:18 -07:00
|
|
|
mUnusedStreams.push_back(snditer->second);
|
2015-11-23 04:00:10 -08:00
|
|
|
mActiveSaySounds.erase(snditer);
|
2012-05-01 20:30:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-15 01:03:41 -07:00
|
|
|
Stream *SoundManager::playTrack(const DecoderPtr& decoder, Type type)
|
2012-12-13 00:05:57 -08:00
|
|
|
{
|
|
|
|
if(!mOutput->isInitialized())
|
2017-09-11 21:33:18 -07:00
|
|
|
return nullptr;
|
2015-12-02 08:04:30 -08:00
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
Stream *track = getStreamRef();
|
2017-09-15 01:03:41 -07:00
|
|
|
track->init(1.0f, volumeFromType(type), 1.0f, PlayMode::NoEnv|type|Play_2D);
|
2017-09-14 03:41:11 -07:00
|
|
|
if(!mOutput->streamSound(decoder, track))
|
2012-12-13 00:05:57 -08:00
|
|
|
{
|
2017-09-14 03:41:11 -07:00
|
|
|
mUnusedStreams.push_back(track);
|
|
|
|
return nullptr;
|
2012-12-13 00:05:57 -08:00
|
|
|
}
|
2017-09-14 03:41:11 -07:00
|
|
|
|
|
|
|
mActiveTracks.insert(
|
|
|
|
std::lower_bound(mActiveTracks.begin(), mActiveTracks.end(), track), track
|
|
|
|
);
|
2012-12-13 00:05:57 -08:00
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
2017-09-11 21:33:18 -07:00
|
|
|
void SoundManager::stopTrack(Stream *stream)
|
2015-11-30 05:42:51 -08:00
|
|
|
{
|
2015-12-11 15:13:14 -08:00
|
|
|
mOutput->finishStream(stream);
|
2015-12-01 11:28:37 -08:00
|
|
|
TrackList::iterator iter = std::lower_bound(mActiveTracks.begin(), mActiveTracks.end(), stream);
|
|
|
|
if(iter != mActiveTracks.end() && *iter == stream)
|
|
|
|
mActiveTracks.erase(iter);
|
2017-09-11 21:33:18 -07:00
|
|
|
mUnusedStreams.push_back(stream);
|
2015-11-30 05:42:51 -08:00
|
|
|
}
|
|
|
|
|
2017-09-11 21:33:18 -07:00
|
|
|
double SoundManager::getTrackTimeDelay(Stream *stream)
|
2015-11-30 05:42:51 -08:00
|
|
|
{
|
2015-11-30 07:32:42 -08:00
|
|
|
return mOutput->getStreamDelay(stream);
|
2015-11-30 05:42:51 -08:00
|
|
|
}
|
|
|
|
|
2012-03-16 17:08:13 -07:00
|
|
|
|
2017-09-15 01:03:41 -07:00
|
|
|
Sound *SoundManager::playSound(const std::string& soundId, float volume, float pitch, Type type, PlayMode mode, float offset)
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
2012-04-06 10:43:14 -07:00
|
|
|
if(!mOutput->isInitialized())
|
2017-09-11 21:33:18 -07:00
|
|
|
return nullptr;
|
2012-03-28 04:58:47 -07:00
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
|
|
|
|
if(!sfx) return nullptr;
|
|
|
|
|
|
|
|
Sound *sound = getSoundRef();
|
|
|
|
sound->init(volume * sfx->mVolume, volumeFromType(type), pitch, mode|type|Play_2D);
|
|
|
|
if(!mOutput->playSound(sound, sfx->mHandle, offset))
|
|
|
|
{
|
|
|
|
mUnusedSounds.push_back(sound);
|
|
|
|
return nullptr;
|
2012-03-21 18:20:32 -07:00
|
|
|
}
|
2017-09-14 03:41:11 -07:00
|
|
|
|
|
|
|
if(sfx->mUses++ == 0)
|
2012-03-21 18:20:32 -07:00
|
|
|
{
|
2017-09-14 03:41:11 -07:00
|
|
|
SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfx);
|
|
|
|
if(iter != mUnusedBuffers.end())
|
|
|
|
mUnusedBuffers.erase(iter);
|
2012-03-17 06:51:44 -07:00
|
|
|
}
|
2017-09-14 03:41:11 -07:00
|
|
|
mActiveSounds[MWWorld::ConstPtr()].push_back(std::make_pair(sound, sfx));
|
2012-03-28 06:08:25 -07:00
|
|
|
return sound;
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
|
|
|
|
2017-09-11 21:33:18 -07:00
|
|
|
Sound *SoundManager::playSound3D(const MWWorld::ConstPtr &ptr, const std::string& soundId,
|
2017-09-15 01:03:41 -07:00
|
|
|
float volume, float pitch, Type type, PlayMode mode,
|
2017-09-11 21:33:18 -07:00
|
|
|
float offset)
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
2012-04-06 10:43:14 -07:00
|
|
|
if(!mOutput->isInitialized())
|
2017-09-11 21:33:18 -07:00
|
|
|
return nullptr;
|
2012-03-21 19:08:11 -07:00
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
// Look up the sound in the ESM data
|
|
|
|
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
|
|
|
|
if(!sfx) return nullptr;
|
2014-12-08 23:26:54 +01:00
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
const osg::Vec3f objpos(ptr.getRefData().getPosition().asVec3());
|
2017-09-15 01:03:41 -07:00
|
|
|
if((mode&PlayMode::RemoveAtDistance) && (mListenerPos-objpos).length2() > 2000*2000)
|
2017-09-14 03:41:11 -07:00
|
|
|
return nullptr;
|
2017-08-31 16:14:48 +04:00
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
// Only one copy of given sound can be played at time on ptr, so stop previous copy
|
|
|
|
stopSound3D(ptr, soundId);
|
|
|
|
|
|
|
|
bool played;
|
|
|
|
Sound *sound = getSoundRef();
|
2017-09-15 01:03:41 -07:00
|
|
|
if(!(mode&PlayMode::NoPlayerLocal) && ptr == MWMechanics::getPlayer())
|
2017-09-14 03:41:11 -07:00
|
|
|
{
|
|
|
|
sound->init(volume * sfx->mVolume, volumeFromType(type), pitch, mode|type|Play_2D);
|
|
|
|
played = mOutput->playSound(sound, sfx->mHandle, offset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sound->init(objpos, volume * sfx->mVolume, volumeFromType(type), pitch,
|
|
|
|
sfx->mMinDist, sfx->mMaxDist, mode|type|Play_3D);
|
|
|
|
played = mOutput->playSound3D(sound, sfx->mHandle, offset);
|
2012-03-21 18:20:32 -07:00
|
|
|
}
|
2017-09-14 03:41:11 -07:00
|
|
|
if(!played)
|
2012-03-21 18:20:32 -07:00
|
|
|
{
|
2017-09-14 03:41:11 -07:00
|
|
|
mUnusedSounds.push_back(sound);
|
|
|
|
return nullptr;
|
2012-03-21 18:20:32 -07:00
|
|
|
}
|
2017-09-14 03:41:11 -07:00
|
|
|
|
|
|
|
if(sfx->mUses++ == 0)
|
|
|
|
{
|
|
|
|
SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfx);
|
|
|
|
if(iter != mUnusedBuffers.end())
|
|
|
|
mUnusedBuffers.erase(iter);
|
|
|
|
}
|
|
|
|
mActiveSounds[ptr].push_back(std::make_pair(sound, sfx));
|
2012-03-28 06:08:25 -07:00
|
|
|
return sound;
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
|
|
|
|
2017-09-11 21:33:18 -07:00
|
|
|
Sound *SoundManager::playSound3D(const osg::Vec3f& initialPos, const std::string& soundId,
|
2017-09-15 01:03:41 -07:00
|
|
|
float volume, float pitch, Type type, PlayMode mode,
|
2017-09-11 21:33:18 -07:00
|
|
|
float offset)
|
2014-05-16 13:09:23 +02:00
|
|
|
{
|
|
|
|
if(!mOutput->isInitialized())
|
2017-09-11 21:33:18 -07:00
|
|
|
return nullptr;
|
2014-05-16 13:09:23 +02:00
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
// Look up the sound in the ESM data
|
|
|
|
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
|
|
|
|
if(!sfx) return nullptr;
|
|
|
|
|
|
|
|
Sound *sound = getSoundRef();
|
|
|
|
sound->init(initialPos, volume * sfx->mVolume, volumeFromType(type), pitch,
|
|
|
|
sfx->mMinDist, sfx->mMaxDist, mode|type|Play_3D);
|
|
|
|
if(!mOutput->playSound3D(sound, sfx->mHandle, offset))
|
|
|
|
{
|
|
|
|
mUnusedSounds.push_back(sound);
|
|
|
|
return nullptr;
|
2014-05-16 13:09:23 +02:00
|
|
|
}
|
2017-09-14 03:41:11 -07:00
|
|
|
|
|
|
|
if(sfx->mUses++ == 0)
|
2014-05-16 13:09:23 +02:00
|
|
|
{
|
2017-09-14 03:41:11 -07:00
|
|
|
SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfx);
|
|
|
|
if(iter != mUnusedBuffers.end())
|
|
|
|
mUnusedBuffers.erase(iter);
|
2014-05-16 13:09:23 +02:00
|
|
|
}
|
2017-09-14 03:41:11 -07:00
|
|
|
mActiveSounds[MWWorld::ConstPtr()].push_back(std::make_pair(sound, sfx));
|
2014-05-16 13:09:23 +02:00
|
|
|
return sound;
|
|
|
|
}
|
|
|
|
|
2017-09-11 21:33:18 -07:00
|
|
|
void SoundManager::stopSound(Sound *sound)
|
2015-11-30 05:42:51 -08:00
|
|
|
{
|
2017-09-11 21:33:18 -07:00
|
|
|
if(sound)
|
2015-12-11 15:13:14 -08:00
|
|
|
mOutput->finishSound(sound);
|
2015-11-30 05:42:51 -08:00
|
|
|
}
|
|
|
|
|
2015-12-18 18:11:30 +01:00
|
|
|
void SoundManager::stopSound3D(const MWWorld::ConstPtr &ptr, const std::string& soundId)
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
2015-11-23 03:07:04 -08:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.find(ptr);
|
|
|
|
if(snditer != mActiveSounds.end())
|
2012-03-28 03:48:51 -07:00
|
|
|
{
|
2015-11-26 01:26:33 -08:00
|
|
|
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
|
2017-09-16 01:56:58 -07:00
|
|
|
for(SoundBufferRefPair &snd : snditer->second)
|
2012-03-28 03:48:51 -07:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
if(snd.second == sfx)
|
|
|
|
mOutput->finishSound(snd.first);
|
2012-03-28 03:48:51 -07:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 03:20:50 -07:00
|
|
|
}
|
|
|
|
|
2015-12-18 18:11:30 +01:00
|
|
|
void SoundManager::stopSound3D(const MWWorld::ConstPtr &ptr)
|
2012-03-27 03:20:50 -07:00
|
|
|
{
|
2015-11-23 03:07:04 -08:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.find(ptr);
|
|
|
|
if(snditer != mActiveSounds.end())
|
2012-03-20 07:22:17 -07:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
for(SoundBufferRefPair &snd : snditer->second)
|
|
|
|
mOutput->finishSound(snd.first);
|
2012-03-20 07:22:17 -07:00
|
|
|
}
|
2017-09-16 01:56:58 -07:00
|
|
|
SaySoundMap::iterator sayiter = mActiveSaySounds.find(ptr);
|
|
|
|
if(sayiter != mActiveSaySounds.end())
|
|
|
|
mOutput->finishStream(sayiter->second);
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
|
|
|
|
2013-12-05 13:21:26 +01:00
|
|
|
void SoundManager::stopSound(const MWWorld::CellStore *cell)
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
for(SoundMap::value_type &snd : mActiveSounds)
|
2012-03-17 22:13:57 -07:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
if(!snd.first.isEmpty() && snd.first != MWMechanics::getPlayer() && snd.first.getCell() == cell)
|
2012-03-20 07:22:17 -07:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
for(SoundBufferRefPair &sndbuf : snd.second)
|
|
|
|
mOutput->finishSound(sndbuf.first);
|
2012-03-20 07:22:17 -07:00
|
|
|
}
|
2012-03-17 22:13:57 -07:00
|
|
|
}
|
2017-09-16 01:56:58 -07:00
|
|
|
for(SaySoundMap::value_type &snd : mActiveSaySounds)
|
2015-11-23 04:00:10 -08:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
if(!snd.first.isEmpty() && snd.first != MWMechanics::getPlayer() && snd.first.getCell() == cell)
|
|
|
|
mOutput->finishStream(snd.second);
|
2015-11-23 04:00:10 -08:00
|
|
|
}
|
2012-01-29 20:27:03 +01:00
|
|
|
}
|
2010-08-16 18:06:27 +02:00
|
|
|
|
2012-03-09 18:10:23 +02:00
|
|
|
void SoundManager::stopSound(const std::string& soundId)
|
|
|
|
{
|
2015-12-18 18:11:30 +01:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.find(MWWorld::ConstPtr());
|
2015-11-23 03:07:04 -08:00
|
|
|
if(snditer != mActiveSounds.end())
|
2012-03-28 03:48:51 -07:00
|
|
|
{
|
2015-11-26 01:26:33 -08:00
|
|
|
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
|
2017-09-16 01:56:58 -07:00
|
|
|
for(SoundBufferRefPair &sndbuf : snditer->second)
|
2012-03-28 03:48:51 -07:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
if(sndbuf.second == sfx)
|
|
|
|
mOutput->finishSound(sndbuf.first);
|
2012-03-28 03:48:51 -07:00
|
|
|
}
|
|
|
|
}
|
2012-03-09 18:10:23 +02:00
|
|
|
}
|
|
|
|
|
2015-12-18 18:11:30 +01:00
|
|
|
void SoundManager::fadeOutSound3D(const MWWorld::ConstPtr &ptr,
|
2013-07-26 18:43:06 +02:00
|
|
|
const std::string& soundId, float duration)
|
|
|
|
{
|
2015-11-23 03:07:04 -08:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.find(ptr);
|
|
|
|
if(snditer != mActiveSounds.end())
|
2013-07-26 18:43:06 +02:00
|
|
|
{
|
2015-11-26 01:26:33 -08:00
|
|
|
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
|
2017-09-16 01:56:58 -07:00
|
|
|
for(SoundBufferRefPair &sndbuf : snditer->second)
|
2013-07-26 18:43:06 +02:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
if(sndbuf.second == sfx)
|
|
|
|
sndbuf.first->setFadeout(duration);
|
2013-07-26 18:43:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-18 18:11:30 +01:00
|
|
|
bool SoundManager::getSoundPlaying(const MWWorld::ConstPtr &ptr, const std::string& soundId) const
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
2015-11-23 04:10:56 -08:00
|
|
|
SoundMap::const_iterator snditer = mActiveSounds.find(ptr);
|
|
|
|
if(snditer != mActiveSounds.end())
|
|
|
|
{
|
2015-11-26 01:26:33 -08:00
|
|
|
Sound_Buffer *sfx = lookupSound(Misc::StringUtils::lowerCase(soundId));
|
2017-09-16 01:56:58 -07:00
|
|
|
return std::find_if(snditer->second.cbegin(), snditer->second.cend(),
|
|
|
|
[this,sfx](const SoundBufferRefPair &snd) -> bool
|
|
|
|
{ return snd.second == sfx && mOutput->isSoundPlaying(snd.first); }
|
|
|
|
) != snditer->second.cend();
|
2015-11-23 04:10:56 -08:00
|
|
|
}
|
|
|
|
return false;
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
2010-08-16 18:06:27 +02:00
|
|
|
|
2011-10-09 09:28:36 +02:00
|
|
|
|
2012-12-18 04:19:35 -08:00
|
|
|
void SoundManager::pauseSounds(int types)
|
2012-12-12 02:33:12 -08:00
|
|
|
{
|
|
|
|
if(mOutput->isInitialized())
|
2012-12-18 04:35:24 -08:00
|
|
|
{
|
2017-09-15 01:03:41 -07:00
|
|
|
types = types & Type::Mask;
|
2012-12-18 04:19:35 -08:00
|
|
|
mOutput->pauseSounds(types);
|
2012-12-18 04:35:24 -08:00
|
|
|
mPausedSoundTypes |= types;
|
|
|
|
}
|
2012-12-12 02:33:12 -08:00
|
|
|
}
|
|
|
|
|
2012-12-18 04:19:35 -08:00
|
|
|
void SoundManager::resumeSounds(int types)
|
2012-12-12 02:33:12 -08:00
|
|
|
{
|
|
|
|
if(mOutput->isInitialized())
|
2012-12-18 04:35:24 -08:00
|
|
|
{
|
2017-09-15 01:03:41 -07:00
|
|
|
types = types & Type::Mask & mPausedSoundTypes;
|
2012-12-18 04:19:35 -08:00
|
|
|
mOutput->resumeSounds(types);
|
2012-12-18 04:35:24 -08:00
|
|
|
mPausedSoundTypes &= ~types;
|
|
|
|
}
|
2012-12-12 02:33:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 06:18:59 -07:00
|
|
|
void SoundManager::updateRegionSound(float duration)
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
2013-08-27 16:04:19 -07:00
|
|
|
static float sTimeToNextEnvSound = 0.0f;
|
2012-03-09 03:22:16 +02:00
|
|
|
static int total = 0;
|
|
|
|
static std::string regionName = "";
|
2013-08-27 16:04:19 -07:00
|
|
|
static float sTimePassed = 0.0;
|
|
|
|
MWBase::World *world = MWBase::Environment::get().getWorld();
|
2015-12-18 18:11:30 +01:00
|
|
|
const MWWorld::ConstPtr player = world->getPlayerPtr();
|
2014-02-21 11:35:46 +01:00
|
|
|
const ESM::Cell *cell = player.getCell()->getCell();
|
2011-10-09 09:28:36 +02:00
|
|
|
|
2013-08-27 16:04:19 -07:00
|
|
|
sTimePassed += duration;
|
|
|
|
if(!cell->isExterior() || sTimePassed < sTimeToNextEnvSound)
|
2012-03-16 17:08:13 -07:00
|
|
|
return;
|
2012-03-21 15:19:40 -07:00
|
|
|
|
2015-04-22 17:58:55 +02:00
|
|
|
float a = Misc::Rng::rollClosedProbability();
|
2013-08-27 16:04:19 -07:00
|
|
|
// NOTE: We should use the "Minimum Time Between Environmental Sounds" and
|
|
|
|
// "Maximum Time Between Environmental Sounds" fallback settings here.
|
|
|
|
sTimeToNextEnvSound = 5.0f*a + 15.0f*(1.0f-a);
|
|
|
|
sTimePassed = 0;
|
|
|
|
|
|
|
|
if(regionName != cell->mRegion)
|
2012-01-29 20:27:03 +01:00
|
|
|
{
|
2013-08-27 16:04:19 -07:00
|
|
|
regionName = cell->mRegion;
|
2012-03-16 17:08:13 -07:00
|
|
|
total = 0;
|
|
|
|
}
|
2012-03-09 03:22:16 +02:00
|
|
|
|
2013-08-27 16:04:19 -07:00
|
|
|
const ESM::Region *regn = world->getStore().get<ESM::Region>().search(regionName);
|
|
|
|
if(regn == NULL)
|
2012-05-17 19:54:09 +02:00
|
|
|
return;
|
|
|
|
|
2012-03-16 17:08:13 -07:00
|
|
|
if(total == 0)
|
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
for(const ESM::Region::SoundRef &sndref : regn->mSoundList)
|
|
|
|
total += (int)sndref.mChance;
|
2012-03-22 18:39:10 -07:00
|
|
|
if(total == 0)
|
|
|
|
return;
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
2011-10-09 09:28:36 +02:00
|
|
|
|
2015-04-22 17:58:55 +02:00
|
|
|
int r = Misc::Rng::rollDice(total);
|
2012-03-16 17:08:13 -07:00
|
|
|
int pos = 0;
|
|
|
|
|
2017-09-16 01:56:58 -07:00
|
|
|
for(const ESM::Region::SoundRef &sndref : regn->mSoundList)
|
|
|
|
{
|
|
|
|
if(r - pos < sndref.mChance)
|
2011-10-09 09:28:36 +02:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
playSound(sndref.mSound.toString(), 1.0f, 1.0f);
|
|
|
|
break;
|
2011-10-09 09:28:36 +02:00
|
|
|
}
|
2017-09-16 01:56:58 -07:00
|
|
|
pos += sndref.mChance;
|
|
|
|
}
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
2012-03-17 06:18:59 -07:00
|
|
|
|
2016-11-27 21:19:52 +01:00
|
|
|
void SoundManager::updateWaterSound(float /*duration*/)
|
|
|
|
{
|
2017-09-12 02:48:10 -07:00
|
|
|
static const ESM::Cell *LastCell;
|
2016-11-27 21:19:52 +01:00
|
|
|
MWBase::World* world = MWBase::Environment::get().getWorld();
|
|
|
|
const MWWorld::ConstPtr player = world->getPlayerPtr();
|
|
|
|
osg::Vec3f pos = player.getRefData().getPosition().asVec3();
|
2017-09-12 02:48:10 -07:00
|
|
|
const ESM::Cell *curcell = player.getCell()->getCell();
|
2016-11-27 21:19:52 +01:00
|
|
|
|
|
|
|
float volume = 0.0f;
|
|
|
|
const std::string& soundId = player.getCell()->isExterior() ? mNearWaterOutdoorID : mNearWaterIndoorID;
|
|
|
|
|
|
|
|
if (!mListenerUnderwater)
|
|
|
|
{
|
2017-09-12 02:48:10 -07:00
|
|
|
if (curcell->hasWater())
|
2016-11-27 21:19:52 +01:00
|
|
|
{
|
|
|
|
float dist = std::abs(player.getCell()->getWaterLevel() - pos.z());
|
|
|
|
|
|
|
|
if (player.getCell()->isExterior() && dist < mNearWaterOutdoorTolerance)
|
|
|
|
{
|
|
|
|
volume = (mNearWaterOutdoorTolerance - dist) / mNearWaterOutdoorTolerance;
|
|
|
|
|
|
|
|
if (mNearWaterPoints > 1)
|
|
|
|
{
|
|
|
|
int underwaterPoints = 0;
|
|
|
|
|
|
|
|
float step = mNearWaterRadius * 2.0f / (mNearWaterPoints - 1);
|
|
|
|
|
|
|
|
for (int x = 0; x < mNearWaterPoints; x++)
|
|
|
|
{
|
|
|
|
for (int y = 0; y < mNearWaterPoints; y++)
|
|
|
|
{
|
|
|
|
float height = world->getTerrainHeightAt(
|
|
|
|
osg::Vec3f(pos.x() - mNearWaterRadius + x*step, pos.y() - mNearWaterRadius + y*step, 0.0f));
|
|
|
|
|
|
|
|
if (height < 0)
|
|
|
|
underwaterPoints++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
volume *= underwaterPoints * 2.0f / (mNearWaterPoints*mNearWaterPoints);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!player.getCell()->isExterior() && dist < mNearWaterIndoorTolerance)
|
|
|
|
{
|
|
|
|
volume = (mNearWaterIndoorTolerance - dist) / mNearWaterIndoorTolerance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
volume = 1.0f;
|
|
|
|
|
|
|
|
volume = std::min(volume, 1.0f);
|
|
|
|
|
|
|
|
if (mNearWaterSound)
|
|
|
|
{
|
|
|
|
if (volume == 0.0f)
|
|
|
|
{
|
|
|
|
mOutput->finishSound(mNearWaterSound);
|
2017-09-11 21:33:18 -07:00
|
|
|
mNearWaterSound = nullptr;
|
2016-11-27 21:19:52 +01:00
|
|
|
}
|
2017-09-12 17:00:39 -07:00
|
|
|
else
|
2016-11-27 21:19:52 +01:00
|
|
|
{
|
|
|
|
bool soundIdChanged = false;
|
|
|
|
|
2017-09-12 17:00:39 -07:00
|
|
|
Sound_Buffer *sfx = lookupSound(soundId);
|
|
|
|
if(LastCell != curcell)
|
2016-11-27 21:19:52 +01:00
|
|
|
{
|
2017-09-12 17:00:39 -07:00
|
|
|
LastCell = curcell;
|
|
|
|
SoundMap::const_iterator snditer = mActiveSounds.find(MWWorld::Ptr());
|
|
|
|
if(snditer != mActiveSounds.end())
|
|
|
|
{
|
|
|
|
SoundBufferRefPairList::const_iterator pairiter = std::find_if(
|
|
|
|
snditer->second.begin(), snditer->second.end(),
|
|
|
|
[this](const SoundBufferRefPairList::value_type &item) -> bool
|
|
|
|
{ return mNearWaterSound == item.first; }
|
|
|
|
);
|
|
|
|
if (pairiter != snditer->second.end() && pairiter->second != sfx)
|
|
|
|
soundIdChanged = true;
|
|
|
|
}
|
2016-11-27 21:19:52 +01:00
|
|
|
}
|
|
|
|
|
2017-09-12 17:00:39 -07:00
|
|
|
if(soundIdChanged)
|
2016-11-27 21:19:52 +01:00
|
|
|
{
|
|
|
|
mOutput->finishSound(mNearWaterSound);
|
2017-09-15 01:03:41 -07:00
|
|
|
mNearWaterSound = playSound(soundId, volume, 1.0f, Type::Sfx, PlayMode::Loop);
|
2016-11-27 21:19:52 +01:00
|
|
|
}
|
|
|
|
else if (sfx)
|
|
|
|
mNearWaterSound->setVolume(volume * sfx->mVolume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (volume > 0.0f)
|
2017-09-12 02:48:10 -07:00
|
|
|
{
|
|
|
|
LastCell = curcell;
|
2017-09-15 01:03:41 -07:00
|
|
|
mNearWaterSound = playSound(soundId, volume, 1.0f, Type::Sfx, PlayMode::Loop);
|
2017-09-12 02:48:10 -07:00
|
|
|
}
|
2016-11-27 21:19:52 +01:00
|
|
|
}
|
|
|
|
|
2012-03-21 19:21:36 -07:00
|
|
|
void SoundManager::updateSounds(float duration)
|
2012-03-17 06:18:59 -07:00
|
|
|
{
|
|
|
|
static float timePassed = 0.0;
|
|
|
|
|
|
|
|
timePassed += duration;
|
2012-03-21 19:21:36 -07:00
|
|
|
if(timePassed < (1.0f/30.0f))
|
|
|
|
return;
|
2013-07-26 18:43:06 +02:00
|
|
|
duration = timePassed;
|
2012-03-21 19:21:36 -07:00
|
|
|
timePassed = 0.0f;
|
|
|
|
|
|
|
|
// Make sure music is still playing
|
2016-06-07 01:36:09 +02:00
|
|
|
if(!isMusicPlaying() && !mCurrentPlaylist.empty())
|
2012-03-21 19:21:36 -07:00
|
|
|
startRandomTitle();
|
|
|
|
|
2012-03-31 10:06:12 -07:00
|
|
|
Environment env = Env_Normal;
|
2014-06-25 18:10:26 +02:00
|
|
|
if (mListenerUnderwater)
|
2012-03-31 10:06:12 -07:00
|
|
|
env = Env_Underwater;
|
2013-08-27 13:48:20 -07:00
|
|
|
else if(mUnderwaterSound)
|
2013-08-08 19:16:05 +02:00
|
|
|
{
|
2015-12-11 15:13:14 -08:00
|
|
|
mOutput->finishSound(mUnderwaterSound);
|
2017-09-11 21:33:18 -07:00
|
|
|
mUnderwaterSound = nullptr;
|
2013-08-08 19:16:05 +02:00
|
|
|
}
|
2012-03-31 10:06:12 -07:00
|
|
|
|
2015-11-24 20:37:38 -08:00
|
|
|
mOutput->startUpdate();
|
2012-08-09 17:01:03 +04:00
|
|
|
mOutput->updateListener(
|
|
|
|
mListenerPos,
|
|
|
|
mListenerDir,
|
2012-09-30 17:23:05 -07:00
|
|
|
mListenerUp,
|
2012-08-09 17:01:03 +04:00
|
|
|
env
|
|
|
|
);
|
2012-03-21 19:21:36 -07:00
|
|
|
|
2017-08-08 03:31:01 +02:00
|
|
|
updateMusic(duration);
|
|
|
|
|
2012-03-21 19:21:36 -07:00
|
|
|
// Check if any sounds are finished playing, and trash them
|
|
|
|
SoundMap::iterator snditer = mActiveSounds.begin();
|
|
|
|
while(snditer != mActiveSounds.end())
|
2012-03-17 06:18:59 -07:00
|
|
|
{
|
2017-09-13 02:47:26 -07:00
|
|
|
MWWorld::ConstPtr ptr = snditer->first;
|
2015-11-26 01:26:33 -08:00
|
|
|
SoundBufferRefPairList::iterator sndidx = snditer->second.begin();
|
2015-11-24 03:18:23 -08:00
|
|
|
while(sndidx != snditer->second.end())
|
2012-03-30 07:01:37 -07:00
|
|
|
{
|
2017-09-13 02:47:26 -07:00
|
|
|
Sound *sound;
|
|
|
|
Sound_Buffer *sfx;
|
|
|
|
|
|
|
|
std::tie(sound, sfx) = *sndidx;
|
2015-11-30 05:42:51 -08:00
|
|
|
if(!ptr.isEmpty() && sound->getIs3D())
|
2015-11-23 06:35:01 -08:00
|
|
|
{
|
2015-11-30 05:42:51 -08:00
|
|
|
const ESM::Position &pos = ptr.getRefData().getPosition();
|
|
|
|
const osg::Vec3f objpos(pos.asVec3());
|
|
|
|
sound->setPosition(objpos);
|
|
|
|
|
|
|
|
if(sound->getDistanceCull())
|
|
|
|
{
|
|
|
|
if((mListenerPos - objpos).length2() > 2000*2000)
|
2015-12-11 15:13:14 -08:00
|
|
|
mOutput->finishSound(sound);
|
2015-11-30 05:42:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!mOutput->isSoundPlaying(sound))
|
2015-11-23 06:35:01 -08:00
|
|
|
{
|
2015-12-11 15:13:14 -08:00
|
|
|
mOutput->finishSound(sound);
|
2017-09-11 21:33:18 -07:00
|
|
|
mUnusedSounds.push_back(sound);
|
|
|
|
if(sound == mUnderwaterSound)
|
|
|
|
mUnderwaterSound = nullptr;
|
|
|
|
if(sound == mNearWaterSound)
|
|
|
|
mNearWaterSound = nullptr;
|
2015-11-25 03:34:44 -08:00
|
|
|
if(sfx->mUses-- == 1)
|
2015-11-26 01:26:33 -08:00
|
|
|
mUnusedBuffers.push_front(sfx);
|
2015-11-24 03:18:23 -08:00
|
|
|
sndidx = snditer->second.erase(sndidx);
|
2015-11-23 06:35:01 -08:00
|
|
|
}
|
2015-11-23 04:31:15 -08:00
|
|
|
else
|
2015-11-30 05:42:51 -08:00
|
|
|
{
|
|
|
|
sound->updateFade(duration);
|
|
|
|
|
2015-11-30 14:34:14 -08:00
|
|
|
mOutput->updateSound(sound);
|
2015-11-24 03:18:23 -08:00
|
|
|
++sndidx;
|
2015-11-30 05:42:51 -08:00
|
|
|
}
|
2012-03-30 07:01:37 -07:00
|
|
|
}
|
2015-11-23 03:07:04 -08:00
|
|
|
if(snditer->second.empty())
|
2017-09-13 02:47:26 -07:00
|
|
|
snditer = mActiveSounds.erase(snditer);
|
2015-11-23 03:07:04 -08:00
|
|
|
else
|
|
|
|
++snditer;
|
2012-03-17 06:18:59 -07:00
|
|
|
}
|
2015-11-23 04:00:10 -08:00
|
|
|
|
|
|
|
SaySoundMap::iterator sayiter = mActiveSaySounds.begin();
|
|
|
|
while(sayiter != mActiveSaySounds.end())
|
|
|
|
{
|
2015-12-18 18:11:30 +01:00
|
|
|
MWWorld::ConstPtr ptr = sayiter->first;
|
2017-09-11 21:33:18 -07:00
|
|
|
Stream *sound = sayiter->second;
|
2015-11-30 05:42:51 -08:00
|
|
|
if(!ptr.isEmpty() && sound->getIs3D())
|
|
|
|
{
|
2015-12-10 17:48:45 -08:00
|
|
|
MWBase::World *world = MWBase::Environment::get().getWorld();
|
|
|
|
const osg::Vec3f pos = world->getActorHeadTransform(ptr).getTrans();
|
|
|
|
sound->setPosition(pos);
|
2015-11-23 04:00:10 -08:00
|
|
|
|
2015-11-30 05:42:51 -08:00
|
|
|
if(sound->getDistanceCull())
|
|
|
|
{
|
2015-12-10 17:48:45 -08:00
|
|
|
if((mListenerPos - pos).length2() > 2000*2000)
|
2015-12-11 15:13:14 -08:00
|
|
|
mOutput->finishStream(sound);
|
2015-11-30 05:42:51 -08:00
|
|
|
}
|
|
|
|
}
|
2015-11-23 04:00:10 -08:00
|
|
|
|
2015-11-30 05:42:51 -08:00
|
|
|
if(!mOutput->isStreamPlaying(sound))
|
2015-11-23 04:00:10 -08:00
|
|
|
{
|
2015-12-11 15:13:14 -08:00
|
|
|
mOutput->finishStream(sound);
|
2017-09-11 21:33:18 -07:00
|
|
|
mUnusedStreams.push_back(sound);
|
2015-11-23 04:00:10 -08:00
|
|
|
mActiveSaySounds.erase(sayiter++);
|
|
|
|
}
|
2015-11-23 04:31:15 -08:00
|
|
|
else
|
2015-11-30 05:42:51 -08:00
|
|
|
{
|
|
|
|
sound->updateFade(duration);
|
2015-11-26 09:11:52 -08:00
|
|
|
|
2015-11-30 14:34:14 -08:00
|
|
|
mOutput->updateStream(sound);
|
2015-11-23 04:31:15 -08:00
|
|
|
++sayiter;
|
2015-11-30 05:42:51 -08:00
|
|
|
}
|
2015-11-23 04:31:15 -08:00
|
|
|
}
|
2015-11-23 04:00:10 -08:00
|
|
|
|
2015-12-01 11:28:37 -08:00
|
|
|
TrackList::iterator trkiter = mActiveTracks.begin();
|
|
|
|
for(;trkiter != mActiveTracks.end();++trkiter)
|
2015-11-23 04:31:15 -08:00
|
|
|
{
|
2017-09-11 21:33:18 -07:00
|
|
|
Stream *sound = *trkiter;
|
2015-12-01 11:28:37 -08:00
|
|
|
if(!mOutput->isStreamPlaying(sound))
|
2015-11-23 04:00:10 -08:00
|
|
|
{
|
2015-12-11 15:13:14 -08:00
|
|
|
mOutput->finishStream(sound);
|
2015-12-01 11:28:37 -08:00
|
|
|
trkiter = mActiveTracks.erase(trkiter);
|
2015-11-23 04:00:10 -08:00
|
|
|
}
|
2015-12-01 11:28:37 -08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
sound->updateFade(duration);
|
2015-11-26 09:11:52 -08:00
|
|
|
|
2015-12-01 11:28:37 -08:00
|
|
|
mOutput->updateStream(sound);
|
|
|
|
++trkiter;
|
|
|
|
}
|
|
|
|
}
|
2015-12-14 13:54:53 -08:00
|
|
|
|
|
|
|
if(mListenerUnderwater)
|
|
|
|
{
|
|
|
|
// Play underwater sound (after updating sounds)
|
2017-09-11 21:33:18 -07:00
|
|
|
if(!mUnderwaterSound)
|
2017-09-15 01:03:41 -07:00
|
|
|
mUnderwaterSound = playSound("Underwater", 1.0f, 1.0f, Type::Sfx, PlayMode::LoopNoEnv);
|
2015-12-14 13:54:53 -08:00
|
|
|
}
|
2015-11-30 05:42:51 -08:00
|
|
|
mOutput->finishUpdate();
|
2012-03-21 19:21:36 -07:00
|
|
|
}
|
2015-11-23 04:31:15 -08:00
|
|
|
|
2012-03-21 19:21:36 -07:00
|
|
|
|
2017-08-06 20:10:56 +02:00
|
|
|
void SoundManager::updateMusic(float duration)
|
|
|
|
{
|
|
|
|
if (!mNextMusic.empty())
|
|
|
|
{
|
|
|
|
mMusic->updateFade(duration);
|
|
|
|
|
|
|
|
mOutput->updateStream(mMusic);
|
|
|
|
|
|
|
|
if (mMusic->getRealVolume() <= 0.f)
|
|
|
|
{
|
|
|
|
streamMusicFull(mNextMusic);
|
|
|
|
mNextMusic.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-21 19:21:36 -07:00
|
|
|
void SoundManager::update(float duration)
|
|
|
|
{
|
2012-04-06 10:43:14 -07:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
2014-01-21 14:50:58 +01:00
|
|
|
|
|
|
|
if (MWBase::Environment::get().getStateManager()->getState()!=
|
|
|
|
MWBase::StateManager::State_NoGame)
|
|
|
|
{
|
|
|
|
updateSounds(duration);
|
|
|
|
updateRegionSound(duration);
|
2016-11-27 21:19:52 +01:00
|
|
|
updateWaterSound(duration);
|
2014-01-21 14:50:58 +01:00
|
|
|
}
|
2012-03-17 06:18:59 -07:00
|
|
|
}
|
2012-03-19 02:31:40 -07:00
|
|
|
|
2012-03-21 19:21:36 -07:00
|
|
|
|
2012-05-24 04:34:53 +02:00
|
|
|
void SoundManager::processChangedSettings(const Settings::CategorySettingVector& settings)
|
|
|
|
{
|
|
|
|
mMasterVolume = Settings::Manager::getFloat("master volume", "Sound");
|
|
|
|
mMusicVolume = Settings::Manager::getFloat("music volume", "Sound");
|
|
|
|
mSFXVolume = Settings::Manager::getFloat("sfx volume", "Sound");
|
|
|
|
mFootstepsVolume = Settings::Manager::getFloat("footsteps volume", "Sound");
|
|
|
|
mVoiceVolume = Settings::Manager::getFloat("voice volume", "Sound");
|
2012-05-24 05:30:22 -07:00
|
|
|
|
2015-11-24 20:37:38 -08:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
|
|
|
mOutput->startUpdate();
|
2017-09-16 01:56:58 -07:00
|
|
|
for(SoundMap::value_type &snd : mActiveSounds)
|
2012-05-24 05:30:22 -07:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
for(SoundBufferRefPair &sndbuf : snd.second)
|
2015-11-23 03:07:04 -08:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
Sound *sound = sndbuf.first;
|
2015-11-26 09:11:52 -08:00
|
|
|
sound->setBaseVolume(volumeFromType(sound->getPlayType()));
|
2015-11-30 14:34:14 -08:00
|
|
|
mOutput->updateSound(sound);
|
2015-11-23 03:07:04 -08:00
|
|
|
}
|
2012-05-24 05:30:22 -07:00
|
|
|
}
|
2017-09-16 01:56:58 -07:00
|
|
|
for(SaySoundMap::value_type &snd : mActiveSaySounds)
|
2015-11-23 04:48:18 -08:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
Stream *sound = snd.second;
|
2015-11-26 09:11:52 -08:00
|
|
|
sound->setBaseVolume(volumeFromType(sound->getPlayType()));
|
2015-11-30 14:34:14 -08:00
|
|
|
mOutput->updateStream(sound);
|
2015-11-23 04:48:18 -08:00
|
|
|
}
|
2017-09-16 01:56:58 -07:00
|
|
|
for(Stream *sound : mActiveTracks)
|
2015-12-01 11:28:37 -08:00
|
|
|
{
|
2015-11-26 09:11:52 -08:00
|
|
|
sound->setBaseVolume(volumeFromType(sound->getPlayType()));
|
2015-12-01 11:28:37 -08:00
|
|
|
mOutput->updateStream(sound);
|
2015-11-23 04:48:18 -08:00
|
|
|
}
|
2012-05-24 05:30:22 -07:00
|
|
|
if(mMusic)
|
|
|
|
{
|
2015-11-26 09:11:52 -08:00
|
|
|
mMusic->setBaseVolume(volumeFromType(mMusic->getPlayType()));
|
2015-11-30 14:34:14 -08:00
|
|
|
mOutput->updateStream(mMusic);
|
2012-05-24 05:30:22 -07:00
|
|
|
}
|
2015-11-24 20:37:38 -08:00
|
|
|
mOutput->finishUpdate();
|
2012-05-24 04:34:53 +02:00
|
|
|
}
|
|
|
|
|
2015-12-03 15:16:20 +01:00
|
|
|
void SoundManager::setListenerPosDir(const osg::Vec3f &pos, const osg::Vec3f &dir, const osg::Vec3f &up, bool underwater)
|
2012-08-09 17:01:03 +04:00
|
|
|
{
|
|
|
|
mListenerPos = pos;
|
|
|
|
mListenerDir = dir;
|
2012-09-30 17:23:05 -07:00
|
|
|
mListenerUp = up;
|
2014-06-25 18:10:26 +02:00
|
|
|
|
2015-12-03 15:16:20 +01:00
|
|
|
mListenerUnderwater = underwater;
|
2012-08-09 17:01:03 +04:00
|
|
|
}
|
|
|
|
|
2015-12-18 18:11:30 +01:00
|
|
|
void SoundManager::updatePtr(const MWWorld::ConstPtr &old, const MWWorld::ConstPtr &updated)
|
2014-01-20 12:05:13 +01:00
|
|
|
{
|
2015-11-23 03:07:04 -08:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.find(old);
|
|
|
|
if(snditer != mActiveSounds.end())
|
2014-01-20 12:05:13 +01:00
|
|
|
{
|
2017-09-13 20:47:20 -07:00
|
|
|
SoundBufferRefPairList sndlist = std::move(snditer->second);
|
2015-11-23 03:07:04 -08:00
|
|
|
mActiveSounds.erase(snditer);
|
2017-09-13 20:47:20 -07:00
|
|
|
mActiveSounds.emplace(updated, std::move(sndlist));
|
2014-01-20 12:05:13 +01:00
|
|
|
}
|
2015-11-23 04:00:10 -08:00
|
|
|
SaySoundMap::iterator sayiter = mActiveSaySounds.find(old);
|
|
|
|
if(sayiter != mActiveSaySounds.end())
|
|
|
|
{
|
2017-09-11 21:33:18 -07:00
|
|
|
Stream *stream = sayiter->second;
|
2015-11-23 04:00:10 -08:00
|
|
|
mActiveSaySounds.erase(sayiter);
|
2017-09-13 20:47:20 -07:00
|
|
|
mActiveSaySounds.emplace(updated, stream);
|
2015-11-27 09:47:14 -08:00
|
|
|
}
|
2014-01-20 12:05:13 +01:00
|
|
|
}
|
|
|
|
|
2012-03-20 17:57:28 -07:00
|
|
|
// Default readAll implementation, for decoders that can't do anything
|
|
|
|
// better
|
|
|
|
void Sound_Decoder::readAll(std::vector<char> &output)
|
|
|
|
{
|
|
|
|
size_t total = output.size();
|
|
|
|
size_t got;
|
|
|
|
|
|
|
|
output.resize(total+32768);
|
|
|
|
while((got=read(&output[total], output.size()-total)) > 0)
|
|
|
|
{
|
|
|
|
total += got;
|
|
|
|
output.resize(total*2);
|
|
|
|
}
|
|
|
|
output.resize(total);
|
|
|
|
}
|
|
|
|
|
2012-03-19 02:31:40 -07:00
|
|
|
|
|
|
|
const char *getSampleTypeName(SampleType type)
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case SampleType_UInt8: return "U8";
|
|
|
|
case SampleType_Int16: return "S16";
|
2013-02-26 10:19:33 -08:00
|
|
|
case SampleType_Float32: return "Float32";
|
2012-03-19 02:31:40 -07:00
|
|
|
}
|
|
|
|
return "(unknown sample type)";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *getChannelConfigName(ChannelConfig config)
|
|
|
|
{
|
|
|
|
switch(config)
|
|
|
|
{
|
2012-12-13 04:10:19 -08:00
|
|
|
case ChannelConfig_Mono: return "Mono";
|
|
|
|
case ChannelConfig_Stereo: return "Stereo";
|
|
|
|
case ChannelConfig_Quad: return "Quad";
|
|
|
|
case ChannelConfig_5point1: return "5.1 Surround";
|
|
|
|
case ChannelConfig_7point1: return "7.1 Surround";
|
2012-03-19 02:31:40 -07:00
|
|
|
}
|
|
|
|
return "(unknown channel config)";
|
|
|
|
}
|
2012-03-19 05:29:04 -07:00
|
|
|
|
|
|
|
size_t framesToBytes(size_t frames, ChannelConfig config, SampleType type)
|
|
|
|
{
|
|
|
|
switch(config)
|
|
|
|
{
|
2012-12-13 04:10:19 -08:00
|
|
|
case ChannelConfig_Mono: frames *= 1; break;
|
|
|
|
case ChannelConfig_Stereo: frames *= 2; break;
|
|
|
|
case ChannelConfig_Quad: frames *= 4; break;
|
|
|
|
case ChannelConfig_5point1: frames *= 6; break;
|
|
|
|
case ChannelConfig_7point1: frames *= 8; break;
|
2012-03-19 05:29:04 -07:00
|
|
|
}
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case SampleType_UInt8: frames *= 1; break;
|
|
|
|
case SampleType_Int16: frames *= 2; break;
|
2013-02-26 10:19:33 -08:00
|
|
|
case SampleType_Float32: frames *= 4; break;
|
2012-03-19 05:29:04 -07:00
|
|
|
}
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t bytesToFrames(size_t bytes, ChannelConfig config, SampleType type)
|
|
|
|
{
|
|
|
|
return bytes / framesToBytes(1, config, type);
|
|
|
|
}
|
2014-01-21 14:13:13 +01:00
|
|
|
|
|
|
|
void SoundManager::clear()
|
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
stopMusic();
|
|
|
|
|
|
|
|
for(SoundMap::value_type &snd : mActiveSounds)
|
2015-11-23 03:07:04 -08:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
for(SoundBufferRefPair &sndbuf : snd.second)
|
2015-11-23 06:35:01 -08:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
mOutput->finishSound(sndbuf.first);
|
|
|
|
mUnusedSounds.push_back(sndbuf.first);
|
|
|
|
Sound_Buffer *sfx = sndbuf.second;
|
2015-11-25 03:34:44 -08:00
|
|
|
if(sfx->mUses-- == 1)
|
2015-11-26 01:26:33 -08:00
|
|
|
mUnusedBuffers.push_front(sfx);
|
2015-11-23 06:35:01 -08:00
|
|
|
}
|
2015-11-23 03:07:04 -08:00
|
|
|
}
|
2014-01-21 14:13:13 +01:00
|
|
|
mActiveSounds.clear();
|
2017-09-16 01:56:58 -07:00
|
|
|
mUnderwaterSound = nullptr;
|
|
|
|
mNearWaterSound = nullptr;
|
|
|
|
|
|
|
|
for(SaySoundMap::value_type &snd : mActiveSaySounds)
|
2017-09-11 21:33:18 -07:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
mOutput->finishStream(snd.second);
|
|
|
|
mUnusedStreams.push_back(snd.second);
|
2017-09-11 21:33:18 -07:00
|
|
|
}
|
2015-11-23 04:00:10 -08:00
|
|
|
mActiveSaySounds.clear();
|
2017-09-16 01:56:58 -07:00
|
|
|
|
|
|
|
for(Stream *sound : mActiveTracks)
|
2017-09-11 21:33:18 -07:00
|
|
|
{
|
2017-09-16 01:56:58 -07:00
|
|
|
mOutput->finishStream(sound);
|
|
|
|
mUnusedStreams.push_back(sound);
|
2017-09-11 21:33:18 -07:00
|
|
|
}
|
2015-12-01 11:28:37 -08:00
|
|
|
mActiveTracks.clear();
|
2014-01-21 14:13:13 +01:00
|
|
|
}
|
2010-07-03 15:04:00 +02:00
|
|
|
}
|