2010-07-03 13:04:00 +00:00
|
|
|
#include "soundmanager.hpp"
|
|
|
|
|
2010-08-14 12:28:17 +00:00
|
|
|
#include <iostream>
|
2011-10-09 07:28:36 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include <OgreRoot.h>
|
|
|
|
|
2010-08-14 12:28:17 +00:00
|
|
|
#include <components/esm_store/store.hpp>
|
|
|
|
|
2011-10-09 07:28:36 +00:00
|
|
|
#include "../mwworld/environment.hpp"
|
|
|
|
#include "../mwworld/world.hpp"
|
|
|
|
#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-03-18 15:42:55 +00:00
|
|
|
/* Set up the sound manager to use FFMPEG or MPG123+libsndfile 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
|
|
|
|
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-03-24 07:22:54 +00:00
|
|
|
SoundManager::SoundManager(bool useSound, MWWorld::Environment& environment)
|
|
|
|
: mResourceMgr(Ogre::ResourceGroupManager::getSingleton())
|
2012-03-05 23:21:00 +00:00
|
|
|
, mEnvironment(environment)
|
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-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-18 19:19:54 +00:00
|
|
|
mOutput.reset(new DEFAULT_OUTPUT(*this));
|
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-03-18 19:19:54 +00:00
|
|
|
mOutput->init();
|
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
std::cout <<"Sound init failed: "<<e.what()<< std::endl;
|
2012-03-18 18:17:45 +00:00
|
|
|
mOutput.reset();
|
2012-03-17 05:12:17 +00:00
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
mLooseSounds.clear();
|
|
|
|
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-03-17 00:08:13 +00:00
|
|
|
const ESM::Sound *snd = mEnvironment.mWorld->getStore().sounds.search(soundId);
|
2012-03-22 01:20:32 +00:00
|
|
|
if(snd == NULL)
|
|
|
|
throw std::runtime_error(std::string("Failed to lookup sound ")+soundId);
|
2012-03-17 00:08:13 +00:00
|
|
|
|
|
|
|
if(snd->data.volume == 0)
|
|
|
|
volume = 0.0f;
|
|
|
|
else
|
|
|
|
volume *= pow(10.0, (snd->data.volume/255.0f*3348.0 - 3348.0) / 2000.0);
|
|
|
|
|
|
|
|
if(snd->data.minRange == 0 && snd->data.maxRange == 0)
|
|
|
|
{
|
|
|
|
min = 100.0f;
|
|
|
|
max = 2000.0f;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
min = snd->data.minRange * 20.0f;
|
|
|
|
max = snd->data.maxRange * 50.0f;
|
|
|
|
min = std::max(min, 1.0f);
|
|
|
|
max = std::max(min, max);
|
|
|
|
}
|
|
|
|
|
2012-03-22 01:35:20 +00:00
|
|
|
return std::string("Sound/")+snd->sound;
|
2010-07-03 13:04:00 +00:00
|
|
|
}
|
2010-08-14 07:26:00 +00:00
|
|
|
|
2010-07-03 13:04:00 +00:00
|
|
|
|
2012-03-05 23:21:00 +00:00
|
|
|
bool SoundManager::isPlaying(MWWorld::Ptr ptr, const std::string &id) const
|
2010-08-14 07:26:00 +00:00
|
|
|
{
|
2012-03-18 18:17:45 +00:00
|
|
|
SoundMap::const_iterator snditer = mActiveSounds.find(ptr);
|
|
|
|
if(snditer == mActiveSounds.end())
|
2012-03-17 16:16:09 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
IDMap::const_iterator iditer = snditer->second.find(id);
|
|
|
|
if(iditer == snditer->second.end())
|
|
|
|
return false;
|
|
|
|
|
2012-03-19 19:08:40 +00:00
|
|
|
return true;
|
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-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-20 17:34:36 +00:00
|
|
|
if(mMusic)
|
|
|
|
mMusic->stop();
|
|
|
|
mMusic.reset(mOutput->streamSound(filename, 0.4f, 1.0f));
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2012-03-20 18:31:13 +00:00
|
|
|
Ogre::StringVectorPtr filelist;
|
2012-03-24 07:22:54 +00:00
|
|
|
filelist = mResourceMgr.findResourceNames(Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
|
|
|
|
"Music/"+mCurrentPlaylist+"/*");
|
2012-03-20 18:31:13 +00:00
|
|
|
if(!filelist->size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
int i = rand()%filelist->size();
|
2012-03-20 19:39:49 +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
|
|
|
}
|
|
|
|
|
2012-03-17 00:08:13 +00:00
|
|
|
void SoundManager::say(MWWorld::Ptr ptr, const std::string& filename)
|
2012-01-29 19:27:03 +00:00
|
|
|
{
|
2012-03-22 02:08:11 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// The range values are not tested
|
|
|
|
const ESM::Position &pos = ptr.getCellRef().pos;
|
|
|
|
std::string filePath = std::string("Sound/")+filename;
|
|
|
|
|
|
|
|
SoundPtr sound(mOutput->playSound3D(filePath, pos.pos, 1.0f, 1.0f, 100.0f, 20000.0f, false));
|
|
|
|
mActiveSounds[ptr]["_say_sound"] = sound;
|
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
std::cout <<"Sound Error: "<<e.what()<< std::endl;
|
|
|
|
}
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SoundManager::sayDone(MWWorld::Ptr ptr) const
|
|
|
|
{
|
|
|
|
return !isPlaying(ptr, "_say_sound");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SoundManager::playSound(const std::string& soundId, float volume, float pitch, bool loop)
|
|
|
|
{
|
|
|
|
float min, max;
|
2012-03-22 01:20:32 +00:00
|
|
|
try
|
2012-03-17 13:51:44 +00:00
|
|
|
{
|
2012-03-22 01:20:32 +00:00
|
|
|
std::string file = lookup(soundId, volume, min, max);
|
|
|
|
Sound *sound = mOutput->playSound(file, volume, pitch, loop);
|
|
|
|
mLooseSounds[soundId] = SoundPtr(sound);
|
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
2012-03-22 02:08:11 +00:00
|
|
|
std::cout <<"Sound Error: "<<e.what()<< std::endl;
|
2012-03-17 13:51:44 +00:00
|
|
|
}
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundManager::playSound3D(MWWorld::Ptr ptr, const std::string& soundId,
|
|
|
|
float volume, float pitch, bool loop, bool untracked)
|
|
|
|
{
|
|
|
|
float min, max;
|
2012-03-22 01:20:32 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
// Look up the sound in the ESM data
|
2012-03-22 02:08:11 +00:00
|
|
|
const ESM::Position &pos = ptr.getCellRef().pos;
|
2012-03-22 01:20:32 +00:00
|
|
|
std::string file = lookup(soundId, volume, min, max);
|
2012-03-22 02:08:11 +00:00
|
|
|
|
|
|
|
SoundPtr sound(mOutput->playSound3D(file, pos.pos, volume, pitch, min, max, loop));
|
|
|
|
if(untracked) mLooseSounds[soundId] = sound;
|
|
|
|
else mActiveSounds[ptr][soundId] = sound;
|
2012-03-22 01:20:32 +00:00
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
2012-03-22 02:08:11 +00:00
|
|
|
std::cout <<"Sound Error: "<<e.what()<< std::endl;
|
2012-03-22 01:20:32 +00:00
|
|
|
}
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundManager::stopSound3D(MWWorld::Ptr ptr, const std::string& soundId)
|
|
|
|
{
|
2012-03-18 05:13:57 +00:00
|
|
|
// Stop a sound and remove it from the list. If soundId="" then
|
|
|
|
// stop all its sounds.
|
2012-03-18 18:17:45 +00:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.find(ptr);
|
|
|
|
if(snditer == mActiveSounds.end())
|
2012-03-18 05:13:57 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if(!soundId.empty())
|
|
|
|
{
|
|
|
|
IDMap::iterator iditer = snditer->second.find(soundId);
|
|
|
|
if(iditer != snditer->second.end())
|
|
|
|
{
|
2012-03-20 14:22:17 +00:00
|
|
|
iditer->second->stop();
|
2012-03-18 05:13:57 +00:00
|
|
|
snditer->second.erase(iditer);
|
2012-03-19 19:08:40 +00:00
|
|
|
if(snditer->second.empty())
|
2012-03-18 18:17:45 +00:00
|
|
|
mActiveSounds.erase(snditer);
|
2012-03-18 05:13:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2012-03-20 14:22:17 +00:00
|
|
|
{
|
|
|
|
IDMap::iterator iditer = snditer->second.begin();
|
|
|
|
while(iditer != snditer->second.end())
|
|
|
|
{
|
|
|
|
iditer->second->stop();
|
|
|
|
iditer++;
|
|
|
|
}
|
2012-03-18 18:17:45 +00:00
|
|
|
mActiveSounds.erase(snditer);
|
2012-03-20 14:22:17 +00:00
|
|
|
}
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SoundManager::stopSound(MWWorld::Ptr::CellStore *cell)
|
|
|
|
{
|
2012-03-18 05:13:57 +00:00
|
|
|
// Remove all references to objects belonging to a given cell
|
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
|
|
|
{
|
|
|
|
if(snditer->first.getCell() == cell)
|
2012-03-20 14:22:17 +00:00
|
|
|
{
|
|
|
|
IDMap::iterator iditer = snditer->second.begin();
|
|
|
|
while(iditer != snditer->second.end())
|
|
|
|
{
|
|
|
|
iditer->second->stop();
|
|
|
|
iditer++;
|
|
|
|
}
|
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-18 18:17:45 +00:00
|
|
|
IDMap::iterator iditer = mLooseSounds.find(soundId);
|
|
|
|
if(iditer != mLooseSounds.end())
|
2012-03-20 14:22:17 +00:00
|
|
|
{
|
|
|
|
iditer->second->stop();
|
2012-03-18 18:17:45 +00:00
|
|
|
mLooseSounds.erase(iditer);
|
2012-03-20 14:22:17 +00:00
|
|
|
}
|
2012-03-09 16:10:23 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 00:08:13 +00:00
|
|
|
bool SoundManager::getSoundPlaying(MWWorld::Ptr ptr, const std::string& soundId) const
|
|
|
|
{
|
|
|
|
return isPlaying(ptr, soundId);
|
|
|
|
}
|
2010-08-16 16:06:27 +00:00
|
|
|
|
2012-03-17 00:08:13 +00:00
|
|
|
void SoundManager::updateObject(MWWorld::Ptr ptr)
|
|
|
|
{
|
2012-03-18 18:17:45 +00:00
|
|
|
SoundMap::iterator snditer = mActiveSounds.find(ptr);
|
|
|
|
if(snditer == mActiveSounds.end())
|
2012-03-17 16:51:03 +00:00
|
|
|
return;
|
|
|
|
|
2012-03-24 15:12:04 +00:00
|
|
|
const ESM::Position &pos = ptr.getCellRef().pos;
|
2012-03-17 16:51:03 +00:00
|
|
|
IDMap::iterator iditer = snditer->second.begin();
|
|
|
|
while(iditer != snditer->second.end())
|
|
|
|
{
|
2012-03-18 19:03:15 +00:00
|
|
|
iditer->second->update(pos.pos);
|
2012-03-17 16:51:03 +00:00
|
|
|
iditer++;
|
|
|
|
}
|
2012-03-17 00:08:13 +00:00
|
|
|
}
|
2011-10-09 07:28:36 +00:00
|
|
|
|
2012-03-17 13:18:59 +00:00
|
|
|
void SoundManager::updateRegionSound(float duration)
|
2012-03-17 00:08:13 +00:00
|
|
|
{
|
2011-10-09 07:28:36 +00:00
|
|
|
MWWorld::Ptr::CellStore *current = mEnvironment.mWorld->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;
|
|
|
|
if((current->cell->data.flags & current->cell->Interior) || timePassed < 10)
|
|
|
|
return;
|
|
|
|
timePassed = 0;
|
2012-03-21 22:19:40 +00:00
|
|
|
|
2012-03-17 00:08:13 +00:00
|
|
|
if(regionName != current->cell->region)
|
2012-01-29 19:27:03 +00:00
|
|
|
{
|
2012-03-17 00:08:13 +00:00
|
|
|
regionName = current->cell->region;
|
|
|
|
total = 0;
|
|
|
|
}
|
2012-03-09 01:22:16 +00:00
|
|
|
|
2012-03-21 22:19:40 +00:00
|
|
|
const ESM::Region *regn = mEnvironment.mWorld->getStore().regions.find(regionName);
|
|
|
|
std::vector<ESM::Region::SoundRef>::const_iterator soundIter;
|
2012-03-17 00:08:13 +00:00
|
|
|
if(total == 0)
|
|
|
|
{
|
2012-03-21 22:19:40 +00:00
|
|
|
soundIter = regn->soundList.begin();
|
|
|
|
while(soundIter != regn->soundList.end())
|
2011-10-09 07:28:36 +00:00
|
|
|
{
|
2012-03-21 22:19:40 +00:00
|
|
|
total += (int)soundIter->chance;
|
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-03-21 22:19:40 +00:00
|
|
|
soundIter = regn->soundList.begin();
|
|
|
|
while(soundIter != regn->soundList.end())
|
2012-03-17 00:08:13 +00:00
|
|
|
{
|
|
|
|
const std::string go = soundIter->sound.toString();
|
|
|
|
int chance = (int) soundIter->chance;
|
|
|
|
//std::cout << "Sound: " << go.name <<" Chance:" << chance << "\n";
|
|
|
|
soundIter++;
|
|
|
|
if(r - pos < chance)
|
2011-10-09 07:28:36 +00:00
|
|
|
{
|
2012-03-17 00:08:13 +00:00
|
|
|
//play sound
|
|
|
|
std::cout << "Sound: " << go <<" Chance:" << chance << "\n";
|
2012-03-17 15:02:46 +00:00
|
|
|
playSound(go, 1.0f, 1.0f);
|
2012-03-17 00:08:13 +00:00
|
|
|
break;
|
2011-10-09 07:28:36 +00:00
|
|
|
}
|
2012-03-17 00:08:13 +00:00
|
|
|
pos += chance;
|
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;
|
|
|
|
timePassed = 0.0f;
|
|
|
|
|
|
|
|
// Make sure music is still playing
|
|
|
|
if(!isMusicPlaying())
|
|
|
|
startRandomTitle();
|
|
|
|
|
|
|
|
Ogre::Camera *cam = mEnvironment.mWorld->getPlayer().getRenderer()->getCamera();
|
|
|
|
Ogre::Vector3 nPos, nDir, nUp;
|
|
|
|
nPos = cam->getRealPosition();
|
|
|
|
nDir = cam->getRealDirection();
|
|
|
|
nUp = cam->getRealUp();
|
|
|
|
|
|
|
|
// The output handler is expecting vectors oriented like the game
|
|
|
|
// (that is, -Z goes down, +Y goes forward), but that's not what we
|
|
|
|
// get from Ogre's camera, so we have to convert.
|
|
|
|
float pos[3] = { nPos[0], -nPos[2], nPos[1] };
|
|
|
|
float at[3] = { nDir[0], -nDir[2], nDir[1] };
|
|
|
|
float up[3] = { nUp[0], -nUp[2], nUp[1] };
|
|
|
|
mOutput->updateListener(pos, at, up);
|
|
|
|
|
|
|
|
// 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
|
|
|
{
|
2012-03-22 02:21:36 +00:00
|
|
|
IDMap::iterator iditer = snditer->second.begin();
|
|
|
|
while(iditer != snditer->second.end())
|
2012-03-19 19:08:40 +00:00
|
|
|
{
|
|
|
|
if(!iditer->second->isPlaying())
|
2012-03-22 02:21:36 +00:00
|
|
|
snditer->second.erase(iditer++);
|
2012-03-19 19:08:40 +00:00
|
|
|
else
|
|
|
|
iditer++;
|
|
|
|
}
|
2012-03-22 02:21:36 +00:00
|
|
|
if(snditer->second.empty())
|
|
|
|
mActiveSounds.erase(snditer++);
|
|
|
|
else
|
|
|
|
snditer++;
|
2012-03-17 13:18:59 +00:00
|
|
|
}
|
|
|
|
|
2012-03-22 02:21:36 +00:00
|
|
|
IDMap::iterator iditer = mLooseSounds.begin();
|
|
|
|
while(iditer != mLooseSounds.end())
|
|
|
|
{
|
|
|
|
if(!iditer->second->isPlaying())
|
|
|
|
mLooseSounds.erase(iditer++);
|
|
|
|
else
|
|
|
|
iditer++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundManager::update(float duration)
|
|
|
|
{
|
|
|
|
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-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";
|
|
|
|
}
|
|
|
|
return "(unknown sample type)";
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *getChannelConfigName(ChannelConfig config)
|
|
|
|
{
|
|
|
|
switch(config)
|
|
|
|
{
|
|
|
|
case ChannelConfig_Mono: return "Mono";
|
|
|
|
case ChannelConfig_Stereo: return "Stereo";
|
|
|
|
}
|
|
|
|
return "(unknown channel config)";
|
|
|
|
}
|
2012-03-19 12:29:04 +00:00
|
|
|
|
|
|
|
size_t framesToBytes(size_t frames, ChannelConfig config, SampleType type)
|
|
|
|
{
|
|
|
|
switch(config)
|
|
|
|
{
|
|
|
|
case ChannelConfig_Mono: frames *= 1; break;
|
|
|
|
case ChannelConfig_Stereo: frames *= 2; break;
|
|
|
|
}
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case SampleType_UInt8: frames *= 1; break;
|
|
|
|
case SampleType_Int16: frames *= 2; break;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|