Use accurate timing for sounds. Fixes #2049

This commit is contained in:
Marianne Gagnon 2015-03-26 20:49:36 -04:00
parent efcfdcced7
commit 26bd5a89a0
2 changed files with 16 additions and 4 deletions

View File

@ -74,6 +74,7 @@ SFXManager::SFXManager()
// The sound manager initialises OpenAL
m_initialized = music_manager->initialized();
m_master_gain = UserConfigParams::m_sfx_volume;
m_last_update_time = -1.0f;
// Init position, since it can be used before positionListener is called.
// No need to use lock here, since the thread will be created later.
m_listener_position.getData() = Vec3(0, 0, 0);
@ -669,9 +670,9 @@ void SFXManager::deleteSFXMapping(const std::string &name)
* adds an update command for the music manager.
* \param dt Time step size.
*/
void SFXManager::update(float dt)
void SFXManager::update()
{
queue(SFX_UPDATE, (SFXBase*)NULL, dt);
queue(SFX_UPDATE, (SFXBase*)NULL);
// Wake up the sfx thread to handle all queued up audio commands.
pthread_cond_signal(&m_cond_request);
} // update
@ -683,8 +684,17 @@ void SFXManager::update(float dt)
*/
void SFXManager::reallyUpdateNow(SFXCommand *current)
{
if (m_last_update_time < 0.0)
{
// first time
m_last_update_time = StkTime::getRealTime();
}
double previous_update_time = m_last_update_time;
m_last_update_time = StkTime::getRealTime();
double dt = m_last_update_time - previous_update_time;
assert(current->m_command==SFX_UPDATE);
float dt = current->m_parameter.getX();
if (music_manager->getCurrentMusic())
music_manager->getCurrentMusic()->update(dt);
m_all_sfx.lock();

View File

@ -200,6 +200,8 @@ private:
/** Thread id of the thread running in this object. */
Synchronised<pthread_t *> m_thread_id;
double m_last_update_time;
/** A conditional variable to wake up the main loop. */
pthread_cond_t m_cond_request;
@ -253,7 +255,7 @@ public:
void reallyPauseAllNow();
void resumeAll();
void reallyResumeAllNow();
void update(float dt);
void update();
void reallyUpdateNow(SFXCommand *current);
bool soundExist(const std::string &name);
void setMasterSFXVolume(float gain);