2012-08-09 14:33:21 +02:00
|
|
|
#include "soundmanagerimp.hpp"
|
2010-07-03 15:04:00 +02:00
|
|
|
|
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>
|
2018-08-14 23:05:43 +04:00
|
|
|
#include <components/debug/debuglog.hpp>
|
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"
|
|
|
|
|
2015-11-22 04:53:24 -08:00
|
|
|
#include "sound_buffer.hpp"
|
2012-03-16 23:18:15 -07:00
|
|
|
#include "sound_decoder.hpp"
|
2018-05-07 20:40:53 +04:00
|
|
|
#include "sound_output.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
|
|
|
|
{
|
2020-06-28 14:41:05 +02:00
|
|
|
namespace
|
|
|
|
{
|
2020-06-28 13:49:05 +02:00
|
|
|
constexpr float sMinUpdateInterval = 1.0f / 30.0f;
|
|
|
|
|
2020-06-28 14:41:05 +02: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 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); }
|
|
|
|
|
2019-01-22 10:08:48 +04:00
|
|
|
SoundManager::SoundManager(const VFS::Manager* vfs, bool useSound)
|
2015-04-13 22:48:37 +02:00
|
|
|
: mVFS(vfs)
|
2021-01-26 00:48:45 +01:00
|
|
|
, mOutput(new OpenAL_Output(*this))
|
2020-06-28 14:41:05 +02:00
|
|
|
, mWaterSoundUpdater(makeWaterSoundUpdaterSettings())
|
2020-07-18 23:56:14 +02:00
|
|
|
, mSoundBuffers(*vfs, *mOutput)
|
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)
|
2017-09-11 21:33:18 -07:00
|
|
|
, mUnderwaterSound(nullptr)
|
|
|
|
, mNearWaterSound(nullptr)
|
2020-03-31 13:15:26 +04:00
|
|
|
, mPlaybackPaused(false)
|
2020-10-28 18:02:31 +04:00
|
|
|
, mTimePassed(0.f)
|
|
|
|
, mLastCell(nullptr)
|
|
|
|
, mCurrentRegionSound(nullptr)
|
2010-08-12 16:13:54 +02:00
|
|
|
{
|
2015-12-08 21:12:05 +01:00
|
|
|
if(!useSound)
|
2017-09-15 21:21:49 -07:00
|
|
|
{
|
2018-08-14 23:05:43 +04:00
|
|
|
Log(Debug::Info) << "Sound disabled.";
|
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))
|
|
|
|
{
|
2018-08-14 23:05:43 +04:00
|
|
|
Log(Debug::Error) << "Failed to initialize audio output, sound disabled";
|
2017-09-15 21:21:49 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
std::vector<std::string> names = mOutput->enumerate();
|
2018-08-14 23:05:43 +04:00
|
|
|
std::stringstream stream;
|
|
|
|
|
|
|
|
stream << "Enumerated output devices:\n";
|
2017-09-16 01:56:58 -07:00
|
|
|
for(const std::string &name : names)
|
2018-08-14 23:05:43 +04:00
|
|
|
stream << " " << name;
|
|
|
|
|
|
|
|
Log(Debug::Info) << stream.str();
|
|
|
|
stream.str("");
|
2015-12-04 12:54:41 -08:00
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
names = mOutput->enumerateHrtf();
|
|
|
|
if(!names.empty())
|
|
|
|
{
|
2018-08-14 23:05:43 +04:00
|
|
|
stream << "Enumerated HRTF names:\n";
|
2017-09-16 01:56:58 -07:00
|
|
|
for(const std::string &name : names)
|
2018-08-14 23:05:43 +04:00
|
|
|
stream << " " << name;
|
|
|
|
|
|
|
|
Log(Debug::Info) << stream.str();
|
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
|
|
|
{
|
2021-01-24 21:53:23 +01:00
|
|
|
SoundManager::clear();
|
2020-07-18 23:56:14 +02:00
|
|
|
mSoundBuffers.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()
|
|
|
|
{
|
2021-01-08 22:18:37 +01:00
|
|
|
return std::make_shared<FFmpeg_Decoder>(mVFS);
|
2012-03-17 23:30:43 -07:00
|
|
|
}
|
|
|
|
|
2016-06-27 20:54:42 +02:00
|
|
|
DecoderPtr SoundManager::loadVoice(const std::string &voicefile)
|
2015-11-23 01:33:59 -08:00
|
|
|
{
|
2018-10-18 14:59:39 +04:00
|
|
|
try
|
2015-11-23 01:33:59 -08:00
|
|
|
{
|
2018-10-18 14:59:39 +04: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-23 20:13:24 -08:00
|
|
|
}
|
2015-11-23 01:33:59 -08:00
|
|
|
|
2018-10-18 14:59:39 +04:00
|
|
|
return nullptr;
|
2015-11-23 01:33:59 -08:00
|
|
|
}
|
|
|
|
|
2020-06-28 18:25:23 +02:00
|
|
|
SoundPtr SoundManager::getSoundRef()
|
2017-09-11 21:33:18 -07:00
|
|
|
{
|
2020-06-28 17:17:43 +02:00
|
|
|
return mSounds.get();
|
2017-09-11 21:33:18 -07:00
|
|
|
}
|
|
|
|
|
2020-06-28 18:25:23 +02:00
|
|
|
StreamPtr SoundManager::getStreamRef()
|
2017-09-11 21:33:18 -07:00
|
|
|
{
|
2020-06-28 17:17:43 +02:00
|
|
|
return mStreams.get();
|
2017-09-11 21:33:18 -07:00
|
|
|
}
|
|
|
|
|
2020-06-28 18:25:23 +02:00
|
|
|
StreamPtr 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();
|
2018-08-29 18:38:12 +03: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 09:47:14 -08:00
|
|
|
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);
|
2020-06-28 18:25:23 +02:00
|
|
|
StreamPtr sound = getStreamRef();
|
2015-11-27 09:47:14 -08:00
|
|
|
if(playlocal)
|
2015-12-02 08:04:30 -08:00
|
|
|
{
|
2020-07-12 15:25:14 +02:00
|
|
|
sound->init([&] {
|
|
|
|
SoundParams params;
|
|
|
|
params.mBaseVolume = basevol;
|
|
|
|
params.mFlags = PlayMode::NoEnv | Type::Voice | Play_2D;
|
|
|
|
return params;
|
|
|
|
} ());
|
2020-06-28 18:25:23 +02:00
|
|
|
played = mOutput->streamSound(decoder, sound.get(), true);
|
2015-12-02 08:04:30 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-12 15:25:14 +02: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 18:25:23 +02:00
|
|
|
played = mOutput->streamSound3D(decoder, sound.get(), true);
|
2017-09-14 03:41:11 -07:00
|
|
|
}
|
|
|
|
if(!played)
|
|
|
|
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
|
|
|
{
|
2020-07-01 18:37:31 +02:00
|
|
|
return mVolumeSettings.getVolumeFromType(type);
|
2012-12-18 02:01:04 -08:00
|
|
|
}
|
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
|
|
|
{
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->finishStream(mMusic.get());
|
2017-09-11 21:33:18 -07:00
|
|
|
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;
|
2018-08-14 23:05:43 +04:00
|
|
|
Log(Debug::Info) << "Playing " << filename;
|
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();
|
2020-07-12 15:25:14 +02:00
|
|
|
mMusic->init([&] {
|
|
|
|
SoundParams params;
|
|
|
|
params.mBaseVolume = volumeFromType(Type::Music);
|
|
|
|
params.mFlags = PlayMode::NoEnv | Type::Music | Play_2D;
|
|
|
|
return params;
|
|
|
|
} ());
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->streamSound(decoder, mMusic.get());
|
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;
|
|
|
|
|
2019-04-25 20:26:40 +03:00
|
|
|
mMusic->setFadeout(1.f);
|
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
|
|
|
{
|
2020-06-28 18:25:23 +02:00
|
|
|
return mMusic && mOutput->isStreamPlaying(mMusic.get());
|
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;
|
|
|
|
|
2021-09-06 22:01:41 +02:00
|
|
|
for (const auto& name : mVFS->getRecursiveDirectoryIterator("Music/" + playlist))
|
2021-09-06 21:56:32 +02:00
|
|
|
filelist.push_back(name);
|
2018-04-21 22:20:07 +03:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-03-04 01:31:51 +03:00
|
|
|
void SoundManager::playTitleMusic()
|
|
|
|
{
|
|
|
|
if (mCurrentPlaylist == "Title")
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mMusicFiles.find("Title") == mMusicFiles.end())
|
|
|
|
{
|
|
|
|
std::vector<std::string> filelist;
|
|
|
|
// Is there an ini setting for this filename or something?
|
|
|
|
std::string filename = "music/special/morrowind title.mp3";
|
2021-09-06 21:56:32 +02:00
|
|
|
if (mVFS->exists(filename))
|
2019-03-04 01:31:51 +03:00
|
|
|
{
|
2021-09-06 21:56:32 +02:00
|
|
|
filelist.emplace_back(filename);
|
2019-03-04 01:31:51 +03:00
|
|
|
mMusicFiles["Title"] = filelist;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log(Debug::Warning) << "Title music not found";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mMusicFiles["Title"].empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
mCurrentPlaylist = "Title";
|
|
|
|
startRandomTitle();
|
|
|
|
}
|
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
|
|
|
|
2021-09-06 22:01:41 +02:00
|
|
|
DecoderPtr decoder = loadVoice(mVFS->normalizeFilename("Sound/" + filename));
|
2018-10-18 14:59:39 +04:00
|
|
|
if (!decoder)
|
|
|
|
return;
|
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);
|
2020-06-28 18:25:23 +02:00
|
|
|
StreamPtr sound = playVoice(decoder, pos, (ptr == MWMechanics::getPlayer()));
|
2017-09-14 03:41:11 -07:00
|
|
|
if(!sound) return;
|
|
|
|
|
2020-06-28 18:25:23 +02:00
|
|
|
mSaySoundsQueue.emplace(ptr, std::move(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
|
|
|
{
|
2020-06-28 18:25:23 +02:00
|
|
|
Stream *sound = snditer->second.get();
|
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;
|
|
|
|
|
2021-09-06 22:01:41 +02:00
|
|
|
DecoderPtr decoder = loadVoice(mVFS->normalizeFilename("Sound/" + filename));
|
2018-10-18 14:59:39 +04:00
|
|
|
if (!decoder)
|
|
|
|
return;
|
2017-09-14 03:41:11 -07:00
|
|
|
|
|
|
|
stopSay(MWWorld::ConstPtr());
|
2020-06-28 18:25:23 +02:00
|
|
|
StreamPtr sound = playVoice(decoder, osg::Vec3f(), true);
|
2017-09-14 03:41:11 -07:00
|
|
|
if(!sound) return;
|
|
|
|
|
2020-06-28 18:25:23 +02:00
|
|
|
mActiveSaySounds.emplace(MWWorld::ConstPtr(), std::move(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())
|
|
|
|
{
|
2020-06-28 18:25:23 +02:00
|
|
|
if(mOutput->isStreamPlaying(snditer->second.get()))
|
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
|
|
|
}
|
|
|
|
|
2019-09-06 09:19:41 +04:00
|
|
|
bool SoundManager::sayActive(const MWWorld::ConstPtr &ptr) const
|
|
|
|
{
|
|
|
|
SaySoundMap::const_iterator snditer = mSaySoundsQueue.find(ptr);
|
|
|
|
if(snditer != mSaySoundsQueue.end())
|
|
|
|
{
|
2020-06-28 18:25:23 +02:00
|
|
|
if(mOutput->isStreamPlaying(snditer->second.get()))
|
2019-09-06 09:19:41 +04:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
snditer = mActiveSaySounds.find(ptr);
|
|
|
|
if(snditer != mActiveSaySounds.end())
|
|
|
|
{
|
2020-06-28 18:25:23 +02:00
|
|
|
if(mOutput->isStreamPlaying(snditer->second.get()))
|
2019-09-06 09:19:41 +04:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-18 18:11:30 +01:00
|
|
|
void SoundManager::stopSay(const MWWorld::ConstPtr &ptr)
|
2012-05-01 20:30:31 -07:00
|
|
|
{
|
2019-05-26 14:55:29 +03:00
|
|
|
SaySoundMap::iterator snditer = mSaySoundsQueue.find(ptr);
|
|
|
|
if(snditer != mSaySoundsQueue.end())
|
|
|
|
{
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->finishStream(snditer->second.get());
|
2019-05-26 14:55:29 +03:00
|
|
|
mSaySoundsQueue.erase(snditer);
|
|
|
|
}
|
|
|
|
|
|
|
|
snditer = mActiveSaySounds.find(ptr);
|
2015-11-23 04:00:10 -08:00
|
|
|
if(snditer != mActiveSaySounds.end())
|
2012-05-01 20:30:31 -07:00
|
|
|
{
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->finishStream(snditer->second.get());
|
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
|
|
|
|
2020-06-28 18:25:23 +02:00
|
|
|
StreamPtr track = getStreamRef();
|
2020-07-12 15:25:14 +02:00
|
|
|
track->init([&] {
|
|
|
|
SoundParams params;
|
|
|
|
params.mBaseVolume = volumeFromType(type);
|
|
|
|
params.mFlags = PlayMode::NoEnv | type | Play_2D;
|
|
|
|
return params;
|
|
|
|
} ());
|
2020-06-28 18:25:23 +02:00
|
|
|
if(!mOutput->streamSound(decoder, track.get()))
|
2017-09-14 03:41:11 -07:00
|
|
|
return nullptr;
|
|
|
|
|
2020-06-28 18:25:23 +02: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 00:05:57 -08:00
|
|
|
}
|
|
|
|
|
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);
|
2020-06-28 18:25:23 +02: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 11:28:37 -08:00
|
|
|
mActiveTracks.erase(iter);
|
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
|
|
|
|
2020-06-28 18:25:23 +02: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
|
|
|
|
2021-01-09 14:11:49 +01:00
|
|
|
Sound_Buffer *sfx = mSoundBuffers.load(Misc::StringUtils::lowerCase(soundId));
|
2017-09-14 03:41:11 -07:00
|
|
|
if(!sfx) return nullptr;
|
|
|
|
|
2018-05-01 16:21:58 +04:00
|
|
|
// Only one copy of given sound can be played at time, so stop previous copy
|
2018-05-07 20:40:53 +04:00
|
|
|
stopSound(sfx, MWWorld::ConstPtr());
|
2018-05-01 16:21:58 +04:00
|
|
|
|
2020-06-28 18:25:23 +02:00
|
|
|
SoundPtr sound = getSoundRef();
|
2020-07-12 15:25:14 +02:00
|
|
|
sound->init([&] {
|
|
|
|
SoundParams params;
|
2020-07-18 23:56:14 +02:00
|
|
|
params.mVolume = volume * sfx->getVolume();
|
2020-07-12 15:25:14 +02:00
|
|
|
params.mBaseVolume = volumeFromType(type);
|
|
|
|
params.mPitch = pitch;
|
|
|
|
params.mFlags = mode | type | Play_2D;
|
|
|
|
return params;
|
|
|
|
} ());
|
2020-07-18 23:56:14 +02:00
|
|
|
if(!mOutput->playSound(sound.get(), sfx->getHandle(), offset))
|
2017-09-14 03:41:11 -07:00
|
|
|
return nullptr;
|
|
|
|
|
2020-06-28 18:25:23 +02:00
|
|
|
Sound* result = sound.get();
|
|
|
|
mActiveSounds[MWWorld::ConstPtr()].emplace_back(std::move(sound), sfx);
|
2020-07-18 23:56:14 +02:00
|
|
|
mSoundBuffers.use(*sfx);
|
2020-06-28 18:25:23 +02:00
|
|
|
return result;
|
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
|
|
|
|
2020-06-28 19:59:53 +02:00
|
|
|
const osg::Vec3f objpos(ptr.getRefData().getPosition().asVec3());
|
|
|
|
if ((mode & PlayMode::RemoveAtDistance) && (mListenerPos - objpos).length2() > 2000 * 2000)
|
|
|
|
return nullptr;
|
|
|
|
|
2017-09-14 03:41:11 -07:00
|
|
|
// Look up the sound in the ESM data
|
2021-01-09 14:11:49 +01:00
|
|
|
Sound_Buffer *sfx = mSoundBuffers.load(Misc::StringUtils::lowerCase(soundId));
|
2017-09-14 03:41:11 -07:00
|
|
|
if(!sfx) return nullptr;
|
2014-12-08 23:26:54 +01: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
|
2018-05-07 20:40:53 +04:00
|
|
|
stopSound(sfx, ptr);
|
2017-09-14 03:41:11 -07:00
|
|
|
|
|
|
|
bool played;
|
2020-06-28 18:25:23 +02:00
|
|
|
SoundPtr 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
|
|
|
{
|
2020-07-12 15:25:14 +02:00
|
|
|
sound->init([&] {
|
|
|
|
SoundParams params;
|
2020-07-18 23:56:14 +02:00
|
|
|
params.mVolume = volume * sfx->getVolume();
|
2020-07-12 15:25:14 +02:00
|
|
|
params.mBaseVolume = volumeFromType(type);
|
|
|
|
params.mPitch = pitch;
|
|
|
|
params.mFlags = mode | type | Play_2D;
|
|
|
|
return params;
|
|
|
|
} ());
|
2020-07-18 23:56:14 +02:00
|
|
|
played = mOutput->playSound(sound.get(), sfx->getHandle(), offset);
|
2017-09-14 03:41:11 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-12 15:25:14 +02:00
|
|
|
sound->init([&] {
|
|
|
|
SoundParams params;
|
|
|
|
params.mPos = objpos;
|
2020-07-18 23:56:14 +02:00
|
|
|
params.mVolume = volume * sfx->getVolume();
|
2020-07-12 15:25:14 +02:00
|
|
|
params.mBaseVolume = volumeFromType(type);
|
|
|
|
params.mPitch = pitch;
|
2020-07-18 23:56:14 +02:00
|
|
|
params.mMinDistance = sfx->getMinDist();
|
|
|
|
params.mMaxDistance = sfx->getMaxDist();
|
2020-07-12 15:25:14 +02:00
|
|
|
params.mFlags = mode | type | Play_3D;
|
|
|
|
return params;
|
|
|
|
} ());
|
2020-07-18 23:56:14 +02:00
|
|
|
played = mOutput->playSound3D(sound.get(), sfx->getHandle(), offset);
|
2012-03-21 18:20:32 -07:00
|
|
|
}
|
2017-09-14 03:41:11 -07:00
|
|
|
if(!played)
|
|
|
|
return nullptr;
|
|
|
|
|
2020-06-28 18:25:23 +02:00
|
|
|
Sound* result = sound.get();
|
|
|
|
mActiveSounds[ptr].emplace_back(std::move(sound), sfx);
|
2020-07-18 23:56:14 +02:00
|
|
|
mSoundBuffers.use(*sfx);
|
2020-06-28 18:25:23 +02:00
|
|
|
return result;
|
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
|
2021-01-09 14:11:49 +01:00
|
|
|
Sound_Buffer *sfx = mSoundBuffers.load(Misc::StringUtils::lowerCase(soundId));
|
2017-09-14 03:41:11 -07:00
|
|
|
if(!sfx) return nullptr;
|
|
|
|
|
2020-06-28 18:25:23 +02:00
|
|
|
SoundPtr sound = getSoundRef();
|
2020-07-12 15:25:14 +02:00
|
|
|
sound->init([&] {
|
|
|
|
SoundParams params;
|
|
|
|
params.mPos = initialPos;
|
2020-07-18 23:56:14 +02:00
|
|
|
params.mVolume = volume * sfx->getVolume();
|
2020-07-12 15:25:14 +02:00
|
|
|
params.mBaseVolume = volumeFromType(type);
|
|
|
|
params.mPitch = pitch;
|
2020-07-18 23:56:14 +02:00
|
|
|
params.mMinDistance = sfx->getMinDist();
|
|
|
|
params.mMaxDistance = sfx->getMaxDist();
|
2020-07-12 15:25:14 +02:00
|
|
|
params.mFlags = mode | type | Play_3D;
|
|
|
|
return params;
|
|
|
|
} ());
|
2020-07-18 23:56:14 +02:00
|
|
|
if(!mOutput->playSound3D(sound.get(), sfx->getHandle(), offset))
|
2017-09-14 03:41:11 -07:00
|
|
|
return nullptr;
|
|
|
|
|
2020-06-28 18:25:23 +02:00
|
|
|
Sound* result = sound.get();
|
|
|
|
mActiveSounds[MWWorld::ConstPtr()].emplace_back(std::move(sound), sfx);
|
2020-07-18 23:56:14 +02:00
|
|
|
mSoundBuffers.use(*sfx);
|
2020-06-28 18:25:23 +02:00
|
|
|
return result;
|
2014-05-16 13:09:23 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-07 20:40:53 +04:00
|
|
|
void SoundManager::stopSound(Sound_Buffer *sfx, const MWWorld::ConstPtr &ptr)
|
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
|
|
|
{
|
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)
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->finishSound(snd.first.get());
|
2012-03-28 03:48:51 -07:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 03:20:50 -07:00
|
|
|
}
|
|
|
|
|
2018-05-07 20:40:53 +04:00
|
|
|
void SoundManager::stopSound3D(const MWWorld::ConstPtr &ptr, const std::string& soundId)
|
|
|
|
{
|
2019-07-04 22:48:33 +02:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
|
|
|
|
2021-01-09 14:11:49 +01:00
|
|
|
Sound_Buffer *sfx = mSoundBuffers.lookup(Misc::StringUtils::lowerCase(soundId));
|
2018-05-07 20:40:53 +04:00
|
|
|
if (!sfx) return;
|
|
|
|
|
|
|
|
stopSound(sfx, ptr);
|
|
|
|
}
|
|
|
|
|
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)
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->finishSound(snd.first.get());
|
2012-03-20 07:22:17 -07:00
|
|
|
}
|
2019-05-26 19:23:42 +03:00
|
|
|
SaySoundMap::iterator sayiter = mSaySoundsQueue.find(ptr);
|
|
|
|
if(sayiter != mSaySoundsQueue.end())
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->finishStream(sayiter->second.get());
|
2019-05-26 19:23:42 +03:00
|
|
|
sayiter = mActiveSaySounds.find(ptr);
|
2017-09-16 01:56:58 -07:00
|
|
|
if(sayiter != mActiveSaySounds.end())
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->finishStream(sayiter->second.get());
|
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)
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->finishSound(sndbuf.first.get());
|
2012-03-20 07:22:17 -07:00
|
|
|
}
|
2012-03-17 22:13:57 -07:00
|
|
|
}
|
2018-05-07 20:40:53 +04:00
|
|
|
|
2019-05-26 19:23:42 +03:00
|
|
|
for(SaySoundMap::value_type &snd : mSaySoundsQueue)
|
|
|
|
{
|
|
|
|
if(!snd.first.isEmpty() && snd.first != MWMechanics::getPlayer() && snd.first.getCell() == cell)
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->finishStream(snd.second.get());
|
2019-05-26 19:23:42 +03: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)
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->finishStream(snd.second.get());
|
2015-11-23 04:00:10 -08:00
|
|
|
}
|
2012-01-29 20:27:03 +01:00
|
|
|
}
|
2010-08-16 18:06:27 +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
|
|
|
{
|
2021-01-09 14:11:49 +01:00
|
|
|
Sound_Buffer *sfx = mSoundBuffers.lookup(Misc::StringUtils::lowerCase(soundId));
|
2020-06-29 00:05:25 +02:00
|
|
|
if (sfx == nullptr)
|
|
|
|
return;
|
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())
|
|
|
|
{
|
2021-01-09 14:11:49 +01:00
|
|
|
Sound_Buffer *sfx = mSoundBuffers.lookup(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
|
2020-06-28 18:25:23 +02:00
|
|
|
{ return snd.second == sfx && mOutput->isSoundPlaying(snd.first.get()); }
|
2017-09-16 01:56:58 -07:00
|
|
|
) != 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
|
|
|
|
2020-04-05 18:10:05 +04:00
|
|
|
void SoundManager::pauseSounds(BlockerType blocker, int types)
|
2012-12-12 02:33:12 -08:00
|
|
|
{
|
|
|
|
if(mOutput->isInitialized())
|
2012-12-18 04:35:24 -08:00
|
|
|
{
|
2020-04-05 18:10:05 +04:00
|
|
|
if (mPausedSoundTypes[blocker] != 0)
|
|
|
|
resumeSounds(blocker);
|
2020-03-31 12:29:58 +04:00
|
|
|
|
2017-09-15 01:03:41 -07:00
|
|
|
types = types & Type::Mask;
|
2012-12-18 04:19:35 -08:00
|
|
|
mOutput->pauseSounds(types);
|
2020-04-05 18:10:05 +04:00
|
|
|
mPausedSoundTypes[blocker] = types;
|
2012-12-18 04:35:24 -08:00
|
|
|
}
|
2012-12-12 02:33:12 -08:00
|
|
|
}
|
|
|
|
|
2020-04-05 18:10:05 +04:00
|
|
|
void SoundManager::resumeSounds(BlockerType blocker)
|
2012-12-12 02:33:12 -08:00
|
|
|
{
|
|
|
|
if(mOutput->isInitialized())
|
2012-12-18 04:35:24 -08:00
|
|
|
{
|
2020-04-05 18:10:05 +04:00
|
|
|
mPausedSoundTypes[blocker] = 0;
|
2020-03-31 12:29:58 +04:00
|
|
|
int types = int(Type::Mask);
|
2020-04-05 18:10:05 +04:00
|
|
|
for (int currentBlocker = 0; currentBlocker < BlockerType::MaxCount; currentBlocker++)
|
2020-03-31 12:29:58 +04:00
|
|
|
{
|
2020-04-05 18:10:05 +04:00
|
|
|
if (currentBlocker != blocker)
|
|
|
|
types &= ~mPausedSoundTypes[currentBlocker];
|
2020-03-31 12:29:58 +04:00
|
|
|
}
|
|
|
|
|
2012-12-18 04:19:35 -08:00
|
|
|
mOutput->resumeSounds(types);
|
2012-12-18 04:35:24 -08:00
|
|
|
}
|
2012-12-12 02:33:12 -08:00
|
|
|
}
|
|
|
|
|
2020-03-31 13:15:26 +04:00
|
|
|
void SoundManager::pausePlayback()
|
|
|
|
{
|
|
|
|
if (mPlaybackPaused)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mPlaybackPaused = true;
|
|
|
|
mOutput->pauseActiveDevice();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundManager::resumePlayback()
|
|
|
|
{
|
|
|
|
if (!mPlaybackPaused)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mPlaybackPaused = false;
|
|
|
|
mOutput->resumeActiveDevice();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
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
|
|
|
|
2020-06-28 13:12:42 +02:00
|
|
|
if (!cell->isExterior())
|
2012-03-16 17:08:13 -07:00
|
|
|
return;
|
2020-10-25 12:20:14 +01:00
|
|
|
if (mCurrentRegionSound && mOutput->isSoundPlaying(mCurrentRegionSound))
|
|
|
|
return;
|
2012-03-21 15:19:40 -07:00
|
|
|
|
2020-06-28 13:12:42 +02:00
|
|
|
if (const auto next = mRegionSoundSelector.getNextRandom(duration, cell->mRegion, *world))
|
2020-10-25 12:20:14 +01:00
|
|
|
mCurrentRegionSound = playSound(*next, 1.0f, 1.0f);
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
2012-03-17 06:18:59 -07:00
|
|
|
|
2020-06-28 13:50:02 +02:00
|
|
|
void SoundManager::updateWaterSound()
|
2016-11-27 21:19:52 +01:00
|
|
|
{
|
|
|
|
MWBase::World* world = MWBase::Environment::get().getWorld();
|
|
|
|
const MWWorld::ConstPtr player = world->getPlayerPtr();
|
2017-09-12 02:48:10 -07:00
|
|
|
const ESM::Cell *curcell = player.getCell()->getCell();
|
2020-06-28 14:41:05 +02:00
|
|
|
const auto update = mWaterSoundUpdater.update(player, *world);
|
2016-11-27 21:19:52 +01:00
|
|
|
|
2020-06-28 15:38:30 +02:00
|
|
|
WaterSoundAction action;
|
|
|
|
Sound_Buffer* sfx;
|
|
|
|
std::tie(action, sfx) = getWaterSoundAction(update, curcell);
|
|
|
|
|
|
|
|
switch (action)
|
|
|
|
{
|
|
|
|
case WaterSoundAction::DoNothing:
|
|
|
|
break;
|
|
|
|
case WaterSoundAction::SetVolume:
|
2020-07-18 23:56:14 +02:00
|
|
|
mNearWaterSound->setVolume(update.mVolume * sfx->getVolume());
|
2020-06-28 15:38:30 +02:00
|
|
|
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 21:19:52 +01:00
|
|
|
if (mNearWaterSound)
|
|
|
|
{
|
2020-06-28 14:41:05 +02:00
|
|
|
if (update.mVolume == 0.0f)
|
2020-06-28 15:38:30 +02:00
|
|
|
return {WaterSoundAction::FinishSound, nullptr};
|
2016-11-27 21:19:52 +01:00
|
|
|
|
2020-06-28 15:38:30 +02:00
|
|
|
bool soundIdChanged = false;
|
2016-11-27 21:19:52 +01:00
|
|
|
|
2021-01-09 14:11:49 +01:00
|
|
|
Sound_Buffer* sfx = mSoundBuffers.lookup(update.mId);
|
2020-06-28 15:38:30 +02:00
|
|
|
if (mLastCell != cell)
|
|
|
|
{
|
|
|
|
const auto snditer = mActiveSounds.find(MWWorld::ConstPtr());
|
|
|
|
if (snditer != mActiveSounds.end())
|
2016-11-27 21:19:52 +01:00
|
|
|
{
|
2020-06-28 15:38:30 +02:00
|
|
|
const auto pairiter = std::find_if(
|
|
|
|
snditer->second.begin(), snditer->second.end(),
|
|
|
|
[this](const SoundBufferRefPairList::value_type &item) -> bool
|
2020-06-28 18:25:23 +02:00
|
|
|
{ return mNearWaterSound == item.first.get(); }
|
2020-06-28 15:38:30 +02:00
|
|
|
);
|
|
|
|
if (pairiter != snditer->second.end() && pairiter->second != sfx)
|
|
|
|
soundIdChanged = true;
|
2016-11-27 21:19:52 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-28 15:38:30 +02:00
|
|
|
|
|
|
|
if (soundIdChanged)
|
|
|
|
return {WaterSoundAction::PlaySound, nullptr};
|
|
|
|
|
|
|
|
if (sfx)
|
|
|
|
return {WaterSoundAction::SetVolume, sfx};
|
2016-11-27 21:19:52 +01:00
|
|
|
}
|
2020-06-28 14:41:05 +02:00
|
|
|
else if (update.mVolume > 0.0f)
|
2020-06-28 15:38:30 +02:00
|
|
|
return {WaterSoundAction::PlaySound, nullptr};
|
|
|
|
|
|
|
|
return {WaterSoundAction::DoNothing, nullptr};
|
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
|
|
|
{
|
2019-05-26 18:46:53 +03: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 18:25:23 +02: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 18:46:53 +03:00
|
|
|
mSaySoundsQueue.erase(queuesayiter++);
|
|
|
|
}
|
|
|
|
|
2020-06-28 13:49:05 +02:00
|
|
|
mTimePassed += duration;
|
|
|
|
if (mTimePassed < sMinUpdateInterval)
|
2012-03-21 19:21:36 -07:00
|
|
|
return;
|
2020-06-28 13:49:05 +02:00
|
|
|
duration = mTimePassed;
|
|
|
|
mTimePassed = 0.0f;
|
2012-03-21 19:21:36 -07:00
|
|
|
|
|
|
|
// 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
|
|
|
{
|
2020-06-28 18:25:23 +02:00
|
|
|
Sound *sound = sndidx->first.get();
|
2017-09-13 02:47:26 -07:00
|
|
|
|
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);
|
2020-06-28 18:25:23 +02:00
|
|
|
if (sound == mUnderwaterSound)
|
2017-09-11 21:33:18 -07:00
|
|
|
mUnderwaterSound = nullptr;
|
2020-06-28 18:25:23 +02:00
|
|
|
if (sound == mNearWaterSound)
|
2017-09-11 21:33:18 -07:00
|
|
|
mNearWaterSound = nullptr;
|
2020-07-18 23:56:14 +02:00
|
|
|
mSoundBuffers.release(*sndidx->second);
|
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
|
|
|
|
2019-05-26 18:46:53 +03:00
|
|
|
SaySoundMap::iterator sayiter = mActiveSaySounds.begin();
|
2019-05-26 19:23:42 +03:00
|
|
|
while(sayiter != mActiveSaySounds.end())
|
2015-11-23 04:00:10 -08:00
|
|
|
{
|
2015-12-18 18:11:30 +01:00
|
|
|
MWWorld::ConstPtr ptr = sayiter->first;
|
2020-06-28 18:25:23 +02:00
|
|
|
Stream *sound = sayiter->second.get();
|
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);
|
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
|
|
|
{
|
2020-06-28 18:25:23 +02:00
|
|
|
Stream *sound = trkiter->get();
|
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);
|
|
|
|
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->updateStream(mMusic.get());
|
2020-06-28 14:41:05 +02:00
|
|
|
|
2017-08-06 20:10:56 +02:00
|
|
|
if (mMusic->getRealVolume() <= 0.f)
|
|
|
|
{
|
|
|
|
streamMusicFull(mNextMusic);
|
|
|
|
mNextMusic.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-21 19:21:36 -07:00
|
|
|
void SoundManager::update(float duration)
|
|
|
|
{
|
2020-03-31 13:15:26 +04:00
|
|
|
if(!mOutput->isInitialized() || mPlaybackPaused)
|
2012-04-06 10:43:14 -07:00
|
|
|
return;
|
2014-01-21 14:50:58 +01:00
|
|
|
|
2019-03-04 01:31:51 +03:00
|
|
|
updateSounds(duration);
|
2014-01-21 14:50:58 +01:00
|
|
|
if (MWBase::Environment::get().getStateManager()->getState()!=
|
|
|
|
MWBase::StateManager::State_NoGame)
|
|
|
|
{
|
|
|
|
updateRegionSound(duration);
|
2020-06-28 13:50:02 +02:00
|
|
|
updateWaterSound();
|
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)
|
|
|
|
{
|
2020-07-01 18:37:31 +02:00
|
|
|
mVolumeSettings.update();
|
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
|
|
|
{
|
2020-06-28 18:25:23 +02:00
|
|
|
Sound *sound = sndbuf.first.get();
|
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
|
|
|
{
|
2020-06-28 18:25:23 +02:00
|
|
|
Stream *sound = snd.second.get();
|
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
|
|
|
}
|
2019-05-26 14:55:29 +03:00
|
|
|
for(SaySoundMap::value_type &snd : mSaySoundsQueue)
|
|
|
|
{
|
2020-06-28 18:25:23 +02:00
|
|
|
Stream *sound = snd.second.get();
|
2019-05-26 14:55:29 +03:00
|
|
|
sound->setBaseVolume(volumeFromType(sound->getPlayType()));
|
|
|
|
mOutput->updateStream(sound);
|
|
|
|
}
|
2020-06-28 18:25:23 +02:00
|
|
|
for (const StreamPtr& sound : mActiveTracks)
|
2015-12-01 11:28:37 -08:00
|
|
|
{
|
2015-11-26 09:11:52 -08:00
|
|
|
sound->setBaseVolume(volumeFromType(sound->getPlayType()));
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->updateStream(sound.get());
|
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()));
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->updateStream(mMusic.get());
|
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;
|
2020-06-28 14:41:05 +02:00
|
|
|
|
|
|
|
mWaterSoundUpdater.setUnderwater(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
|
|
|
}
|
2019-05-26 14:55:29 +03:00
|
|
|
|
|
|
|
SaySoundMap::iterator sayiter = mSaySoundsQueue.find(old);
|
|
|
|
if(sayiter != mSaySoundsQueue.end())
|
|
|
|
{
|
2020-06-28 18:25:23 +02:00
|
|
|
StreamPtr stream = std::move(sayiter->second);
|
2019-05-26 14:55:29 +03:00
|
|
|
mSaySoundsQueue.erase(sayiter);
|
2020-06-28 18:25:23 +02:00
|
|
|
mSaySoundsQueue.emplace(updated, std::move(stream));
|
2019-05-26 14:55:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
sayiter = mActiveSaySounds.find(old);
|
2015-11-23 04:00:10 -08:00
|
|
|
if(sayiter != mActiveSaySounds.end())
|
|
|
|
{
|
2020-06-28 18:25:23 +02:00
|
|
|
StreamPtr stream = std::move(sayiter->second);
|
2015-11-23 04:00:10 -08:00
|
|
|
mActiveSaySounds.erase(sayiter);
|
2020-06-28 18:25:23 +02:00
|
|
|
mActiveSaySounds.emplace(updated, std::move(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()
|
|
|
|
{
|
2019-11-17 18:49:13 +01:00
|
|
|
SoundManager::stopMusic();
|
2017-09-16 01:56:58 -07:00
|
|
|
|
|
|
|
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
|
|
|
{
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->finishSound(sndbuf.first.get());
|
2020-07-18 23:56:14 +02:00
|
|
|
mSoundBuffers.release(*sndbuf.second);
|
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;
|
|
|
|
|
2019-05-26 14:55:29 +03:00
|
|
|
for(SaySoundMap::value_type &snd : mSaySoundsQueue)
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->finishStream(snd.second.get());
|
2019-05-26 14:55:29 +03:00
|
|
|
mSaySoundsQueue.clear();
|
|
|
|
|
2017-09-16 01:56:58 -07:00
|
|
|
for(SaySoundMap::value_type &snd : mActiveSaySounds)
|
2020-06-28 18:25:23 +02:00
|
|
|
mOutput->finishStream(snd.second.get());
|
2015-11-23 04:00:10 -08:00
|
|
|
mActiveSaySounds.clear();
|
2017-09-16 01:56:58 -07:00
|
|
|
|
2020-06-28 18:25:23 +02:00
|
|
|
for(StreamPtr& sound : mActiveTracks)
|
|
|
|
mOutput->finishStream(sound.get());
|
2015-12-01 11:28:37 -08:00
|
|
|
mActiveTracks.clear();
|
2020-03-31 13:15:26 +04:00
|
|
|
mPlaybackPaused = false;
|
2020-04-05 18:10:05 +04:00
|
|
|
std::fill(std::begin(mPausedSoundTypes), std::end(mPausedSoundTypes), 0);
|
2014-01-21 14:13:13 +01:00
|
|
|
}
|
2010-07-03 15:04:00 +02:00
|
|
|
}
|