Properly use the ENABLE_SOUND flag

This commit is contained in:
Benau 2018-07-07 10:43:05 +08:00
parent 02fea1ee43
commit 622f46d95a
4 changed files with 95 additions and 61 deletions

View File

@ -21,7 +21,7 @@
#define HEADER_DUMMY_SFX_HPP #define HEADER_DUMMY_SFX_HPP
#include "audio/sfx_base.hpp" #include "audio/sfx_base.hpp"
#include "utils/cpp2011.hpp"
/** /**
* \brief Dummy sound when ogg or openal aren't available * \brief Dummy sound when ogg or openal aren't available
@ -32,41 +32,41 @@ class DummySFX : public SFXBase
public: public:
DummySFX(SFXBuffer* buffer, bool positional, DummySFX(SFXBuffer* buffer, bool positional,
float gain) {} float gain) {}
virtual ~DummySFX() {} virtual ~DummySFX() {}
/** Late creation, if SFX was initially disabled */ /** Late creation, if SFX was initially disabled */
virtual bool init() { return true; } virtual bool init() OVERRIDE { return true; }
virtual bool isLooped() { return false; } virtual bool isLooped() OVERRIDE { return false; }
virtual void updatePlayingSFX(float dt) {} virtual void updatePlayingSFX(float dt) OVERRIDE {}
virtual void setLoop(bool status) {} virtual void setLoop(bool status) OVERRIDE {}
virtual void reallySetLoop(bool status) {} virtual void reallySetLoop(bool status) OVERRIDE {}
virtual void setPosition(const Vec3 &p) {} virtual void setPosition(const Vec3 &p) OVERRIDE {}
virtual void reallySetPosition(const Vec3 &p) {} virtual void reallySetPosition(const Vec3 &p) OVERRIDE {}
virtual void setSpeedPosition(float factor, virtual void setSpeedPosition(float factor,
const Vec3 &p) {} const Vec3 &p) OVERRIDE {}
virtual void reallySetSpeedPosition(float f, virtual void reallySetSpeedPosition(float f,
const Vec3 &p) {} const Vec3 &p) OVERRIDE {}
virtual void play() {} virtual void play() OVERRIDE {}
virtual void reallyPlayNow(SFXBuffer* buffer = NULL) {} virtual void reallyPlayNow(SFXBuffer* buffer = NULL) OVERRIDE {}
virtual void play(const Vec3 &xyz, SFXBuffer* buffer = NULL) {} virtual void play(const Vec3 &xyz, SFXBuffer* buffer = NULL) OVERRIDE {}
virtual void reallyPlayNow(const Vec3 &xyz, SFXBuffer* buffer = NULL) {} virtual void reallyPlayNow(const Vec3 &xyz, SFXBuffer* buffer = NULL) OVERRIDE {}
virtual void stop() {} virtual void stop() OVERRIDE {}
virtual void reallyStopNow() {} virtual void reallyStopNow() OVERRIDE {}
virtual void pause() {} virtual void pause() OVERRIDE {}
virtual void reallyPauseNow() {} virtual void reallyPauseNow() OVERRIDE {}
virtual void resume() {} virtual void resume() OVERRIDE {}
virtual void reallyResumeNow() {} virtual void reallyResumeNow() OVERRIDE {}
virtual void deleteSFX() { delete this; } virtual void deleteSFX() OVERRIDE {}
virtual void setSpeed(float factor) {} virtual void setSpeed(float factor) OVERRIDE {}
virtual void reallySetSpeed(float factor) {} virtual void reallySetSpeed(float factor) OVERRIDE {}
virtual void setVolume(float gain) {} virtual void setVolume(float gain) OVERRIDE {}
virtual void reallySetVolume(float gain) {} virtual void reallySetVolume(float gain) OVERRIDE {}
virtual void setMasterVolume(float gain) {} virtual void setMasterVolume(float gain) OVERRIDE {}
virtual void reallySetMasterVolumeNow(float gain) {} virtual void reallySetMasterVolumeNow(float gain) OVERRIDE {}
virtual SFXStatus getStatus() { return SFX_STOPPED; } virtual SFXStatus getStatus() OVERRIDE { return SFX_STOPPED; }
virtual void onSoundEnabledBack() {} virtual void onSoundEnabledBack() OVERRIDE {}
virtual void setRolloff(float rolloff) {} virtual void setRolloff(float rolloff) OVERRIDE {}
virtual const SFXBuffer* getBuffer() const { return NULL; } virtual const SFXBuffer* getBuffer() const OVERRIDE { return NULL; }
}; // DummySFX }; // DummySFX

