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>
|
|
|
|
|
2012-10-01 15:17:04 +00:00
|
|
|
#include "../mwworld/esmstore.hpp"
|
2010-08-14 12:28:17 +00:00
|
|
|
|
2012-04-23 13:27:03 +00:00
|
|
|
#include "../mwbase/environment.hpp"
|
2012-07-03 10:30:50 +00:00
|
|
|
#include "../mwbase/world.hpp"
|
2012-04-23 13:27:03 +00:00
|
|
|
|
2011-10-09 07:28:36 +00:00
|
|
|
#include "../mwworld/player.hpp"
|
2010-08-14 12:28:17 +00:00
|
|
|
|
2012-03-17 06:40:07 +00:00
|
|
|
#include "sound_output.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-04-01 22:02:07 +00: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 16:06:27 +00:00
|
|
|
*/
|
2010-08-13 15:11:03 +00:00
|
|
|
#ifdef OPENMW_USE_FFMPEG
|
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
|
2012-03-21 22:29:05 +00:00
|
|
|
#endif
|
2010-08-12 14:13:54 +00:00
|
|
|
|
2012-04-01 22:02:07 +00:00
|
|
|
#ifdef OPENMW_USE_AUDIERE
|
|
|
|
#include "audiere_decoder.hpp"
|
|
|
|
#ifndef SOUND_IN
|
|
|
|
#define SOUND_IN "Audiere"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2010-08-13 20:30:22 +00:00
|
|
|
#ifdef OPENMW_USE_MPG123
|
2012-03-17 06:18:15 +00:00
|
|
|
#include "mpgsnd_decoder.hpp"
|
2012-03-21 22:29:05 +00:00
|
|
|
#ifndef SOUND_IN
|
2010-08-18 12:50:49 +00:00
|
|
|
#define SOUND_IN "mpg123,sndfile"
|
2010-08-13 20:30:22 +00:00
|
|
|
#endif
|
2012-03-21 22:29:05 +00:00
|
|
|
#endif
|
2010-08-13 20:30:22 +00:00
|
|
|
|
2010-08-12 14:13:54 +00:00
|
|
|
|
2010-07-03 13:04:00 +00:00
|
|
|
namespace MWSound
|
|
|
|
{
|
2012-04-23 13:27:03 +00:00
|
|
|
SoundManager::SoundManager(bool useSound)
|
2012-03-24 07:22:54 +00:00
|
|
|
: mResourceMgr(Ogre::ResourceGroupManager::getSingleton())
|
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
|
|
|
, mFootstepsVolume(1.0f)
|
|
|
|
, mVoiceVolume(1.0f)
|
2012-12-18 12:35:24 +00:00
|
|
|
, mPausedSoundTypes(0)
|
2013-07-30 21:24:18 +00:00
|
|
|
, mListenerPos(0,0,0)
|
|
|
|
, mListenerDir(1,0,0)
|
|
|
|
, mListenerUp(0,0,1)
|
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
|
|
|
{
|
2012-03-18 18:17:45 +00:00
|
|
|
mActiveSounds.clear();
|
2012-03-17 10:01:51 +00:00
|
|
|
mMusic.reset();
|
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()
|
|
|
|
{
|
|
|
|
return DecoderPtr(new DEFAULT_DECODER);
|
|
|
|
}
|
|
|
|
|
2010-08-16 16:06:27 +00:00
|
|
|
// Convert a soundId to file name, and modify the volume
|
|
|
|
// according to the sounds local volume setting, minRange and
|
|
|
|
// maxRange.
|
2012-03-05 23:21:00 +00:00
|
|
|
std::string SoundManager::lookup(const std::string &soundId,
|
2010-08-16 16:06:27 +00:00
|
|
|
float &volume, float &min, float &max)
|
2010-07-03 13:04:00 +00:00
|
|
|
{
|
2012-11-05 17:45:18 +00:00
|
|
|
const ESM::Sound *snd =
|
|
|
|
MWBase::Environment::get().getWorld()->getStore().get<ESM::Sound>().find(soundId);
|
2012-03-17 00:08:13 +00:00
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
volume *= pow(10.0, (snd->mData.mVolume/255.0*3348.0 - 3348.0) / 2000.0);
|
2012-03-17 00:08:13 +00:00
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
if(snd->mData.mMinRange == 0 && snd->mData.mMaxRange == 0)
|
2012-03-17 00:08:13 +00:00
|
|
|
{
|
|
|
|
min = 100.0f;
|
|
|
|
max = 2000.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-17 07:37:50 +00:00
|
|
|
min = snd->mData.mMinRange * 20.0f;
|
|
|
|
max = snd->mData.mMaxRange * 50.0f;
|
2012-03-17 00:08:13 +00:00
|
|
|
min = std::max(min, 1.0f);
|
|
|
|
max = std::max(min, max);
|
|
|
|
}
|
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
return "Sound/"+snd->mSound;
|
2010-07-03 13:04:00 +00:00
|
|
|
}
|
2010-08-14 07:26:00 +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:
|
|
|
|
case Play_TypeMovie:
|
|
|
|
volume *= mMusicVolume;
|
|
|
|
break;
|
|
|
|
case Play_TypeMask:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return volume;
|
|
|
|
}
|
2010-07-03 13:04:00 +00:00
|
|
|
|
2013-01-15 20:07:15 +00:00
|
|
|
bool SoundManager::isPlaying(const MWWorld::Ptr &ptr, const std::string &id) const
|
2010-08-14 07:26:00 +00:00
|
|
|
{
|
2012-03-28 10:48:51 +00:00
|
|
|
SoundMap::const_iterator snditer = mActiveSounds.begin();
|
|
|
|
while(snditer != mActiveSounds.end())
|
|
|
|
{
|
|
|
|
if(snditer->second.first == ptr && snditer->second.second == id)
|
|
|
|
return snditer->first->isPlaying();
|
|
|
|
snditer++;
|
|
|
|
}
|
|
|
|
return false;
|
2010-08-14 07:26: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;
|
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
|
|
|
{
|
2013-04-04 13:10:27 +00: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 18:31:13 +00:00
|
|
|
return;
|
|
|
|
|
2013-04-04 13:10:27 +00:00
|
|
|
int i = rand()%filelist.size();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-01-15 20:07:15 +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
|
|
|
|
{
|
|
|
|
// The range values are not tested
|
2012-12-18 10:01:04 +00:00
|
|
|
float basevol = volumeFromType(Play_TypeVoice);
|
2012-03-29 18:28:33 +00:00
|
|
|
std::string filePath = "Sound/"+filename;
|
2012-08-29 16:48:20 +00:00
|
|
|
const ESM::Position &pos = ptr.getRefData().getPosition();
|
2012-03-30 18:42:11 +00:00
|
|
|
const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]);
|
2012-03-28 11:58:47 +00:00
|
|
|
|
2012-12-18 07:35:20 +00:00
|
|
|
MWBase::SoundPtr sound = mOutput->playSound3D(filePath, objpos, 1.0f, basevol, 1.0f,
|
2013-07-26 16:43:06 +00:00
|
|
|
20.0f, 12750.0f, Play_Normal|Play_TypeVoice, 0);
|
2012-03-28 10:48:51 +00:00
|
|
|
mActiveSounds[sound] = std::make_pair(ptr, std::string("_say_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
|
|
|
}
|
|
|
|
|
2012-05-02 03:30:31 +00:00
|
|
|
void SoundManager::say(const std::string& filename)
|
|
|
|
{
|
|
|
|
if(!mOutput->isInitialized())
|
|
|
|
return;
|
|
|
|
try
|
|
|
|
{
|
2012-12-18 10:01:04 +00:00
|
|
|
float basevol = volumeFromType(Play_TypeVoice);
|
2012-05-02 03:30:31 +00:00
|
|
|
std::string filePath = "Sound/"+filename;
|
|
|
|
|
2013-07-26 16:43:06 +00:00
|
|
|
MWBase::SoundPtr sound = mOutput->playSound(filePath, 1.0f, basevol, 1.0f, Play_Normal|Play_TypeVoice, 0);
|
2012-05-02 03:30:31 +00: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 20:07:15 +00:00
|
|
|
bool SoundManager::sayDone(const MWWorld::Ptr &ptr) const
|
2012-03-17 00:08:13 +00:00
|
|
|
{
|
|
|
|
return !isPlaying(ptr, "_say_sound");
|
|
|
|
}
|
|
|
|
|
2013-01-15 20:07:15 +00:00
|
|
|
void SoundManager::stopSay(const MWWorld::Ptr &ptr)
|
2012-05-02 03:30:31 +00: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
|
|
|
|
snditer++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2013-07-19 02:01:13 +00:00
|
|
|
float basevol = volumeFromType(type);
|
2012-03-28 11:58:47 +00:00
|
|
|
float min, max;
|
2012-12-18 08:56:29 +00:00
|
|
|
std::string file = lookup(soundId, volume, min, max);
|
2012-03-28 11:58:47 +00:00
|
|
|
|
2013-07-26 16:43:06 +00:00
|
|
|
sound = mOutput->playSound(file, volume, basevol, pitch, mode|type, offset);
|
2012-03-28 10:48:51 +00:00
|
|
|
mActiveSounds[sound] = std::make_pair(MWWorld::Ptr(), soundId);
|
2012-03-22 01:20:32 +00:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
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
|
2013-07-19 02:01:13 +00:00
|
|
|
float basevol = volumeFromType(type);
|
2012-03-28 11:58:47 +00:00
|
|
|
float min, max;
|
2012-12-18 08:56:29 +00:00
|
|
|
std::string file = lookup(soundId, volume, min, max);
|
2013-07-26 16:43:06 +00:00
|
|
|
const ESM::Position &pos = ptr.getRefData().getPosition();
|
2012-03-31 14:41:26 +00:00
|
|
|
const Ogre::Vector3 objpos(pos.pos[0], pos.pos[1], pos.pos[2]);
|
2012-03-22 02:08:11 +00:00
|
|
|
|
2013-07-26 16:43:06 +00:00
|
|
|
sound = mOutput->playSound3D(file, objpos, volume, basevol, pitch, min, max, mode|type, offset);
|
2012-03-31 14:31:55 +00:00
|
|
|
if((mode&Play_NoTrack))
|
2012-03-31 12:57:03 +00:00
|
|
|
mActiveSounds[sound] = std::make_pair(MWWorld::Ptr(), soundId);
|
|
|
|
else
|
|
|
|
mActiveSounds[sound] = std::make_pair(ptr, soundId);
|
2012-03-22 01:20:32 +00:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2012-03-28 10:48:51 +00: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
|
|
|
|
snditer++;
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
SoundMap::iterator snditer = mActiveSounds.begin();
|
|
|
|
while(snditer != mActiveSounds.end())
|
2012-03-20 14:22:17 +00:00
|
|
|
{
|
2012-03-28 10:48:51 +00:00
|
|
|
if(snditer->second.first == ptr)
|
2012-03-20 14:22:17 +00:00
|
|
|
{
|
2012-03-28 10:48:51 +00:00
|
|
|
snditer->first->stop();
|
2012-03-27 10:20:50 +00:00
|
|
|
mActiveSounds.erase(snditer++);
|
2012-03-20 14:22:17 +00:00
|
|
|
}
|
2012-03-27 10:20:50 +00:00
|
|
|
else
|
|
|
|
snditer++;
|
2012-03-20 14:22:17 +00:00
|
|
|
}
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
2012-03-27 10:00:04 +00:00
|
|
|
void SoundManager::stopSound(const MWWorld::Ptr::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
|
|
|
{
|
2012-03-28 10:48:51 +00:00
|
|
|
if(snditer->second.first != MWWorld::Ptr() &&
|
|
|
|
snditer->second.first.getCell() == cell)
|
2012-03-20 14:22:17 +00:00
|
|
|
{
|
2012-03-28 10:48:51 +00:00
|
|
|
snditer->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
|
|
|
|
snditer++;
|
|
|
|
}
|
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)
|
|
|
|
{
|
2012-03-28 10:48:51 +00: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
|
|
|
|
snditer++;
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
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 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
|
|
|
{
|
|
|
|
return isPlaying(ptr, soundId);
|
|
|
|
}
|
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
|
|
|
{
|
2012-04-23 13:27:03 +00:00
|
|
|
MWWorld::Ptr::CellStore *current = MWBase::Environment::get().getWorld()->getPlayer().getPlayer().getCell();
|
2012-03-09 01:22:16 +00:00
|
|
|
static int total = 0;
|
|
|
|
static std::string regionName = "";
|
|
|
|
static float timePassed = 0.0;
|
2011-10-09 07:28:36 +00:00
|
|
|
|
|
|
|
//If the region has changed
|
2012-03-17 00:08:13 +00:00
|
|
|
timePassed += duration;
|
2012-11-05 12:07:59 +00:00
|
|
|
if(!current->mCell->isExterior() || timePassed < 10)
|
2012-03-17 00:08:13 +00:00
|
|
|
return;
|
|
|
|
timePassed = 0;
|
2012-03-21 22:19:40 +00:00
|
|
|
|
2012-11-05 12:07:59 +00:00
|
|
|
if(regionName != current->mCell->mRegion)
|
2012-01-29 19:27:03 +00:00
|
|
|
{
|
2012-11-05 12:07:59 +00:00
|
|
|
regionName = current->mCell->mRegion;
|
2012-03-17 00:08:13 +00:00
|
|
|
total = 0;
|
|
|
|
}
|
2012-03-09 01:22:16 +00:00
|
|
|
|
2012-11-05 17:45:18 +00:00
|
|
|
const ESM::Region *regn =
|
|
|
|
MWBase::Environment::get().getWorld()->getStore().get<ESM::Region>().search(regionName);
|
|
|
|
|
2012-05-17 17:54:09 +00:00
|
|
|
if (regn == NULL)
|
|
|
|
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;
|
2012-03-17 00:08:13 +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
|
|
|
|
2012-03-25 04:05:03 +00:00
|
|
|
int r = (int)(rand()/((double)RAND_MAX+1) * 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;
|
|
|
|
|
|
|
|
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-08-09 09:27:32 +00:00
|
|
|
MWWorld::Ptr player =
|
|
|
|
MWBase::Environment::get().getWorld()->getPlayer().getPlayer();
|
2012-11-05 12:07:59 +00:00
|
|
|
const ESM::Cell *cell = player.getCell()->mCell;
|
2012-03-22 02:21:36 +00:00
|
|
|
|
2012-03-31 17:06:12 +00:00
|
|
|
Environment env = Env_Normal;
|
2012-09-17 07:37:50 +00:00
|
|
|
if((cell->mData.mFlags&cell->HasWater) && mListenerPos.z < cell->mWater)
|
2012-03-31 17:06:12 +00:00
|
|
|
env = Env_Underwater;
|
|
|
|
|
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
|
|
|
|
|
|
|
// Check if any sounds are finished playing, and trash them
|
2013-07-26 16:43:06 +00:00
|
|
|
// Lower volume on fading out sounds
|
2012-03-22 02:21:36 +00:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.begin();
|
|
|
|
while(snditer != mActiveSounds.end())
|
2012-03-17 13:18:59 +00:00
|
|
|
{
|
2012-03-28 10:48:51 +00:00
|
|
|
if(!snditer->first->isPlaying())
|
2012-03-22 02:21:36 +00:00
|
|
|
mActiveSounds.erase(snditer++);
|
|
|
|
else
|
2012-03-30 14:01:37 +00:00
|
|
|
{
|
2013-07-27 14:24:18 +00: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 16:43:06 +00: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 14:01:37 +00:00
|
|
|
snditer->first->update();
|
2012-03-22 02:21:36 +00:00
|
|
|
snditer++;
|
2012-03-30 14:01:37 +00:00
|
|
|
}
|
2012-03-17 13:18:59 +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;
|
2012-03-22 02:21:36 +00:00
|
|
|
updateSounds(duration);
|
2012-03-17 13:18:59 +00:00
|
|
|
updateRegionSound(duration);
|
|
|
|
}
|
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();
|
|
|
|
while(snditer != mActiveSounds.end())
|
|
|
|
{
|
2012-12-18 10:01:04 +00:00
|
|
|
snditer->first->mBaseVolume = volumeFromType(snditer->first->getPlayType());
|
2012-05-24 12:30:22 +00:00
|
|
|
snditer->first->update();
|
|
|
|
snditer++;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-10-01 00:23:05 +00:00
|
|
|
void SoundManager::setListenerPosDir(const Ogre::Vector3 &pos, const Ogre::Vector3 &dir, const Ogre::Vector3 &up)
|
2012-08-09 13:01:03 +00:00
|
|
|
{
|
|
|
|
mListenerPos = pos;
|
|
|
|
mListenerDir = dir;
|
2012-10-01 00:23:05 +00:00
|
|
|
mListenerUp = up;
|
2012-08-09 13:01:03 +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);
|
|
|
|
}
|
2010-07-03 13:04:00 +00:00
|
|
|
}
|