2012-08-09 12:33:21 +00:00
|
|
|
#include "soundmanagerimp.hpp"
|
2010-07-03 13:04:00 +00:00
|
|
|
|
2010-08-14 12:28:17 +00:00
|
|
|
#include <iostream>
|
2011-10-09 07:28:36 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
|
|
|
|
2015-12-11 01:48:45 +00:00
|
|
|
#include <osg/Matrixf>
|
|
|
|
|
2015-04-22 15:58:55 +00:00
|
|
|
#include <components/misc/rng.hpp>
|
2015-03-15 01:07:47 +00:00
|
|
|
|
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"
|
|
|
|
|
2012-03-17 06:40:07 +00:00
|
|
|
#include "sound_output.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"
|
2012-03-17 09:45:18 +00:00
|
|
|
#include "sound.hpp"
|
2012-03-17 05:12:17 +00:00
|
|
|
|
|
|
|
#include "openal_output.hpp"
|
|
|
|
#define SOUND_OUT "OpenAL"
|
2012-03-18 05:45:28 +00:00
|
|
|
#include "ffmpeg_decoder.hpp"
|
2012-03-21 22:29:05 +00:00
|
|
|
#ifndef SOUND_IN
|
2010-08-18 12:50:49 +00:00
|
|
|
#define SOUND_IN "FFmpeg"
|
2010-08-13 15:11:03 +00:00
|
|
|
#endif
|
2010-08-12 14:13:54 +00:00
|
|
|
|
|
|
|
|
2010-07-03 13:04:00 +00:00
|
|
|
namespace MWSound
|
|
|
|
{
|
2017-08-06 18:10:56 +00:00
|
|
|
SoundManager::SoundManager(const VFS::Manager* vfs, const std::map<std::string, std::string>& fallbackMap, bool useSound)
|
2015-04-13 20:48:37 +00:00
|
|
|
: mVFS(vfs)
|
2016-11-27 20:19:52 +00:00
|
|
|
, mFallback(fallbackMap)
|
2012-03-31 10:31:41 +00:00
|
|
|
, mOutput(new DEFAULT_OUTPUT(*this))
|
2012-04-07 23:00:30 +00:00
|
|
|
, mMasterVolume(1.0f)
|
|
|
|
, mSFXVolume(1.0f)
|
|
|
|
, mMusicVolume(1.0f)
|
2012-05-24 02:34:53 +00:00
|
|
|
, mVoiceVolume(1.0f)
|
2015-05-01 00:24:27 +00:00
|
|
|
, mFootstepsVolume(1.0f)
|
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)
|
2015-05-01 00:24:27 +00:00
|
|
|
, mPausedSoundTypes(0)
|
2010-08-12 14:13:54 +00:00
|
|
|
{
|
2012-04-07 23:00:30 +00:00
|
|
|
mMasterVolume = Settings::Manager::getFloat("master volume", "Sound");
|
|
|
|
mMasterVolume = std::min(std::max(mMasterVolume, 0.0f), 1.0f);
|
|
|
|
mSFXVolume = Settings::Manager::getFloat("sfx volume", "Sound");
|
|
|
|
mSFXVolume = std::min(std::max(mSFXVolume, 0.0f), 1.0f);
|
|
|
|
mMusicVolume = Settings::Manager::getFloat("music volume", "Sound");
|
|
|
|
mMusicVolume = std::min(std::max(mMusicVolume, 0.0f), 1.0f);
|
2012-05-24 02:37:41 +00:00
|
|
|
mVoiceVolume = Settings::Manager::getFloat("voice volume", "Sound");
|
2012-05-24 06:56:45 +00:00
|
|
|
mVoiceVolume = std::min(std::max(mVoiceVolume, 0.0f), 1.0f);
|
2012-05-24 02:37:41 +00:00
|
|
|
mFootstepsVolume = Settings::Manager::getFloat("footsteps volume", "Sound");
|
2012-05-24 06:56:45 +00:00
|
|
|
mFootstepsVolume = std::min(std::max(mFootstepsVolume, 0.0f), 1.0f);
|
2012-04-07 23:00:30 +00:00
|
|
|
|
2016-11-27 20:19:52 +00:00
|
|
|
mNearWaterRadius = mFallback.getFallbackInt("Water_NearWaterRadius");
|
|
|
|
mNearWaterPoints = mFallback.getFallbackInt("Water_NearWaterPoints");
|
|
|
|
mNearWaterIndoorTolerance = mFallback.getFallbackFloat("Water_NearWaterIndoorTolerance");
|
|
|
|
mNearWaterOutdoorTolerance = mFallback.getFallbackFloat("Water_NearWaterOutdoorTolerance");
|
|
|
|
mNearWaterIndoorID = mFallback.getFallbackString("Water_NearWaterIndoorID");
|
|
|
|
mNearWaterOutdoorID = mFallback.getFallbackString("Water_NearWaterOutdoorID");
|
|
|
|
|
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)
|
|
|
|
return;
|
|
|
|
|
2015-12-04 20:54:41 +00:00
|
|
|
std::string hrtfname = Settings::Manager::getString("hrtf", "Sound");
|
|
|
|
int hrtfstate = Settings::Manager::getInt("hrtf enable", "Sound");
|
|
|
|
|
2012-03-17 05:12:17 +00:00
|
|
|
std::cout << "Sound output: " << SOUND_OUT << std::endl;
|
|
|
|
std::cout << "Sound decoder: " << SOUND_IN << std::endl;
|
|
|
|
|
2015-12-04 20:54:41 +00:00
|
|
|
try {
|
2012-03-22 03:15:01 +00:00
|
|
|
std::vector<std::string> names = mOutput->enumerate();
|
|
|
|
std::cout <<"Enumerated output devices:"<< std::endl;
|
|
|
|
for(size_t i = 0;i < names.size();i++)
|
|
|
|
std::cout <<" "<<names[i]<< std::endl;
|
|
|
|
|
2012-04-07 22:28:38 +00:00
|
|
|
std::string devname = Settings::Manager::getString("device", "Sound");
|
2015-12-04 20:54:41 +00:00
|
|
|
try {
|
2012-04-07 22:28:38 +00:00
|
|
|
mOutput->init(devname);
|
|
|
|
}
|
2015-12-04 20:54:41 +00:00
|
|
|
catch(std::exception &e) {
|
2012-04-07 22:28:38 +00:00
|
|
|
if(devname.empty())
|
|
|
|
throw;
|
2012-12-18 05:09:57 +00:00
|
|
|
std::cerr <<"Failed to open device \""<<devname<<"\": " << e.what() << std::endl;
|
2012-04-07 22:28:38 +00:00
|
|
|
mOutput->init();
|
|
|
|
Settings::Manager::setString("device", "Sound", "");
|
|
|
|
}
|
2015-12-04 20:54:41 +00:00
|
|
|
|
|
|
|
names = mOutput->enumerateHrtf();
|
|
|
|
if(!names.empty())
|
|
|
|
{
|
|
|
|
std::cout <<"Enumerated HRTF names:"<< std::endl;
|
|
|
|
for(size_t i = 0;i < names.size();i++)
|
|
|
|
std::cout <<" "<<names[i]<< std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(hrtfstate == 0)
|
|
|
|
mOutput->disableHrtf();
|
|
|
|
else if(!hrtfname.empty())
|
|
|
|
mOutput->enableHrtf(hrtfname, hrtfstate<0);
|
2012-03-18 19:19:54 +00:00
|
|
|
}
|
2015-12-04 20:54:41 +00:00
|
|
|
catch(std::exception &e) {
|
2012-03-18 19:19:54 +00:00
|
|
|
std::cout <<"Sound init failed: "<<e.what()<< std::endl;
|
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();
|
2015-12-04 20:54:41 +00:00
|
|
|
SoundBufferList::element_type::iterator sfxiter = mSoundBuffers->begin();
|
|
|
|
for(;sfxiter != mSoundBuffers->end();++sfxiter)
|
2015-11-23 09:01:20 +00:00
|
|
|
{
|
2015-12-04 20:54:41 +00:00
|
|
|
if(sfxiter->mHandle)
|
|
|
|
mOutput->unloadSound(sfxiter->mHandle);
|
|
|
|
sfxiter->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();
|
|
|
|
static const float fAudioDefaultMinDistance = world->getStore().get<ESM::GameSetting>().find("fAudioDefaultMinDistance")->getFloat();
|
|
|
|
static const float fAudioDefaultMaxDistance = world->getStore().get<ESM::GameSetting>().find("fAudioDefaultMaxDistance")->getFloat();
|
|
|
|
static const float fAudioMinDistanceMult = world->getStore().get<ESM::GameSetting>().find("fAudioMinDistanceMult")->getFloat();
|
|
|
|
static const float fAudioMaxDistanceMult = world->getStore().get<ESM::GameSetting>().find("fAudioMaxDistanceMult")->getFloat();
|
|
|
|
float volume, min, max;
|
|
|
|
|
|
|
|
volume = static_cast<float>(pow(10.0, (sound->mData.mVolume / 255.0*3348.0 - 3348.0) / 2000.0));
|
|
|
|
if(sound->mData.mMinRange == 0 && sound->mData.mMaxRange == 0)
|
|
|
|
{
|
|
|
|
min = fAudioDefaultMinDistance;
|
|
|
|
max = fAudioDefaultMaxDistance;
|
|
|
|
}
|
2015-11-24 08:03:54 +00:00
|
|
|
else
|
2012-03-17 00:08:13 +00:00
|
|
|
{
|
2015-11-24 09:40:14 +00:00
|
|
|
min = sound->mData.mMinRange;
|
|
|
|
max = sound->mData.mMaxRange;
|
|
|
|
}
|
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);
|
|
|
|
if(snd != mBufferNameMap.end()) return snd->second;
|
|
|
|
return 0;
|
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
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
Sound_Buffer *sfx;
|
|
|
|
NameBufferMap::const_iterator snd = mBufferNameMap.find(soundId);
|
|
|
|
if(snd != mBufferNameMap.end())
|
|
|
|
sfx = snd->second;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MWBase::World *world = MWBase::Environment::get().getWorld();
|
|
|
|
const ESM::Sound *sound = world->getStore().get<ESM::Sound>().find(soundId);
|
|
|
|
sfx = insertSound(soundId, sound);
|
|
|
|
}
|
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
|
|
|
{
|
2015-11-24 08:03:54 +00:00
|
|
|
sfx->mHandle = mOutput->loadSound(sfx->mResourceName);
|
|
|
|
mBufferCacheSize += mOutput->getSoundDataSize(sfx->mHandle);
|
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
std::cerr<< "No unused sound buffers to free, using "<<mBufferCacheSize<<" bytes!" <<std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Sound_Buffer *unused = mUnusedBuffers.back();
|
|
|
|
|
|
|
|
mBufferCacheSize -= mOutput->getSoundDataSize(unused->mHandle);
|
|
|
|
mOutput->unloadSound(unused->mHandle);
|
|
|
|
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
|
|
|
{
|
2015-11-24 04:13:24 +00:00
|
|
|
DecoderPtr decoder = getDecoder();
|
|
|
|
// Workaround: Bethesda at some point converted some of the files to mp3, but the references were kept as .wav.
|
2015-12-10 23:37:39 +00:00
|
|
|
if(mVFS->exists(voicefile))
|
2015-11-24 04:13:24 +00:00
|
|
|
decoder->open(voicefile);
|
|
|
|
else
|
2015-11-23 09:33:59 +00:00
|
|
|
{
|
2015-11-24 04:13:24 +00:00
|
|
|
std::string file = voicefile;
|
|
|
|
std::string::size_type pos = file.rfind('.');
|
|
|
|
if(pos != std::string::npos)
|
|
|
|
file = file.substr(0, pos)+".mp3";
|
|
|
|
decoder->open(file);
|
|
|
|
}
|
2015-11-23 09:33:59 +00:00
|
|
|
|
2015-11-24 12:00:17 +00:00
|
|
|
return decoder;
|
2015-11-23 09:33:59 +00:00
|
|
|
}
|
|
|
|
|
2015-11-30 15:32:42 +00:00
|
|
|
MWBase::SoundStreamPtr 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();
|
|
|
|
static const float fAudioMinDistanceMult = world->getStore().get<ESM::GameSetting>().find("fAudioMinDistanceMult")->getFloat();
|
|
|
|
static const float fAudioMaxDistanceMult = world->getStore().get<ESM::GameSetting>().find("fAudioMaxDistanceMult")->getFloat();
|
|
|
|
static const float fAudioVoiceDefaultMinDistance = world->getStore().get<ESM::GameSetting>().find("fAudioVoiceDefaultMinDistance")->getFloat();
|
|
|
|
static const float fAudioVoiceDefaultMaxDistance = world->getStore().get<ESM::GameSetting>().find("fAudioVoiceDefaultMaxDistance")->getFloat();
|
|
|
|
static float minDistance = std::max(fAudioVoiceDefaultMinDistance * fAudioMinDistanceMult, 1.0f);
|
|
|
|
static float maxDistance = std::max(fAudioVoiceDefaultMaxDistance * fAudioMaxDistanceMult, minDistance);
|
|
|
|
|
2015-12-02 16:04:30 +00:00
|
|
|
MWBase::SoundStreamPtr sound;
|
2015-11-27 17:47:14 +00:00
|
|
|
float basevol = volumeFromType(Play_TypeVoice);
|
|
|
|
if(playlocal)
|
2015-12-02 16:04:30 +00:00
|
|
|
{
|
2017-04-30 16:49:32 +00:00
|
|
|
sound.reset(new Stream(1.0f, basevol, 1.0f, Play_NoEnv|Play_TypeVoice|Play_2D));
|
2015-12-02 16:04:30 +00:00
|
|
|
mOutput->streamSound(decoder, sound);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sound.reset(new Stream(pos, 1.0f, basevol, 1.0f, minDistance, maxDistance,
|
|
|
|
Play_Normal|Play_TypeVoice|Play_3D));
|
2016-06-27 19:37:02 +00:00
|
|
|
mOutput->streamSound3D(decoder, sound, true);
|
2015-12-02 16:04:30 +00:00
|
|
|
}
|
|
|
|
return sound;
|
2015-11-27 17:47:14 +00:00
|
|
|
}
|
|
|
|
|
2015-11-23 09:33:59 +00:00
|
|
|
|
2012-12-18 10:01:04 +00:00
|
|
|
// Gets the combined volume settings for the given sound type
|
|
|
|
float SoundManager::volumeFromType(PlayType type) const
|
|
|
|
{
|
|
|
|
float volume = mMasterVolume;
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case Play_TypeSfx:
|
|
|
|
volume *= mSFXVolume;
|
|
|
|
break;
|
|
|
|
case Play_TypeVoice:
|
|
|
|
volume *= mVoiceVolume;
|
|
|
|
break;
|
2013-07-19 02:01:13 +00:00
|
|
|
case Play_TypeFoot:
|
|
|
|
volume *= mFootstepsVolume;
|
|
|
|
break;
|
2012-12-18 10:01:04 +00:00
|
|
|
case Play_TypeMusic:
|
|
|
|
volume *= mMusicVolume;
|
|
|
|
break;
|
|
|
|
case Play_TypeMask:
|
|
|
|
break;
|
2014-01-14 02:08:37 +00:00
|
|
|
default:
|
|
|
|
break;
|
2012-12-18 10:01:04 +00:00
|
|
|
}
|
|
|
|
return volume;
|
|
|
|
}
|
2010-07-03 13:04:00 +00:00
|
|
|
|
2010-08-14 12:28:17 +00:00
|
|
|
|
2012-03-09 01:56:29 +00:00
|
|
|
void SoundManager::stopMusic()
|
|
|
|
{
|
2012-03-17 09:45:18 +00:00
|
|
|
if(mMusic)
|
2015-12-11 23:13:14 +00:00
|
|
|
mOutput->finishStream(mMusic);
|
2012-03-20 17:34:36 +00:00
|
|
|
mMusic.reset();
|
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;
|
2012-03-20 18:31:13 +00:00
|
|
|
std::cout <<"Playing "<<filename<< std::endl;
|
2014-06-11 16:10:58 +00:00
|
|
|
mLastPlayedMusic = filename;
|
2015-11-30 13:42:51 +00:00
|
|
|
try {
|
2012-03-31 17:59:29 +00:00
|
|
|
stopMusic();
|
2012-12-13 06:32:02 +00:00
|
|
|
|
|
|
|
DecoderPtr decoder = getDecoder();
|
|
|
|
decoder->open(filename);
|
|
|
|
|
2015-12-02 16:04:30 +00:00
|
|
|
mMusic.reset(new Stream(1.0f, volumeFromType(Play_TypeMusic), 1.0f,
|
|
|
|
Play_NoEnv|Play_TypeMusic|Play_2D));
|
|
|
|
mOutput->streamSound(decoder, mMusic);
|
2012-03-20 17:34:36 +00:00
|
|
|
}
|
2015-11-30 13:42:51 +00:00
|
|
|
catch(std::exception &e) {
|
2012-03-20 17:34:36 +00:00
|
|
|
std::cout << "Music Error: " << e.what() << "\n";
|
2015-12-02 16:04:30 +00:00
|
|
|
mMusic.reset();
|
2012-03-17 10:06:35 +00:00
|
|
|
}
|
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;
|
|
|
|
|
2017-08-08 01:17:40 +00:00
|
|
|
mMusic->setFadeout(0.5f);
|
2017-08-06 18:10:56 +00:00
|
|
|
}
|
|
|
|
|
2012-03-20 19:39:49 +00:00
|
|
|
void SoundManager::streamMusic(const std::string& filename)
|
|
|
|
{
|
2017-08-06 18:10:56 +00:00
|
|
|
advanceMusic("Music/"+filename);
|
2012-03-20 19:39:49 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 00:08:13 +00:00
|
|
|
void SoundManager::startRandomTitle()
|
2011-01-05 21:18:21 +00:00
|
|
|
{
|
2015-04-13 20:48:37 +00:00
|
|
|
std::vector<std::string> filelist;
|
2014-06-11 16:10:58 +00:00
|
|
|
if (mMusicFiles.find(mCurrentPlaylist) == mMusicFiles.end())
|
2013-04-04 13:10:27 +00:00
|
|
|
{
|
2015-04-13 20:48:37 +00:00
|
|
|
const std::map<std::string, VFS::File*>& index = mVFS->getIndex();
|
|
|
|
|
|
|
|
std::string pattern = "Music/" + mCurrentPlaylist;
|
|
|
|
mVFS->normalizeFilename(pattern);
|
|
|
|
|
|
|
|
std::map<std::string, VFS::File*>::const_iterator found = index.lower_bound(pattern);
|
|
|
|
while (found != index.end())
|
2014-06-11 16:10:58 +00:00
|
|
|
{
|
2015-04-13 20:48:37 +00:00
|
|
|
if (found->first.size() >= pattern.size() && found->first.substr(0, pattern.size()) == pattern)
|
|
|
|
filelist.push_back(found->first);
|
2015-04-30 23:15:25 +00:00
|
|
|
else
|
|
|
|
break;
|
2015-04-13 20:48:37 +00:00
|
|
|
++found;
|
2014-06-11 16:10:58 +00:00
|
|
|
}
|
2015-04-13 20:48:37 +00:00
|
|
|
|
2014-06-11 16:10:58 +00:00
|
|
|
mMusicFiles[mCurrentPlaylist] = filelist;
|
2015-04-01 15:02:15 +00:00
|
|
|
|
2013-04-04 13:10:27 +00:00
|
|
|
}
|
2014-06-11 16:10:58 +00:00
|
|
|
else
|
|
|
|
filelist = mMusicFiles[mCurrentPlaylist];
|
2013-04-04 13:10:27 +00:00
|
|
|
|
2016-02-22 18:06:12 +00:00
|
|
|
if(filelist.empty())
|
2012-03-20 18:31:13 +00:00
|
|
|
return;
|
|
|
|
|
2015-04-22 15:58:55 +00:00
|
|
|
int i = Misc::Rng::rollDice(filelist.size());
|
2014-06-11 16:10:58 +00:00
|
|
|
|
|
|
|
// Don't play the same music track twice in a row
|
|
|
|
if (filelist[i] == mLastPlayedMusic)
|
|
|
|
{
|
2014-06-12 21:12:48 +00:00
|
|
|
i = (i+1) % filelist.size();
|
2014-06-11 16:10:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-06 18:10:56 +00:00
|
|
|
advanceMusic(filelist[i]);
|
2011-01-05 21:18:21 +00:00
|
|
|
}
|
2010-11-12 00:47:26 +00:00
|
|
|
|
2010-10-31 16:23:03 +00:00
|
|
|
bool SoundManager::isMusicPlaying()
|
2011-01-01 21:33:08 +00:00
|
|
|
{
|
2015-11-30 13:42:51 +00:00
|
|
|
return mMusic && mOutput->isStreamPlaying(mMusic);
|
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
|
|
|
{
|
2012-03-20 18:31:13 +00:00
|
|
|
mCurrentPlaylist = playlist;
|
|
|
|
startRandomTitle();
|
2012-03-07 15:46:51 +00:00
|
|
|
}
|
|
|
|
|
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-22 02:08:11 +00:00
|
|
|
try
|
|
|
|
{
|
2015-11-27 09:58:13 +00:00
|
|
|
std::string voicefile = "Sound/"+filename;
|
2012-03-28 11:58:47 +00:00
|
|
|
|
2015-11-27 09:58:13 +00:00
|
|
|
mVFS->normalizeFilename(voicefile);
|
2016-06-27 18:54:42 +00:00
|
|
|
DecoderPtr decoder = loadVoice(voicefile);
|
2015-11-24 04:13:24 +00:00
|
|
|
|
2016-06-27 18:54:42 +00:00
|
|
|
MWBase::World *world = MWBase::Environment::get().getWorld();
|
|
|
|
const osg::Vec3f pos = world->getActorHeadTransform(ptr).getTrans();
|
2015-12-15 14:12:48 +00:00
|
|
|
|
2016-06-27 18:54:42 +00:00
|
|
|
SaySoundMap::iterator oldIt = mActiveSaySounds.find(ptr);
|
|
|
|
if (oldIt != mActiveSaySounds.end())
|
|
|
|
{
|
|
|
|
mOutput->finishStream(oldIt->second);
|
|
|
|
mActiveSaySounds.erase(oldIt);
|
|
|
|
}
|
2015-12-15 14:12:48 +00:00
|
|
|
|
2016-06-27 18:54:42 +00:00
|
|
|
MWBase::SoundStreamPtr sound = playVoice(decoder, pos, (ptr == MWMechanics::getPlayer()));
|
2015-12-15 14:12:48 +00:00
|
|
|
|
2016-06-27 18:54:42 +00:00
|
|
|
mActiveSaySounds.insert(std::make_pair(ptr, sound));
|
2012-03-22 02:08:11 +00:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
std::cout <<"Sound Error: "<<e.what()<< std::endl;
|
|
|
|
}
|
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
|
|
|
{
|
2016-06-27 18:54:42 +00:00
|
|
|
MWBase::SoundStreamPtr sound = snditer->second;
|
|
|
|
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;
|
|
|
|
try
|
|
|
|
{
|
2015-11-27 09:58:13 +00:00
|
|
|
std::string voicefile = "Sound/"+filename;
|
2012-05-02 03:30:31 +00:00
|
|
|
|
2015-11-27 09:58:13 +00:00
|
|
|
mVFS->normalizeFilename(voicefile);
|
2016-06-27 18:54:42 +00:00
|
|
|
DecoderPtr decoder = loadVoice(voicefile);
|
2015-11-24 04:13:24 +00:00
|
|
|
|
2016-06-27 18:54:42 +00:00
|
|
|
SaySoundMap::iterator oldIt = mActiveSaySounds.find(MWWorld::ConstPtr());
|
|
|
|
if (oldIt != mActiveSaySounds.end())
|
2015-11-27 17:47:14 +00:00
|
|
|
{
|
2016-06-27 18:54:42 +00:00
|
|
|
mOutput->finishStream(oldIt->second);
|
|
|
|
mActiveSaySounds.erase(oldIt);
|
2015-11-27 17:47:14 +00:00
|
|
|
}
|
2016-06-27 18:54:42 +00:00
|
|
|
|
|
|
|
mActiveSaySounds.insert(std::make_pair(MWWorld::ConstPtr(),
|
|
|
|
playVoice(decoder, osg::Vec3f(), true)));
|
2012-05-02 03:30:31 +00:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
std::cout <<"Sound Error: "<<e.what()<< std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
{
|
2016-06-27 18:54:42 +00:00
|
|
|
if(mOutput->isStreamPlaying(snditer->second))
|
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
|
|
|
}
|
|
|
|
|
2015-12-18 17:11:30 +00:00
|
|
|
void SoundManager::stopSay(const MWWorld::ConstPtr &ptr)
|
2012-05-02 03:30:31 +00:00
|
|
|
{
|
2015-11-23 12:00:10 +00:00
|
|
|
SaySoundMap::iterator snditer = mActiveSaySounds.find(ptr);
|
|
|
|
if(snditer != mActiveSaySounds.end())
|
2012-05-02 03:30:31 +00:00
|
|
|
{
|
2016-06-27 18:54:42 +00:00
|
|
|
mOutput->finishStream(snditer->second);
|
2015-11-23 12:00:10 +00:00
|
|
|
mActiveSaySounds.erase(snditer);
|
2012-05-02 03:30:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-30 15:32:42 +00:00
|
|
|
MWBase::SoundStreamPtr SoundManager::playTrack(const DecoderPtr& decoder, PlayType type)
|
2012-12-13 08:05:57 +00:00
|
|
|
{
|
2015-11-30 15:32:42 +00:00
|
|
|
MWBase::SoundStreamPtr track;
|
2012-12-13 08:05:57 +00:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return track;
|
|
|
|
try
|
|
|
|
{
|
2015-12-02 16:04:30 +00:00
|
|
|
track.reset(new Stream(1.0f, volumeFromType(type), 1.0f, Play_NoEnv|type|Play_2D));
|
|
|
|
mOutput->streamSound(decoder, track);
|
|
|
|
|
2015-12-01 19:28:37 +00:00
|
|
|
TrackList::iterator iter = std::lower_bound(mActiveTracks.begin(), mActiveTracks.end(), track);
|
|
|
|
mActiveTracks.insert(iter, track);
|
2012-12-13 08:05:57 +00:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
std::cout <<"Sound Error: "<<e.what()<< std::endl;
|
|
|
|
}
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
2015-11-30 15:32:42 +00:00
|
|
|
void SoundManager::stopTrack(MWBase::SoundStreamPtr stream)
|
2015-11-30 13:42:51 +00:00
|
|
|
{
|
2015-12-11 23:13:14 +00:00
|
|
|
mOutput->finishStream(stream);
|
2015-12-01 19:28:37 +00:00
|
|
|
TrackList::iterator iter = std::lower_bound(mActiveTracks.begin(), mActiveTracks.end(), stream);
|
|
|
|
if(iter != mActiveTracks.end() && *iter == stream)
|
|
|
|
mActiveTracks.erase(iter);
|
2015-11-30 13:42:51 +00:00
|
|
|
}
|
|
|
|
|
2015-11-30 15:32:42 +00:00
|
|
|
double SoundManager::getTrackTimeDelay(MWBase::SoundStreamPtr 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
|
|
|
|
2013-07-26 16:43:06 +00:00
|
|
|
MWBase::SoundPtr SoundManager::playSound(const std::string& soundId, float volume, float pitch, PlayType type, PlayMode mode, float offset)
|
2012-03-17 00:08:13 +00:00
|
|
|
{
|
2012-08-09 12:33:21 +00:00
|
|
|
MWBase::SoundPtr sound;
|
2012-04-06 17:43:14 +00:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return sound;
|
2012-03-22 01:20:32 +00:00
|
|
|
try
|
2012-03-17 13:51:44 +00:00
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
|
2013-07-19 02:01:13 +00:00
|
|
|
float basevol = volumeFromType(type);
|
2012-03-28 11:58:47 +00:00
|
|
|
|
2015-12-02 14:35:35 +00:00
|
|
|
sound.reset(new Sound(volume * sfx->mVolume, basevol, pitch, mode|type|Play_2D));
|
|
|
|
mOutput->playSound(sound, sfx->mHandle, offset);
|
2015-11-25 11:34:44 +00:00
|
|
|
if(sfx->mUses++ == 0)
|
2015-11-25 05:40:15 +00:00
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfx);
|
2015-11-25 05:40:15 +00:00
|
|
|
if(iter != mUnusedBuffers.end())
|
|
|
|
mUnusedBuffers.erase(iter);
|
|
|
|
}
|
2015-12-18 17:11:30 +00:00
|
|
|
mActiveSounds[MWWorld::ConstPtr()].push_back(std::make_pair(sound, sfx));
|
2012-03-22 01:20:32 +00:00
|
|
|
}
|
2014-05-15 01:12:52 +00:00
|
|
|
catch(std::exception&)
|
2012-03-22 01:20:32 +00:00
|
|
|
{
|
2012-10-01 00:38:55 +00:00
|
|
|
//std::cout <<"Sound Error: "<<e.what()<< std::endl;
|
2015-12-02 14:35:35 +00:00
|
|
|
sound.reset();
|
2012-03-17 13:51:44 +00:00
|
|
|
}
|
2012-03-28 13:08:25 +00:00
|
|
|
return sound;
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 17:11:30 +00:00
|
|
|
MWBase::SoundPtr SoundManager::playSound3D(const MWWorld::ConstPtr &ptr, const std::string& soundId,
|
2013-07-26 16:43:06 +00:00
|
|
|
float volume, float pitch, PlayType type, PlayMode mode, float offset)
|
2012-03-17 00:08:13 +00:00
|
|
|
{
|
2012-08-09 12:33:21 +00:00
|
|
|
MWBase::SoundPtr sound;
|
2012-04-06 17:43:14 +00:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return sound;
|
2012-03-22 01:20:32 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// Look up the sound in the ESM data
|
2015-11-26 09:26:33 +00:00
|
|
|
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
|
2013-07-19 02:01:13 +00:00
|
|
|
float basevol = volumeFromType(type);
|
2013-07-26 16:43:06 +00:00
|
|
|
const ESM::Position &pos = ptr.getRefData().getPosition();
|
2015-05-12 17:02:56 +00:00
|
|
|
const osg::Vec3f objpos(pos.asVec3());
|
2012-03-22 02:08:11 +00:00
|
|
|
|
2015-11-22 12:53:24 +00:00
|
|
|
if((mode&Play_RemoveAtDistance) && (mListenerPos-objpos).length2() > 2000*2000)
|
2014-12-08 22:26:54 +00:00
|
|
|
return MWBase::SoundPtr();
|
|
|
|
|
2015-11-27 09:02:53 +00:00
|
|
|
if(!(mode&Play_NoPlayerLocal) && ptr == MWMechanics::getPlayer())
|
2015-12-02 14:35:35 +00:00
|
|
|
{
|
|
|
|
sound.reset(new Sound(volume * sfx->mVolume, basevol, pitch, mode|type|Play_2D));
|
|
|
|
mOutput->playSound(sound, sfx->mHandle, offset);
|
|
|
|
}
|
2015-11-27 09:02:53 +00:00
|
|
|
else
|
2015-12-02 14:35:35 +00:00
|
|
|
{
|
|
|
|
sound.reset(new Sound(objpos, volume * sfx->mVolume, basevol, pitch,
|
|
|
|
sfx->mMinDist, sfx->mMaxDist, mode|type|Play_3D));
|
|
|
|
mOutput->playSound3D(sound, sfx->mHandle, offset);
|
|
|
|
}
|
2015-11-25 11:34:44 +00:00
|
|
|
if(sfx->mUses++ == 0)
|
2015-11-25 05:40:15 +00:00
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfx);
|
2015-11-25 05:40:15 +00:00
|
|
|
if(iter != mUnusedBuffers.end())
|
|
|
|
mUnusedBuffers.erase(iter);
|
|
|
|
}
|
2015-11-26 09:26:33 +00:00
|
|
|
mActiveSounds[ptr].push_back(std::make_pair(sound, sfx));
|
2012-03-22 01:20:32 +00:00
|
|
|
}
|
2014-05-15 01:12:52 +00:00
|
|
|
catch(std::exception&)
|
2012-03-22 01:20:32 +00:00
|
|
|
{
|
2012-10-01 00:38:55 +00:00
|
|
|
//std::cout <<"Sound Error: "<<e.what()<< std::endl;
|
2015-12-02 14:35:35 +00:00
|
|
|
sound.reset();
|
2012-03-22 01:20:32 +00:00
|
|
|
}
|
2012-03-28 13:08:25 +00:00
|
|
|
return sound;
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 07:24:42 +00:00
|
|
|
MWBase::SoundPtr SoundManager::playSound3D(const osg::Vec3f& initialPos, const std::string& soundId,
|
|
|
|
float volume, float pitch, PlayType type, PlayMode mode, float offset)
|
2014-05-16 11:09:23 +00:00
|
|
|
{
|
|
|
|
MWBase::SoundPtr sound;
|
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return sound;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Look up the sound in the ESM data
|
2015-11-26 09:26:33 +00:00
|
|
|
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
|
2014-05-16 11:09:23 +00:00
|
|
|
float basevol = volumeFromType(type);
|
|
|
|
|
2015-12-02 14:35:35 +00:00
|
|
|
sound.reset(new Sound(initialPos, volume * sfx->mVolume, basevol, pitch,
|
|
|
|
sfx->mMinDist, sfx->mMaxDist, mode|type|Play_3D));
|
|
|
|
mOutput->playSound3D(sound, sfx->mHandle, offset);
|
2015-11-25 11:34:44 +00:00
|
|
|
if(sfx->mUses++ == 0)
|
2015-11-25 05:40:15 +00:00
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
SoundList::iterator iter = std::find(mUnusedBuffers.begin(), mUnusedBuffers.end(), sfx);
|
2015-11-25 05:40:15 +00:00
|
|
|
if(iter != mUnusedBuffers.end())
|
|
|
|
mUnusedBuffers.erase(iter);
|
|
|
|
}
|
2015-12-18 17:11:30 +00:00
|
|
|
mActiveSounds[MWWorld::ConstPtr()].push_back(std::make_pair(sound, sfx));
|
2014-05-16 11:09:23 +00:00
|
|
|
}
|
2014-06-23 06:13:30 +00:00
|
|
|
catch(std::exception &)
|
2014-05-16 11:09:23 +00:00
|
|
|
{
|
|
|
|
//std::cout <<"Sound Error: "<<e.what()<< std::endl;
|
2015-12-02 14:35:35 +00:00
|
|
|
sound.reset();
|
2014-05-16 11:09:23 +00:00
|
|
|
}
|
|
|
|
return sound;
|
|
|
|
}
|
|
|
|
|
2015-11-30 13:42:51 +00:00
|
|
|
void SoundManager::stopSound(MWBase::SoundPtr sound)
|
|
|
|
{
|
2015-12-05 00:54:16 +00:00
|
|
|
if (sound.get())
|
2015-12-11 23:13:14 +00:00
|
|
|
mOutput->finishSound(sound);
|
2015-11-30 13:42:51 +00:00
|
|
|
}
|
|
|
|
|
2015-12-18 17:11:30 +00:00
|
|
|
void SoundManager::stopSound3D(const MWWorld::ConstPtr &ptr, const std::string& soundId)
|
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
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
|
|
|
|
SoundBufferRefPairList::iterator sndidx = snditer->second.begin();
|
2015-11-24 11:18:23 +00:00
|
|
|
for(;sndidx != snditer->second.end();++sndidx)
|
2012-03-28 10:48:51 +00:00
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
if(sndidx->second == sfx)
|
2015-12-11 23:13:14 +00:00
|
|
|
mOutput->finishSound(sndidx->first);
|
2012-03-28 10:48:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 10:20:50 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
SoundBufferRefPairList::iterator sndidx = snditer->second.begin();
|
2015-11-24 11:18:23 +00:00
|
|
|
for(;sndidx != snditer->second.end();++sndidx)
|
2015-12-11 23:13:14 +00:00
|
|
|
mOutput->finishSound(sndidx->first);
|
2012-03-20 14:22:17 +00:00
|
|
|
}
|
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
|
|
|
{
|
2012-03-18 18:17:45 +00:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.begin();
|
|
|
|
while(snditer != mActiveSounds.end())
|
2012-03-18 05:13:57 +00:00
|
|
|
{
|
2015-12-18 17:11:30 +00:00
|
|
|
if(snditer->first != MWWorld::ConstPtr() &&
|
2015-11-23 11:07:04 +00:00
|
|
|
snditer->first != MWMechanics::getPlayer() &&
|
|
|
|
snditer->first.getCell() == cell)
|
2012-03-20 14:22:17 +00:00
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
SoundBufferRefPairList::iterator sndidx = snditer->second.begin();
|
2015-11-24 11:18:23 +00:00
|
|
|
for(;sndidx != snditer->second.end();++sndidx)
|
2015-12-11 23:13:14 +00:00
|
|
|
mOutput->finishSound(sndidx->first);
|
2012-03-20 14:22:17 +00:00
|
|
|
}
|
2015-11-23 14:35:01 +00:00
|
|
|
++snditer;
|
2012-03-18 05:13:57 +00:00
|
|
|
}
|
2015-11-23 12:00:10 +00:00
|
|
|
SaySoundMap::iterator sayiter = mActiveSaySounds.begin();
|
|
|
|
while(sayiter != mActiveSaySounds.end())
|
|
|
|
{
|
2015-12-18 17:11:30 +00:00
|
|
|
if(sayiter->first != MWWorld::ConstPtr() &&
|
2015-11-23 12:00:10 +00:00
|
|
|
sayiter->first != MWMechanics::getPlayer() &&
|
|
|
|
sayiter->first.getCell() == cell)
|
|
|
|
{
|
2016-06-27 18:54:42 +00:00
|
|
|
mOutput->finishStream(sayiter->second);
|
2015-11-23 12:00:10 +00:00
|
|
|
}
|
2015-11-23 14:35:01 +00:00
|
|
|
++sayiter;
|
2015-11-23 12:00:10 +00:00
|
|
|
}
|
2012-01-29 19:27:03 +00:00
|
|
|
}
|
2010-08-16 16:06:27 +00:00
|
|
|
|
2012-03-09 16:10:23 +00:00
|
|
|
void SoundManager::stopSound(const std::string& soundId)
|
|
|
|
{
|
2015-12-18 17:11:30 +00:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.find(MWWorld::ConstPtr());
|
2015-11-23 11:07:04 +00:00
|
|
|
if(snditer != mActiveSounds.end())
|
2012-03-28 10:48:51 +00:00
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
|
|
|
|
SoundBufferRefPairList::iterator sndidx = snditer->second.begin();
|
2015-11-24 11:18:23 +00:00
|
|
|
for(;sndidx != snditer->second.end();++sndidx)
|
2012-03-28 10:48:51 +00:00
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
if(sndidx->second == sfx)
|
2015-12-11 23:13:14 +00:00
|
|
|
mOutput->finishSound(sndidx->first);
|
2012-03-28 10:48:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-09 16:10:23 +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
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
Sound_Buffer *sfx = loadSound(Misc::StringUtils::lowerCase(soundId));
|
|
|
|
SoundBufferRefPairList::iterator sndidx = snditer->second.begin();
|
2015-11-24 11:18:23 +00:00
|
|
|
for(;sndidx != snditer->second.end();++sndidx)
|
2013-07-26 16:43:06 +00:00
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
if(sndidx->second == sfx)
|
2015-11-24 11:18:23 +00:00
|
|
|
sndidx->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));
|
|
|
|
SoundBufferRefPairList::const_iterator sndidx = snditer->second.begin();
|
2015-11-24 11:18:23 +00:00
|
|
|
for(;sndidx != snditer->second.end();++sndidx)
|
2015-11-23 12:10:56 +00:00
|
|
|
{
|
2015-11-30 13:42:51 +00:00
|
|
|
if(sndidx->second == sfx && mOutput->isSoundPlaying(sndidx->first))
|
2015-11-23 12:10:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
2010-08-16 16:06:27 +00:00
|
|
|
|
2011-10-09 07:28:36 +00:00
|
|
|
|
2012-12-18 12:19:35 +00:00
|
|
|
void SoundManager::pauseSounds(int types)
|
2012-12-12 10:33:12 +00:00
|
|
|
{
|
|
|
|
if(mOutput->isInitialized())
|
2012-12-18 12:35:24 +00:00
|
|
|
{
|
|
|
|
types &= Play_TypeMask;
|
2012-12-18 12:19:35 +00:00
|
|
|
mOutput->pauseSounds(types);
|
2012-12-18 12:35:24 +00:00
|
|
|
mPausedSoundTypes |= types;
|
|
|
|
}
|
2012-12-12 10:33:12 +00:00
|
|
|
}
|
|
|
|
|
2012-12-18 12:19:35 +00:00
|
|
|
void SoundManager::resumeSounds(int types)
|
2012-12-12 10:33:12 +00:00
|
|
|
{
|
|
|
|
if(mOutput->isInitialized())
|
2012-12-18 12:35:24 +00:00
|
|
|
{
|
|
|
|
types &= types&Play_TypeMask&mPausedSoundTypes;
|
2012-12-18 12:19:35 +00:00
|
|
|
mOutput->resumeSounds(types);
|
2012-12-18 12:35:24 +00:00
|
|
|
mPausedSoundTypes &= ~types;
|
|
|
|
}
|
2012-12-12 10:33:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
static float sTimeToNextEnvSound = 0.0f;
|
2012-03-09 01:22:16 +00:00
|
|
|
static int total = 0;
|
|
|
|
static std::string regionName = "";
|
2013-08-27 23:04:19 +00:00
|
|
|
static float sTimePassed = 0.0;
|
|
|
|
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
|
|
|
|
2013-08-27 23:04:19 +00:00
|
|
|
sTimePassed += duration;
|
|
|
|
if(!cell->isExterior() || sTimePassed < sTimeToNextEnvSound)
|
2012-03-17 00:08:13 +00:00
|
|
|
return;
|
2012-03-21 22:19:40 +00:00
|
|
|
|
2015-04-22 15:58:55 +00:00
|
|
|
float a = Misc::Rng::rollClosedProbability();
|
2013-08-27 23:04:19 +00:00
|
|
|
// NOTE: We should use the "Minimum Time Between Environmental Sounds" and
|
|
|
|
// "Maximum Time Between Environmental Sounds" fallback settings here.
|
|
|
|
sTimeToNextEnvSound = 5.0f*a + 15.0f*(1.0f-a);
|
|
|
|
sTimePassed = 0;
|
|
|
|
|
|
|
|
if(regionName != cell->mRegion)
|
2012-01-29 19:27:03 +00:00
|
|
|
{
|
2013-08-27 23:04:19 +00:00
|
|
|
regionName = cell->mRegion;
|
2012-03-17 00:08:13 +00:00
|
|
|
total = 0;
|
|
|
|
}
|
2012-03-09 01:22:16 +00:00
|
|
|
|
2013-08-27 23:04:19 +00:00
|
|
|
const ESM::Region *regn = world->getStore().get<ESM::Region>().search(regionName);
|
|
|
|
if(regn == NULL)
|
2012-05-17 17:54:09 +00:00
|
|
|
return;
|
|
|
|
|
2012-03-21 22:19:40 +00:00
|
|
|
std::vector<ESM::Region::SoundRef>::const_iterator soundIter;
|
2012-03-17 00:08:13 +00:00
|
|
|
if(total == 0)
|
|
|
|
{
|
2012-09-17 07:37:50 +00:00
|
|
|
soundIter = regn->mSoundList.begin();
|
|
|
|
while(soundIter != regn->mSoundList.end())
|
2011-10-09 07:28:36 +00:00
|
|
|
{
|
2012-09-17 07:37:50 +00:00
|
|
|
total += (int)soundIter->mChance;
|
2013-07-31 16:46:32 +00:00
|
|
|
++soundIter;
|
2011-10-09 07:28:36 +00:00
|
|
|
}
|
2012-03-23 01:39:10 +00:00
|
|
|
if(total == 0)
|
|
|
|
return;
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
2011-10-09 07:28:36 +00:00
|
|
|
|
2015-04-22 15:58:55 +00:00
|
|
|
int r = Misc::Rng::rollDice(total);
|
2012-03-17 00:08:13 +00:00
|
|
|
int pos = 0;
|
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
soundIter = regn->mSoundList.begin();
|
|
|
|
while(soundIter != regn->mSoundList.end())
|
2012-03-17 00:08:13 +00:00
|
|
|
{
|
2013-07-23 10:44:52 +00:00
|
|
|
if(r - pos < soundIter->mChance)
|
2011-10-09 07:28:36 +00:00
|
|
|
{
|
2013-07-23 10:44:52 +00:00
|
|
|
playSound(soundIter->mSound.toString(), 1.0f, 1.0f);
|
2012-03-17 00:08:13 +00:00
|
|
|
break;
|
2011-10-09 07:28:36 +00:00
|
|
|
}
|
2013-07-23 10:44:52 +00:00
|
|
|
pos += soundIter->mChance;
|
|
|
|
|
2013-07-31 16:46:32 +00:00
|
|
|
++soundIter;
|
2011-10-09 07:28:36 +00:00
|
|
|
}
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
2012-03-17 13:18:59 +00:00
|
|
|
|
2016-11-27 20:19:52 +00:00
|
|
|
void SoundManager::updateWaterSound(float /*duration*/)
|
|
|
|
{
|
|
|
|
MWBase::World* world = MWBase::Environment::get().getWorld();
|
|
|
|
const MWWorld::ConstPtr player = world->getPlayerPtr();
|
|
|
|
osg::Vec3f pos = player.getRefData().getPosition().asVec3();
|
|
|
|
|
|
|
|
float volume = 0.0f;
|
|
|
|
const std::string& soundId = player.getCell()->isExterior() ? mNearWaterOutdoorID : mNearWaterIndoorID;
|
|
|
|
|
|
|
|
if (!mListenerUnderwater)
|
|
|
|
{
|
|
|
|
if (player.getCell()->getCell()->hasWater())
|
|
|
|
{
|
|
|
|
float dist = std::abs(player.getCell()->getWaterLevel() - pos.z());
|
|
|
|
|
|
|
|
if (player.getCell()->isExterior() && dist < mNearWaterOutdoorTolerance)
|
|
|
|
{
|
|
|
|
volume = (mNearWaterOutdoorTolerance - dist) / mNearWaterOutdoorTolerance;
|
|
|
|
|
|
|
|
if (mNearWaterPoints > 1)
|
|
|
|
{
|
|
|
|
int underwaterPoints = 0;
|
|
|
|
|
|
|
|
float step = mNearWaterRadius * 2.0f / (mNearWaterPoints - 1);
|
|
|
|
|
|
|
|
for (int x = 0; x < mNearWaterPoints; x++)
|
|
|
|
{
|
|
|
|
for (int y = 0; y < mNearWaterPoints; y++)
|
|
|
|
{
|
|
|
|
float height = world->getTerrainHeightAt(
|
|
|
|
osg::Vec3f(pos.x() - mNearWaterRadius + x*step, pos.y() - mNearWaterRadius + y*step, 0.0f));
|
|
|
|
|
|
|
|
if (height < 0)
|
|
|
|
underwaterPoints++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
volume *= underwaterPoints * 2.0f / (mNearWaterPoints*mNearWaterPoints);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!player.getCell()->isExterior() && dist < mNearWaterIndoorTolerance)
|
|
|
|
{
|
|
|
|
volume = (mNearWaterIndoorTolerance - dist) / mNearWaterIndoorTolerance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
volume = 1.0f;
|
|
|
|
|
|
|
|
volume = std::min(volume, 1.0f);
|
|
|
|
|
|
|
|
if (mNearWaterSound)
|
|
|
|
{
|
|
|
|
if (volume == 0.0f)
|
|
|
|
{
|
|
|
|
mOutput->finishSound(mNearWaterSound);
|
|
|
|
mNearWaterSound.reset();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool soundIdChanged = false;
|
|
|
|
|
|
|
|
Sound_Buffer* sfx = lookupSound(Misc::StringUtils::lowerCase(soundId));
|
|
|
|
|
|
|
|
for (SoundMap::const_iterator snditer = mActiveSounds.begin(); snditer != mActiveSounds.end(); ++snditer)
|
|
|
|
{
|
|
|
|
for (SoundBufferRefPairList::const_iterator pairiter = snditer->second.begin(); pairiter != snditer->second.end(); ++pairiter)
|
|
|
|
{
|
|
|
|
if (pairiter->first == mNearWaterSound)
|
|
|
|
{
|
|
|
|
if (pairiter->second != sfx)
|
|
|
|
soundIdChanged = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (soundIdChanged)
|
|
|
|
{
|
|
|
|
mOutput->finishSound(mNearWaterSound);
|
|
|
|
mNearWaterSound = playSound(soundId, volume, 1.0f, Play_TypeSfx, Play_Loop);
|
|
|
|
}
|
|
|
|
else if (sfx)
|
|
|
|
mNearWaterSound->setVolume(volume * sfx->mVolume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (volume > 0.0f)
|
|
|
|
mNearWaterSound = playSound(soundId, volume, 1.0f, Play_TypeSfx, Play_Loop);
|
|
|
|
}
|
|
|
|
|
2012-03-22 02:21:36 +00:00
|
|
|
void SoundManager::updateSounds(float duration)
|
2012-03-17 13:18:59 +00:00
|
|
|
{
|
|
|
|
static float timePassed = 0.0;
|
|
|
|
|
|
|
|
timePassed += duration;
|
2012-03-22 02:21:36 +00:00
|
|
|
if(timePassed < (1.0f/30.0f))
|
|
|
|
return;
|
2013-07-26 16:43:06 +00:00
|
|
|
duration = timePassed;
|
2012-03-22 02:21:36 +00:00
|
|
|
timePassed = 0.0f;
|
|
|
|
|
|
|
|
// 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);
|
2013-08-27 20:48:20 +00:00
|
|
|
mUnderwaterSound.reset();
|
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
|
|
|
{
|
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
|
|
|
{
|
2015-12-18 17:11:30 +00:00
|
|
|
MWWorld::ConstPtr ptr = snditer->first;
|
2015-11-30 13:42:51 +00:00
|
|
|
MWBase::SoundPtr sound = sndidx->first;
|
|
|
|
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);
|
2015-11-26 09:26:33 +00:00
|
|
|
Sound_Buffer *sfx = sndidx->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-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())
|
|
|
|
mActiveSounds.erase(snditer++);
|
|
|
|
else
|
|
|
|
++snditer;
|
2012-03-17 13:18:59 +00:00
|
|
|
}
|
2015-11-23 12:00:10 +00:00
|
|
|
|
|
|
|
SaySoundMap::iterator sayiter = mActiveSaySounds.begin();
|
|
|
|
while(sayiter != mActiveSaySounds.end())
|
|
|
|
{
|
2015-12-18 17:11:30 +00:00
|
|
|
MWWorld::ConstPtr ptr = sayiter->first;
|
2016-06-27 18:54:42 +00:00
|
|
|
MWBase::SoundStreamPtr sound = sayiter->second;
|
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
|
|
|
{
|
2015-12-01 19:28:37 +00:00
|
|
|
MWBase::SoundStreamPtr sound = *trkiter;
|
|
|
|
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)
|
|
|
|
if(!(mUnderwaterSound && mOutput->isSoundPlaying(mUnderwaterSound)))
|
|
|
|
mUnderwaterSound = playSound("Underwater", 1.0f, 1.0f, Play_TypeSfx, Play_LoopNoEnv);
|
|
|
|
}
|
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);
|
|
|
|
|
|
|
|
mOutput->updateStream(mMusic);
|
|
|
|
|
|
|
|
if (mMusic->getRealVolume() <= 0.f)
|
|
|
|
{
|
|
|
|
streamMusicFull(mNextMusic);
|
|
|
|
mNextMusic.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-22 02:21:36 +00:00
|
|
|
void SoundManager::update(float duration)
|
|
|
|
{
|
2012-04-06 17:43:14 +00:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
2014-01-21 13:50:58 +00:00
|
|
|
|
|
|
|
if (MWBase::Environment::get().getStateManager()->getState()!=
|
|
|
|
MWBase::StateManager::State_NoGame)
|
|
|
|
{
|
|
|
|
updateSounds(duration);
|
|
|
|
updateRegionSound(duration);
|
2016-11-27 20:19:52 +00:00
|
|
|
updateWaterSound(duration);
|
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)
|
|
|
|
{
|
|
|
|
mMasterVolume = Settings::Manager::getFloat("master volume", "Sound");
|
|
|
|
mMusicVolume = Settings::Manager::getFloat("music volume", "Sound");
|
|
|
|
mSFXVolume = Settings::Manager::getFloat("sfx volume", "Sound");
|
|
|
|
mFootstepsVolume = Settings::Manager::getFloat("footsteps volume", "Sound");
|
|
|
|
mVoiceVolume = Settings::Manager::getFloat("voice volume", "Sound");
|
2012-05-24 12:30:22 +00:00
|
|
|
|
2015-11-25 04:37:38 +00:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
|
|
|
mOutput->startUpdate();
|
2012-05-24 12:30:22 +00:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.begin();
|
2015-11-23 11:07:04 +00:00
|
|
|
for(;snditer != mActiveSounds.end();++snditer)
|
2012-05-24 12:30:22 +00:00
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
SoundBufferRefPairList::iterator sndidx = snditer->second.begin();
|
2015-11-24 11:18:23 +00:00
|
|
|
for(;sndidx != snditer->second.end();++sndidx)
|
2015-11-23 11:07:04 +00:00
|
|
|
{
|
2015-11-24 11:18:23 +00:00
|
|
|
MWBase::SoundPtr sound = sndidx->first;
|
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
|
|
|
}
|
2015-11-23 12:48:18 +00:00
|
|
|
SaySoundMap::iterator sayiter = mActiveSaySounds.begin();
|
|
|
|
for(;sayiter != mActiveSaySounds.end();++sayiter)
|
|
|
|
{
|
2016-06-27 18:54:42 +00:00
|
|
|
MWBase::SoundStreamPtr sound = sayiter->second;
|
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
|
|
|
}
|
2015-12-01 19:28:37 +00:00
|
|
|
TrackList::iterator trkiter = mActiveTracks.begin();
|
|
|
|
for(;trkiter != mActiveTracks.end();++trkiter)
|
|
|
|
{
|
|
|
|
MWBase::SoundStreamPtr sound = *trkiter;
|
2015-11-26 17:11:52 +00:00
|
|
|
sound->setBaseVolume(volumeFromType(sound->getPlayType()));
|
2015-12-01 19:28:37 +00:00
|
|
|
mOutput->updateStream(sound);
|
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()));
|
2015-11-30 22:34:14 +00:00
|
|
|
mOutput->updateStream(mMusic);
|
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;
|
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
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
SoundBufferRefPairList sndlist = snditer->second;
|
2015-11-23 11:07:04 +00:00
|
|
|
mActiveSounds.erase(snditer);
|
|
|
|
mActiveSounds[updated] = sndlist;
|
2014-01-20 11:05:13 +00:00
|
|
|
}
|
2015-11-23 12:00:10 +00:00
|
|
|
SaySoundMap::iterator sayiter = mActiveSaySounds.find(old);
|
|
|
|
if(sayiter != mActiveSaySounds.end())
|
|
|
|
{
|
2016-06-27 18:54:42 +00:00
|
|
|
MWBase::SoundStreamPtr stream = sayiter->second;
|
2015-11-23 12:00:10 +00:00
|
|
|
mActiveSaySounds.erase(sayiter);
|
2016-06-27 18:54:42 +00:00
|
|
|
mActiveSaySounds[updated] = 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()
|
|
|
|
{
|
2015-11-23 11:07:04 +00:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.begin();
|
|
|
|
for(;snditer != mActiveSounds.end();++snditer)
|
|
|
|
{
|
2015-11-26 09:26:33 +00:00
|
|
|
SoundBufferRefPairList::iterator sndidx = snditer->second.begin();
|
2015-11-24 11:18:23 +00:00
|
|
|
for(;sndidx != snditer->second.end();++sndidx)
|
2015-11-23 14:35:01 +00:00
|
|
|
{
|
2015-12-11 23:13:14 +00:00
|
|
|
mOutput->finishSound(sndidx->first);
|
2015-11-26 09:26:33 +00:00
|
|
|
Sound_Buffer *sfx = sndidx->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();
|
2015-11-23 12:00:10 +00:00
|
|
|
SaySoundMap::iterator sayiter = mActiveSaySounds.begin();
|
|
|
|
for(;sayiter != mActiveSaySounds.end();++sayiter)
|
2016-06-27 18:54:42 +00:00
|
|
|
mOutput->finishStream(sayiter->second);
|
2015-11-23 12:00:10 +00:00
|
|
|
mActiveSaySounds.clear();
|
2015-12-01 19:28:37 +00:00
|
|
|
TrackList::iterator trkiter = mActiveTracks.begin();
|
|
|
|
for(;trkiter != mActiveTracks.end();++trkiter)
|
2015-12-11 23:13:14 +00:00
|
|
|
mOutput->finishStream(*trkiter);
|
2015-12-01 19:28:37 +00:00
|
|
|
mActiveTracks.clear();
|
2015-11-23 12:48:18 +00:00
|
|
|
mUnderwaterSound.reset();
|
2016-11-27 20:19:52 +00:00
|
|
|
mNearWaterSound.reset();
|
2014-01-21 13:13:13 +00:00
|
|
|
stopMusic();
|
|
|
|
}
|
2010-07-03 13:04:00 +00:00
|
|
|
}
|