2012-08-09 14:33:21 +02:00
|
|
|
#include "soundmanagerimp.hpp"
|
2010-07-03 15:04:00 +02:00
|
|
|
|
2010-08-14 14:28:17 +02:00
|
|
|
#include <iostream>
|
2011-10-09 09:28:36 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
|
|
|
|
2012-04-23 15:27:03 +02:00
|
|
|
#include "../mwbase/environment.hpp"
|
2012-07-03 12:30:50 +02:00
|
|
|
#include "../mwbase/world.hpp"
|
2012-04-23 15:27:03 +02:00
|
|
|
|
2013-08-27 16:04:19 -07:00
|
|
|
#include "../mwworld/esmstore.hpp"
|
2011-10-09 09:28:36 +02:00
|
|
|
#include "../mwworld/player.hpp"
|
2010-08-14 14:28:17 +02:00
|
|
|
|
2012-03-16 23:40:07 -07:00
|
|
|
#include "sound_output.hpp"
|
2012-03-16 23:18:15 -07:00
|
|
|
#include "sound_decoder.hpp"
|
2012-03-17 02:45:18 -07:00
|
|
|
#include "sound.hpp"
|
2012-03-16 22:12:17 -07:00
|
|
|
|
|
|
|
#include "openal_output.hpp"
|
|
|
|
#define SOUND_OUT "OpenAL"
|
2012-04-01 15:02:07 -07:00
|
|
|
/* Set up the sound manager to use FFMPEG, MPG123+libsndfile, or Audiere for
|
|
|
|
* input. The OPENMW_USE_x macros are set in CMakeLists.txt.
|
2010-08-16 18:06:27 +02:00
|
|
|
*/
|
2010-08-13 17:11:03 +02:00
|
|
|
#ifdef OPENMW_USE_FFMPEG
|
2012-03-17 22:45:28 -07:00
|
|
|
#include "ffmpeg_decoder.hpp"
|
2012-03-21 15:29:05 -07:00
|
|
|
#ifndef SOUND_IN
|
2010-08-18 14:50:49 +02:00
|
|
|
#define SOUND_IN "FFmpeg"
|
2010-08-13 17:11:03 +02:00
|
|
|
#endif
|
2012-03-21 15:29:05 -07:00
|
|
|
#endif
|
2010-08-12 16:13:54 +02:00
|
|
|
|
2012-04-01 15:02:07 -07:00
|
|
|
#ifdef OPENMW_USE_AUDIERE
|
|
|
|
#include "audiere_decoder.hpp"
|
|
|
|
#ifndef SOUND_IN
|
|
|
|
#define SOUND_IN "Audiere"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2010-08-13 22:30:22 +02:00
|
|
|
#ifdef OPENMW_USE_MPG123
|
2012-03-16 23:18:15 -07:00
|
|
|
#include "mpgsnd_decoder.hpp"
|
2012-03-21 15:29:05 -07:00
|
|
|
#ifndef SOUND_IN
|
2010-08-18 14:50:49 +02:00
|
|
|
#define SOUND_IN "mpg123,sndfile"
|
2010-08-13 22:30:22 +02:00
|
|
|
#endif
|
2012-03-21 15:29:05 -07:00
|
|
|
#endif
|
2010-08-13 22:30:22 +02:00
|
|
|
|
2010-08-12 16:13:54 +02:00
|
|
|
|
2010-07-03 15:04:00 +02:00
|
|
|
namespace MWSound
|
|
|
|
{
|
2012-04-23 15:27:03 +02:00
|
|
|
SoundManager::SoundManager(bool useSound)
|
2012-03-24 00:22:54 -07:00
|
|
|
: mResourceMgr(Ogre::ResourceGroupManager::getSingleton())
|
2012-03-31 03:31:41 -07:00
|
|
|
, mOutput(new DEFAULT_OUTPUT(*this))
|
2012-04-07 16:00:30 -07:00
|
|
|
, mMasterVolume(1.0f)
|
|
|
|
, mSFXVolume(1.0f)
|
|
|
|
, mMusicVolume(1.0f)
|
2012-05-24 04:34:53 +02:00
|
|
|
, mFootstepsVolume(1.0f)
|
|
|
|
, mVoiceVolume(1.0f)
|
2012-12-18 04:35:24 -08:00
|
|
|
, mPausedSoundTypes(0)
|
2013-07-30 23:24:18 +02:00
|
|
|
, mListenerPos(0,0,0)
|
|
|
|
, mListenerDir(1,0,0)
|
|
|
|
, mListenerUp(0,0,1)
|
2010-08-12 16:13:54 +02:00
|
|
|
{
|
2012-03-16 17:08:13 -07:00
|
|
|
if(!useSound)
|
|
|
|
return;
|
2012-03-07 02:20:15 +02:00
|
|
|
|
2012-04-07 16:00:30 -07:00
|
|
|
mMasterVolume = Settings::Manager::getFloat("master volume", "Sound");
|
|
|
|
mMasterVolume = std::min(std::max(mMasterVolume, 0.0f), 1.0f);
|
|
|
|
mSFXVolume = Settings::Manager::getFloat("sfx volume", "Sound");
|
|
|
|
mSFXVolume = std::min(std::max(mSFXVolume, 0.0f), 1.0f);
|
|
|
|
mMusicVolume = Settings::Manager::getFloat("music volume", "Sound");
|
|
|
|
mMusicVolume = std::min(std::max(mMusicVolume, 0.0f), 1.0f);
|
2012-05-24 04:37:41 +02:00
|
|
|
mVoiceVolume = Settings::Manager::getFloat("voice volume", "Sound");
|
2012-05-24 08:56:45 +02:00
|
|
|
mVoiceVolume = std::min(std::max(mVoiceVolume, 0.0f), 1.0f);
|
2012-05-24 04:37:41 +02:00
|
|
|
mFootstepsVolume = Settings::Manager::getFloat("footsteps volume", "Sound");
|
2012-05-24 08:56:45 +02:00
|
|
|
mFootstepsVolume = std::min(std::max(mFootstepsVolume, 0.0f), 1.0f);
|
2012-04-07 16:00:30 -07:00
|
|
|
|
2012-03-16 22:12:17 -07:00
|
|
|
std::cout << "Sound output: " << SOUND_OUT << std::endl;
|
|
|
|
std::cout << "Sound decoder: " << SOUND_IN << std::endl;
|
|
|
|
|
2012-03-18 12:19:54 -07:00
|
|
|
try
|
2012-03-16 22:12:17 -07:00
|
|
|
{
|
2012-03-21 20:15:01 -07: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 15:28:38 -07:00
|
|
|
std::string devname = Settings::Manager::getString("device", "Sound");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
mOutput->init(devname);
|
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
if(devname.empty())
|
|
|
|
throw;
|
2012-12-17 21:09:57 -08:00
|
|
|
std::cerr <<"Failed to open device \""<<devname<<"\": " << e.what() << std::endl;
|
2012-04-07 15:28:38 -07:00
|
|
|
mOutput->init();
|
|
|
|
Settings::Manager::setString("device", "Sound", "");
|
|
|
|
}
|
2012-03-18 12:19:54 -07:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
std::cout <<"Sound init failed: "<<e.what()<< std::endl;
|
2012-03-16 22:12:17 -07:00
|
|
|
}
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
2010-08-14 14:28:17 +02:00
|
|
|
|
2012-03-06 01:21:00 +02:00
|
|
|
SoundManager::~SoundManager()
|
2010-09-07 15:21:38 +01:00
|
|
|
{
|
2013-08-27 13:48:20 -07:00
|
|
|
mUnderwaterSound.reset();
|
2012-03-18 11:17:45 -07:00
|
|
|
mActiveSounds.clear();
|
2012-03-17 03:01:51 -07:00
|
|
|
mMusic.reset();
|
2012-03-18 11:17:45 -07:00
|
|
|
mOutput.reset();
|
2010-09-07 15:21:38 +01:00
|
|
|
}
|
|
|
|
|
2012-03-17 23:30:43 -07:00
|
|
|
// Return a new decoder instance, used as needed by the output implementations
|
|
|
|
DecoderPtr SoundManager::getDecoder()
|
|
|
|
{
|
|
|
|
return DecoderPtr(new DEFAULT_DECODER);
|
|
|
|
}
|
|
|
|
|
2010-08-16 18:06:27 +02:00
|
|
|
// Convert a soundId to file name, and modify the volume
|
|
|
|
// according to the sounds local volume setting, minRange and
|
|
|
|
// maxRange.
|
2012-03-06 01:21:00 +02:00
|
|
|
std::string SoundManager::lookup(const std::string &soundId,
|
2010-08-16 18:06:27 +02:00
|
|
|
float &volume, float &min, float &max)
|
2010-07-03 15:04:00 +02:00
|
|
|
{
|
2012-11-05 21:45:18 +04:00
|
|
|
const ESM::Sound *snd =
|
|
|
|
MWBase::Environment::get().getWorld()->getStore().get<ESM::Sound>().find(soundId);
|
2012-03-16 17:08:13 -07:00
|
|
|
|
2012-09-17 11:37:50 +04:00
|
|
|
volume *= pow(10.0, (snd->mData.mVolume/255.0*3348.0 - 3348.0) / 2000.0);
|
2012-03-16 17:08:13 -07:00
|
|
|
|
2012-09-17 11:37:50 +04:00
|
|
|
if(snd->mData.mMinRange == 0 && snd->mData.mMaxRange == 0)
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
|
|
|
min = 100.0f;
|
|
|
|
max = 2000.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-17 11:37:50 +04:00
|
|
|
min = snd->mData.mMinRange * 20.0f;
|
|
|
|
max = snd->mData.mMaxRange * 50.0f;
|
2012-03-16 17:08:13 -07:00
|
|
|
min = std::max(min, 1.0f);
|
|
|
|
max = std::max(min, max);
|
|
|
|
}
|
|
|
|
|
2012-09-17 11:37:50 +04:00
|
|
|
return "Sound/"+snd->mSound;
|
2010-07-03 15:04:00 +02:00
|
|
|
}
|
2010-08-14 09:26:00 +02:00
|
|
|
|
2012-12-18 02:01:04 -08: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-18 19:01:13 -07:00
|
|
|
case Play_TypeFoot:
|
|
|
|
volume *= mFootstepsVolume;
|
|
|
|
break;
|
2012-12-18 02:01:04 -08:00
|
|
|
case Play_TypeMusic:
|
|
|
|
case Play_TypeMovie:
|
|
|
|
volume *= mMusicVolume;
|
|
|
|
break;
|
|
|
|
case Play_TypeMask:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return volume;
|
|
|
|
}
|
2010-07-03 15:04:00 +02:00
|
|
|
|
2013-01-15 12:07:15 -08:00
|
|
|
bool SoundManager::isPlaying(const MWWorld::Ptr &ptr, const std::string &id) const
|
2010-08-14 09:26:00 +02:00
|
|
|
{
|
2012-03-28 03:48:51 -07:00
|
|
|
SoundMap::const_iterator snditer = mActiveSounds.begin();
|
|
|
|
while(snditer != mActiveSounds.end())
|
|
|
|
{
|
|
|
|
if(snditer->second.first == ptr && snditer->second.second == id)
|
|
|
|
return snditer->first->isPlaying();
|
2013-07-31 18:46:32 +02:00
|
|
|
++snditer;
|
2012-03-28 03:48:51 -07:00
|
|
|
}
|
|
|
|
return false;
|
2010-08-14 09:26:00 +02:00
|
|
|
}
|
|
|
|
|
2010-08-14 14:28:17 +02:00
|
|
|
|
2012-03-09 03:56:29 +02:00
|
|
|
void SoundManager::stopMusic()
|
|
|
|
{
|
2012-03-17 02:45:18 -07:00
|
|
|
if(mMusic)
|
2012-03-18 12:03:15 -07:00
|
|
|
mMusic->stop();
|
2012-03-20 10:34:36 -07:00
|
|
|
mMusic.reset();
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
2011-06-12 16:36:18 -04:00
|
|
|
|
2012-03-20 12:39:49 -07:00
|
|
|
void SoundManager::streamMusicFull(const std::string& filename)
|
2011-06-15 22:33:31 +02:00
|
|
|
{
|
2012-04-06 10:43:14 -07:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
2012-03-20 11:31:13 -07:00
|
|
|
std::cout <<"Playing "<<filename<< std::endl;
|
2012-03-20 10:34:36 -07:00
|
|
|
try
|
2012-03-17 03:06:35 -07:00
|
|
|
{
|
2012-03-31 10:59:29 -07:00
|
|
|
stopMusic();
|
2012-12-12 22:32:02 -08:00
|
|
|
|
|
|
|
DecoderPtr decoder = getDecoder();
|
|
|
|
decoder->open(filename);
|
|
|
|
|
2012-12-18 02:01:04 -08:00
|
|
|
mMusic = mOutput->streamSound(decoder, volumeFromType(Play_TypeMusic),
|
|
|
|
1.0f, Play_NoEnv|Play_TypeMusic);
|
2012-03-20 10:34:36 -07:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
std::cout << "Music Error: " << e.what() << "\n";
|
2012-03-17 03:06:35 -07:00
|
|
|
}
|
2011-01-05 22:18:21 +01:00
|
|
|
}
|
2010-11-11 19:47:26 -05:00
|
|
|
|
2012-03-20 12:39:49 -07:00
|
|
|
void SoundManager::streamMusic(const std::string& filename)
|
|
|
|
{
|
|
|
|
streamMusicFull("Music/"+filename);
|
|
|
|
}
|
|
|
|
|
2012-03-16 17:08:13 -07:00
|
|
|
void SoundManager::startRandomTitle()
|
2011-01-05 22:18:21 +01:00
|
|
|
{
|
2013-04-04 15:10:27 +02:00
|
|
|
Ogre::StringVector filelist;
|
|
|
|
|
|
|
|
Ogre::StringVector groups = Ogre::ResourceGroupManager::getSingleton().getResourceGroups ();
|
|
|
|
for (Ogre::StringVector::iterator it = groups.begin(); it != groups.end(); ++it)
|
|
|
|
{
|
|
|
|
Ogre::StringVectorPtr resourcesInThisGroup = mResourceMgr.findResourceNames(*it,
|
|
|
|
"Music/"+mCurrentPlaylist+"/*");
|
|
|
|
filelist.insert(filelist.end(), resourcesInThisGroup->begin(), resourcesInThisGroup->end());
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!filelist.size())
|
2012-03-20 11:31:13 -07:00
|
|
|
return;
|
|
|
|
|
2013-04-04 15:10:27 +02:00
|
|
|
int i = rand()%filelist.size();
|
|
|
|
streamMusicFull(filelist[i]);
|
2011-01-05 22:18:21 +01:00
|
|
|
}
|
2010-11-11 19:47:26 -05:00
|
|
|
|
2010-10-31 12:23:03 -04:00
|
|
|
bool SoundManager::isMusicPlaying()
|
2011-01-01 22:33:08 +01:00
|
|
|
{
|
2012-03-17 02:45:18 -07:00
|
|
|
return mMusic && mMusic->isPlaying();
|
2011-01-01 22:33:08 +01:00
|
|
|
}
|
2010-10-31 12:23:03 -04:00
|
|
|
|
2012-03-20 11:31:13 -07:00
|
|
|
void SoundManager::playPlaylist(const std::string &playlist)
|
2012-03-07 17:46:51 +02:00
|
|
|
{
|
2012-03-20 11:31:13 -07:00
|
|
|
mCurrentPlaylist = playlist;
|
|
|
|
startRandomTitle();
|
2012-03-07 17:46:51 +02:00
|
|
|
}
|
|
|
|
|
2013-01-15 12:07:15 -08:00
|
|
|
void SoundManager::say(const MWWorld::Ptr &ptr, const std::string& filename)
|
2012-01-29 20:27:03 +01:00
|
|
|
{
|
2012-04-06 10:43:14 -07:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
2012-03-21 19:08:11 -07:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// The range values are not tested
|
2012-12-18 02:01:04 -08:00
|
|
|
float basevol = volumeFromType(Play_TypeVoice);
|
2012-03-29 11:28:33 -07:00
|
|
|
std::string filePath = "Sound/"+filename;
|
2012-08-29 18:48:20 +02:00
|
|
|
const ESM::Position &pos = ptr.getRefData().getPosition();
|
2012-03-30 11:42:11 -07:00
|
|
|
const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]);
|
2012-03-28 04:58:47 -07:00
|
|
|
|
2012-12-17 23:35:20 -08:00
|
|
|
MWBase::SoundPtr sound = mOutput->playSound3D(filePath, objpos, 1.0f, basevol, 1.0f,
|
2013-07-26 18:43:06 +02:00
|
|
|
20.0f, 12750.0f, Play_Normal|Play_TypeVoice, 0);
|
2012-03-28 03:48:51 -07:00
|
|
|
mActiveSounds[sound] = std::make_pair(ptr, std::string("_say_sound"));
|
2012-03-21 19:08:11 -07:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
std::cout <<"Sound Error: "<<e.what()<< std::endl;
|
|
|
|
}
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
|
|
|
|
2012-05-01 20:30:31 -07:00
|
|
|
void SoundManager::say(const std::string& filename)
|
|
|
|
{
|
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
|
|
|
try
|
|
|
|
{
|
2012-12-18 02:01:04 -08:00
|
|
|
float basevol = volumeFromType(Play_TypeVoice);
|
2012-05-01 20:30:31 -07:00
|
|
|
std::string filePath = "Sound/"+filename;
|
|
|
|
|
2013-07-26 18:43:06 +02:00
|
|
|
MWBase::SoundPtr sound = mOutput->playSound(filePath, 1.0f, basevol, 1.0f, Play_Normal|Play_TypeVoice, 0);
|
2012-05-01 20:30:31 -07:00
|
|
|
mActiveSounds[sound] = std::make_pair(MWWorld::Ptr(), std::string("_say_sound"));
|
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
std::cout <<"Sound Error: "<<e.what()<< std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 12:07:15 -08:00
|
|
|
bool SoundManager::sayDone(const MWWorld::Ptr &ptr) const
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
|
|
|
return !isPlaying(ptr, "_say_sound");
|
|
|
|
}
|
|
|
|
|
2013-01-15 12:07:15 -08:00
|
|
|
void SoundManager::stopSay(const MWWorld::Ptr &ptr)
|
2012-05-01 20:30:31 -07:00
|
|
|
{
|
|
|
|
SoundMap::iterator snditer = mActiveSounds.begin();
|
|
|
|
while(snditer != mActiveSounds.end())
|
|
|
|
{
|
|
|
|
if(snditer->second.first == ptr && snditer->second.second == "_say_sound")
|
|
|
|
{
|
|
|
|
snditer->first->stop();
|
|
|
|
mActiveSounds.erase(snditer++);
|
|
|
|
}
|
|
|
|
else
|
2013-07-31 18:46:32 +02:00
|
|
|
++snditer;
|
2012-05-01 20:30:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-18 01:35:20 -08:00
|
|
|
MWBase::SoundPtr SoundManager::playTrack(const DecoderPtr& decoder, PlayType type)
|
2012-12-13 00:05:57 -08:00
|
|
|
{
|
|
|
|
MWBase::SoundPtr track;
|
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return track;
|
|
|
|
try
|
|
|
|
{
|
2012-12-18 02:01:04 -08:00
|
|
|
track = mOutput->streamSound(decoder, volumeFromType(type), 1.0f, Play_NoEnv|type);
|
2012-12-13 00:05:57 -08:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
std::cout <<"Sound Error: "<<e.what()<< std::endl;
|
|
|
|
}
|
|
|
|
return track;
|
|
|
|
}
|
|
|
|
|
2012-03-16 17:08:13 -07:00
|
|
|
|
2013-07-26 18:43:06 +02:00
|
|
|
MWBase::SoundPtr SoundManager::playSound(const std::string& soundId, float volume, float pitch, PlayType type, PlayMode mode, float offset)
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
2012-08-09 14:33:21 +02:00
|
|
|
MWBase::SoundPtr sound;
|
2012-04-06 10:43:14 -07:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return sound;
|
2012-03-21 18:20:32 -07:00
|
|
|
try
|
2012-03-17 06:51:44 -07:00
|
|
|
{
|
2013-07-18 19:01:13 -07:00
|
|
|
float basevol = volumeFromType(type);
|
2012-03-28 04:58:47 -07:00
|
|
|
float min, max;
|
2012-12-18 00:56:29 -08:00
|
|
|
std::string file = lookup(soundId, volume, min, max);
|
2012-03-28 04:58:47 -07:00
|
|
|
|
2013-07-26 18:43:06 +02:00
|
|
|
sound = mOutput->playSound(file, volume, basevol, pitch, mode|type, offset);
|
2012-03-28 03:48:51 -07:00
|
|
|
mActiveSounds[sound] = std::make_pair(MWWorld::Ptr(), soundId);
|
2012-03-21 18:20:32 -07:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
2012-09-30 17:38:55 -07:00
|
|
|
//std::cout <<"Sound Error: "<<e.what()<< std::endl;
|
2012-03-17 06:51:44 -07:00
|
|
|
}
|
2012-03-28 06:08:25 -07:00
|
|
|
return sound;
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
|
|
|
|
2013-01-15 12:07:15 -08:00
|
|
|
MWBase::SoundPtr SoundManager::playSound3D(const MWWorld::Ptr &ptr, const std::string& soundId,
|
2013-07-26 18:43:06 +02:00
|
|
|
float volume, float pitch, PlayType type, PlayMode mode, float offset)
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
2012-08-09 14:33:21 +02:00
|
|
|
MWBase::SoundPtr sound;
|
2012-04-06 10:43:14 -07:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return sound;
|
2012-03-21 18:20:32 -07:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// Look up the sound in the ESM data
|
2013-07-18 19:01:13 -07:00
|
|
|
float basevol = volumeFromType(type);
|
2012-03-28 04:58:47 -07:00
|
|
|
float min, max;
|
2012-12-18 00:56:29 -08:00
|
|
|
std::string file = lookup(soundId, volume, min, max);
|
2013-07-26 18:43:06 +02:00
|
|
|
const ESM::Position &pos = ptr.getRefData().getPosition();
|
2012-03-31 07:41:26 -07:00
|
|
|
const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]);
|
2012-03-21 19:08:11 -07:00
|
|
|
|
2013-07-26 18:43:06 +02:00
|
|
|
sound = mOutput->playSound3D(file, objpos, volume, basevol, pitch, min, max, mode|type, offset);
|
2012-03-31 07:31:55 -07:00
|
|
|
if((mode&Play_NoTrack))
|
2012-03-31 05:57:03 -07:00
|
|
|
mActiveSounds[sound] = std::make_pair(MWWorld::Ptr(), soundId);
|
|
|
|
else
|
|
|
|
mActiveSounds[sound] = std::make_pair(ptr, soundId);
|
2012-03-21 18:20:32 -07:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
2012-09-30 17:38:55 -07:00
|
|
|
//std::cout <<"Sound Error: "<<e.what()<< std::endl;
|
2012-03-21 18:20:32 -07:00
|
|
|
}
|
2012-03-28 06:08:25 -07:00
|
|
|
return sound;
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
|
|
|
|
2013-01-15 12:07:15 -08:00
|
|
|
void SoundManager::stopSound3D(const MWWorld::Ptr &ptr, const std::string& soundId)
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
2012-03-28 03:48:51 -07:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.begin();
|
|
|
|
while(snditer != mActiveSounds.end())
|
|
|
|
{
|
|
|
|
if(snditer->second.first == ptr && snditer->second.second == soundId)
|
|
|
|
{
|
|
|
|
snditer->first->stop();
|
|
|
|
mActiveSounds.erase(snditer++);
|
|
|
|
}
|
|
|
|
else
|
2013-07-31 18:46:32 +02:00
|
|
|
++snditer;
|
2012-03-28 03:48:51 -07:00
|
|
|
}
|
2012-03-27 03:20:50 -07:00
|
|
|
}
|
|
|
|
|
2013-01-15 12:07:15 -08:00
|
|
|
void SoundManager::stopSound3D(const MWWorld::Ptr &ptr)
|
2012-03-27 03:20:50 -07:00
|
|
|
{
|
|
|
|
SoundMap::iterator snditer = mActiveSounds.begin();
|
|
|
|
while(snditer != mActiveSounds.end())
|
2012-03-20 07:22:17 -07:00
|
|
|
{
|
2012-03-28 03:48:51 -07:00
|
|
|
if(snditer->second.first == ptr)
|
2012-03-20 07:22:17 -07:00
|
|
|
{
|
2012-03-28 03:48:51 -07:00
|
|
|
snditer->first->stop();
|
2012-03-27 03:20:50 -07:00
|
|
|
mActiveSounds.erase(snditer++);
|
2012-03-20 07:22:17 -07:00
|
|
|
}
|
2012-03-27 03:20:50 -07:00
|
|
|
else
|
2013-07-31 18:46:32 +02:00
|
|
|
++snditer;
|
2012-03-20 07:22:17 -07:00
|
|
|
}
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
|
|
|
|
2012-03-27 03:00:04 -07:00
|
|
|
void SoundManager::stopSound(const MWWorld::Ptr::CellStore *cell)
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
2012-03-18 11:17:45 -07:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.begin();
|
|
|
|
while(snditer != mActiveSounds.end())
|
2012-03-17 22:13:57 -07:00
|
|
|
{
|
2012-03-28 03:48:51 -07:00
|
|
|
if(snditer->second.first != MWWorld::Ptr() &&
|
|
|
|
snditer->second.first.getCell() == cell)
|
2012-03-20 07:22:17 -07:00
|
|
|
{
|
2012-03-28 03:48:51 -07:00
|
|
|
snditer->first->stop();
|
2012-03-18 11:17:45 -07:00
|
|
|
mActiveSounds.erase(snditer++);
|
2012-03-20 07:22:17 -07:00
|
|
|
}
|
2012-03-17 22:13:57 -07:00
|
|
|
else
|
2013-07-31 18:46:32 +02:00
|
|
|
++snditer;
|
2012-03-17 22:13:57 -07:00
|
|
|
}
|
2012-01-29 20:27:03 +01:00
|
|
|
}
|
2010-08-16 18:06:27 +02:00
|
|
|
|
2012-03-09 18:10:23 +02:00
|
|
|
void SoundManager::stopSound(const std::string& soundId)
|
|
|
|
{
|
2012-03-28 03:48:51 -07:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.begin();
|
|
|
|
while(snditer != mActiveSounds.end())
|
|
|
|
{
|
|
|
|
if(snditer->second.first == MWWorld::Ptr() &&
|
|
|
|
snditer->second.second == soundId)
|
|
|
|
{
|
|
|
|
snditer->first->stop();
|
|
|
|
mActiveSounds.erase(snditer++);
|
|
|
|
}
|
|
|
|
else
|
2013-07-31 18:46:32 +02:00
|
|
|
++snditer;
|
2012-03-28 03:48:51 -07:00
|
|
|
}
|
2012-03-09 18:10:23 +02:00
|
|
|
}
|
|
|
|
|
2013-07-26 18:43:06 +02:00
|
|
|
void SoundManager::fadeOutSound3D(const MWWorld::Ptr &ptr,
|
|
|
|
const std::string& soundId, float duration)
|
|
|
|
{
|
|
|
|
SoundMap::iterator snditer = mActiveSounds.begin();
|
|
|
|
while(snditer != mActiveSounds.end())
|
|
|
|
{
|
|
|
|
if(snditer->second.first == ptr && snditer->second.second == soundId)
|
|
|
|
{
|
|
|
|
snditer->first->setFadeout(duration);
|
|
|
|
}
|
|
|
|
snditer++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 12:07:15 -08:00
|
|
|
bool SoundManager::getSoundPlaying(const MWWorld::Ptr &ptr, const std::string& soundId) const
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
|
|
|
return isPlaying(ptr, soundId);
|
|
|
|
}
|
2010-08-16 18:06:27 +02:00
|
|
|
|
2011-10-09 09:28:36 +02:00
|
|
|
|
2012-12-18 04:19:35 -08:00
|
|
|
void SoundManager::pauseSounds(int types)
|
2012-12-12 02:33:12 -08:00
|
|
|
{
|
|
|
|
if(mOutput->isInitialized())
|
2012-12-18 04:35:24 -08:00
|
|
|
{
|
|
|
|
types &= Play_TypeMask;
|
2012-12-18 04:19:35 -08:00
|
|
|
mOutput->pauseSounds(types);
|
2012-12-18 04:35:24 -08:00
|
|
|
mPausedSoundTypes |= types;
|
|
|
|
}
|
2012-12-12 02:33:12 -08:00
|
|
|
}
|
|
|
|
|
2012-12-18 04:19:35 -08:00
|
|
|
void SoundManager::resumeSounds(int types)
|
2012-12-12 02:33:12 -08:00
|
|
|
{
|
|
|
|
if(mOutput->isInitialized())
|
2012-12-18 04:35:24 -08:00
|
|
|
{
|
|
|
|
types &= types&Play_TypeMask&mPausedSoundTypes;
|
2012-12-18 04:19:35 -08:00
|
|
|
mOutput->resumeSounds(types);
|
2012-12-18 04:35:24 -08:00
|
|
|
mPausedSoundTypes &= ~types;
|
|
|
|
}
|
2012-12-12 02:33:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 06:18:59 -07:00
|
|
|
void SoundManager::updateRegionSound(float duration)
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
2013-08-27 16:04:19 -07:00
|
|
|
static float sTimeToNextEnvSound = 0.0f;
|
2012-03-09 03:22:16 +02:00
|
|
|
static int total = 0;
|
|
|
|
static std::string regionName = "";
|
2013-08-27 16:04:19 -07:00
|
|
|
static float sTimePassed = 0.0;
|
|
|
|
MWBase::World *world = MWBase::Environment::get().getWorld();
|
|
|
|
const MWWorld::Ptr player = world->getPlayer().getPlayer();
|
|
|
|
const ESM::Cell *cell = player.getCell()->mCell;
|
2011-10-09 09:28:36 +02:00
|
|
|
|
2013-08-27 16:04:19 -07:00
|
|
|
sTimePassed += duration;
|
|
|
|
if(!cell->isExterior() || sTimePassed < sTimeToNextEnvSound)
|
2012-03-16 17:08:13 -07:00
|
|
|
return;
|
2012-03-21 15:19:40 -07:00
|
|
|
|
2013-08-27 16:04:19 -07:00
|
|
|
float a = std::rand() / (double)RAND_MAX;
|
|
|
|
// NOTE: We should use the "Minimum Time Between Environmental Sounds" and
|
|
|
|
// "Maximum Time Between Environmental Sounds" fallback settings here.
|
|
|
|
sTimeToNextEnvSound = 5.0f*a + 15.0f*(1.0f-a);
|
|
|
|
sTimePassed = 0;
|
|
|
|
|
|
|
|
if(regionName != cell->mRegion)
|
2012-01-29 20:27:03 +01:00
|
|
|
{
|
2013-08-27 16:04:19 -07:00
|
|
|
regionName = cell->mRegion;
|
2012-03-16 17:08:13 -07:00
|
|
|
total = 0;
|
|
|
|
}
|
2012-03-09 03:22:16 +02:00
|
|
|
|
2013-08-27 16:04:19 -07:00
|
|
|
const ESM::Region *regn = world->getStore().get<ESM::Region>().search(regionName);
|
|
|
|
if(regn == NULL)
|
2012-05-17 19:54:09 +02:00
|
|
|
return;
|
|
|
|
|
2012-03-21 15:19:40 -07:00
|
|
|
std::vector<ESM::Region::SoundRef>::const_iterator soundIter;
|
2012-03-16 17:08:13 -07:00
|
|
|
if(total == 0)
|
|
|
|
{
|
2012-09-17 11:37:50 +04:00
|
|
|
soundIter = regn->mSoundList.begin();
|
|
|
|
while(soundIter != regn->mSoundList.end())
|
2011-10-09 09:28:36 +02:00
|
|
|
{
|
2012-09-17 11:37:50 +04:00
|
|
|
total += (int)soundIter->mChance;
|
2013-07-31 18:46:32 +02:00
|
|
|
++soundIter;
|
2011-10-09 09:28:36 +02:00
|
|
|
}
|
2012-03-22 18:39:10 -07:00
|
|
|
if(total == 0)
|
|
|
|
return;
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
2011-10-09 09:28:36 +02:00
|
|
|
|
2012-03-24 21:05:03 -07:00
|
|
|
int r = (int)(rand()/((double)RAND_MAX+1) * total);
|
2012-03-16 17:08:13 -07:00
|
|
|
int pos = 0;
|
|
|
|
|
2012-09-17 11:37:50 +04:00
|
|
|
soundIter = regn->mSoundList.begin();
|
|
|
|
while(soundIter != regn->mSoundList.end())
|
2012-03-16 17:08:13 -07:00
|
|
|
{
|
2013-07-23 03:44:52 -07:00
|
|
|
if(r - pos < soundIter->mChance)
|
2011-10-09 09:28:36 +02:00
|
|
|
{
|
2013-07-23 03:44:52 -07:00
|
|
|
playSound(soundIter->mSound.toString(), 1.0f, 1.0f);
|
2012-03-16 17:08:13 -07:00
|
|
|
break;
|
2011-10-09 09:28:36 +02:00
|
|
|
}
|
2013-07-23 03:44:52 -07:00
|
|
|
pos += soundIter->mChance;
|
|
|
|
|
2013-07-31 18:46:32 +02:00
|
|
|
++soundIter;
|
2011-10-09 09:28:36 +02:00
|
|
|
}
|
2012-03-16 17:08:13 -07:00
|
|
|
}
|
2012-03-17 06:18:59 -07:00
|
|
|
|
2012-03-21 19:21:36 -07:00
|
|
|
void SoundManager::updateSounds(float duration)
|
2012-03-17 06:18:59 -07:00
|
|
|
{
|
|
|
|
static float timePassed = 0.0;
|
|
|
|
|
|
|
|
timePassed += duration;
|
2012-03-21 19:21:36 -07:00
|
|
|
if(timePassed < (1.0f/30.0f))
|
|
|
|
return;
|
2013-07-26 18:43:06 +02:00
|
|
|
duration = timePassed;
|
2012-03-21 19:21:36 -07:00
|
|
|
timePassed = 0.0f;
|
|
|
|
|
|
|
|
// Make sure music is still playing
|
|
|
|
if(!isMusicPlaying())
|
|
|
|
startRandomTitle();
|
|
|
|
|
2012-08-09 13:27:32 +04:00
|
|
|
MWWorld::Ptr player =
|
|
|
|
MWBase::Environment::get().getWorld()->getPlayer().getPlayer();
|
2012-11-05 16:07:59 +04:00
|
|
|
const ESM::Cell *cell = player.getCell()->mCell;
|
2012-03-21 19:21:36 -07:00
|
|
|
|
2012-03-31 10:06:12 -07:00
|
|
|
Environment env = Env_Normal;
|
2012-09-17 11:37:50 +04:00
|
|
|
if((cell->mData.mFlags&cell->HasWater) && mListenerPos.z < cell->mWater)
|
2013-08-08 19:16:05 +02:00
|
|
|
{
|
2012-03-31 10:06:12 -07:00
|
|
|
env = Env_Underwater;
|
2013-08-08 19:16:05 +02:00
|
|
|
//play underwater sound
|
2013-08-27 13:48:20 -07:00
|
|
|
if(!(mUnderwaterSound && mUnderwaterSound->isPlaying()))
|
|
|
|
mUnderwaterSound = playSound("Underwater", 1.0f, 1.0f, Play_TypeSfx, Play_LoopNoEnv);
|
2013-08-08 19:16:05 +02:00
|
|
|
}
|
2013-08-27 13:48:20 -07:00
|
|
|
else if(mUnderwaterSound)
|
2013-08-08 19:16:05 +02:00
|
|
|
{
|
2013-08-27 13:48:20 -07:00
|
|
|
mUnderwaterSound->stop();
|
|
|
|
mUnderwaterSound.reset();
|
2013-08-08 19:16:05 +02:00
|
|
|
}
|
2012-03-31 10:06:12 -07:00
|
|
|
|
2012-08-09 17:01:03 +04:00
|
|
|
mOutput->updateListener(
|
|
|
|
mListenerPos,
|
|
|
|
mListenerDir,
|
2012-09-30 17:23:05 -07:00
|
|
|
mListenerUp,
|
2012-08-09 17:01:03 +04:00
|
|
|
env
|
|
|
|
);
|
2012-03-21 19:21:36 -07:00
|
|
|
|
|
|
|
// Check if any sounds are finished playing, and trash them
|
2013-07-26 18:43:06 +02:00
|
|
|
// Lower volume on fading out sounds
|
2012-03-21 19:21:36 -07:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.begin();
|
|
|
|
while(snditer != mActiveSounds.end())
|
2012-03-17 06:18:59 -07:00
|
|
|
{
|
2012-03-28 03:48:51 -07:00
|
|
|
if(!snditer->first->isPlaying())
|
2012-03-21 19:21:36 -07:00
|
|
|
mActiveSounds.erase(snditer++);
|
|
|
|
else
|
2012-03-30 07:01:37 -07:00
|
|
|
{
|
2013-07-27 07:24:18 -07:00
|
|
|
const MWWorld::Ptr &ptr = snditer->second.first;
|
|
|
|
if(!ptr.isEmpty())
|
|
|
|
{
|
|
|
|
const ESM::Position &pos = ptr.getRefData().getPosition();
|
|
|
|
const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]);
|
|
|
|
snditer->first->setPosition(objpos);
|
|
|
|
}
|
2013-07-26 18:43:06 +02:00
|
|
|
//update fade out
|
|
|
|
if(snditer->first->mFadeOutTime>0)
|
|
|
|
{
|
|
|
|
float soundDuration=duration;
|
|
|
|
if(soundDuration>snditer->first->mFadeOutTime)
|
|
|
|
soundDuration=snditer->first->mFadeOutTime;
|
|
|
|
snditer->first->setVolume(snditer->first->mVolume
|
|
|
|
- soundDuration / snditer->first->mFadeOutTime * snditer->first->mVolume);
|
|
|
|
snditer->first->mFadeOutTime -= soundDuration;
|
|
|
|
}
|
2012-03-30 07:01:37 -07:00
|
|
|
snditer->first->update();
|
2013-07-31 18:46:32 +02:00
|
|
|
++snditer;
|
2012-03-30 07:01:37 -07:00
|
|
|
}
|
2012-03-17 06:18:59 -07:00
|
|
|
}
|
2012-03-21 19:21:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundManager::update(float duration)
|
|
|
|
{
|
2012-04-06 10:43:14 -07:00
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
2012-03-21 19:21:36 -07:00
|
|
|
updateSounds(duration);
|
2012-03-17 06:18:59 -07:00
|
|
|
updateRegionSound(duration);
|
|
|
|
}
|
2012-03-19 02:31:40 -07:00
|
|
|
|
2012-03-21 19:21:36 -07:00
|
|
|
|
2012-05-24 04:34:53 +02:00
|
|
|
void SoundManager::processChangedSettings(const Settings::CategorySettingVector& settings)
|
|
|
|
{
|
|
|
|
mMasterVolume = Settings::Manager::getFloat("master volume", "Sound");
|
|
|
|
mMusicVolume = Settings::Manager::getFloat("music volume", "Sound");
|
|
|
|
mSFXVolume = Settings::Manager::getFloat("sfx volume", "Sound");
|
|
|
|
mFootstepsVolume = Settings::Manager::getFloat("footsteps volume", "Sound");
|
|
|
|
mVoiceVolume = Settings::Manager::getFloat("voice volume", "Sound");
|
2012-05-24 05:30:22 -07:00
|
|
|
|
|
|
|
SoundMap::iterator snditer = mActiveSounds.begin();
|
|
|
|
while(snditer != mActiveSounds.end())
|
|
|
|
{
|
2012-12-18 02:01:04 -08:00
|
|
|
snditer->first->mBaseVolume = volumeFromType(snditer->first->getPlayType());
|
2012-05-24 05:30:22 -07:00
|
|
|
snditer->first->update();
|
2013-07-31 18:46:32 +02:00
|
|
|
++snditer;
|
2012-05-24 05:30:22 -07:00
|
|
|
}
|
|
|
|
if(mMusic)
|
|
|
|
{
|
2012-12-18 02:01:04 -08:00
|
|
|
mMusic->mBaseVolume = volumeFromType(mMusic->getPlayType());
|
2012-05-24 05:30:22 -07:00
|
|
|
mMusic->update();
|
|
|
|
}
|
2012-05-24 04:34:53 +02:00
|
|
|
}
|
|
|
|
|
2012-09-30 17:23:05 -07:00
|
|
|
void SoundManager::setListenerPosDir(const Ogre::Vector3 &pos, const Ogre::Vector3 &dir, const Ogre::Vector3 &up)
|
2012-08-09 17:01:03 +04:00
|
|
|
{
|
|
|
|
mListenerPos = pos;
|
|
|
|
mListenerDir = dir;
|
2012-09-30 17:23:05 -07:00
|
|
|
mListenerUp = up;
|
2012-08-09 17:01:03 +04:00
|
|
|
}
|
|
|
|
|
2012-03-20 17:57:28 -07:00
|
|
|
// Default readAll implementation, for decoders that can't do anything
|
|
|
|
// better
|
|
|
|
void Sound_Decoder::readAll(std::vector<char> &output)
|
|
|
|
{
|
|
|
|
size_t total = output.size();
|
|
|
|
size_t got;
|
|
|
|
|
|
|
|
output.resize(total+32768);
|
|
|
|
while((got=read(&output[total], output.size()-total)) > 0)
|
|
|
|
{
|
|
|
|
total += got;
|
|
|
|
output.resize(total*2);
|
|
|
|
}
|
|
|
|
output.resize(total);
|
|
|
|
}
|
|
|
|
|
2012-03-19 02:31:40 -07:00
|
|
|
|
|
|
|
const char *getSampleTypeName(SampleType type)
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case SampleType_UInt8: return "U8";
|
|
|
|
case SampleType_Int16: return "S16";
|
2013-02-26 10:19:33 -08:00
|
|
|
case SampleType_Float32: return "Float32";
|
2012-03-19 02:31:40 -07:00
|
|
|
}
|
|
|
|
return "(unknown sample type)";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *getChannelConfigName(ChannelConfig config)
|
|
|
|
{
|
|
|
|
switch(config)
|
|
|
|
{
|
2012-12-13 04:10:19 -08:00
|
|
|
case ChannelConfig_Mono: return "Mono";
|
|
|
|
case ChannelConfig_Stereo: return "Stereo";
|
|
|
|
case ChannelConfig_Quad: return "Quad";
|
|
|
|
case ChannelConfig_5point1: return "5.1 Surround";
|
|
|
|
case ChannelConfig_7point1: return "7.1 Surround";
|
2012-03-19 02:31:40 -07:00
|
|
|
}
|
|
|
|
return "(unknown channel config)";
|
|
|
|
}
|
2012-03-19 05:29:04 -07:00
|
|
|
|
|
|
|
size_t framesToBytes(size_t frames, ChannelConfig config, SampleType type)
|
|
|
|
{
|
|
|
|
switch(config)
|
|
|
|
{
|
2012-12-13 04:10:19 -08:00
|
|
|
case ChannelConfig_Mono: frames *= 1; break;
|
|
|
|
case ChannelConfig_Stereo: frames *= 2; break;
|
|
|
|
case ChannelConfig_Quad: frames *= 4; break;
|
|
|
|
case ChannelConfig_5point1: frames *= 6; break;
|
|
|
|
case ChannelConfig_7point1: frames *= 8; break;
|
2012-03-19 05:29:04 -07:00
|
|
|
}
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case SampleType_UInt8: frames *= 1; break;
|
|
|
|
case SampleType_Int16: frames *= 2; break;
|
2013-02-26 10:19:33 -08:00
|
|
|
case SampleType_Float32: frames *= 4; break;
|
2012-03-19 05:29:04 -07:00
|
|
|
}
|
|
|
|
return frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t bytesToFrames(size_t bytes, ChannelConfig config, SampleType type)
|
|
|
|
{
|
|
|
|
return bytes / framesToBytes(1, config, type);
|
|
|
|
}
|
2010-07-03 15:04:00 +02:00
|
|
|
}
|