2012-08-09 12:33:21 +00:00
|
|
|
#include "soundmanagerimp.hpp"
|
2010-07-03 13:04:00 +00:00
|
|
|
|
2011-10-09 07:28:36 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
2017-08-25 20:08:49 +00:00
|
|
|
#include <numeric>
|
2011-10-09 07:28:36 +00:00
|
|
|
|
2015-12-11 01:48:45 +00:00
|
|
|
#include <osg/Matrixf>
|
|
|
|
|
2015-04-22 15:58:55 +00:00
|
|
|
#include <components/misc/rng.hpp>
|
2018-08-14 19:05:43 +00:00
|
|
|
#include <components/debug/debuglog.hpp>
|
2015-04-13 20:48:37 +00:00
|
|
|
#include <components/vfs/manager.hpp>
|
|
|
|
|
2012-04-23 13:27:03 +00:00
|
|
|
#include "../mwbase/environment.hpp"
|
2012-07-03 10:30:50 +00:00
|
|
|
#include "../mwbase/world.hpp"
|
2014-01-21 13:50:58 +00:00
|
|
|
#include "../mwbase/statemanager.hpp"
|
2012-04-23 13:27:03 +00:00
|
|
|
|
2013-08-27 23:04:19 +00:00
|
|
|
#include "../mwworld/esmstore.hpp"
|
2014-02-23 19:11:05 +00:00
|
|
|
#include "../mwworld/cellstore.hpp"
|
2010-08-14 12:28:17 +00:00
|
|
|
|
2015-08-21 09:12:39 +00:00
|
|
|
#include "../mwmechanics/actorutil.hpp"
|
|
|
|
|
2015-11-22 12:53:24 +00:00
|
|
|
#include "sound_buffer.hpp"
|
2012-03-17 06:18:15 +00:00
|
|
|
#include "sound_decoder.hpp"
|
2018-05-07 16:40:53 +00:00
|
|
|
#include "sound_output.hpp"
|
2012-03-17 09:45:18 +00:00
|
|
|
#include "sound.hpp"
|
2012-03-17 05:12:17 +00:00
|
|
|
|
|
|
|
#include "openal_output.hpp"
|
2012-03-18 05:45:28 +00:00
|
|
|
#include "ffmpeg_decoder.hpp"
|
2010-08-12 14:13:54 +00:00
|
|
|
|
|
|
|
|
2010-07-03 13:04:00 +00:00
|
|
|
namespace MWSound
|
|
|
|
{
|
2020-06-28 12:41:05 +00:00
|
|
|
namespace
|
|
|
|
{
|
2020-06-28 11:49:05 +00:00
|
|
|
constexpr float sMinUpdateInterval = 1.0f / 30.0f;
|
|
|
|
|
2020-06-28 12:41:05 +00:00
|
|
|
WaterSoundUpdaterSettings makeWaterSoundUpdaterSettings()
|
|
|
|
{
|
|
|
|
WaterSoundUpdaterSettings settings;
|
|
|
|
|
|
|
|
settings.mNearWaterRadius = Fallback::Map::getInt("Water_NearWaterRadius");
|
|
|
|
settings.mNearWaterPoints = Fallback::Map::getInt("Water_NearWaterPoints");
|
|
|
|
settings.mNearWaterIndoorTolerance = Fallback::Map::getFloat("Water_NearWaterIndoorTolerance");
|
|
|
|
settings.mNearWaterOutdoorTolerance = Fallback::Map::getFloat("Water_NearWaterOutdoorTolerance");
|
|
|
|
settings.mNearWaterIndoorID = Misc::StringUtils::lowerCase(Fallback::Map::getString("Water_NearWaterIndoorID"));
|
|
|
|
settings.mNearWaterOutdoorID = Misc::StringUtils::lowerCase(Fallback::Map::getString("Water_NearWaterOutdoorID"));
|
|
|
|
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-15 08:03:41 +00:00
|
|
|
// For combining PlayMode and Type flags
|
|
|
|
inline int operator|(PlayMode a, Type b) { return static_cast<int>(a) | static_cast<int>(b); }
|
|
|
|
|
2019-01-22 06:08:48 +00:00
|
|
|
SoundManager::SoundManager(const VFS::Manager* vfs, bool useSound)
|
2015-04-13 20:48:37 +00:00
|
|
|
: mVFS(vfs)
|
2012-03-31 10:31:41 +00:00
|
|
|
, mOutput(new DEFAULT_OUTPUT(*this))
|
2020-06-28 12:41:05 +00:00
|
|
|
, mWaterSoundUpdater(makeWaterSoundUpdaterSettings())
|
2015-11-30 16:42:25 +00:00
|
|
|
, mSoundBuffers(new SoundBufferList::element_type())
|
2015-11-23 14:52:37 +00:00
|
|
|
, mBufferCacheSize(0)
|
2015-05-01 00:24:27 +00:00
|
|
|
, mListenerUnderwater(false)
|
2013-07-30 21:24:18 +00:00
|
|
|
, mListenerPos(0,0,0)
|
|
|
|
, mListenerDir(1,0,0)
|
|
|
|
, mListenerUp(0,0,1)
|
2017-09-12 04:33:18 +00:00
|
|
|
, mUnderwaterSound(nullptr)
|
|
|
|
, mNearWaterSound(nullptr)
|
2020-03-31 09:15:26 +00:00
|
|
|
, mPlaybackPaused(false)
|
2010-08-12 14:13:54 +00:00
|
|
|
{
|
2015-11-26 10:13:37 +00: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 20:12:05 +00:00
|
|
|
if(!useSound)
|
2017-09-16 04:21:49 +00:00
|
|
|
{
|
2018-08-14 19:05:43 +00:00
|
|
|
Log(Debug::Info) << "Sound disabled.";
|
2015-12-08 20:12:05 +00:00
|
|
|
return;
|
2017-09-16 04:21:49 +00:00
|
|
|
}
|
2015-12-08 20:12:05 +00:00
|
|
|
|
2015-12-04 20:54:41 +00:00
|
|
|
std::string hrtfname = Settings::Manager::getString("hrtf", "Sound");
|
|
|
|
int hrtfstate = Settings::Manager::getInt("hrtf enable", "Sound");
|
2017-09-14 11:48:12 +00:00
|
|
|
HrtfMode hrtfmode = hrtfstate < 0 ? HrtfMode::Auto :
|
|
|
|
hrtfstate > 0 ? HrtfMode::Enable : HrtfMode::Disable;
|
2015-12-04 20:54:41 +00:00
|
|
|
|
2017-09-16 04:21:49 +00:00
|
|
|
std::string devname = Settings::Manager::getString("device", "Sound");
|
|
|
|
if(!mOutput->init(devname, hrtfname, hrtfmode))
|
|
|
|
{
|
2018-08-14 19:05:43 +00:00
|
|
|
Log(Debug::Error) << "Failed to initialize audio output, sound disabled";
|
2017-09-16 04:21:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-14 10:41:11 +00:00
|
|
|
std::vector<std::string> names = mOutput->enumerate();
|
2018-08-14 19:05:43 +00:00
|
|
|
std::stringstream stream;
|
|
|
|
|
|
|
|
stream << "Enumerated output devices:\n";
|
2017-09-16 08:56:58 +00:00
|
|
|
for(const std::string &name : names)
|
2018-08-14 19:05:43 +00:00
|
|
|
stream << " " << name;
|
|
|
|
|
|
|
|
Log(Debug::Info) << stream.str();
|
|
|
|
stream.str("");
|
2015-12-04 20:54:41 +00:00
|
|
|
|
2017-09-14 10:41:11 +00:00
|
|
|
names = mOutput->enumerateHrtf();
|
|
|
|
if(!names.empty())
|
|
|
|
{
|
2018-08-14 19:05:43 +00:00
|
|
|
stream << "Enumerated HRTF names:\n";
|
2017-09-16 08:56:58 +00:00
|
|
|
for(const std::string &name : names)
|
2018-08-14 19:05:43 +00:00
|
|
|
stream << " " << name;
|
|
|
|
|
|
|
|
Log(Debug::Info) << stream.str();
|
2012-03-17 05:12:17 +00:00
|
|
|
}
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
2010-08-14 12:28:17 +00:00
|
|
|
|
2012-03-05 23:21:00 +00:00
|
|
|
SoundManager::~SoundManager()
|
2010-09-07 14:21:38 +00:00
|
|
|
{
|
2015-11-23 14:35:01 +00:00
|
|
|
clear();
|
2017-09-16 08:56:58 +00:00
|
|
|
for(Sound_Buffer &sfx : *mSoundBuffers)
|
2015-11-23 09:01:20 +00:00
|
|
|
{
|
2017-09-16 08:56:58 +00:00
|
|
|
if(sfx.mHandle)
|
|
|
|
mOutput->unloadSound(sfx.mHandle);
|
|
|
|
sfx.mHandle = 0;
|
2015-11-23 09:01:20 +00:00
|
|
|
}
|
2015-12-04 20:54:41 +00:00
|
|
|
mUnusedBuffers.clear();
|
2012-03-18 18:17:45 +00:00
|
|
|
mOutput.reset();
|
2010-09-07 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2012-03-18 06:30:43 +00:00
|
|
|
// Return a new decoder instance, used as needed by the output implementations
|
|
|
|
DecoderPtr SoundManager::getDecoder()
|
|
|
|
{
|
2015-04-13 20:48:37 +00:00
|
|
|
return DecoderPtr(new DEFAULT_DECODER (mVFS));
|
2012-03-18 06:30:43 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 09:26:33 +00:00
|
|
|
Sound_Buffer *SoundManager::insertSound(const std::string &soundId, const ESM::Sound *sound)
|
2010-07-03 13:04:00 +00:00
|
|
|
{
|
2015-11-24 09:40:14 +00:00
|
|
|
MWBase::World* world = MWBase::Environment::get().getWorld();
|
2018-08-29 15:38:12 +00:00
|
|
|
static const float fAudioDefaultMinDistance = world->getStore().get<ESM::GameSetting>().find("fAudioDefaultMinDistance")->mValue.getFloat();
|
|
|
|
static const float fAudioDefaultMaxDistance = world->getStore().get<ESM::GameSetting>().find("fAudioDefaultMaxDistance")->mValue.getFloat();
|
|
|
|
static const float fAudioMinDistanceMult = world->getStore().get<ESM::GameSetting>().find("fAudioMinDistanceMult")->mValue.getFloat();
|
|
|
|
static const float fAudioMaxDistanceMult = world->getStore().get<ESM::GameSetting>().find("fAudioMaxDistanceMult")->mValue.getFloat();
|
2015-11-24 09:40:14 +00:00
|
|
|
float volume, min, max;
|
|
|
|
|
|
|
|
volume = static_cast<float>(pow(10.0, (sound->mData.mVolume / 255.0*3348.0 - 3348.0) / 2000.0));
|
2018-08-02 09:35:55 +00:00
|
|
|
min = sound->mData.mMinRange;
|
|
|
|
max = sound->mData.mMaxRange;
|
2020-06-10 21:06:37 +00:00
|
|
|
if (min == 0 && max == 0)
|
|
|
|
{
|
2015-11-24 09:40:14 +00:00
|
|
|
min = fAudioDefaultMinDistance;
|
|
|
|
max = fAudioDefaultMaxDistance;
|
2020-06-10 21:06:37 +00:00
|
|
|
}
|
2012-03-17 00:08:13 +00:00
|
|
|
|
2015-11-24 09:40:14 +00:00
|
|
|
min *= fAudioMinDistanceMult;
|
|
|
|
max *= fAudioMaxDistanceMult;
|
|
|
|
min = std::max(min, 1.0f);
|
|
|
|
max = std::max(min, max);
|
2014-12-08 16:24:45 +00:00
|
|
|
|
2015-11-30 16:42:25 +00:00
|
|
|
Sound_Buffer *sfx = &*mSoundBuffers->insert(mSoundBuffers->end(),
|
2015-11-30 16:38:46 +00:00
|
|
|
Sound_Buffer("Sound/"+sound->mSound, volume, min, max)
|
2015-11-26 09:26:33 +00:00
|
|
|
);
|
2015-11-24 09:40:14 +00:00
|
|
|
mVFS->normalizeFilename(sfx->mResourceName);
|
2015-11-24 11:18:23 +00:00
|
|
|
|
2015-11-26 09:26:33 +00:00
|
|
|
mBufferNameMap.insert(std::make_pair(soundId, sfx));
|
2015-11-23 15:50:46 +00:00
|
|
|
|
2015-11-26 09:26:33 +00:00
|
|
|
return sfx;
|
2015-11-24 11:18:23 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 09:26:33 +00: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 11:18:23 +00:00
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
NameBufferMap::const_iterator snd = mBufferNameMap.find(soundId);
|
2017-09-14 10:41:11 +00:00
|
|
|
if(snd != mBufferNameMap.end())
|
|
|
|
{
|
|
|
|
Sound_Buffer *sfx = snd->second;
|
|
|
|
if(sfx->mHandle) return sfx;
|
|
|
|
}
|
|
|
|
return nullptr;
|
2015-11-24 11:18:23 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 09:26:33 +00: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 11:18:23 +00:00
|
|
|
{
|
2017-09-16 10:48:41 +00: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 09:26:33 +00:00
|
|
|
Sound_Buffer *sfx;
|
|
|
|
NameBufferMap::const_iterator snd = mBufferNameMap.find(soundId);
|
2017-09-16 10:48:41 +00:00
|
|
|
if(LIKELY(snd != mBufferNameMap.end()))
|
2015-11-26 09:26:33 +00:00
|
|
|
sfx = snd->second;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MWBase::World *world = MWBase::Environment::get().getWorld();
|
2017-09-14 10:41:11 +00:00
|
|
|
const ESM::Sound *sound = world->getStore().get<ESM::Sound>().search(soundId);
|
|
|
|
if(!sound) return nullptr;
|
2015-11-26 09:26:33 +00:00
|
|
|
sfx = insertSound(soundId, sound);
|
|
|
|
}
|
2017-09-16 10:48:41 +00:00
|
|
|
#undef LIKELY
|
|
|
|
#undef UNLIKELY
|
2015-11-24 09:40:14 +00:00
|
|
|
|
2015-11-24 08:03:54 +00:00
|
|
|
if(!sfx->mHandle)
|
2015-11-23 14:52:37 +00:00
|
|
|
{
|
2017-09-16 11:24:01 +00:00
|
|
|
size_t size;
|
|
|
|
std::tie(sfx->mHandle, size) = mOutput->loadSound(sfx->mResourceName);
|
2017-09-14 10:41:11 +00:00
|
|
|
if(!sfx->mHandle) return nullptr;
|
2015-11-24 08:03:54 +00:00
|
|
|
|
2017-09-16 11:24:01 +00:00
|
|
|
mBufferCacheSize += size;
|
2015-11-26 10:13:37 +00:00
|
|
|
if(mBufferCacheSize > mBufferCacheMax)
|
2015-11-23 14:52:37 +00:00
|
|
|
{
|
2015-11-26 10:13:37 +00:00
|
|
|
do {
|
|
|
|
if(mUnusedBuffers.empty())
|
|
|
|
{
|
2018-08-14 19:05:43 +00:00
|
|
|
Log(Debug::Warning) << "No unused sound buffers to free, using " << mBufferCacheSize << " bytes!";
|
2015-11-26 10:13:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
Sound_Buffer *unused = mUnusedBuffers.back();
|
|
|
|
|
2017-09-16 11:24:01 +00:00
|
|
|
size = mOutput->unloadSound(unused->mHandle);
|
|
|
|
mBufferCacheSize -= size;
|
2015-11-26 10:13:37 +00:00
|
|
|
unused->mHandle = 0;
|
|
|
|
|
|
|
|
mUnusedBuffers.pop_back();
|
|
|
|
} while(mBufferCacheSize > mBufferCacheMin);
|
2015-11-23 14:52:37 +00:00
|
|
|
}
|
2015-11-26 09:26:33 +00:00
|
|
|
mUnusedBuffers.push_front(sfx);
|
2015-11-23 14:52:37 +00:00
|
|
|
}
|
2015-11-23 09:01:20 +00:00
|
|
|
|
2015-11-23 15:50:46 +00:00
|
|
|
return sfx;
|
2010-07-03 13:04:00 +00:00
|
|
|
}
|
2010-08-14 07:26:00 +00:00
|
|
|
|
2016-06-27 18:54:42 +00:00
|
|
|
DecoderPtr SoundManager::loadVoice(const std::string &voicefile)
|
2015-11-23 09:33:59 +00:00
|
|
|
{
|
2018-10-18 10:59:39 +00:00
|
|
|
try
|
2015-11-23 09:33:59 +00:00
|
|
|
{
|
2018-10-18 10:59:39 +00:00
|
|
|
DecoderPtr decoder = getDecoder();
|
|
|
|
|
|
|
|
// Workaround: Bethesda at some point converted some of the files to mp3, but the references were kept as .wav.
|
|
|
|
if(mVFS->exists(voicefile))
|
|
|
|
decoder->open(voicefile);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return decoder;
|
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
Log(Debug::Error) << "Failed to load audio from " << voicefile << ": " << e.what();
|
2015-11-24 04:13:24 +00:00
|
|
|
}
|
2015-11-23 09:33:59 +00:00
|
|
|
|
2018-10-18 10:59:39 +00:00
|
|
|
return nullptr;
|
2015-11-23 09:33:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 16:25:23 +00:00
|
|
|
SoundPtr SoundManager::getSoundRef()
|
2017-09-12 04:33:18 +00:00
|
|
|
{
|
2020-06-28 15:17:43 +00:00
|
|
|
return mSounds.get();
|
2017-09-12 04:33:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 16:25:23 +00:00
|
|
|
StreamPtr SoundManager::getStreamRef()
|
2017-09-12 04:33:18 +00:00
|
|
|
{
|
2020-06-28 15:17:43 +00:00
|
|
|
return mStreams.get();
|
2017-09-12 04:33:18 +00:00
|
|
|
}
|
|
|
|
|
2020-06-28 16:25:23 +00:00
|
|
|
StreamPtr SoundManager::playVoice(DecoderPtr decoder, const osg::Vec3f &pos, bool playlocal)
|
2015-11-27 17:47:14 +00:00
|
|
|
{
|
|
|
|
MWBase::World* world = MWBase::Environment::get().getWorld();
|
2018-08-29 15:38:12 +00:00
|
|
|
static const float fAudioMinDistanceMult = world->getStore().get<ESM::GameSetting>().find("fAudioMinDistanceMult")->mValue.getFloat();
|
|
|
|
static const float fAudioMaxDistanceMult = world->getStore().get<ESM::GameSetting>().find("fAudioMaxDistanceMult")->mValue.getFloat();
|
|
|
|
static const float fAudioVoiceDefaultMinDistance = world->getStore().get<ESM::GameSetting>().find("fAudioVoiceDefaultMinDistance")->mValue.getFloat();
|
|
|
|
static const float fAudioVoiceDefaultMaxDistance = world->getStore().get<ESM::GameSetting>().find("fAudioVoiceDefaultMaxDistance")->mValue.getFloat();
|
2015-11-27 17:47:14 +00:00
|
|
|
static float minDistance = std::max(fAudioVoiceDefaultMinDistance * fAudioMinDistanceMult, 1.0f);
|
|
|
|
static float maxDistance = std::max(fAudioVoiceDefaultMaxDistance * fAudioMaxDistanceMult, minDistance);
|
|
|
|
|
2017-09-14 10:41:11 +00:00
|
|
|
bool played;
|
2017-09-15 08:03:41 +00:00
|
|
|
float basevol = volumeFromType(Type::Voice);
|
2020-06-28 16:25:23 +00:00
|
|
|
StreamPtr sound = getStreamRef();
|
2015-11-27 17:47:14 +00:00
|
|
|
if(playlocal)
|
2015-12-02 16:04:30 +00:00
|
|
|
{
|
2020-07-12 13:25:14 +00:00
|
|
|
sound->init([&] {
|
|
|
|
SoundParams params;
|
|
|
|
params.mBaseVolume = basevol;
|
|
|
|
params.mFlags = PlayMode::NoEnv | Type::Voice | Play_2D;
|
|
|
|
return params;
|
|
|
|
} ());
|
2020-06-28 16:25:23 +00:00
|
|
|
played = mOutput->streamSound(decoder, sound.get(), true);
|
2015-12-02 16:04:30 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-12 13:25:14 +00:00
|
|
|
sound->init([&] {
|
|
|
|
SoundParams params;
|
|
|
|
params.mPos = pos;
|
|
|
|
params.mBaseVolume = basevol;
|
|
|
|
params.mMinDistance = minDistance;
|
|
|
|
params.mMaxDistance = maxDistance;
|
|
|
|
params.mFlags = PlayMode::Normal | Type::Voice | Play_3D;
|
|
|
|
return params;
|
|
|
|
} ());
|
2020-06-28 16:25:23 +00:00
|
|
|
played = mOutput->streamSound3D(decoder, sound.get(), true);
|
2017-09-14 10:41:11 +00:00
|
|
|
}
|
|
|
|
if(!played)
|
|
|
|
return nullptr;
|
2015-12-02 16:04:30 +00:00
|
|
|
return sound;
|
2015-11-27 17:47:14 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 10:01:04 +00:00
|
|
|
// Gets the combined volume settings for the given sound type
|
2017-09-15 08:03:41 +00:00
|
|
|
float SoundManager::volumeFromType(Type type) const
|
2012-12-18 10:01:04 +00:00
|
|
|
{
|
2020-07-01 16:37:31 +00:00
|
|
|
return mVolumeSettings.getVolumeFromType(type);
|
2012-12-18 10:01:04 +00:00
|
|
|
}
|
2010-07-03 13:04:00 +00:00
|
|
|
|
2012-03-09 01:56:29 +00:00
|
|
|
void SoundManager::stopMusic()
|
|
|
|
{
|
2012-03-17 09:45:18 +00:00
|
|
|
if(mMusic)
|
2017-09-12 04:33:18 +00:00
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->finishStream(mMusic.get());
|
2017-09-12 04:33:18 +00:00
|
|
|
mMusic = nullptr;
|
|
|
|
}
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
2011-06-12 20:36:18 +00:00
|
|
|
|
2012-03-20 19:39:49 +00:00
|
|
|
void SoundManager::streamMusicFull(const std::string& filename)
|
2011-06-15 20:33:31 +00:00
|
|
|
{
|
2012-04-06 17:43:14 +00:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
2018-08-14 19:05:43 +00:00
|
|
|
Log(Debug::Info) << "Playing " << filename;
|
2014-06-11 16:10:58 +00:00
|
|
|
mLastPlayedMusic = filename;
|
2012-12-13 06:32:02 +00:00
|
|
|
|
2017-09-14 10:41:11 +00:00
|
|
|
stopMusic();
|
2012-12-13 06:32:02 +00:00
|
|
|
|
2017-09-14 10:41:11 +00:00
|
|
|
DecoderPtr decoder = getDecoder();
|
|
|
|
decoder->open(filename);
|
|
|
|
|
|
|
|
mMusic = getStreamRef();
|
2020-07-12 13:25:14 +00:00
|
|
|
mMusic->init([&] {
|
|
|
|
SoundParams params;
|
|
|
|
params.mBaseVolume = volumeFromType(Type::Music);
|
|
|
|
params.mFlags = PlayMode::NoEnv | Type::Music | Play_2D;
|
|
|
|
return params;
|
|
|
|
} ());
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->streamSound(decoder, mMusic.get());
|
2011-01-05 21:18:21 +00:00
|
|
|
}
|
2010-11-12 00:47:26 +00:00
|
|
|
|
2017-08-06 18:10:56 +00:00
|
|
|
void SoundManager::advanceMusic(const std::string& filename)
|
|
|
|
{
|
|
|
|
if (!isMusicPlaying())
|
|
|
|
{
|
|
|
|
streamMusicFull(filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mNextMusic = filename;
|
|
|
|
|
2019-04-25 17:26:40 +00:00
|
|
|
mMusic->setFadeout(1.f);
|
2017-08-06 18:10:56 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 00:08:13 +00:00
|
|
|
void SoundManager::startRandomTitle()
|
2011-01-05 21:18:21 +00:00
|
|
|
{
|
2018-04-21 19:20:07 +00:00
|
|
|
const std::vector<std::string> &filelist = mMusicFiles[mCurrentPlaylist];
|
2017-08-25 20:08:49 +00:00
|
|
|
auto &tracklist = mMusicToPlay[mCurrentPlaylist];
|
2012-03-20 18:31:13 +00:00
|
|
|
|
2017-08-25 20:08:49 +00:00
|
|
|
// Do a Fisher-Yates shuffle
|
2017-08-20 20:20:27 +00:00
|
|
|
|
2017-08-25 20:08:49 +00:00
|
|
|
// Repopulate if playlist is empty
|
|
|
|
if(tracklist.empty())
|
2014-06-11 16:10:58 +00:00
|
|
|
{
|
2017-08-25 20:08:49 +00:00
|
|
|
tracklist.resize(filelist.size());
|
|
|
|
std::iota(tracklist.begin(), tracklist.end(), 0);
|
2014-06-11 16:10:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-25 20:08:49 +00: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 21:18:21 +00:00
|
|
|
}
|
2010-11-12 00:47:26 +00:00
|
|
|
|
2017-09-18 04:44:16 +00:00
|
|
|
|
|
|
|
void SoundManager::streamMusic(const std::string& filename)
|
|
|
|
{
|
|
|
|
advanceMusic("Music/"+filename);
|
|
|
|
}
|
|
|
|
|
2010-10-31 16:23:03 +00:00
|
|
|
bool SoundManager::isMusicPlaying()
|
2011-01-01 21:33:08 +00:00
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
return mMusic && mOutput->isStreamPlaying(mMusic.get());
|
2011-01-01 21:33:08 +00:00
|
|
|
}
|
2010-10-31 16:23:03 +00:00
|
|
|
|
2012-03-20 18:31:13 +00:00
|
|
|
void SoundManager::playPlaylist(const std::string &playlist)
|
2012-03-07 15:46:51 +00:00
|
|
|
{
|
2018-04-21 19:20:07 +00: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 18:31:13 +00:00
|
|
|
mCurrentPlaylist = playlist;
|
|
|
|
startRandomTitle();
|
2012-03-07 15:46:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-03 22:31:51 +00:00
|
|
|
void SoundManager::playTitleMusic()
|
|
|
|
{
|
|
|
|
if (mCurrentPlaylist == "Title")
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mMusicFiles.find("Title") == mMusicFiles.end())
|
|
|
|
{
|
|
|
|
std::vector<std::string> filelist;
|
|
|
|
const std::map<std::string, VFS::File*>& index = mVFS->getIndex();
|
|
|
|
// Is there an ini setting for this filename or something?
|
|
|
|
std::string filename = "music/special/morrowind title.mp3";
|
|
|
|
auto found = index.find(filename);
|
|
|
|
if (found != index.end())
|
|
|
|
{
|
|
|
|
filelist.emplace_back(found->first);
|
|
|
|
mMusicFiles["Title"] = filelist;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log(Debug::Warning) << "Title music not found";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mMusicFiles["Title"].empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
mCurrentPlaylist = "Title";
|
|
|
|
startRandomTitle();
|
|
|
|
}
|
2015-11-23 12:00:10 +00:00
|
|
|
|
2015-12-18 17:11:30 +00:00
|
|
|
void SoundManager::say(const MWWorld::ConstPtr &ptr, const std::string &filename)
|
2012-01-29 19:27:03 +00:00
|
|
|
{
|
2012-04-06 17:43:14 +00:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
2012-03-28 11:58:47 +00:00
|
|
|
|
2017-09-14 10:41:11 +00:00
|
|
|
std::string voicefile = "Sound/"+filename;
|
2015-11-24 04:13:24 +00:00
|
|
|
|
2017-09-14 10:41:11 +00:00
|
|
|
mVFS->normalizeFilename(voicefile);
|
|
|
|
DecoderPtr decoder = loadVoice(voicefile);
|
2018-10-18 10:59:39 +00:00
|
|
|
if (!decoder)
|
|
|
|
return;
|
2015-12-15 14:12:48 +00:00
|
|
|
|
2017-09-14 10:41:11 +00:00
|
|
|
MWBase::World *world = MWBase::Environment::get().getWorld();
|
|
|
|
const osg::Vec3f pos = world->getActorHeadTransform(ptr).getTrans();
|
|
|
|
|
|
|
|
stopSay(ptr);
|
2020-06-28 16:25:23 +00:00
|
|
|
StreamPtr sound = playVoice(decoder, pos, (ptr == MWMechanics::getPlayer()));
|
2017-09-14 10:41:11 +00:00
|
|
|
if(!sound) return;
|
|
|
|
|
2020-06-28 16:25:23 +00:00
|
|
|
mSaySoundsQueue.emplace(ptr, std::move(sound));
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 17:11:30 +00:00
|
|
|
float SoundManager::getSaySoundLoudness(const MWWorld::ConstPtr &ptr) const
|
2014-07-28 22:26:26 +00:00
|
|
|
{
|
2015-11-23 12:00:10 +00:00
|
|
|
SaySoundMap::const_iterator snditer = mActiveSaySounds.find(ptr);
|
|
|
|
if(snditer != mActiveSaySounds.end())
|
2014-07-28 22:26:26 +00:00
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
Stream *sound = snditer->second.get();
|
2016-06-27 18:54:42 +00:00
|
|
|
return mOutput->getStreamLoudness(sound);
|
2014-07-28 22:26:26 +00:00
|
|
|
}
|
|
|
|
|
2015-11-23 10:08:27 +00:00
|
|
|
return 0.0f;
|
2014-07-28 22:26:26 +00:00
|
|
|
}
|
|
|
|
|
2012-05-02 03:30:31 +00:00
|
|
|
void SoundManager::say(const std::string& filename)
|
|
|
|
{
|
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
|
|
|
|
2017-09-14 10:41:11 +00:00
|
|
|
std::string voicefile = "Sound/"+filename;
|
2015-11-24 04:13:24 +00:00
|
|
|
|
2017-09-14 10:41:11 +00:00
|
|
|
mVFS->normalizeFilename(voicefile);
|
|
|
|
DecoderPtr decoder = loadVoice(voicefile);
|
2018-10-18 10:59:39 +00:00
|
|
|
if (!decoder)
|
|
|
|
return;
|
2017-09-14 10:41:11 +00:00
|
|
|
|
|
|
|
stopSay(MWWorld::ConstPtr());
|
2020-06-28 16:25:23 +00:00
|
|
|
StreamPtr sound = playVoice(decoder, osg::Vec3f(), true);
|
2017-09-14 10:41:11 +00:00
|
|
|
if(!sound) return;
|
|
|
|
|
2020-06-28 16:25:23 +00:00
|
|
|
mActiveSaySounds.emplace(MWWorld::ConstPtr(), std::move(sound));
|
2012-05-02 03:30:31 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 17:11:30 +00:00
|
|
|
bool SoundManager::sayDone(const MWWorld::ConstPtr &ptr) const
|
2012-03-17 00:08:13 +00:00
|
|
|
{
|
2015-11-23 12:00:10 +00:00
|
|
|
SaySoundMap::const_iterator snditer = mActiveSaySounds.find(ptr);
|
|
|
|
if(snditer != mActiveSaySounds.end())
|
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
if(mOutput->isStreamPlaying(snditer->second.get()))
|
2015-11-23 12:00:10 +00:00
|
|
|
return false;
|
2015-11-27 17:47:14 +00:00
|
|
|
return true;
|
2015-11-23 12:00:10 +00:00
|
|
|
}
|
2016-06-27 18:54:42 +00:00
|
|
|
return true;
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
2019-09-06 05:19:41 +00:00
|
|
|
bool SoundManager::sayActive(const MWWorld::ConstPtr &ptr) const
|
|
|
|
{
|
|
|
|
SaySoundMap::const_iterator snditer = mSaySoundsQueue.find(ptr);
|
|
|
|
if(snditer != mSaySoundsQueue.end())
|
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
if(mOutput->isStreamPlaying(snditer->second.get()))
|
2019-09-06 05:19:41 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
snditer = mActiveSaySounds.find(ptr);
|
|
|
|
if(snditer != mActiveSaySounds.end())
|
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
if(mOutput->isStreamPlaying(snditer->second.get()))
|
2019-09-06 05:19:41 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-18 17:11:30 +00:00
|
|
|
void SoundManager::stopSay(const MWWorld::ConstPtr &ptr)
|
2012-05-02 03:30:31 +00:00
|
|
|
{
|
2019-05-26 11:55:29 +00:00
|
|
|
SaySoundMap::iterator snditer = mSaySoundsQueue.find(ptr);
|
|
|
|
if(snditer != mSaySoundsQueue.end())
|
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->finishStream(snditer->second.get());
|
2019-05-26 11:55:29 +00:00
|
|
|
mSaySoundsQueue.erase(snditer);
|
|
|
|
}
|
|
|
|
|
|
|
|
snditer = mActiveSaySounds.find(ptr);
|
2015-11-23 12:00:10 +00:00
|
|
|
if(snditer != mActiveSaySounds.end())
|
2012-05-02 03:30:31 +00:00
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->finishStream(snditer->second.get());
|
2015-11-23 12:00:10 +00:00
|
|
|
mActiveSaySounds.erase(snditer);
|
2012-05-02 03:30:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-15 08:03:41 +00:00
|
|
|
Stream *SoundManager::playTrack(const DecoderPtr& decoder, Type type)
|
2012-12-13 08:05:57 +00:00
|
|
|
{
|
|
|
|
if(!mOutput->isInitialized())
|
2017-09-12 04:33:18 +00:00
|
|
|
return nullptr;
|
2015-12-02 16:04:30 +00:00
|
|
|
|
2020-06-28 16:25:23 +00:00
|
|
|
StreamPtr track = getStreamRef();
|
2020-07-12 13:25:14 +00:00
|
|
|
track->init([&] {
|
|
|
|
SoundParams params;
|
|
|
|
params.mBaseVolume = volumeFromType(type);
|
|
|
|
params.mFlags = PlayMode::NoEnv | type | Play_2D;
|
|
|
|
return params;
|
|
|
|
} ());
|
2020-06-28 16:25:23 +00:00
|
|
|
if(!mOutput->streamSound(decoder, track.get()))
|
2017-09-14 10:41:11 +00:00
|
|
|
return nullptr;
|
|
|
|
|
2020-06-28 16:25:23 +00:00
|
|
|
Stream* result = track.get();
|
|
|
|
const auto it = std::lower_bound(mActiveTracks.begin(), mActiveTracks.end(), track);
|
|
|
|
mActiveTracks.insert(it, std::move(track));
|
|
|
|
return result;
|
2012-12-13 08:05:57 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 04:33:18 +00:00
|
|
|
void SoundManager::stopTrack(Stream *stream)
|
2015-11-30 13:42:51 +00:00
|
|
|
{
|
2015-12-11 23:13:14 +00:00
|
|
|
mOutput->finishStream(stream);
|
2020-06-28 16:25:23 +00:00
|
|
|
TrackList::iterator iter = std::lower_bound(mActiveTracks.begin(), mActiveTracks.end(), stream,
|
|
|
|
[] (const StreamPtr& lhs, Stream* rhs) { return lhs.get() < rhs; });
|
|
|
|
if(iter != mActiveTracks.end() && iter->get() == stream)
|
2015-12-01 19:28:37 +00:00
|
|
|
mActiveTracks.erase(iter);
|
2015-11-30 13:42:51 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 04:33:18 +00:00
|
|
|
double SoundManager::getTrackTimeDelay(Stream *stream)
|
2015-11-30 13:42:51 +00:00
|
|
|
{
|
2015-11-30 15:32:42 +00:00
|
|
|
return mOutput->getStreamDelay(stream);
|
2015-11-30 13:42:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 00:08:13 +00:00
|
|
|
|
2020-06-28 16:25:23 +00:00
|
|
|
Sound* SoundManager::playSound(const std::string& soundId, float volume, float pitch, Type type, PlayMode mode, float offset)
|
2012-03-17 00:08:13 +00:00
|
|
|
{
|
2012-04-06 17:43:14 +00:00
|
|
|
if(!mOutput->isInitialized())
|
2017-09-12 04:33:18 +00:00
|
|
|
return nullptr;
|
2012-03-28 11:58:47 +00:00
|
|
|
|
2017-09-14 10:41:11 +00:00
|
|
|
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
|
|
|
|
if(!sfx) return nullptr;
|
|
|
|
|
2018-05-01 12:21:58 +00:00
|
|
|
// Only one copy of given sound can be played at time, so stop previous copy
|
2018-05-07 16:40:53 +00:00
|
|
|
stopSound(sfx, MWWorld::ConstPtr());
|
2018-05-01 12:21:58 +00:00
|
|
|
|
2020-06-28 16:25:23 +00:00
|
|
|
SoundPtr sound = getSoundRef();
|
2020-07-12 13:25:14 +00:00
|
|
|
sound->init([&] {
|
|
|
|
SoundParams params;
|
|
|
|
params.mVolume = volume * sfx->mVolume;
|
|
|
|
params.mBaseVolume = volumeFromType(type);
|
|
|
|
params.mPitch = pitch;
|
|
|
|
params.mFlags = mode | type | Play_2D;
|
|
|
|
return params;
|
|
|
|
} ());
|
2020-06-28 16:25:23 +00:00
|
|
|
if(!mOutput->playSound(sound.get(), sfx->mHandle, offset))
|
2017-09-14 10:41:11 +00:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if(sfx->mUses++ == 0)
|
2012-03-22 01:20:32 +00:00
|
|
|
{
|
2017-09-14 10:41:11 +00:00
|
|
|
SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfx);
|
|
|
|
if(iter != mUnusedBuffers.end())
|
|
|
|
mUnusedBuffers.erase(iter);
|
2012-03-17 13:51:44 +00:00
|
|
|
}
|
2020-06-28 16:25:23 +00:00
|
|
|
Sound* result = sound.get();
|
|
|
|
mActiveSounds[MWWorld::ConstPtr()].emplace_back(std::move(sound), sfx);
|
|
|
|
return result;
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 04:33:18 +00:00
|
|
|
Sound *SoundManager::playSound3D(const MWWorld::ConstPtr &ptr, const std::string& soundId,
|
2017-09-15 08:03:41 +00:00
|
|
|
float volume, float pitch, Type type, PlayMode mode,
|
2017-09-12 04:33:18 +00:00
|
|
|
float offset)
|
2012-03-17 00:08:13 +00:00
|
|
|
{
|
2012-04-06 17:43:14 +00:00
|
|
|
if(!mOutput->isInitialized())
|
2017-09-12 04:33:18 +00:00
|
|
|
return nullptr;
|
2012-03-22 02:08:11 +00:00
|
|
|
|
2020-06-28 17:59:53 +00:00
|
|
|
const osg::Vec3f objpos(ptr.getRefData().getPosition().asVec3());
|
|
|
|
if ((mode & PlayMode::RemoveAtDistance) && (mListenerPos - objpos).length2() > 2000 * 2000)
|
|
|
|
return nullptr;
|
|
|
|
|
2017-09-14 10:41:11 +00:00
|
|
|
// Look up the sound in the ESM data
|
|
|
|
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
|
|
|
|
if(!sfx) return nullptr;
|
2014-12-08 22:26:54 +00:00
|
|
|
|
2017-09-14 10:41:11 +00:00
|
|
|
// Only one copy of given sound can be played at time on ptr, so stop previous copy
|
2018-05-07 16:40:53 +00:00
|
|
|
stopSound(sfx, ptr);
|
2017-09-14 10:41:11 +00:00
|
|
|
|
|
|
|
bool played;
|
2020-06-28 16:25:23 +00:00
|
|
|
SoundPtr sound = getSoundRef();
|
2017-09-15 08:03:41 +00:00
|
|
|
if(!(mode&PlayMode::NoPlayerLocal) && ptr == MWMechanics::getPlayer())
|
2017-09-14 10:41:11 +00:00
|
|
|
{
|
2020-07-12 13:25:14 +00:00
|
|
|
sound->init([&] {
|
|
|
|
SoundParams params;
|
|
|
|
params.mVolume = volume * sfx->mVolume;
|
|
|
|
params.mBaseVolume = volumeFromType(type);
|
|
|
|
params.mPitch = pitch;
|
|
|
|
params.mFlags = mode | type | Play_2D;
|
|
|
|
return params;
|
|
|
|
} ());
|
2020-06-28 16:25:23 +00:00
|
|
|
played = mOutput->playSound(sound.get(), sfx->mHandle, offset);
|
2017-09-14 10:41:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-12 13:25:14 +00:00
|
|
|
sound->init([&] {
|
|
|
|
SoundParams params;
|
|
|
|
params.mPos = objpos;
|
|
|
|
params.mVolume = volume * sfx->mVolume;
|
|
|
|
params.mBaseVolume = volumeFromType(type);
|
|
|
|
params.mPitch = pitch;
|
|
|
|
params.mMinDistance = sfx->mMinDist;
|
|
|
|
params.mMaxDistance = sfx->mMaxDist;
|
|
|
|
params.mFlags = mode | type | Play_3D;
|
|
|
|
return params;
|
|
|
|
} ());
|
2020-06-28 16:25:23 +00:00
|
|
|
played = mOutput->playSound3D(sound.get(), sfx->mHandle, offset);
|
2012-03-22 01:20:32 +00:00
|
|
|
}
|
2017-09-14 10:41:11 +00:00
|
|
|
if(!played)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if(sfx->mUses++ == 0)
|
|
|
|
{
|
|
|
|
SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfx);
|
|
|
|
if(iter != mUnusedBuffers.end())
|
|
|
|
mUnusedBuffers.erase(iter);
|
|
|
|
}
|
2020-06-28 16:25:23 +00:00
|
|
|
Sound* result = sound.get();
|
|
|
|
mActiveSounds[ptr].emplace_back(std::move(sound), sfx);
|
|
|
|
return result;
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 04:33:18 +00:00
|
|
|
Sound *SoundManager::playSound3D(const osg::Vec3f& initialPos, const std::string& soundId,
|
2017-09-15 08:03:41 +00:00
|
|
|
float volume, float pitch, Type type, PlayMode mode,
|
2017-09-12 04:33:18 +00:00
|
|
|
float offset)
|
2014-05-16 11:09:23 +00:00
|
|
|
{
|
|
|
|
if(!mOutput->isInitialized())
|
2017-09-12 04:33:18 +00:00
|
|
|
return nullptr;
|
2014-05-16 11:09:23 +00:00
|
|
|
|
2017-09-14 10:41:11 +00:00
|
|
|
// Look up the sound in the ESM data
|
|
|
|
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
|
|
|
|
if(!sfx) return nullptr;
|
|
|
|
|
2020-06-28 16:25:23 +00:00
|
|
|
SoundPtr sound = getSoundRef();
|
2020-07-12 13:25:14 +00:00
|
|
|
sound->init([&] {
|
|
|
|
SoundParams params;
|
|
|
|
params.mPos = initialPos;
|
|
|
|
params.mVolume = volume * sfx->mVolume;
|
|
|
|
params.mBaseVolume = volumeFromType(type);
|
|
|
|
params.mPitch = pitch;
|
|
|
|
params.mMinDistance = sfx->mMinDist;
|
|
|
|
params.mMaxDistance = sfx->mMaxDist;
|
|
|
|
params.mFlags = mode | type | Play_3D;
|
|
|
|
return params;
|
|
|
|
} ());
|
2020-06-28 16:25:23 +00:00
|
|
|
if(!mOutput->playSound3D(sound.get(), sfx->mHandle, offset))
|
2017-09-14 10:41:11 +00:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if(sfx->mUses++ == 0)
|
2014-05-16 11:09:23 +00:00
|
|
|
{
|
2017-09-14 10:41:11 +00:00
|
|
|
SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfx);
|
|
|
|
if(iter != mUnusedBuffers.end())
|
|
|
|
mUnusedBuffers.erase(iter);
|
2014-05-16 11:09:23 +00:00
|
|
|
}
|
2020-06-28 16:25:23 +00:00
|
|
|
Sound* result = sound.get();
|
|
|
|
mActiveSounds[MWWorld::ConstPtr()].emplace_back(std::move(sound), sfx);
|
|
|
|
return result;
|
2014-05-16 11:09:23 +00:00
|
|
|
}
|
|
|
|
|
2017-09-12 04:33:18 +00:00
|
|
|
void SoundManager::stopSound(Sound *sound)
|
2015-11-30 13:42:51 +00:00
|
|
|
{
|
2017-09-12 04:33:18 +00:00
|
|
|
if(sound)
|
2015-12-11 23:13:14 +00:00
|
|
|
mOutput->finishSound(sound);
|
2015-11-30 13:42:51 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 16:40:53 +00:00
|
|
|
void SoundManager::stopSound(Sound_Buffer *sfx, const MWWorld::ConstPtr &ptr)
|
2012-03-17 00:08:13 +00:00
|
|
|
{
|
2015-11-23 11:07:04 +00:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.find(ptr);
|
|
|
|
if(snditer != mActiveSounds.end())
|
2012-03-28 10:48:51 +00:00
|
|
|
{
|
2017-09-16 08:56:58 +00:00
|
|
|
for(SoundBufferRefPair &snd : snditer->second)
|
2012-03-28 10:48:51 +00:00
|
|
|
{
|
2017-09-16 08:56:58 +00:00
|
|
|
if(snd.second == sfx)
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->finishSound(snd.first.get());
|
2012-03-28 10:48:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 10:20:50 +00:00
|
|
|
}
|
|
|
|
|
2018-05-07 16:40:53 +00:00
|
|
|
void SoundManager::stopSound3D(const MWWorld::ConstPtr &ptr, const std::string& soundId)
|
|
|
|
{
|
2019-07-04 20:48:33 +00:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
|
|
|
|
2020-06-28 20:16:45 +00:00
|
|
|
Sound_Buffer *sfx = lookupSound(Misc::StringUtils::lowerCase(soundId));
|
2018-05-07 16:40:53 +00:00
|
|
|
if (!sfx) return;
|
|
|
|
|
|
|
|
stopSound(sfx, ptr);
|
|
|
|
}
|
|
|
|
|
2015-12-18 17:11:30 +00:00
|
|
|
void SoundManager::stopSound3D(const MWWorld::ConstPtr &ptr)
|
2012-03-27 10:20:50 +00:00
|
|
|
{
|
2015-11-23 11:07:04 +00:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.find(ptr);
|
|
|
|
if(snditer != mActiveSounds.end())
|
2012-03-20 14:22:17 +00:00
|
|
|
{
|
2017-09-16 08:56:58 +00:00
|
|
|
for(SoundBufferRefPair &snd : snditer->second)
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->finishSound(snd.first.get());
|
2012-03-20 14:22:17 +00:00
|
|
|
}
|
2019-05-26 16:23:42 +00:00
|
|
|
SaySoundMap::iterator sayiter = mSaySoundsQueue.find(ptr);
|
|
|
|
if(sayiter != mSaySoundsQueue.end())
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->finishStream(sayiter->second.get());
|
2019-05-26 16:23:42 +00:00
|
|
|
sayiter = mActiveSaySounds.find(ptr);
|
2017-09-16 08:56:58 +00:00
|
|
|
if(sayiter != mActiveSaySounds.end())
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->finishStream(sayiter->second.get());
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 12:21:26 +00:00
|
|
|
void SoundManager::stopSound(const MWWorld::CellStore *cell)
|
2012-03-17 00:08:13 +00:00
|
|
|
{
|
2017-09-16 08:56:58 +00:00
|
|
|
for(SoundMap::value_type &snd : mActiveSounds)
|
2012-03-18 05:13:57 +00:00
|
|
|
{
|
2017-09-16 08:56:58 +00:00
|
|
|
if(!snd.first.isEmpty() && snd.first != MWMechanics::getPlayer() && snd.first.getCell() == cell)
|
2012-03-20 14:22:17 +00:00
|
|
|
{
|
2017-09-16 08:56:58 +00:00
|
|
|
for(SoundBufferRefPair &sndbuf : snd.second)
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->finishSound(sndbuf.first.get());
|
2012-03-20 14:22:17 +00:00
|
|
|
}
|
2012-03-18 05:13:57 +00:00
|
|
|
}
|
2018-05-07 16:40:53 +00:00
|
|
|
|
2019-05-26 16:23:42 +00:00
|
|
|
for(SaySoundMap::value_type &snd : mSaySoundsQueue)
|
|
|
|
{
|
|
|
|
if(!snd.first.isEmpty() && snd.first != MWMechanics::getPlayer() && snd.first.getCell() == cell)
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->finishStream(snd.second.get());
|
2019-05-26 16:23:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-16 08:56:58 +00:00
|
|
|
for(SaySoundMap::value_type &snd : mActiveSaySounds)
|
2015-11-23 12:00:10 +00:00
|
|
|
{
|
2017-09-16 08:56:58 +00:00
|
|
|
if(!snd.first.isEmpty() && snd.first != MWMechanics::getPlayer() && snd.first.getCell() == cell)
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->finishStream(snd.second.get());
|
2015-11-23 12:00:10 +00:00
|
|
|
}
|
2012-01-29 19:27:03 +00:00
|
|
|
}
|
2010-08-16 16:06:27 +00:00
|
|
|
|
2015-12-18 17:11:30 +00:00
|
|
|
void SoundManager::fadeOutSound3D(const MWWorld::ConstPtr &ptr,
|
2013-07-26 16:43:06 +00:00
|
|
|
const std::string& soundId, float duration)
|
|
|
|
{
|
2015-11-23 11:07:04 +00:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.find(ptr);
|
|
|
|
if(snditer != mActiveSounds.end())
|
2013-07-26 16:43:06 +00:00
|
|
|
{
|
2020-06-28 22:05:25 +00:00
|
|
|
Sound_Buffer *sfx = lookupSound(Misc::StringUtils::lowerCase(soundId));
|
|
|
|
if (sfx == nullptr)
|
|
|
|
return;
|
2017-09-16 08:56:58 +00:00
|
|
|
for(SoundBufferRefPair &sndbuf : snditer->second)
|
2013-07-26 16:43:06 +00:00
|
|
|
{
|
2017-09-16 08:56:58 +00:00
|
|
|
if(sndbuf.second == sfx)
|
|
|
|
sndbuf.first->setFadeout(duration);
|
2013-07-26 16:43:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-18 17:11:30 +00:00
|
|
|
bool SoundManager::getSoundPlaying(const MWWorld::ConstPtr &ptr, const std::string& soundId) const
|
2012-03-17 00:08:13 +00:00
|
|
|
{
|
2015-11-23 12:10:56 +00:00
|
|
|
SoundMap::const_iterator snditer = mActiveSounds.find(ptr);
|
|
|
|
if(snditer != mActiveSounds.end())
|
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
Sound_Buffer *sfx = lookupSound(Misc::StringUtils::lowerCase(soundId));
|
2017-09-16 08:56:58 +00:00
|
|
|
return std::find_if(snditer->second.cbegin(), snditer->second.cend(),
|
|
|
|
[this,sfx](const SoundBufferRefPair &snd) -> bool
|
2020-06-28 16:25:23 +00:00
|
|
|
{ return snd.second == sfx && mOutput->isSoundPlaying(snd.first.get()); }
|
2017-09-16 08:56:58 +00:00
|
|
|
) != snditer->second.cend();
|
2015-11-23 12:10:56 +00:00
|
|
|
}
|
|
|
|
return false;
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
2010-08-16 16:06:27 +00:00
|
|
|
|
2020-04-05 14:10:05 +00:00
|
|
|
void SoundManager::pauseSounds(BlockerType blocker, int types)
|
2012-12-12 10:33:12 +00:00
|
|
|
{
|
|
|
|
if(mOutput->isInitialized())
|
2012-12-18 12:35:24 +00:00
|
|
|
{
|
2020-04-05 14:10:05 +00:00
|
|
|
if (mPausedSoundTypes[blocker] != 0)
|
|
|
|
resumeSounds(blocker);
|
2020-03-31 08:29:58 +00:00
|
|
|
|
2017-09-15 08:03:41 +00:00
|
|
|
types = types & Type::Mask;
|
2012-12-18 12:19:35 +00:00
|
|
|
mOutput->pauseSounds(types);
|
2020-04-05 14:10:05 +00:00
|
|
|
mPausedSoundTypes[blocker] = types;
|
2012-12-18 12:35:24 +00:00
|
|
|
}
|
2012-12-12 10:33:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 14:10:05 +00:00
|
|
|
void SoundManager::resumeSounds(BlockerType blocker)
|
2012-12-12 10:33:12 +00:00
|
|
|
{
|
|
|
|
if(mOutput->isInitialized())
|
2012-12-18 12:35:24 +00:00
|
|
|
{
|
2020-04-05 14:10:05 +00:00
|
|
|
mPausedSoundTypes[blocker] = 0;
|
2020-03-31 08:29:58 +00:00
|
|
|
int types = int(Type::Mask);
|
2020-04-05 14:10:05 +00:00
|
|
|
for (int currentBlocker = 0; currentBlocker < BlockerType::MaxCount; currentBlocker++)
|
2020-03-31 08:29:58 +00:00
|
|
|
{
|
2020-04-05 14:10:05 +00:00
|
|
|
if (currentBlocker != blocker)
|
|
|
|
types &= ~mPausedSoundTypes[currentBlocker];
|
2020-03-31 08:29:58 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 12:19:35 +00:00
|
|
|
mOutput->resumeSounds(types);
|
2012-12-18 12:35:24 +00:00
|
|
|
}
|
2012-12-12 10:33:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-31 09:15:26 +00:00
|
|
|
void SoundManager::pausePlayback()
|
|
|
|
{
|
|
|
|
if (mPlaybackPaused)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mPlaybackPaused = true;
|
|
|
|
mOutput->pauseActiveDevice();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundManager::resumePlayback()
|
|
|
|
{
|
|
|
|
if (!mPlaybackPaused)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mPlaybackPaused = false;
|
|
|
|
mOutput->resumeActiveDevice();
|
|
|
|
}
|
|
|
|
|
2012-03-17 13:18:59 +00:00
|
|
|
void SoundManager::updateRegionSound(float duration)
|
2012-03-17 00:08:13 +00:00
|
|
|
{
|
2013-08-27 23:04:19 +00:00
|
|
|
MWBase::World *world = MWBase::Environment::get().getWorld();
|
2015-12-18 17:11:30 +00:00
|
|
|
const MWWorld::ConstPtr player = world->getPlayerPtr();
|
2014-02-21 10:35:46 +00:00
|
|
|
const ESM::Cell *cell = player.getCell()->getCell();
|
2011-10-09 07:28:36 +00:00
|
|
|
|
2020-06-28 11:12:42 +00:00
|
|
|
if (!cell->isExterior())
|
2012-03-17 00:08:13 +00:00
|
|
|
return;
|
2020-10-25 11:20:14 +00:00
|
|
|
if (mCurrentRegionSound && mOutput->isSoundPlaying(mCurrentRegionSound))
|
|
|
|
return;
|
2012-03-21 22:19:40 +00:00
|
|
|
|
2020-06-28 11:12:42 +00:00
|
|
|
if (const auto next = mRegionSoundSelector.getNextRandom(duration, cell->mRegion, *world))
|
2020-10-25 11:20:14 +00:00
|
|
|
mCurrentRegionSound = playSound(*next, 1.0f, 1.0f);
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
2012-03-17 13:18:59 +00:00
|
|
|
|
2020-06-28 11:50:02 +00:00
|
|
|
void SoundManager::updateWaterSound()
|
2016-11-27 20:19:52 +00:00
|
|
|
{
|
|
|
|
MWBase::World* world = MWBase::Environment::get().getWorld();
|
|
|
|
const MWWorld::ConstPtr player = world->getPlayerPtr();
|
2017-09-12 09:48:10 +00:00
|
|
|
const ESM::Cell *curcell = player.getCell()->getCell();
|
2020-06-28 12:41:05 +00:00
|
|
|
const auto update = mWaterSoundUpdater.update(player, *world);
|
2016-11-27 20:19:52 +00:00
|
|
|
|
2020-06-28 13:38:30 +00:00
|
|
|
WaterSoundAction action;
|
|
|
|
Sound_Buffer* sfx;
|
|
|
|
std::tie(action, sfx) = getWaterSoundAction(update, curcell);
|
|
|
|
|
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case WaterSoundAction::DoNothing:
|
|
|
|
break;
|
|
|
|
case WaterSoundAction::SetVolume:
|
|
|
|
mNearWaterSound->setVolume(update.mVolume * sfx->mVolume);
|
|
|
|
break;
|
|
|
|
case WaterSoundAction::FinishSound:
|
|
|
|
mOutput->finishSound(mNearWaterSound);
|
|
|
|
mNearWaterSound = nullptr;
|
|
|
|
break;
|
|
|
|
case WaterSoundAction::PlaySound:
|
|
|
|
if (mNearWaterSound)
|
|
|
|
mOutput->finishSound(mNearWaterSound);
|
|
|
|
mNearWaterSound = playSound(update.mId, update.mVolume, 1.0f, Type::Sfx, PlayMode::Loop);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLastCell = curcell;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<SoundManager::WaterSoundAction, Sound_Buffer*> SoundManager::getWaterSoundAction(
|
|
|
|
const WaterSoundUpdate& update, const ESM::Cell* cell) const
|
|
|
|
{
|
2016-11-27 20:19:52 +00:00
|
|
|
if (mNearWaterSound)
|
|
|
|
{
|
2020-06-28 12:41:05 +00:00
|
|
|
if (update.mVolume == 0.0f)
|
2020-06-28 13:38:30 +00:00
|
|
|
return {WaterSoundAction::FinishSound, nullptr};
|
2016-11-27 20:19:52 +00:00
|
|
|
|
2020-06-28 13:38:30 +00:00
|
|
|
bool soundIdChanged = false;
|
2016-11-27 20:19:52 +00:00
|
|
|
|
2020-06-28 13:38:30 +00:00
|
|
|
Sound_Buffer* sfx = lookupSound(update.mId);
|
|
|
|
if (mLastCell != cell)
|
|
|
|
{
|
|
|
|
const auto snditer = mActiveSounds.find(MWWorld::ConstPtr());
|
|
|
|
if (snditer != mActiveSounds.end())
|
2016-11-27 20:19:52 +00:00
|
|
|
{
|
2020-06-28 13:38:30 +00:00
|
|
|
const auto pairiter = std::find_if(
|
|
|
|
snditer->second.begin(), snditer->second.end(),
|
|
|
|
[this](const SoundBufferRefPairList::value_type &item) -> bool
|
2020-06-28 16:25:23 +00:00
|
|
|
{ return mNearWaterSound == item.first.get(); }
|
2020-06-28 13:38:30 +00:00
|
|
|
);
|
|
|
|
if (pairiter != snditer->second.end() && pairiter->second != sfx)
|
|
|
|
soundIdChanged = true;
|
2016-11-27 20:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-28 13:38:30 +00:00
|
|
|
|
|
|
|
if (soundIdChanged)
|
|
|
|
return {WaterSoundAction::PlaySound, nullptr};
|
|
|
|
|
|
|
|
if (sfx)
|
|
|
|
return {WaterSoundAction::SetVolume, sfx};
|
2016-11-27 20:19:52 +00:00
|
|
|
}
|
2020-06-28 12:41:05 +00:00
|
|
|
else if (update.mVolume > 0.0f)
|
2020-06-28 13:38:30 +00:00
|
|
|
return {WaterSoundAction::PlaySound, nullptr};
|
|
|
|
|
|
|
|
return {WaterSoundAction::DoNothing, nullptr};
|
2016-11-27 20:19:52 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 02:21:36 +00:00
|
|
|
void SoundManager::updateSounds(float duration)
|
2012-03-17 13:18:59 +00:00
|
|
|
{
|
2019-05-26 15:46:53 +00:00
|
|
|
// We update active say sounds map for specific actors here
|
|
|
|
// because for vanilla compatibility we can't do it immediately.
|
|
|
|
SaySoundMap::iterator queuesayiter = mSaySoundsQueue.begin();
|
|
|
|
while (queuesayiter != mSaySoundsQueue.end())
|
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
const auto dst = mActiveSaySounds.find(queuesayiter->first);
|
|
|
|
if (dst == mActiveSaySounds.end())
|
|
|
|
mActiveSaySounds.emplace(queuesayiter->first, std::move(queuesayiter->second));
|
|
|
|
else
|
|
|
|
dst->second = std::move(queuesayiter->second);
|
2019-05-26 15:46:53 +00:00
|
|
|
mSaySoundsQueue.erase(queuesayiter++);
|
|
|
|
}
|
|
|
|
|
2020-06-28 11:49:05 +00:00
|
|
|
mTimePassed += duration;
|
|
|
|
if (mTimePassed < sMinUpdateInterval)
|
2012-03-22 02:21:36 +00:00
|
|
|
return;
|
2020-06-28 11:49:05 +00:00
|
|
|
duration = mTimePassed;
|
|
|
|
mTimePassed = 0.0f;
|
2012-03-22 02:21:36 +00:00
|
|
|
|
|
|
|
// Make sure music is still playing
|
2016-06-06 23:36:09 +00:00
|
|
|
if(!isMusicPlaying() && !mCurrentPlaylist.empty())
|
2012-03-22 02:21:36 +00:00
|
|
|
startRandomTitle();
|
|
|
|
|
2012-03-31 17:06:12 +00:00
|
|
|
Environment env = Env_Normal;
|
2014-06-25 16:10:26 +00:00
|
|
|
if (mListenerUnderwater)
|
2012-03-31 17:06:12 +00:00
|
|
|
env = Env_Underwater;
|
2013-08-27 20:48:20 +00:00
|
|
|
else if(mUnderwaterSound)
|
2013-08-08 17:16:05 +00:00
|
|
|
{
|
2015-12-11 23:13:14 +00:00
|
|
|
mOutput->finishSound(mUnderwaterSound);
|
2017-09-12 04:33:18 +00:00
|
|
|
mUnderwaterSound = nullptr;
|
2013-08-08 17:16:05 +00:00
|
|
|
}
|
2012-03-31 17:06:12 +00:00
|
|
|
|
2015-11-25 04:37:38 +00:00
|
|
|
mOutput->startUpdate();
|
2012-08-09 13:01:03 +00:00
|
|
|
mOutput->updateListener(
|
|
|
|
mListenerPos,
|
|
|
|
mListenerDir,
|
2012-10-01 00:23:05 +00:00
|
|
|
mListenerUp,
|
2012-08-09 13:01:03 +00:00
|
|
|
env
|
|
|
|
);
|
2012-03-22 02:21:36 +00:00
|
|
|
|
2017-08-08 01:31:01 +00:00
|
|
|
updateMusic(duration);
|
|
|
|
|
2012-03-22 02:21:36 +00:00
|
|
|
// Check if any sounds are finished playing, and trash them
|
|
|
|
SoundMap::iterator snditer = mActiveSounds.begin();
|
|
|
|
while(snditer != mActiveSounds.end())
|
2012-03-17 13:18:59 +00:00
|
|
|
{
|
2017-09-13 09:47:26 +00:00
|
|
|
MWWorld::ConstPtr ptr = snditer->first;
|
2015-11-26 09:26:33 +00:00
|
|
|
SoundBufferRefPairList::iterator sndidx = snditer->second.begin();
|
2015-11-24 11:18:23 +00:00
|
|
|
while(sndidx != snditer->second.end())
|
2012-03-30 14:01:37 +00:00
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
Sound *sound = sndidx->first.get();
|
|
|
|
Sound_Buffer *sfx = sndidx->second;
|
2017-09-13 09:47:26 +00:00
|
|
|
|
2015-11-30 13:42:51 +00:00
|
|
|
if(!ptr.isEmpty() && sound->getIs3D())
|
2015-11-23 14:35:01 +00:00
|
|
|
{
|
2015-11-30 13:42:51 +00: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 23:13:14 +00:00
|
|
|
mOutput->finishSound(sound);
|
2015-11-30 13:42:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!mOutput->isSoundPlaying(sound))
|
2015-11-23 14:35:01 +00:00
|
|
|
{
|
2015-12-11 23:13:14 +00:00
|
|
|
mOutput->finishSound(sound);
|
2020-06-28 16:25:23 +00:00
|
|
|
if (sound == mUnderwaterSound)
|
2017-09-12 04:33:18 +00:00
|
|
|
mUnderwaterSound = nullptr;
|
2020-06-28 16:25:23 +00:00
|
|
|
if (sound == mNearWaterSound)
|
2017-09-12 04:33:18 +00:00
|
|
|
mNearWaterSound = nullptr;
|
2015-11-25 11:34:44 +00:00
|
|
|
if(sfx->mUses-- == 1)
|
2015-11-26 09:26:33 +00:00
|
|
|
mUnusedBuffers.push_front(sfx);
|
2015-11-24 11:18:23 +00:00
|
|
|
sndidx = snditer->second.erase(sndidx);
|
2015-11-23 14:35:01 +00:00
|
|
|
}
|
2015-11-23 12:31:15 +00:00
|
|
|
else
|
2015-11-30 13:42:51 +00:00
|
|
|
{
|
|
|
|
sound->updateFade(duration);
|
|
|
|
|
2015-11-30 22:34:14 +00:00
|
|
|
mOutput->updateSound(sound);
|
2015-11-24 11:18:23 +00:00
|
|
|
++sndidx;
|
2015-11-30 13:42:51 +00:00
|
|
|
}
|
2012-03-30 14:01:37 +00:00
|
|
|
}
|
2015-11-23 11:07:04 +00:00
|
|
|
if(snditer->second.empty())
|
2017-09-13 09:47:26 +00:00
|
|
|
snditer = mActiveSounds.erase(snditer);
|
2015-11-23 11:07:04 +00:00
|
|
|
else
|
|
|
|
++snditer;
|
2012-03-17 13:18:59 +00:00
|
|
|
}
|
2015-11-23 12:00:10 +00:00
|
|
|
|
2019-05-26 15:46:53 +00:00
|
|
|
SaySoundMap::iterator sayiter = mActiveSaySounds.begin();
|
2019-05-26 16:23:42 +00:00
|
|
|
while(sayiter != mActiveSaySounds.end())
|
2015-11-23 12:00:10 +00:00
|
|
|
{
|
2015-12-18 17:11:30 +00:00
|
|
|
MWWorld::ConstPtr ptr = sayiter->first;
|
2020-06-28 16:25:23 +00:00
|
|
|
Stream *sound = sayiter->second.get();
|
2015-11-30 13:42:51 +00:00
|
|
|
if(!ptr.isEmpty() && sound->getIs3D())
|
|
|
|
{
|
2015-12-11 01:48:45 +00:00
|
|
|
MWBase::World *world = MWBase::Environment::get().getWorld();
|
|
|
|
const osg::Vec3f pos = world->getActorHeadTransform(ptr).getTrans();
|
|
|
|
sound->setPosition(pos);
|
2015-11-23 12:00:10 +00:00
|
|
|
|
2015-11-30 13:42:51 +00:00
|
|
|
if(sound->getDistanceCull())
|
|
|
|
{
|
2015-12-11 01:48:45 +00:00
|
|
|
if((mListenerPos - pos).length2() > 2000*2000)
|
2015-12-11 23:13:14 +00:00
|
|
|
mOutput->finishStream(sound);
|
2015-11-30 13:42:51 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-23 12:00:10 +00:00
|
|
|
|
2015-11-30 13:42:51 +00:00
|
|
|
if(!mOutput->isStreamPlaying(sound))
|
2015-11-23 12:00:10 +00:00
|
|
|
{
|
2015-12-11 23:13:14 +00:00
|
|
|
mOutput->finishStream(sound);
|
2015-11-23 12:00:10 +00:00
|
|
|
mActiveSaySounds.erase(sayiter++);
|
|
|
|
}
|
2015-11-23 12:31:15 +00:00
|
|
|
else
|
2015-11-30 13:42:51 +00:00
|
|
|
{
|
|
|
|
sound->updateFade(duration);
|
2015-11-26 17:11:52 +00:00
|
|
|
|
2015-11-30 22:34:14 +00:00
|
|
|
mOutput->updateStream(sound);
|
2015-11-23 12:31:15 +00:00
|
|
|
++sayiter;
|
2015-11-30 13:42:51 +00:00
|
|
|
}
|
2015-11-23 12:31:15 +00:00
|
|
|
}
|
2015-11-23 12:00:10 +00:00
|
|
|
|
2015-12-01 19:28:37 +00:00
|
|
|
TrackList::iterator trkiter = mActiveTracks.begin();
|
|
|
|
for(;trkiter != mActiveTracks.end();++trkiter)
|
2015-11-23 12:31:15 +00:00
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
Stream *sound = trkiter->get();
|
2015-12-01 19:28:37 +00:00
|
|
|
if(!mOutput->isStreamPlaying(sound))
|
2015-11-23 12:00:10 +00:00
|
|
|
{
|
2015-12-11 23:13:14 +00:00
|
|
|
mOutput->finishStream(sound);
|
2015-12-01 19:28:37 +00:00
|
|
|
trkiter = mActiveTracks.erase(trkiter);
|
2015-11-23 12:00:10 +00:00
|
|
|
}
|
2015-12-01 19:28:37 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
sound->updateFade(duration);
|
2015-11-26 17:11:52 +00:00
|
|
|
|
2015-12-01 19:28:37 +00:00
|
|
|
mOutput->updateStream(sound);
|
|
|
|
++trkiter;
|
|
|
|
}
|
|
|
|
}
|
2015-12-14 21:54:53 +00:00
|
|
|
|
|
|
|
if(mListenerUnderwater)
|
|
|
|
{
|
|
|
|
// Play underwater sound (after updating sounds)
|
2017-09-12 04:33:18 +00:00
|
|
|
if(!mUnderwaterSound)
|
2017-09-15 08:03:41 +00:00
|
|
|
mUnderwaterSound = playSound("Underwater", 1.0f, 1.0f, Type::Sfx, PlayMode::LoopNoEnv);
|
2015-12-14 21:54:53 +00:00
|
|
|
}
|
2015-11-30 13:42:51 +00:00
|
|
|
mOutput->finishUpdate();
|
2012-03-22 02:21:36 +00:00
|
|
|
}
|
2015-11-23 12:31:15 +00:00
|
|
|
|
2012-03-22 02:21:36 +00:00
|
|
|
|
2017-08-06 18:10:56 +00:00
|
|
|
void SoundManager::updateMusic(float duration)
|
|
|
|
{
|
|
|
|
if (!mNextMusic.empty())
|
|
|
|
{
|
|
|
|
mMusic->updateFade(duration);
|
|
|
|
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->updateStream(mMusic.get());
|
2020-06-28 12:41:05 +00:00
|
|
|
|
2017-08-06 18:10:56 +00:00
|
|
|
if (mMusic->getRealVolume() <= 0.f)
|
|
|
|
{
|
|
|
|
streamMusicFull(mNextMusic);
|
|
|
|
mNextMusic.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-22 02:21:36 +00:00
|
|
|
void SoundManager::update(float duration)
|
|
|
|
{
|
2020-03-31 09:15:26 +00:00
|
|
|
if(!mOutput->isInitialized() || mPlaybackPaused)
|
2012-04-06 17:43:14 +00:00
|
|
|
return;
|
2014-01-21 13:50:58 +00:00
|
|
|
|
2019-03-03 22:31:51 +00:00
|
|
|
updateSounds(duration);
|
2014-01-21 13:50:58 +00:00
|
|
|
if (MWBase::Environment::get().getStateManager()->getState()!=
|
|
|
|
MWBase::StateManager::State_NoGame)
|
|
|
|
{
|
|
|
|
updateRegionSound(duration);
|
2020-06-28 11:50:02 +00:00
|
|
|
updateWaterSound();
|
2014-01-21 13:50:58 +00:00
|
|
|
}
|
2012-03-17 13:18:59 +00:00
|
|
|
}
|
2012-03-19 09:31:40 +00:00
|
|
|
|
2012-03-22 02:21:36 +00:00
|
|
|
|
2012-05-24 02:34:53 +00:00
|
|
|
void SoundManager::processChangedSettings(const Settings::CategorySettingVector& settings)
|
|
|
|
{
|
2020-07-01 16:37:31 +00:00
|
|
|
mVolumeSettings.update();
|
2012-05-24 12:30:22 +00:00
|
|
|
|
2015-11-25 04:37:38 +00:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
|
|
|
mOutput->startUpdate();
|
2017-09-16 08:56:58 +00:00
|
|
|
for(SoundMap::value_type &snd : mActiveSounds)
|
2012-05-24 12:30:22 +00:00
|
|
|
{
|
2017-09-16 08:56:58 +00:00
|
|
|
for(SoundBufferRefPair &sndbuf : snd.second)
|
2015-11-23 11:07:04 +00:00
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
Sound *sound = sndbuf.first.get();
|
2015-11-26 17:11:52 +00:00
|
|
|
sound->setBaseVolume(volumeFromType(sound->getPlayType()));
|
2015-11-30 22:34:14 +00:00
|
|
|
mOutput->updateSound(sound);
|
2015-11-23 11:07:04 +00:00
|
|
|
}
|
2012-05-24 12:30:22 +00:00
|
|
|
}
|
2017-09-16 08:56:58 +00:00
|
|
|
for(SaySoundMap::value_type &snd : mActiveSaySounds)
|
2015-11-23 12:48:18 +00:00
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
Stream *sound = snd.second.get();
|
2015-11-26 17:11:52 +00:00
|
|
|
sound->setBaseVolume(volumeFromType(sound->getPlayType()));
|
2015-11-30 22:34:14 +00:00
|
|
|
mOutput->updateStream(sound);
|
2015-11-23 12:48:18 +00:00
|
|
|
}
|
2019-05-26 11:55:29 +00:00
|
|
|
for(SaySoundMap::value_type &snd : mSaySoundsQueue)
|
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
Stream *sound = snd.second.get();
|
2019-05-26 11:55:29 +00:00
|
|
|
sound->setBaseVolume(volumeFromType(sound->getPlayType()));
|
|
|
|
mOutput->updateStream(sound);
|
|
|
|
}
|
2020-06-28 16:25:23 +00:00
|
|
|
for (const StreamPtr& sound : mActiveTracks)
|
2015-12-01 19:28:37 +00:00
|
|
|
{
|
2015-11-26 17:11:52 +00:00
|
|
|
sound->setBaseVolume(volumeFromType(sound->getPlayType()));
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->updateStream(sound.get());
|
2015-11-23 12:48:18 +00:00
|
|
|
}
|
2012-05-24 12:30:22 +00:00
|
|
|
if(mMusic)
|
|
|
|
{
|
2015-11-26 17:11:52 +00:00
|
|
|
mMusic->setBaseVolume(volumeFromType(mMusic->getPlayType()));
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->updateStream(mMusic.get());
|
2012-05-24 12:30:22 +00:00
|
|
|
}
|
2015-11-25 04:37:38 +00:00
|
|
|
mOutput->finishUpdate();
|
2012-05-24 02:34:53 +00:00
|
|
|
}
|
|
|
|
|
2015-12-03 14:16:20 +00:00
|
|
|
void SoundManager::setListenerPosDir(const osg::Vec3f &pos, const osg::Vec3f &dir, const osg::Vec3f &up, bool underwater)
|
2012-08-09 13:01:03 +00:00
|
|
|
{
|
|
|
|
mListenerPos = pos;
|
|
|
|
mListenerDir = dir;
|
2012-10-01 00:23:05 +00:00
|
|
|
mListenerUp = up;
|
2014-06-25 16:10:26 +00:00
|
|
|
|
2015-12-03 14:16:20 +00:00
|
|
|
mListenerUnderwater = underwater;
|
2020-06-28 12:41:05 +00:00
|
|
|
|
|
|
|
mWaterSoundUpdater.setUnderwater(underwater);
|
2012-08-09 13:01:03 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 17:11:30 +00:00
|
|
|
void SoundManager::updatePtr(const MWWorld::ConstPtr &old, const MWWorld::ConstPtr &updated)
|
2014-01-20 11:05:13 +00:00
|
|
|
{
|
2015-11-23 11:07:04 +00:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.find(old);
|
|
|
|
if(snditer != mActiveSounds.end())
|
2014-01-20 11:05:13 +00:00
|
|
|
{
|
2017-09-14 03:47:20 +00:00
|
|
|
SoundBufferRefPairList sndlist = std::move(snditer->second);
|
2015-11-23 11:07:04 +00:00
|
|
|
mActiveSounds.erase(snditer);
|
2017-09-14 03:47:20 +00:00
|
|
|
mActiveSounds.emplace(updated, std::move(sndlist));
|
2014-01-20 11:05:13 +00:00
|
|
|
}
|
2019-05-26 11:55:29 +00:00
|
|
|
|
|
|
|
SaySoundMap::iterator sayiter = mSaySoundsQueue.find(old);
|
|
|
|
if(sayiter != mSaySoundsQueue.end())
|
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
StreamPtr stream = std::move(sayiter->second);
|
2019-05-26 11:55:29 +00:00
|
|
|
mSaySoundsQueue.erase(sayiter);
|
2020-06-28 16:25:23 +00:00
|
|
|
mSaySoundsQueue.emplace(updated, std::move(stream));
|
2019-05-26 11:55:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sayiter = mActiveSaySounds.find(old);
|
2015-11-23 12:00:10 +00:00
|
|
|
if(sayiter != mActiveSaySounds.end())
|
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
StreamPtr stream = std::move(sayiter->second);
|
2015-11-23 12:00:10 +00:00
|
|
|
mActiveSaySounds.erase(sayiter);
|
2020-06-28 16:25:23 +00:00
|
|
|
mActiveSaySounds.emplace(updated, std::move(stream));
|
2015-11-27 17:47:14 +00:00
|
|
|
}
|
2014-01-20 11:05:13 +00:00
|
|
|
}
|
|
|
|
|
2012-03-21 00:57:28 +00: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 09:31:40 +00:00
|
|
|
|
|
|
|
const char *getSampleTypeName(SampleType type)
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case SampleType_UInt8: return "U8";
|
|
|
|
case SampleType_Int16: return "S16";
|
2013-02-26 18:19:33 +00:00
|
|
|
case SampleType_Float32: return "Float32";
|
2012-03-19 09:31:40 +00:00
|
|
|
}
|
|
|
|
return "(unknown sample type)";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *getChannelConfigName(ChannelConfig config)
|
|
|
|
{
|
|
|
|
switch(config)
|
|
|
|
{
|
2012-12-13 12:10:19 +00: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 09:31:40 +00:00
|
|
|
}
|
|
|
|
return "(unknown channel config)";
|
|
|
|
}
|
2012-03-19 12:29:04 +00:00
|
|
|
|
|
|
|
size_t framesToBytes(size_t frames, ChannelConfig config, SampleType type)
|
|
|
|
{
|
|
|
|
switch(config)
|
|
|
|
{
|
2012-12-13 12:10:19 +00: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 12:29:04 +00:00
|
|
|
}
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case SampleType_UInt8: frames *= 1; break;
|
|
|
|
case SampleType_Int16: frames *= 2; break;
|
2013-02-26 18:19:33 +00:00
|
|
|
case SampleType_Float32: frames *= 4; break;
|
2012-03-19 12:29:04 +00:00
|
|
|
}
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t bytesToFrames(size_t bytes, ChannelConfig config, SampleType type)
|
|
|
|
{
|
|
|
|
return bytes / framesToBytes(1, config, type);
|
|
|
|
}
|
2014-01-21 13:13:13 +00:00
|
|
|
|
|
|
|
void SoundManager::clear()
|
|
|
|
{
|
2019-11-17 17:49:13 +00:00
|
|
|
SoundManager::stopMusic();
|
2017-09-16 08:56:58 +00:00
|
|
|
|
|
|
|
for(SoundMap::value_type &snd : mActiveSounds)
|
2015-11-23 11:07:04 +00:00
|
|
|
{
|
2017-09-16 08:56:58 +00:00
|
|
|
for(SoundBufferRefPair &sndbuf : snd.second)
|
2015-11-23 14:35:01 +00:00
|
|
|
{
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->finishSound(sndbuf.first.get());
|
2017-09-16 08:56:58 +00:00
|
|
|
Sound_Buffer *sfx = sndbuf.second;
|
2015-11-25 11:34:44 +00:00
|
|
|
if(sfx->mUses-- == 1)
|
2015-11-26 09:26:33 +00:00
|
|
|
mUnusedBuffers.push_front(sfx);
|
2015-11-23 14:35:01 +00:00
|
|
|
}
|
2015-11-23 11:07:04 +00:00
|
|
|
}
|
2014-01-21 13:13:13 +00:00
|
|
|
mActiveSounds.clear();
|
2017-09-16 08:56:58 +00:00
|
|
|
mUnderwaterSound = nullptr;
|
|
|
|
mNearWaterSound = nullptr;
|
|
|
|
|
2019-05-26 11:55:29 +00:00
|
|
|
for(SaySoundMap::value_type &snd : mSaySoundsQueue)
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->finishStream(snd.second.get());
|
2019-05-26 11:55:29 +00:00
|
|
|
mSaySoundsQueue.clear();
|
|
|
|
|
2017-09-16 08:56:58 +00:00
|
|
|
for(SaySoundMap::value_type &snd : mActiveSaySounds)
|
2020-06-28 16:25:23 +00:00
|
|
|
mOutput->finishStream(snd.second.get());
|
2015-11-23 12:00:10 +00:00
|
|
|
mActiveSaySounds.clear();
|
2017-09-16 08:56:58 +00:00
|
|
|
|
2020-06-28 16:25:23 +00:00
|
|
|
for(StreamPtr& sound : mActiveTracks)
|
|
|
|
mOutput->finishStream(sound.get());
|
2015-12-01 19:28:37 +00:00
|
|
|
mActiveTracks.clear();
|
2020-03-31 09:15:26 +00:00
|
|
|
mPlaybackPaused = false;
|
2020-04-05 14:10:05 +00:00
|
|
|
std::fill(std::begin(mPausedSoundTypes), std::end(mPausedSoundTypes), 0);
|
2014-01-21 13:13:13 +00:00
|
|
|
}
|
2010-07-03 13:04:00 +00:00
|
|
|
}
|