- sound system is now threaded by default (hopefully this fixes buffer underruns errors from openal)

- background music now plays when game is loading
This commit is contained in:
Mark Vejvoda 2011-01-18 01:24:45 +00:00
parent 9ba8668751
commit d16f6a93bc
8 changed files with 54 additions and 12 deletions

View File

@ -611,7 +611,10 @@ void Game::init(bool initForPreviewOnly)
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
//sounds
SoundRenderer &soundRenderer= SoundRenderer::getInstance();
soundRenderer.stopAllSounds();
soundRenderer= SoundRenderer::getInstance();
Tileset *tileset= world.getTileset();
AmbientSounds *ambientSounds= tileset->getAmbientSounds();

View File

@ -301,7 +301,7 @@ void Program::loopWorker() {
if(prevState == this->programState) {
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
if(soundThreadManager == NULL) {
if(soundThreadManager == NULL || soundThreadManager->isThreadExecutionLagging()) {
SoundRenderer::getInstance().update();
if(chronoUpdateLoop.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] SoundRenderer::getInstance().update() took msecs: %lld, updateCount = %d\n",__FILE__,__FUNCTION__,__LINE__,chronoUpdateLoop.getMillis(),updateCount);
if(chronoUpdateLoop.getMillis() > 0) chronoUpdateLoop.start();
@ -553,10 +553,11 @@ void Program::init(WindowGl *window, bool initSound, bool toggleFullScreen){
}
// Run sound streaming in a background thread if enabled
if(config.getBool("ThreadedSoundStream","false") == true) {
BaseThread::shutdownAndWait(soundThreadManager);
delete soundThreadManager;
soundThreadManager = new SimpleTaskThread(&SoundRenderer::getInstance(),0,50);
if(SoundRenderer::getInstance().runningThreaded() == true) {
if(BaseThread::shutdownAndWait(soundThreadManager) == true) {
delete soundThreadManager;
}
soundThreadManager = new SimpleTaskThread(&SoundRenderer::getInstance(),0,10);
soundThreadManager->setUniqueID(__FILE__);
soundThreadManager->start();
}

View File

@ -76,8 +76,8 @@ MainMenu::~MainMenu() {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
SoundRenderer &soundRenderer= SoundRenderer::getInstance();
soundRenderer.stopAllSounds();
//SoundRenderer &soundRenderer= SoundRenderer::getInstance();
//soundRenderer.stopAllSounds();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
}

View File

@ -1361,6 +1361,10 @@ void MenuStateCustomGame::update() {
Chrono chrono;
chrono.start();
// Test openal buffer underrun issue
//sleep(200);
// END
MutexSafeWrapper safeMutex((publishToMasterserverThread != NULL ? publishToMasterserverThread->getMutexThreadObjectAccessor() : NULL));
try {

View File

@ -37,8 +37,10 @@ SoundRenderer::SoundRenderer() {
soundPlayer = NULL;
loadConfig();
runThreadSafe = false;
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
Config &config= Config::getInstance();
runThreadSafe = config.getBool("ThreadedSoundStream","true");
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] runThreadSafe = %d\n",__FILE__,__FUNCTION__,__LINE__,runThreadSafe);
}
bool SoundRenderer::init(Window *window) {
@ -46,14 +48,18 @@ bool SoundRenderer::init(Window *window) {
SoundInterface &si= SoundInterface::getInstance();
FactoryRepository &fr= FactoryRepository::getInstance();
Config &config= Config::getInstance();
runThreadSafe = config.getBool("ThreadedSoundStream","false");
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
Config &config= Config::getInstance();
si.setFactory(fr.getSoundFactory(config.getString("FactorySound")));
stopAllSounds();
MutexSafeWrapper safeMutex(NULL);
if(runThreadSafe == true) {
safeMutex.setMutex(&mutex);
}
soundPlayer= si.newSoundPlayer();
if(soundPlayer != NULL) {
SoundPlayerParams soundPlayerParams;
@ -61,6 +67,7 @@ bool SoundRenderer::init(Window *window) {
soundPlayerParams.strBufferCount= config.getInt("SoundStreamingBuffers");
soundPlayer->init(&soundPlayerParams);
}
safeMutex.ReleaseLock();
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
@ -89,6 +96,10 @@ SoundRenderer::~SoundRenderer() {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__);
MutexSafeWrapper safeMutex(NULL);
if(runThreadSafe == true) {
safeMutex.setMutex(&mutex);
}
delete soundPlayer;
soundPlayer = NULL;

View File

@ -79,6 +79,8 @@ public:
void loadConfig();
bool wasInitOk() const;
bool runningThreaded() const { return runThreadSafe; }
};
}}//end namespace

View File

@ -63,6 +63,9 @@ protected:
bool taskSignalled;
bool needTaskSignal;
Mutex mutexLastExecuteTimestamp;
time_t lastExecuteTimestamp;
public:
SimpleTaskThread();
SimpleTaskThread(SimpleTaskCallbackInterface *simpleTaskInterface,
@ -74,6 +77,8 @@ public:
void setTaskSignalled(bool value);
bool getTaskSignalled();
bool isThreadExecutionLagging();
};
// =====================================================

View File

@ -85,6 +85,18 @@ SimpleTaskThread::SimpleTaskThread( SimpleTaskCallbackInterface *simpleTaskInter
this->millisecsBetweenExecutions = millisecsBetweenExecutions;
this->needTaskSignal = needTaskSignal;
setTaskSignalled(false);
MutexSafeWrapper safeMutex(&mutexLastExecuteTimestamp);
lastExecuteTimestamp = time(NULL);
}
bool SimpleTaskThread::isThreadExecutionLagging() {
bool result = false;
MutexSafeWrapper safeMutex(&mutexLastExecuteTimestamp);
result = (difftime(time(NULL),lastExecuteTimestamp) >= 5.0);
safeMutex.ReleaseLock();
return result;
}
bool SimpleTaskThread::canShutdown(bool deleteSelfIfShutdownDelayed) {
@ -124,8 +136,12 @@ void SimpleTaskThread::execute() {
else if(runTask == true) {
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s]\n",__FILE__,__FUNCTION__,__LINE__,this->getUniqueID().c_str());
if(getQuitStatus() == false) {
ExecutingTaskSafeWrapper safeExecutingTaskMutex(this);
ExecutingTaskSafeWrapper safeExecutingTaskMutex(this);
this->simpleTaskInterface->simpleTask(this);
MutexSafeWrapper safeMutex(&mutexLastExecuteTimestamp);
lastExecuteTimestamp = time(NULL);
safeMutex.ReleaseLock();
}
//SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] uniqueID [%s]\n",__FILE__,__FUNCTION__,__LINE__,this->getUniqueID().c_str());
}