From 9b9785e8ec840353bde020266ead90ce7509295d Mon Sep 17 00:00:00 2001 From: Deve Date: Sat, 18 Aug 2018 23:11:51 +0200 Subject: [PATCH] Add a possibility to disable sound in commandline --- src/audio/music_information.cpp | 23 +++-- src/audio/music_manager.cpp | 67 +++++++------- src/audio/sfx_buffer.cpp | 49 ++++++----- src/audio/sfx_manager.cpp | 150 ++++++++++++++++++++++---------- src/config/user_config.hpp | 2 + src/main.cpp | 4 +- 6 files changed, 190 insertions(+), 105 deletions(-) diff --git a/src/audio/music_information.cpp b/src/audio/music_information.cpp index 3b1b478fb..b301a5380 100644 --- a/src/audio/music_information.cpp +++ b/src/audio/music_information.cpp @@ -23,6 +23,7 @@ #include "audio/music_dummy.hpp" #include "audio/music_ogg.hpp" +#include "config/user_config.hpp" #include "io/file_manager.hpp" #include "tracks/track.hpp" #include "tracks/track_manager.hpp" @@ -170,10 +171,15 @@ void MusicInformation::startMusic() } #ifdef ENABLE_SOUND - m_normal_music = new MusicOggStream(m_normal_loop_start); -#else - m_normal_music = new MusicDummy(); + if (UserConfigParams::m_enable_sound) + { + m_normal_music = new MusicOggStream(m_normal_loop_start); + } + else #endif + { + m_normal_music = new MusicDummy(); + } if (m_normal_music->load(m_normal_filename) == false) { @@ -201,10 +207,15 @@ void MusicInformation::startMusic() } #ifdef ENABLE_SOUND - m_fast_music = new MusicOggStream(m_fast_loop_start); -#else - m_fast_music = new MusicDummy(); + if (UserConfigParams::m_enable_sound) + { + m_fast_music = new MusicOggStream(m_fast_loop_start); + } + else #endif + { + m_fast_music = new MusicDummy(); + } if (m_fast_music->load(m_fast_filename) == false) { diff --git a/src/audio/music_manager.cpp b/src/audio/music_manager.cpp index 6ef12a973..8c1699d50 100644 --- a/src/audio/music_manager.cpp +++ b/src/audio/music_manager.cpp @@ -50,43 +50,44 @@ MusicManager::MusicManager() //FIXME: I'm not sure that this code goes here #ifdef ENABLE_SOUND - + if (UserConfigParams::m_enable_sound) + { #if defined(__APPLE__) && !defined(NDEBUG) - // HACK: On OSX, when OpenAL is initialized, breaking in a debugger causes - // my iTunes music to stop too, which is highly annoying ;) so in debug - // mode, require a restart to enable sound - if (UserConfigParams::m_sfx || UserConfigParams::m_music) - { + // HACK: On OSX, when OpenAL is initialized, breaking in a debugger + // causes my iTunes music to stop too, which is highly annoying ;) so in + // debug mode, require a restart to enable sound + if (UserConfigParams::m_sfx || UserConfigParams::m_music) + { #endif - - ALCdevice* device = alcOpenDevice ( NULL ); //The default sound device - if( device == NULL ) - { - Log::warn("MusicManager", "Could not open the default sound device."); - m_initialized = false; - } - else - { - - ALCcontext* context = alcCreateContext( device, NULL ); - - if( context == NULL ) - { - Log::warn("MusicManager", "Could not create a sound context."); - m_initialized = false; - } - else - { - alcMakeContextCurrent( context ); - m_initialized = true; - } - } - + ALCdevice* device = alcOpenDevice(NULL); //The default sound device + + if (device == NULL) + { + Log::warn("MusicManager", "Could not open the default sound " + "device."); + m_initialized = false; + } + else + { + ALCcontext* context = alcCreateContext(device, NULL); + + if (context == NULL) + { + Log::warn("MusicManager", "Could not create a sound " + "context."); + m_initialized = false; + } + else + { + alcMakeContextCurrent(context); + m_initialized = true; + } + } #if defined(__APPLE__) && !defined(NDEBUG) - } + } #endif - - alGetError(); //Called here to clear any non-important errors found + alGetError(); //Called here to clear any non-important errors found + } #endif loadMusicInformation(); diff --git a/src/audio/sfx_buffer.cpp b/src/audio/sfx_buffer.cpp index 03e3a4210..febc4bba2 100644 --- a/src/audio/sfx_buffer.cpp +++ b/src/audio/sfx_buffer.cpp @@ -97,24 +97,27 @@ bool SFXBuffer::load() if (UserConfigParams::m_sfx == false) return false; #ifdef ENABLE_SOUND - if (m_loaded) return false; - - alGetError(); // clear errors from previously - - alGenBuffers(1, &m_buffer); - if (!SFXManager::checkError("generating a buffer")) + if (UserConfigParams::m_enable_sound) { - return false; - } - - assert( alIsBuffer(m_buffer) ); - - if (!loadVorbisBuffer(m_file, m_buffer)) - { - Log::error("SFXBuffer", "Could not load sound effect %s", - m_file.c_str()); - // TODO: free al buffer here? - return false; + if (m_loaded) return false; + + alGetError(); // clear errors from previously + + alGenBuffers(1, &m_buffer); + if (!SFXManager::checkError("generating a buffer")) + { + return false; + } + + assert(alIsBuffer(m_buffer)); + + if (!loadVorbisBuffer(m_file, m_buffer)) + { + Log::error("SFXBuffer", "Could not load sound effect %s", + m_file.c_str()); + // TODO: free al buffer here? + return false; + } } #endif @@ -131,10 +134,13 @@ bool SFXBuffer::load() void SFXBuffer::unload() { #ifdef ENABLE_SOUND - if (m_loaded) + if (UserConfigParams::m_enable_sound) { - alDeleteBuffers(1, &m_buffer); - m_buffer = 0; + if (m_loaded) + { + alDeleteBuffers(1, &m_buffer); + m_buffer = 0; + } } #endif m_loaded = false; @@ -148,6 +154,9 @@ void SFXBuffer::unload() bool SFXBuffer::loadVorbisBuffer(const std::string &name, ALuint buffer) { #ifdef ENABLE_SOUND + if (!UserConfigParams::m_enable_sound) + return false; + const int ogg_endianness = (IS_LITTLE_ENDIAN ? 0 : 1); diff --git a/src/audio/sfx_manager.cpp b/src/audio/sfx_manager.cpp index 4176a81c9..8847ae0f0 100644 --- a/src/audio/sfx_manager.cpp +++ b/src/audio/sfx_manager.cpp @@ -98,32 +98,35 @@ SFXManager::SFXManager() loadSfx(); #ifdef ENABLE_SOUND - pthread_cond_init(&m_cond_request, NULL); - - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - - m_thread_id.setAtomic(new pthread_t()); - // The thread is created even if there atm sfx are disabled - // (since the user might enable it later). - int error = pthread_create(m_thread_id.getData(), &attr, - &SFXManager::mainLoop, this); - if (error) + if (UserConfigParams::m_enable_sound) { - m_thread_id.lock(); - delete m_thread_id.getData(); - m_thread_id.unlock(); - m_thread_id.setAtomic(0); - Log::error("SFXManager", "Could not create thread, error=%d.", - errno); + pthread_cond_init(&m_cond_request, NULL); + + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + + m_thread_id.setAtomic(new pthread_t()); + // The thread is created even if there atm sfx are disabled + // (since the user might enable it later). + int error = pthread_create(m_thread_id.getData(), &attr, + &SFXManager::mainLoop, this); + if (error) + { + m_thread_id.lock(); + delete m_thread_id.getData(); + m_thread_id.unlock(); + m_thread_id.setAtomic(0); + Log::error("SFXManager", "Could not create thread, error=%d.", + errno); + } + pthread_attr_destroy(&attr); + + setMasterSFXVolume( UserConfigParams::m_sfx_volume ); + m_sfx_commands.lock(); + m_sfx_commands.getData().clear(); + m_sfx_commands.unlock(); } - pthread_attr_destroy(&attr); - - setMasterSFXVolume( UserConfigParams::m_sfx_volume ); - m_sfx_commands.lock(); - m_sfx_commands.getData().clear(); - m_sfx_commands.unlock(); #endif } // SoundManager @@ -133,11 +136,14 @@ SFXManager::SFXManager() SFXManager::~SFXManager() { #ifdef ENABLE_SOUND - m_thread_id.lock(); - pthread_join(*m_thread_id.getData(), NULL); - delete m_thread_id.getData(); - m_thread_id.unlock(); - pthread_cond_destroy(&m_cond_request); + if (UserConfigParams::m_enable_sound) + { + m_thread_id.lock(); + pthread_join(*m_thread_id.getData(), NULL); + delete m_thread_id.getData(); + m_thread_id.unlock(); + pthread_cond_destroy(&m_cond_request); + } #endif // ---- clear m_all_sfx @@ -189,6 +195,9 @@ SFXManager::~SFXManager() void SFXManager::queue(SFXCommands command, SFXBase *sfx) { #ifdef ENABLE_SOUND + if (!UserConfigParams::m_enable_sound) + return; + SFXCommand *sfx_command = new SFXCommand(command, sfx); queueCommand(sfx_command); #endif @@ -205,6 +214,9 @@ void SFXManager::queue(SFXCommands command, SFXBase *sfx) void SFXManager::queue(SFXCommands command, SFXBase *sfx, float f) { #ifdef ENABLE_SOUND + if (!UserConfigParams::m_enable_sound) + return; + SFXCommand *sfx_command = new SFXCommand(command, sfx, f); queueCommand(sfx_command); #endif @@ -221,8 +233,11 @@ void SFXManager::queue(SFXCommands command, SFXBase *sfx, float f) void SFXManager::queue(SFXCommands command, SFXBase *sfx, const Vec3 &p) { #ifdef ENABLE_SOUND - SFXCommand *sfx_command = new SFXCommand(command, sfx, p); - queueCommand(sfx_command); + if (!UserConfigParams::m_enable_sound) + return; + + SFXCommand *sfx_command = new SFXCommand(command, sfx, p); + queueCommand(sfx_command); #endif } // queue (Vec3) @@ -231,6 +246,9 @@ void SFXManager::queue(SFXCommands command, SFXBase *sfx, const Vec3 &p) void SFXManager::queue(SFXCommands command, SFXBase *sfx, const Vec3 &p, SFXBuffer* buffer) { #ifdef ENABLE_SOUND + if (!UserConfigParams::m_enable_sound) + return; + SFXCommand *sfx_command = new SFXCommand(command, sfx, p); sfx_command->m_buffer = buffer; queueCommand(sfx_command); @@ -250,6 +268,9 @@ void SFXManager::queue(SFXCommands command, SFXBase *sfx, float f, const Vec3 &p) { #ifdef ENABLE_SOUND + if (!UserConfigParams::m_enable_sound) + return; + SFXCommand *sfx_command = new SFXCommand(command, sfx, f, p); queueCommand(sfx_command); #endif @@ -262,6 +283,9 @@ void SFXManager::queue(SFXCommands command, SFXBase *sfx, float f, void SFXManager::queue(SFXCommands command, MusicInformation *mi) { #ifdef ENABLE_SOUND + if (!UserConfigParams::m_enable_sound) + return; + SFXCommand *sfx_command = new SFXCommand(command, mi); queueCommand(sfx_command); #endif @@ -275,6 +299,9 @@ void SFXManager::queue(SFXCommands command, MusicInformation *mi) void SFXManager::queue(SFXCommands command, MusicInformation *mi, float f) { #ifdef ENABLE_SOUND + if (!UserConfigParams::m_enable_sound) + return; + SFXCommand *sfx_command = new SFXCommand(command, mi, f); queueCommand(sfx_command); #endif @@ -288,6 +315,9 @@ void SFXManager::queue(SFXCommands command, MusicInformation *mi, float f) void SFXManager::queueCommand(SFXCommand *command) { #ifdef ENABLE_SOUND + if (!UserConfigParams::m_enable_sound) + return; + m_sfx_commands.lock(); if(World::getWorld() && m_sfx_commands.getData().size() > 20*race_manager->getNumberOfKarts()+20 && @@ -321,12 +351,17 @@ void SFXManager::queueCommand(SFXCommand *command) void SFXManager::stopThread() { #ifdef ENABLE_SOUND - queue(SFX_EXIT); - // Make sure the thread wakes up. - pthread_cond_signal(&m_cond_request); -#else - setCanBeDeleted(); + if (UserConfigParams::m_enable_sound) + { + queue(SFX_EXIT); + // Make sure the thread wakes up. + pthread_cond_signal(&m_cond_request); + } + else #endif + { + setCanBeDeleted(); + } } // stopThread //---------------------------------------------------------------------------- @@ -338,6 +373,9 @@ void SFXManager::stopThread() void* SFXManager::mainLoop(void *obj) { #ifdef ENABLE_SOUND + if (!UserConfigParams::m_enable_sound) + return NULL; + VS::setThreadName("SFXManager"); SFXManager *me = (SFXManager*)obj; @@ -675,13 +713,23 @@ SFXBase* SFXManager::createSoundSource(SFXBuffer* buffer, } #ifdef ENABLE_SOUND - //assert( alIsBuffer(buffer->getBufferID()) ); crashes on server - SFXBase* sfx = new SFXOpenAL(buffer, positional, buffer->getGain(), owns_buffer); -#else - SFXBase* sfx = new DummySFX(buffer, positional, buffer->getGain()); - if (owns_buffer) - delete buffer; + SFXBase* sfx = NULL; + + if (UserConfigParams::m_enable_sound) + { + //assert( alIsBuffer(buffer->getBufferID()) ); crashes on server + sfx = new SFXOpenAL(buffer, positional, buffer->getGain(), owns_buffer); + } + else #endif + { + sfx = new DummySFX(buffer, positional, buffer->getGain()); + + if (owns_buffer) + { + delete buffer; + } + } sfx->setMasterVolume(m_master_gain); @@ -766,6 +814,9 @@ void SFXManager::deleteSFXMapping(const std::string &name) void SFXManager::update() { #ifdef ENABLE_SOUND + if (!UserConfigParams::m_enable_sound) + return; + queue(SFX_UPDATE, (SFXBase*)NULL); // Wake up the sfx thread to handle all queued up audio commands. pthread_cond_signal(&m_cond_request); @@ -780,6 +831,9 @@ void SFXManager::update() void SFXManager::reallyUpdateNow(SFXCommand *current) { #ifdef ENABLE_SOUND + if (!UserConfigParams::m_enable_sound) + return; + if (m_last_update_time < 0.0) { // first time @@ -906,10 +960,13 @@ void SFXManager::reallyResumeAllNow() bool SFXManager::checkError(const std::string &context) { #ifdef ENABLE_SOUND + if (!UserConfigParams::m_enable_sound) + return true; + // Check (and clear) the error flag int error = alGetError(); - if(error != AL_NO_ERROR) + if (error != AL_NO_ERROR) { Log::error("SFXManager", "SFXOpenAL OpenAL error while %s: %s", context.c_str(), SFXManager::getErrorString(error).c_str()); @@ -958,6 +1015,9 @@ void SFXManager::setMasterSFXVolume(float gain) const std::string SFXManager::getErrorString(int err) { #ifdef ENABLE_SOUND + if (!UserConfigParams::m_enable_sound) + return std::string("sound disabled"); + switch(err) { case AL_NO_ERROR: return std::string("AL_NO_ERROR" ); @@ -968,9 +1028,9 @@ const std::string SFXManager::getErrorString(int err) case AL_OUT_OF_MEMORY: return std::string("AL_OUT_OF_MEMORY" ); default: return std::string("UNKNOWN"); }; -#else - return std::string("sound disabled"); #endif + + return std::string("sound disabled"); } // getErrorString //----------------------------------------------------------------------------- diff --git a/src/config/user_config.hpp b/src/config/user_config.hpp index b586745bf..d1894d1fa 100644 --- a/src/config/user_config.hpp +++ b/src/config/user_config.hpp @@ -672,6 +672,8 @@ namespace UserConfigParams PARAM_PREFIX bool m_race_now PARAM_DEFAULT( false ); PARAM_PREFIX bool m_enforce_current_player PARAM_DEFAULT( false ); + + PARAM_PREFIX bool m_enable_sound PARAM_DEFAULT( true ); /** True to test funky ambient/diffuse/specularity in RGB & * all anisotropic */ diff --git a/src/main.cpp b/src/main.cpp index 1f177d3a8..c34f29d34 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -901,7 +901,9 @@ int handleCmdLinePreliminary() if(CommandLine::has("--dont-load-navmesh")) Track::m_dont_load_navmesh = true; - + if (CommandLine::has("--no-sound")) + UserConfigParams::m_enable_sound = false; + return 0; } // handleCmdLinePreliminary