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-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
|
|
|
|
{
|
2015-04-13 20:48:37 +00:00
|
|
|
SoundManager::SoundManager(const VFS::Manager* vfs, bool useSound)
|
|
|
|
: mVFS(vfs)
|
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)
|
|
|
|
, 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-03-17 00:08:13 +00:00
|
|
|
if(!useSound)
|
|
|
|
return;
|
2012-03-07 00:20:15 +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
|
|
|
|
2012-03-17 05:12:17 +00:00
|
|
|
std::cout << "Sound output: " << SOUND_OUT << std::endl;
|
|
|
|
std::cout << "Sound decoder: " << SOUND_IN << std::endl;
|
|
|
|
|
2012-03-18 19:19:54 +00:00
|
|
|
try
|
2012-03-17 05:12:17 +00:00
|
|
|
{
|
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");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
mOutput->init(devname);
|
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
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", "");
|
|
|
|
}
|
2012-03-18 19:19:54 +00:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
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 12:00:10 +00:00
|
|
|
mUnderwaterSound.reset();
|
|
|
|
mActiveSounds.clear();
|
|
|
|
mActiveSaySounds.clear();
|
2015-11-23 12:48:18 +00:00
|
|
|
mUnderwaterSound.reset();
|
2015-11-23 12:00:10 +00:00
|
|
|
mMusic.reset();
|
2015-11-23 09:01:20 +00:00
|
|
|
if(mOutput->isInitialized())
|
|
|
|
{
|
|
|
|
NameBufferMap::iterator sfxiter = mSoundBuffers.begin();
|
|
|
|
for(;sfxiter != mSoundBuffers.end();++sfxiter)
|
|
|
|
{
|
|
|
|
if(sfxiter->second.mHandle)
|
|
|
|
mOutput->unloadSound(sfxiter->second.mHandle);
|
|
|
|
sfxiter->second.mHandle = 0;
|
|
|
|
}
|
2015-11-23 09:33:59 +00:00
|
|
|
sfxiter = mVoiceSoundBuffers.begin();
|
|
|
|
for(;sfxiter != mVoiceSoundBuffers.end();++sfxiter)
|
|
|
|
{
|
|
|
|
if(sfxiter->second.mHandle)
|
|
|
|
mOutput->unloadSound(sfxiter->second.mHandle);
|
|
|
|
sfxiter->second.mHandle = 0;
|
|
|
|
}
|
2015-11-23 09:01:20 +00:00
|
|
|
}
|
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-22 12:53:24 +00:00
|
|
|
// Lookup a soundid for its sound data (resource name, local volume,
|
|
|
|
// minRange and maxRange. The returned pointer is only valid temporarily.
|
|
|
|
const Sound_Buffer *SoundManager::lookup(const std::string &soundId)
|
2010-07-03 13:04:00 +00:00
|
|
|
{
|
2015-11-22 12:53:24 +00:00
|
|
|
NameBufferMap::iterator sfxiter = mSoundBuffers.find(soundId);
|
|
|
|
if(sfxiter == mSoundBuffers.end())
|
2012-03-17 00:08:13 +00:00
|
|
|
{
|
2015-11-22 12:53:24 +00:00
|
|
|
// TODO: We could process all available ESM::Sound records on init
|
|
|
|
// to pre-fill a non-resizing list, which would allow subsystems to
|
|
|
|
// reference sounds by index instead of string.
|
|
|
|
MWBase::World* world = MWBase::Environment::get().getWorld();
|
|
|
|
const ESM::Sound *snd = world->getStore().get<ESM::Sound>().find(soundId);
|
2012-03-17 00:08:13 +00:00
|
|
|
|
2015-11-22 12:53:24 +00:00
|
|
|
float volume, min, max;
|
|
|
|
volume = static_cast<float>(pow(10.0, (snd->mData.mVolume / 255.0*3348.0 - 3348.0) / 2000.0));
|
2014-12-08 16:24:45 +00:00
|
|
|
|
2015-11-22 12:53:24 +00:00
|
|
|
if(snd->mData.mMinRange == 0 && snd->mData.mMaxRange == 0)
|
|
|
|
{
|
|
|
|
static const float fAudioDefaultMinDistance = world->getStore().get<ESM::GameSetting>().find("fAudioDefaultMinDistance")->getFloat();
|
|
|
|
static const float fAudioDefaultMaxDistance = world->getStore().get<ESM::GameSetting>().find("fAudioDefaultMaxDistance")->getFloat();
|
|
|
|
min = fAudioDefaultMinDistance;
|
|
|
|
max = fAudioDefaultMaxDistance;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
min = snd->mData.mMinRange;
|
|
|
|
max = snd->mData.mMaxRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const float fAudioMinDistanceMult = world->getStore().get<ESM::GameSetting>().find("fAudioMinDistanceMult")->getFloat();
|
|
|
|
static const float fAudioMaxDistanceMult = world->getStore().get<ESM::GameSetting>().find("fAudioMaxDistanceMult")->getFloat();
|
|
|
|
min *= fAudioMinDistanceMult;
|
|
|
|
max *= fAudioMaxDistanceMult;
|
|
|
|
min = std::max(min, 1.0f);
|
|
|
|
max = std::max(min, max);
|
|
|
|
|
|
|
|
sfxiter = mSoundBuffers.insert(std::make_pair(
|
|
|
|
soundId, Sound_Buffer("Sound/"+snd->mSound, volume, min, max)
|
|
|
|
)).first;
|
|
|
|
mVFS->normalizeFilename(sfxiter->second.mResourceName);
|
2015-11-23 09:01:20 +00:00
|
|
|
sfxiter->second.mHandle = mOutput->loadSound(sfxiter->second.mResourceName);
|
2015-11-22 12:53:24 +00:00
|
|
|
}
|
2015-11-23 09:01:20 +00:00
|
|
|
else if(!sfxiter->second.mHandle)
|
|
|
|
sfxiter->second.mHandle = mOutput->loadSound(sfxiter->second.mResourceName);
|
|
|
|
|
2015-11-22 12:53:24 +00:00
|
|
|
return &sfxiter->second;
|
2010-07-03 13:04:00 +00:00
|
|
|
}
|
2010-08-14 07:26:00 +00:00
|
|
|
|
2015-11-23 09:33:59 +00:00
|
|
|
const Sound_Buffer *SoundManager::lookupVoice(const std::string &voicefile)
|
|
|
|
{
|
|
|
|
NameBufferMap::iterator sfxiter = mVoiceSoundBuffers.find(voicefile);
|
|
|
|
if(sfxiter == mVoiceSoundBuffers.end())
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
|
|
|
float minDistance = fAudioVoiceDefaultMinDistance * fAudioMinDistanceMult;
|
|
|
|
float maxDistance = fAudioVoiceDefaultMaxDistance * fAudioMaxDistanceMult;
|
|
|
|
minDistance = std::max(minDistance, 1.f);
|
|
|
|
maxDistance = std::max(minDistance, maxDistance);
|
|
|
|
|
|
|
|
sfxiter = mVoiceSoundBuffers.insert(std::make_pair(
|
|
|
|
voicefile, Sound_Buffer("sound/"+voicefile, 1.0f, minDistance, maxDistance)
|
|
|
|
)).first;
|
|
|
|
mVFS->normalizeFilename(sfxiter->second.mResourceName);
|
2015-11-23 10:08:27 +00:00
|
|
|
sfxiter->second.mHandle = mOutput->loadSound(sfxiter->second.mResourceName, &sfxiter->second.mLoudness);
|
2015-11-23 09:33:59 +00:00
|
|
|
}
|
|
|
|
else if(!sfxiter->second.mHandle)
|
2015-11-23 10:08:27 +00:00
|
|
|
sfxiter->second.mHandle = mOutput->loadSound(sfxiter->second.mResourceName, &sfxiter->second.mLoudness);
|
2015-11-23 09:33:59 +00:00
|
|
|
|
|
|
|
return &sfxiter->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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)
|
2012-03-18 19:03:15 +00:00
|
|
|
mMusic->stop();
|
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;
|
2012-03-20 17:34:36 +00:00
|
|
|
try
|
2012-03-17 10:06:35 +00:00
|
|
|
{
|
2012-03-31 17:59:29 +00:00
|
|
|
stopMusic();
|
2012-12-13 06:32:02 +00:00
|
|
|
|
|
|
|
DecoderPtr decoder = getDecoder();
|
|
|
|
decoder->open(filename);
|
|
|
|
|
2012-12-18 10:01:04 +00:00
|
|
|
mMusic = mOutput->streamSound(decoder, volumeFromType(Play_TypeMusic),
|
|
|
|
1.0f, Play_NoEnv|Play_TypeMusic);
|
2012-03-20 17:34:36 +00:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
std::cout << "Music Error: " << e.what() << "\n";
|
2012-03-17 10:06:35 +00:00
|
|
|
}
|
2011-01-05 21:18:21 +00:00
|
|
|
}
|
2010-11-12 00:47:26 +00:00
|
|
|
|
2012-03-20 19:39:49 +00:00
|
|
|
void SoundManager::streamMusic(const std::string& filename)
|
|
|
|
{
|
|
|
|
streamMusicFull("Music/"+filename);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
if(!filelist.size())
|
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
|
|
|
}
|
|
|
|
|
2013-04-04 13:10:27 +00:00
|
|
|
streamMusicFull(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
|
|
|
{
|
2012-03-17 09:45:18 +00:00
|
|
|
return mMusic && mMusic->isPlaying();
|
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-11-23 09:33:59 +00:00
|
|
|
void SoundManager::say(const MWWorld::Ptr &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-23 12:00:10 +00:00
|
|
|
std::string voicefile = Misc::StringUtils::lowerCase(filename);
|
|
|
|
const Sound_Buffer *sfx = lookupVoice(voicefile);
|
2012-12-18 10:01:04 +00:00
|
|
|
float basevol = volumeFromType(Play_TypeVoice);
|
2012-08-29 16:48:20 +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-28 11:58:47 +00:00
|
|
|
|
2015-11-23 09:33:59 +00:00
|
|
|
MWBase::SoundPtr sound = mOutput->playSound3D(sfx->mHandle,
|
|
|
|
objpos, sfx->mVolume, basevol, 1.0f, sfx->mMinDist, sfx->mMaxDist, Play_Normal|Play_TypeVoice, 0
|
|
|
|
);
|
2015-11-23 12:00:10 +00:00
|
|
|
mActiveSaySounds[ptr] = std::make_pair(sound, voicefile);
|
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
|
|
|
}
|
|
|
|
|
2014-07-28 22:26:26 +00:00
|
|
|
float SoundManager::getSaySoundLoudness(const MWWorld::Ptr &ptr) const
|
|
|
|
{
|
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
|
|
|
{
|
2015-11-23 12:00:10 +00:00
|
|
|
NameBufferMap::const_iterator sfxiter = mVoiceSoundBuffers.find(snditer->second.second);
|
|
|
|
if(sfxiter != mVoiceSoundBuffers.end())
|
2015-11-23 11:07:04 +00:00
|
|
|
{
|
2015-11-23 12:00:10 +00:00
|
|
|
float sec = snditer->second.first->getTimeOffset();
|
|
|
|
if(snditer->second.first->isPlaying())
|
|
|
|
return sfxiter->second.mLoudness.getLoudnessAtTime(sec);
|
2015-11-23 11:07:04 +00:00
|
|
|
}
|
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-23 12:00:10 +00:00
|
|
|
std::string voicefile = Misc::StringUtils::lowerCase(filename);
|
|
|
|
const Sound_Buffer *sfx = lookupVoice(voicefile);
|
2012-12-18 10:01:04 +00:00
|
|
|
float basevol = volumeFromType(Play_TypeVoice);
|
2012-05-02 03:30:31 +00:00
|
|
|
|
2015-11-23 09:33:59 +00:00
|
|
|
MWBase::SoundPtr sound = mOutput->playSound(sfx->mHandle,
|
|
|
|
sfx->mVolume, basevol, 1.0f, Play_Normal|Play_TypeVoice, 0
|
|
|
|
);
|
2015-11-23 12:00:10 +00:00
|
|
|
mActiveSaySounds[MWWorld::Ptr()] = std::make_pair(sound, voicefile);
|
2012-05-02 03:30:31 +00:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
std::cout <<"Sound Error: "<<e.what()<< std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 20:07:15 +00:00
|
|
|
bool SoundManager::sayDone(const MWWorld::Ptr &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())
|
|
|
|
{
|
|
|
|
if(snditer->second.first->isPlaying())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 20:07:15 +00:00
|
|
|
void SoundManager::stopSay(const MWWorld::Ptr &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
|
|
|
{
|
2015-11-23 12:00:10 +00:00
|
|
|
snditer->second.first->stop();
|
|
|
|
mActiveSaySounds.erase(snditer);
|
2012-05-02 03:30:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-18 09:35:20 +00:00
|
|
|
MWBase::SoundPtr SoundManager::playTrack(const DecoderPtr& decoder, PlayType type)
|
2012-12-13 08:05:57 +00:00
|
|
|
{
|
|
|
|
MWBase::SoundPtr track;
|
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return track;
|
|
|
|
try
|
|
|
|
{
|
2012-12-18 10:01:04 +00:00
|
|
|
track = mOutput->streamSound(decoder, volumeFromType(type), 1.0f, Play_NoEnv|type);
|
2012-12-13 08:05:57 +00:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
std::cout <<"Sound Error: "<<e.what()<< std::endl;
|
|
|
|
}
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
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-22 12:53:24 +00:00
|
|
|
const Sound_Buffer *sfx = lookup(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-11-23 09:01:20 +00:00
|
|
|
sound = mOutput->playSound(sfx->mHandle,
|
2015-11-22 12:53:24 +00:00
|
|
|
volume * sfx->mVolume, basevol, pitch, mode|type, offset
|
|
|
|
);
|
2015-11-23 11:07:04 +00:00
|
|
|
mActiveSounds[MWWorld::Ptr()].push_back(std::make_pair(sound, soundId));
|
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;
|
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
|
|
|
}
|
|
|
|
|
2013-01-15 20:07:15 +00:00
|
|
|
MWBase::SoundPtr SoundManager::playSound3D(const MWWorld::Ptr &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-22 12:53:24 +00:00
|
|
|
const Sound_Buffer *sfx = lookup(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-23 09:01:20 +00:00
|
|
|
sound = mOutput->playSound3D(sfx->mHandle,
|
2015-11-22 12:53:24 +00:00
|
|
|
objpos, volume * sfx->mVolume, basevol, pitch, sfx->mMinDist, sfx->mMaxDist, mode|type, offset
|
|
|
|
);
|
2012-03-31 14:31:55 +00:00
|
|
|
if((mode&Play_NoTrack))
|
2015-11-23 11:07:04 +00:00
|
|
|
mActiveSounds[MWWorld::Ptr()].push_back(std::make_pair(sound, soundId));
|
2012-03-31 12:57:03 +00:00
|
|
|
else
|
2015-11-23 11:07:04 +00:00
|
|
|
mActiveSounds[ptr].push_back(std::make_pair(sound, soundId));
|
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;
|
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-05-12 17:02:56 +00:00
|
|
|
MWBase::SoundPtr SoundManager::playManualSound3D(const osg::Vec3f& initialPos, const std::string& soundId,
|
2014-05-16 11:09:23 +00:00
|
|
|
float volume, float pitch, PlayType type, PlayMode mode, float offset)
|
|
|
|
{
|
|
|
|
MWBase::SoundPtr sound;
|
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return sound;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Look up the sound in the ESM data
|
2015-11-22 12:53:24 +00:00
|
|
|
const Sound_Buffer *sfx = lookup(Misc::StringUtils::lowerCase(soundId));
|
2014-05-16 11:09:23 +00:00
|
|
|
float basevol = volumeFromType(type);
|
|
|
|
|
2015-11-23 09:01:20 +00:00
|
|
|
sound = mOutput->playSound3D(sfx->mHandle,
|
2015-11-22 12:53:24 +00:00
|
|
|
initialPos, volume * sfx->mVolume, basevol, pitch, sfx->mMinDist, sfx->mMaxDist, mode|type, offset
|
|
|
|
);
|
2015-11-23 11:07:04 +00:00
|
|
|
mActiveSounds[MWWorld::Ptr()].push_back(std::make_pair(sound, soundId));
|
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;
|
|
|
|
}
|
|
|
|
return sound;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundManager::stopSound (MWBase::SoundPtr sound)
|
|
|
|
{
|
|
|
|
SoundMap::iterator snditer = mActiveSounds.begin();
|
|
|
|
while(snditer != mActiveSounds.end())
|
|
|
|
{
|
2015-11-23 11:07:04 +00:00
|
|
|
SoundNamePairList::iterator sndname = snditer->second.begin();
|
|
|
|
for(;sndname != snditer->second.end();++sndname)
|
2014-05-16 11:09:23 +00:00
|
|
|
{
|
2015-11-23 11:07:04 +00:00
|
|
|
if(sndname->first != sound)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sndname->first->stop();
|
|
|
|
snditer->second.erase(sndname);
|
|
|
|
if(snditer->second.empty())
|
|
|
|
mActiveSounds.erase(snditer);
|
|
|
|
return;
|
2014-05-16 11:09:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 20:07:15 +00:00
|
|
|
void SoundManager::stopSound3D(const MWWorld::Ptr &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-23 11:07:04 +00:00
|
|
|
SoundNamePairList::iterator sndname = snditer->second.begin();
|
|
|
|
for(;sndname != snditer->second.end();++sndname)
|
2012-03-28 10:48:51 +00:00
|
|
|
{
|
2015-11-23 11:07:04 +00:00
|
|
|
if(sndname->second != soundId)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sndname->first->stop();
|
|
|
|
snditer->second.erase(sndname);
|
|
|
|
if(snditer->second.empty())
|
|
|
|
mActiveSounds.erase(snditer);
|
|
|
|
return;
|
2012-03-28 10:48:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 10:20:50 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 20:07:15 +00:00
|
|
|
void SoundManager::stopSound3D(const MWWorld::Ptr &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-23 11:07:04 +00:00
|
|
|
SoundNamePairList::iterator sndname = snditer->second.begin();
|
|
|
|
for(;sndname != snditer->second.end();++sndname)
|
|
|
|
sndname->first->stop();
|
|
|
|
mActiveSounds.erase(snditer);
|
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-11-23 11:07:04 +00:00
|
|
|
if(snditer->first != MWWorld::Ptr() &&
|
|
|
|
snditer->first != MWMechanics::getPlayer() &&
|
|
|
|
snditer->first.getCell() == cell)
|
2012-03-20 14:22:17 +00:00
|
|
|
{
|
2015-11-23 11:07:04 +00:00
|
|
|
SoundNamePairList::iterator sndname = snditer->second.begin();
|
|
|
|
for(;sndname != snditer->second.end();++sndname)
|
|
|
|
sndname->first->stop();
|
2012-03-18 18:17:45 +00:00
|
|
|
mActiveSounds.erase(snditer++);
|
2012-03-20 14:22:17 +00:00
|
|
|
}
|
2012-03-18 05:13:57 +00:00
|
|
|
else
|
2013-07-31 16:46:32 +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())
|
|
|
|
{
|
|
|
|
if(sayiter->first != MWWorld::Ptr() &&
|
|
|
|
sayiter->first != MWMechanics::getPlayer() &&
|
|
|
|
sayiter->first.getCell() == cell)
|
|
|
|
{
|
|
|
|
sayiter->second.first->stop();
|
|
|
|
mActiveSaySounds.erase(sayiter++);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++sayiter;
|
|
|
|
}
|
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-11-23 11:07:04 +00:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.find(MWWorld::Ptr());
|
|
|
|
if(snditer != mActiveSounds.end())
|
2012-03-28 10:48:51 +00:00
|
|
|
{
|
2015-11-23 11:07:04 +00:00
|
|
|
SoundNamePairList::iterator sndname = snditer->second.begin();
|
|
|
|
for(;sndname != snditer->second.end();++sndname)
|
2012-03-28 10:48:51 +00:00
|
|
|
{
|
2015-11-23 11:07:04 +00:00
|
|
|
if(sndname->second != soundId)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
sndname->first->stop();
|
|
|
|
snditer->second.erase(sndname);
|
|
|
|
if(snditer->second.empty())
|
|
|
|
mActiveSounds.erase(snditer);
|
|
|
|
return;
|
2012-03-28 10:48:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-09 16:10:23 +00:00
|
|
|
}
|
|
|
|
|
2013-07-26 16:43:06 +00:00
|
|
|
void SoundManager::fadeOutSound3D(const MWWorld::Ptr &ptr,
|
|
|
|
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-23 11:07:04 +00:00
|
|
|
SoundNamePairList::iterator sndname = snditer->second.begin();
|
|
|
|
for(;sndname != snditer->second.end();++sndname)
|
2013-07-26 16:43:06 +00:00
|
|
|
{
|
2015-11-23 11:07:04 +00:00
|
|
|
if(sndname->second == soundId)
|
|
|
|
sndname->first->setFadeout(duration);
|
2013-07-26 16:43:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 20:07:15 +00:00
|
|
|
bool SoundManager::getSoundPlaying(const MWWorld::Ptr &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())
|
|
|
|
{
|
|
|
|
SoundNamePairList::const_iterator sndname = snditer->second.begin();
|
|
|
|
for(;sndname != snditer->second.end();++sndname)
|
|
|
|
{
|
|
|
|
if(sndname->second == soundId && sndname->first->isPlaying())
|
|
|
|
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();
|
2014-01-08 17:39:44 +00:00
|
|
|
const MWWorld::Ptr 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
|
|
|
|
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
|
|
|
|
if(!isMusicPlaying())
|
|
|
|
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
|
|
|
{
|
2013-08-27 20:48:20 +00:00
|
|
|
mUnderwaterSound->stop();
|
|
|
|
mUnderwaterSound.reset();
|
2013-08-08 17:16:05 +00:00
|
|
|
}
|
2012-03-31 17:06:12 +00:00
|
|
|
|
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
|
|
|
|
2015-11-23 12:31:15 +00:00
|
|
|
if(mListenerUnderwater)
|
|
|
|
{
|
|
|
|
// Play underwater sound (after updating listener)
|
|
|
|
if(!(mUnderwaterSound && mUnderwaterSound->isPlaying()))
|
|
|
|
mUnderwaterSound = playSound("Underwater", 1.0f, 1.0f, Play_TypeSfx, Play_LoopNoEnv);
|
|
|
|
}
|
|
|
|
|
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-23 11:07:04 +00:00
|
|
|
SoundNamePairList::iterator sndname = snditer->second.begin();
|
|
|
|
while(sndname != snditer->second.end())
|
2012-03-30 14:01:37 +00:00
|
|
|
{
|
2015-11-23 12:31:15 +00:00
|
|
|
if(!updateSound(sndname->first, snditer->first, duration))
|
2015-11-23 11:07:04 +00:00
|
|
|
sndname = snditer->second.erase(sndname);
|
2015-11-23 12:31:15 +00:00
|
|
|
else
|
|
|
|
++sndname;
|
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-11-23 12:31:15 +00:00
|
|
|
if(!updateSound(sayiter->second.first, sayiter->first, duration))
|
2015-11-23 12:00:10 +00:00
|
|
|
mActiveSaySounds.erase(sayiter++);
|
2015-11-23 12:31:15 +00:00
|
|
|
else
|
|
|
|
++sayiter;
|
|
|
|
}
|
|
|
|
}
|
2015-11-23 12:00:10 +00:00
|
|
|
|
2015-11-23 12:31:15 +00:00
|
|
|
bool SoundManager::updateSound(MWBase::SoundPtr sound, const MWWorld::Ptr& ptr, float duration)
|
|
|
|
{
|
|
|
|
if(!ptr.isEmpty())
|
|
|
|
{
|
|
|
|
const ESM::Position &pos = ptr.getRefData().getPosition();
|
|
|
|
const osg::Vec3f objpos(pos.asVec3());
|
|
|
|
sound->setPosition(objpos);
|
2015-11-23 12:00:10 +00:00
|
|
|
|
2015-11-23 12:31:15 +00:00
|
|
|
if((sound->mFlags&Play_RemoveAtDistance))
|
2015-11-23 12:00:10 +00:00
|
|
|
{
|
2015-11-23 12:31:15 +00:00
|
|
|
osg::Vec3f diff = mListenerPos - ptr.getRefData().getPosition().asVec3();
|
|
|
|
if(diff.length2() > 2000*2000)
|
|
|
|
sound->stop();
|
2015-11-23 12:00:10 +00:00
|
|
|
}
|
2015-11-23 12:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!sound->isPlaying())
|
|
|
|
return false;
|
2015-11-23 12:00:10 +00:00
|
|
|
|
2015-11-23 12:31:15 +00:00
|
|
|
// Update fade out
|
|
|
|
if(sound->mFadeOutTime > 0.0f)
|
|
|
|
{
|
|
|
|
float soundDuration = duration;
|
|
|
|
if(soundDuration > sound->mFadeOutTime)
|
|
|
|
soundDuration = sound->mFadeOutTime;
|
|
|
|
sound->setVolume(sound->mVolume - soundDuration/sound->mFadeOutTime*sound->mVolume);
|
|
|
|
sound->mFadeOutTime -= soundDuration;
|
2015-11-23 12:00:10 +00:00
|
|
|
}
|
2015-11-23 12:31:15 +00:00
|
|
|
sound->update();
|
|
|
|
return true;
|
2012-03-22 02:21:36 +00:00
|
|
|
}
|
2015-11-23 12:31:15 +00:00
|
|
|
|
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);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
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-23 11:07:04 +00:00
|
|
|
SoundNamePairList::iterator sndname = snditer->second.begin();
|
|
|
|
for(;sndname != snditer->second.end();++sndname)
|
|
|
|
{
|
2015-11-23 12:48:18 +00:00
|
|
|
MWBase::SoundPtr sound = sndname->first;
|
|
|
|
sound->mBaseVolume = volumeFromType(sound->getPlayType());
|
|
|
|
sound->update();
|
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)
|
|
|
|
{
|
|
|
|
MWBase::SoundPtr sound = sayiter->second.first;
|
|
|
|
sound->mBaseVolume = volumeFromType(sound->getPlayType());
|
|
|
|
sound->update();
|
|
|
|
}
|
2012-05-24 12:30:22 +00:00
|
|
|
if(mMusic)
|
|
|
|
{
|
2012-12-18 10:01:04 +00:00
|
|
|
mMusic->mBaseVolume = volumeFromType(mMusic->getPlayType());
|
2012-05-24 12:30:22 +00:00
|
|
|
mMusic->update();
|
|
|
|
}
|
2012-05-24 02:34:53 +00:00
|
|
|
}
|
|
|
|
|
2015-05-12 17:02:56 +00:00
|
|
|
void SoundManager::setListenerPosDir(const osg::Vec3f &pos, const osg::Vec3f &dir, const osg::Vec3f &up)
|
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-11-23 11:07:04 +00:00
|
|
|
MWWorld::Ptr player = MWMechanics::getPlayer();
|
2015-02-08 14:39:10 +00:00
|
|
|
const MWWorld::CellStore *cell = player.getCell();
|
2014-06-25 16:10:26 +00:00
|
|
|
|
2015-05-12 17:02:56 +00:00
|
|
|
mListenerUnderwater = ((cell->getCell()->mData.mFlags&ESM::Cell::HasWater) && mListenerPos.z() < cell->getWaterLevel());
|
2012-08-09 13:01:03 +00:00
|
|
|
}
|
|
|
|
|
2014-01-20 11:05:13 +00:00
|
|
|
void SoundManager::updatePtr(const MWWorld::Ptr &old, const MWWorld::Ptr &updated)
|
|
|
|
{
|
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-23 11:07:04 +00:00
|
|
|
SoundNamePairList sndlist = snditer->second;
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
SoundNamePair sndlist = sayiter->second;
|
|
|
|
mActiveSaySounds.erase(sayiter);
|
|
|
|
mActiveSaySounds[updated] = sndlist;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
SoundNamePairList::iterator sndname = snditer->second.begin();
|
|
|
|
for(;sndname != snditer->second.end();++sndname)
|
|
|
|
sndname->first->stop();
|
|
|
|
}
|
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)
|
|
|
|
sayiter->second.first->stop();
|
|
|
|
mActiveSaySounds.clear();
|
2015-11-23 12:48:18 +00:00
|
|
|
mUnderwaterSound.reset();
|
2014-01-21 13:13:13 +00:00
|
|
|
stopMusic();
|
|
|
|
}
|
2010-07-03 13:04:00 +00:00
|
|
|
}
|