Properly handle volume setting from sfx thread, removed MusicManager

from friends of MusicInformation to enforce this. Added error checks
to properly detect location of warning when faster music is plaued
(still under investigation).
This commit is contained in:
hiker
2015-02-05 09:22:13 +11:00
parent ddc60f98a1
commit 1e994748cc
6 changed files with 29 additions and 14 deletions

View File

@@ -78,11 +78,10 @@ private:
MusicInformation (const XMLNode *root, const std::string &filename);
// Declare the following functions private, but allow the SFXManager
// and music manager to access them. This makes sure that only the sfx thread calls
// openal/vorbis etc, and so makes it all thread safe.
// to access them. This makes sure that only the sfx thread calls
// openal/vorbis etc, and so makes it is all thread safe.
private:
friend class SFXManager;
friend class MusicManager;
void update(float dt);
void startMusic();
void stopMusic();

View File

@@ -192,7 +192,6 @@ void MusicManager::startMusic(MusicInformation* mi, bool start_right_now)
if(!mi || !UserConfigParams::m_music || !m_initialized) return;
mi->volumeMusic(m_master_gain);
SFXManager::get()->queue(start_right_now ? SFXManager::SFX_MUSIC_START
: SFXManager::SFX_MUSIC_WAITING,
mi);
@@ -269,7 +268,12 @@ void MusicManager::setMasterMusicVolume(float gain)
gain = 0.0f;
m_master_gain = gain;
if(m_current_music) m_current_music->volumeMusic(m_master_gain);
if (m_current_music)
{
// Sets the music volume to m_master_gain
SFXManager::get()->queue(SFXManager::SFX_MUSIC_VOLUME,
m_current_music);
}
UserConfigParams::m_music_volume = m_master_gain;
} // setMasterMusicVolume
@@ -292,7 +296,7 @@ MusicInformation* MusicManager::getMusicInformation(const std::string& filename)
MusicInformation *mi = MusicInformation::create(filename);
if(mi)
{
mi->volumeMusic(m_master_gain);
SFXManager::get()->queue(SFXManager::SFX_MUSIC_VOLUME, mi);
m_all_music[basename] = mi;
}
return mi;

View File

@@ -189,7 +189,7 @@ bool MusicOggStream::playMusic()
alSourcePlay(m_soundSource);
m_pausedMusic = false;
m_playing = true;
check("playMusic");
return true;
} // playMusic
@@ -253,6 +253,7 @@ void MusicOggStream::volumeMusic(float gain)
if (gain < 0.0f) gain = 0.0f;
alSourcef(m_soundSource, AL_GAIN, gain);
check("volume music"); // clear errors
} // volumeMusic
//-----------------------------------------------------------------------------

View File

@@ -22,6 +22,7 @@
#include "audio/sfx_buffer.hpp"
#include "config/user_config.hpp"
#include "io/file_manager.hpp"
#include "modes/world.hpp"
#include "race/race_manager.hpp"
#include <pthread.h>
@@ -230,7 +231,8 @@ void SFXManager::queue(SFXCommands command, MusicInformation *mi, float f)
void SFXManager::queueCommand(SFXCommand *command)
{
m_sfx_commands.lock();
if(m_sfx_commands.getData().size() > 20*race_manager->getNumberOfKarts()+20 &&
if(World::getWorld() &&
m_sfx_commands.getData().size() > 20*race_manager->getNumberOfKarts()+20 &&
race_manager->getMinorMode() != RaceManager::MINOR_MODE_CUTSCENE)
{
if(command->m_command==SFX_POSITION || command->m_command==SFX_LOOP ||
@@ -263,6 +265,7 @@ void SFXManager::stopThread()
pthread_cond_signal(&m_cond_request);
} // stopThread
#include "audio/sfx_openal.hpp"
//----------------------------------------------------------------------------
/** This loops runs in a different threads, and starts sfx to be played.
* This can sometimes take up to 5 ms, so it needs to be handled in a thread
@@ -323,7 +326,11 @@ void* SFXManager::mainLoop(void *obj)
case SFX_LISTENER: me->reallyPositionListenerNow(); break;
case SFX_UPDATE: me->reallyUpdateNow(current); break;
case SFX_MUSIC_START:
{
float gain = music_manager->getMasterMusicVolume();
current->m_music_information->volumeMusic(gain);
current->m_music_information->startMusic(); break;
}
case SFX_MUSIC_STOP:
current->m_music_information->stopMusic(); break;
case SFX_MUSIC_PAUSE:
@@ -344,13 +351,15 @@ void* SFXManager::mainLoop(void *obj)
}
case SFX_MUSIC_WAITING:
current->m_music_information->setMusicWaiting(); break;
case SFX_MUSIC_VOLUME:
{
float gain = music_manager->getMasterMusicVolume();
current->m_music_information->volumeMusic(gain);
}
default: assert("Not yet supported.");
}
static SFXCommand *prev = NULL;
delete prev;
prev = current;
//delete current;
//current = NULL;
delete current;
current = NULL;
me->m_sfx_commands.lock();
} // while

View File

@@ -85,6 +85,7 @@ public:
SFX_MUSIC_SET_TMP_VOLUME,
SFX_MUSIC_RESET_TMP_VOLUME,
SFX_MUSIC_WAITING,
SFX_MUSIC_VOLUME,
SFX_EXIT,
}; // SFXCommands

View File

@@ -174,6 +174,7 @@ void SFXOpenAL::reallySetSpeed(float factor)
factor = 0.5f;
}
alSourcef(m_sound_source,AL_PITCH,factor);
SFXManager::checkError("setting speed");
} // reallySetSpeed
//-----------------------------------------------------------------------------
@@ -415,7 +416,7 @@ void SFXOpenAL::reallySetPosition(const Vec3 &position)
alSource3f(m_sound_source, AL_POSITION, position.getX(),
position.getY(), -position.getZ());
if (SFXManager::get()->getListenerPos().distance(position)
if (SFXManager::get()->getListenerPos().distance(position)
> m_sound_buffer->getMaxDist())
{
alSourcef(m_sound_source, AL_GAIN, 0);