View File

@ -33,6 +33,7 @@
#endif #endif
#include "audio/music_ogg.hpp" #include "audio/music_ogg.hpp"
#include "audio/sfx_manager.hpp"
#include "audio/sfx_openal.hpp" #include "audio/sfx_openal.hpp"
#include "config/user_config.hpp" #include "config/user_config.hpp"
#include "io/file_manager.hpp" #include "io/file_manager.hpp"
@ -44,6 +45,7 @@ MusicManager* music_manager= NULL;
MusicManager::MusicManager() MusicManager::MusicManager()
{ {
m_current_music= NULL; m_current_music= NULL;
m_initialized = false;
setMasterMusicVolume(UserConfigParams::m_music_volume); setMasterMusicVolume(UserConfigParams::m_music_volume);
//FIXME: I'm not sure that this code goes here //FIXME: I'm not sure that this code goes here

View File

@ -97,6 +97,7 @@ SFXManager::SFXManager()
loadSfx(); loadSfx();
#ifdef ENABLE_SOUND
pthread_cond_init(&m_cond_request, NULL); pthread_cond_init(&m_cond_request, NULL);
pthread_attr_t attr; pthread_attr_t attr;
@ -123,7 +124,7 @@ SFXManager::SFXManager()
m_sfx_commands.lock(); m_sfx_commands.lock();
m_sfx_commands.getData().clear(); m_sfx_commands.getData().clear();
m_sfx_commands.unlock(); m_sfx_commands.unlock();
#endif
} // SoundManager } // SoundManager
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -131,11 +132,13 @@ SFXManager::SFXManager()
*/ */
SFXManager::~SFXManager() SFXManager::~SFXManager()
{ {
#ifdef ENABLE_SOUND
m_thread_id.lock(); m_thread_id.lock();
pthread_join(*m_thread_id.getData(), NULL); pthread_join(*m_thread_id.getData(), NULL);
delete m_thread_id.getData(); delete m_thread_id.getData();
m_thread_id.unlock(); m_thread_id.unlock();
pthread_cond_destroy(&m_cond_request); pthread_cond_destroy(&m_cond_request);
#endif
// ---- clear m_all_sfx // ---- clear m_all_sfx
// not strictly necessary, but might avoid copy&paste problems // not strictly necessary, but might avoid copy&paste problems
@ -185,8 +188,10 @@ SFXManager::~SFXManager()
*/ */
void SFXManager::queue(SFXCommands command, SFXBase *sfx) void SFXManager::queue(SFXCommands command, SFXBase *sfx)
{ {
#ifdef ENABLE_SOUND
SFXCommand *sfx_command = new SFXCommand(command, sfx); SFXCommand *sfx_command = new SFXCommand(command, sfx);
queueCommand(sfx_command); queueCommand(sfx_command);
#endif
} // queue } // queue
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -199,8 +204,10 @@ void SFXManager::queue(SFXCommands command, SFXBase *sfx)
*/ */
void SFXManager::queue(SFXCommands command, SFXBase *sfx, float f) void SFXManager::queue(SFXCommands command, SFXBase *sfx, float f)
{ {
#ifdef ENABLE_SOUND
SFXCommand *sfx_command = new SFXCommand(command, sfx, f); SFXCommand *sfx_command = new SFXCommand(command, sfx, f);
queueCommand(sfx_command); queueCommand(sfx_command);
#endif
} // queue(float) } // queue(float)
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -213,17 +220,21 @@ void SFXManager::queue(SFXCommands command, SFXBase *sfx, float f)
*/ */
void SFXManager::queue(SFXCommands command, SFXBase *sfx, const Vec3 &p) void SFXManager::queue(SFXCommands command, SFXBase *sfx, const Vec3 &p)
{ {
#ifdef ENABLE_SOUND
SFXCommand *sfx_command = new SFXCommand(command, sfx, p); SFXCommand *sfx_command = new SFXCommand(command, sfx, p);
queueCommand(sfx_command); queueCommand(sfx_command);
#endif
} // queue (Vec3) } // queue (Vec3)
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
void SFXManager::queue(SFXCommands command, SFXBase *sfx, const Vec3 &p, SFXBuffer* buffer) void SFXManager::queue(SFXCommands command, SFXBase *sfx, const Vec3 &p, SFXBuffer* buffer)
{ {
#ifdef ENABLE_SOUND
SFXCommand *sfx_command = new SFXCommand(command, sfx, p); SFXCommand *sfx_command = new SFXCommand(command, sfx, p);
sfx_command->m_buffer = buffer; sfx_command->m_buffer = buffer;
queueCommand(sfx_command); queueCommand(sfx_command);
#endif
} // queue (Vec3) } // queue (Vec3)
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -238,8 +249,10 @@ void SFXManager::queue(SFXCommands command, SFXBase *sfx, const Vec3 &p, SFXBuff
void SFXManager::queue(SFXCommands command, SFXBase *sfx, float f, void SFXManager::queue(SFXCommands command, SFXBase *sfx, float f,
const Vec3 &p) const Vec3 &p)
{ {
#ifdef ENABLE_SOUND
SFXCommand *sfx_command = new SFXCommand(command, sfx, f, p); SFXCommand *sfx_command = new SFXCommand(command, sfx, f, p);
queueCommand(sfx_command); queueCommand(sfx_command);
#endif
} // queue(float, Vec3) } // queue(float, Vec3)
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -248,8 +261,10 @@ void SFXManager::queue(SFXCommands command, SFXBase *sfx, float f,
*/ */
void SFXManager::queue(SFXCommands command, MusicInformation *mi) void SFXManager::queue(SFXCommands command, MusicInformation *mi)
{ {
#ifdef ENABLE_SOUND
SFXCommand *sfx_command = new SFXCommand(command, mi); SFXCommand *sfx_command = new SFXCommand(command, mi);
queueCommand(sfx_command); queueCommand(sfx_command);
#endif
} // queue(MusicInformation) } // queue(MusicInformation)
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
/** Queues a command for the music manager that takes a floating point value /** Queues a command for the music manager that takes a floating point value
@ -259,8 +274,10 @@ void SFXManager::queue(SFXCommands command, MusicInformation *mi)
*/ */
void SFXManager::queue(SFXCommands command, MusicInformation *mi, float f) void SFXManager::queue(SFXCommands command, MusicInformation *mi, float f)
{ {
#ifdef ENABLE_SOUND
SFXCommand *sfx_command = new SFXCommand(command, mi, f); SFXCommand *sfx_command = new SFXCommand(command, mi, f);
queueCommand(sfx_command); queueCommand(sfx_command);
#endif
} // queue(MusicInformation) } // queue(MusicInformation)
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -270,6 +287,7 @@ void SFXManager::queue(SFXCommands command, MusicInformation *mi, float f)
*/ */
void SFXManager::queueCommand(SFXCommand *command) void SFXManager::queueCommand(SFXCommand *command)
{ {
#ifdef ENABLE_SOUND
m_sfx_commands.lock(); m_sfx_commands.lock();
if(World::getWorld() && if(World::getWorld() &&
m_sfx_commands.getData().size() > 20*race_manager->getNumberOfKarts()+20 && m_sfx_commands.getData().size() > 20*race_manager->getNumberOfKarts()+20 &&
@ -293,6 +311,7 @@ void SFXManager::queueCommand(SFXCommand *command)
} }
m_sfx_commands.getData().push_back(command); m_sfx_commands.getData().push_back(command);
m_sfx_commands.unlock(); m_sfx_commands.unlock();
#endif
} // queueCommand } // queueCommand
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -301,9 +320,13 @@ void SFXManager::queueCommand(SFXCommand *command)
*/ */
void SFXManager::stopThread() void SFXManager::stopThread()
{ {
#ifdef ENABLE_SOUND
queue(SFX_EXIT); queue(SFX_EXIT);
// Make sure the thread wakes up. // Make sure the thread wakes up.
pthread_cond_signal(&m_cond_request); pthread_cond_signal(&m_cond_request);
#else
setCanBeDeleted();
#endif
} // stopThread } // stopThread
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -314,6 +337,7 @@ void SFXManager::stopThread()
*/ */
void* SFXManager::mainLoop(void *obj) void* SFXManager::mainLoop(void *obj)
{ {
#ifdef ENABLE_SOUND
VS::setThreadName("SFXManager"); VS::setThreadName("SFXManager");
SFXManager *me = (SFXManager*)obj; SFXManager *me = (SFXManager*)obj;
@ -436,6 +460,7 @@ void* SFXManager::mainLoop(void *obj)
me->m_sfx_commands.getData().erase(me->m_sfx_commands.getData().begin()); me->m_sfx_commands.getData().erase(me->m_sfx_commands.getData().begin());
} }
me->m_sfx_commands.unlock(); me->m_sfx_commands.unlock();
#endif
return NULL; return NULL;
} // mainLoop } // mainLoop
@ -653,7 +678,7 @@ SFXBase* SFXManager::createSoundSource(SFXBuffer* buffer,
//assert( alIsBuffer(buffer->getBufferID()) ); crashes on server //assert( alIsBuffer(buffer->getBufferID()) ); crashes on server
SFXBase* sfx = new SFXOpenAL(buffer, positional, buffer->getGain(), owns_buffer); SFXBase* sfx = new SFXOpenAL(buffer, positional, buffer->getGain(), owns_buffer);
#else #else
SFXBase* sfx = new DummySFX(buffer, positional, buffer->getGain(), owns_buffer); SFXBase* sfx = new DummySFX(buffer, positional, buffer->getGain());
#endif #endif
sfx->setMasterVolume(m_master_gain); sfx->setMasterVolume(m_master_gain);
@ -738,9 +763,11 @@ void SFXManager::deleteSFXMapping(const std::string &name)
*/ */
void SFXManager::update() void SFXManager::update()
{ {
#ifdef ENABLE_SOUND
queue(SFX_UPDATE, (SFXBase*)NULL); queue(SFX_UPDATE, (SFXBase*)NULL);
// Wake up the sfx thread to handle all queued up audio commands. // Wake up the sfx thread to handle all queued up audio commands.
pthread_cond_signal(&m_cond_request); pthread_cond_signal(&m_cond_request);
#endif
} // update } // update
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -750,6 +777,7 @@ void SFXManager::update()
*/ */
void SFXManager::reallyUpdateNow(SFXCommand *current) void SFXManager::reallyUpdateNow(SFXCommand *current)
{ {
#ifdef ENABLE_SOUND
if (m_last_update_time < 0.0) if (m_last_update_time < 0.0)
{ {
// first time // first time
@ -782,7 +810,7 @@ void SFXManager::reallyUpdateNow(SFXCommand *current)
i->second->updatePlayingSFX(dt); i->second->updatePlayingSFX(dt);
} // for i in m_all_sfx } // for i in m_all_sfx
m_quick_sounds.unlock(); m_quick_sounds.unlock();
#endif
} // reallyUpdateNow } // reallyUpdateNow
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -1002,6 +1030,7 @@ void SFXManager::reallyPositionListenerNow()
*/ */
SFXBase* SFXManager::quickSound(const std::string &sound_type) SFXBase* SFXManager::quickSound(const std::string &sound_type)
{ {
#ifdef ENABLE_SOUND
if (!sfxAllowed()) return NULL; if (!sfxAllowed()) return NULL;
MutexLockerHelper lock(m_quick_sounds); MutexLockerHelper lock(m_quick_sounds);
@ -1024,6 +1053,8 @@ SFXBase* SFXManager::quickSound(const std::string &sound_type)
base_sound->play(); base_sound->play();
return base_sound; return base_sound;
} }
#else
return NULL;
#endif
} // quickSound } // quickSound

View File

@ -81,43 +81,44 @@ public:
bool owns_buffer = false); bool owns_buffer = false);
virtual ~SFXOpenAL(); virtual ~SFXOpenAL();
virtual void updatePlayingSFX(float dt); virtual void updatePlayingSFX(float dt) OVERRIDE;
virtual bool init() OVERRIDE; virtual bool init() OVERRIDE;
virtual void play() OVERRIDE; virtual void play() OVERRIDE;
virtual void reallyPlayNow(SFXBuffer* buffer = NULL) OVERRIDE; virtual void reallyPlayNow(SFXBuffer* buffer = NULL) OVERRIDE;
virtual void play(const Vec3 &xyz, SFXBuffer* buffer = NULL) OVERRIDE; virtual void play(const Vec3 &xyz, SFXBuffer* buffer = NULL) OVERRIDE;
virtual void reallyPlayNow(const Vec3 &xyz, SFXBuffer* buffer = NULL) OVERRIDE; virtual void reallyPlayNow(const Vec3 &xyz, SFXBuffer* buffer = NULL) OVERRIDE;
virtual void setLoop(bool status); virtual void setLoop(bool status) OVERRIDE;
virtual void reallySetLoop(bool status); virtual void reallySetLoop(bool status) OVERRIDE;
virtual void stop(); virtual void stop() OVERRIDE;
virtual void reallyStopNow(); virtual void reallyStopNow() OVERRIDE;
virtual void pause(); virtual void pause() OVERRIDE;
virtual void reallyPauseNow(); virtual void reallyPauseNow() OVERRIDE;
virtual void resume(); virtual void resume() OVERRIDE;
virtual void reallyResumeNow(); virtual void reallyResumeNow() OVERRIDE;
virtual void deleteSFX(); virtual void deleteSFX() OVERRIDE;
virtual void setSpeed(float factor); virtual void setSpeed(float factor) OVERRIDE;
virtual void reallySetSpeed(float factor); virtual void reallySetSpeed(float factor) OVERRIDE;
virtual void setPosition(const Vec3 &position); virtual void setPosition(const Vec3 &position) OVERRIDE;
virtual void reallySetPosition(const Vec3 &p); virtual void reallySetPosition(const Vec3 &p) OVERRIDE;
virtual void setSpeedPosition(float factor, const Vec3 &p); virtual void setSpeedPosition(float factor, const Vec3 &p) OVERRIDE;
virtual void reallySetSpeedPosition(float f,const Vec3 &p); virtual void reallySetSpeedPosition(float f,const Vec3 &p) OVERRIDE;
virtual void setVolume(float volume); virtual void setVolume(float volume) OVERRIDE;
virtual void reallySetVolume(float volume); virtual void reallySetVolume(float volume) OVERRIDE;
virtual void setMasterVolume(float volume); virtual void setMasterVolume(float volume) OVERRIDE;
virtual void reallySetMasterVolumeNow(float volue); virtual void reallySetMasterVolumeNow(float volue) OVERRIDE;
virtual void onSoundEnabledBack(); virtual void onSoundEnabledBack() OVERRIDE;
virtual void setRolloff(float rolloff); virtual void setRolloff(float rolloff) OVERRIDE;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns if this sfx is looped or not. */ /** Returns if this sfx is looped or not. */
virtual bool isLooped() { return m_loop; } virtual bool isLooped() OVERRIDE { return m_loop; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns the status of this sfx. */ /** Returns the status of this sfx. */
virtual SFXStatus getStatus() { return m_status; } virtual SFXStatus getStatus() OVERRIDE { return m_status; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns the buffer associated with this sfx. */ /** Returns the buffer associated with this sfx. */
virtual const SFXBuffer* getBuffer() const { return m_sound_buffer; } virtual const SFXBuffer* getBuffer() const OVERRIDE
{ return m_sound_buffer; }
}; // SFXOpenAL }; // SFXOpenAL