From 58f0ce412e799a50e25878f6134cb7d60427c61e Mon Sep 17 00:00:00 2001 From: hiker Date: Tue, 10 Feb 2015 08:32:05 +1100 Subject: [PATCH] Removed unnecessary m_adjusted_gain variable and resetTempVolume function. Fixed that music volume in race could not be changed. --- src/audio/music.hpp | 2 +- src/audio/music_dummy.hpp | 2 +- src/audio/music_information.cpp | 26 ++++++++++++-------------- src/audio/music_information.hpp | 8 ++------ src/audio/music_manager.cpp | 6 +++--- src/audio/music_ogg.cpp | 11 ++++++----- src/audio/music_ogg.hpp | 2 +- src/audio/sfx_manager.cpp | 18 +++++++----------- src/audio/sfx_manager.hpp | 3 +-- src/audio/sfx_openal.cpp | 30 +++++++++++++++--------------- src/audio/sfx_openal.hpp | 10 +++++----- 11 files changed, 54 insertions(+), 64 deletions(-) diff --git a/src/audio/music.hpp b/src/audio/music.hpp index 824f4d3de..1389bc853 100644 --- a/src/audio/music.hpp +++ b/src/audio/music.hpp @@ -34,7 +34,7 @@ public: virtual bool stopMusic () = 0; virtual bool pauseMusic () = 0; virtual bool resumeMusic () = 0; - virtual void volumeMusic (float gain) = 0; + virtual void setVolume (float volume) = 0; virtual void updateFading(float percent) = 0; virtual void updateFaster(float percent, float pitch) = 0; virtual void update () = 0; diff --git a/src/audio/music_dummy.hpp b/src/audio/music_dummy.hpp index 977c4a5b7..ef9e4a700 100644 --- a/src/audio/music_dummy.hpp +++ b/src/audio/music_dummy.hpp @@ -34,7 +34,7 @@ public: virtual bool stopMusic () { return true; } virtual bool pauseMusic () { return true; } virtual bool resumeMusic () { return true; } - virtual void volumeMusic (float gain) {} + virtual void setVolume (float volume) {} virtual void updateFading(float percent) {} virtual void updateFaster(float percent, float pitch) {} virtual void update () {} diff --git a/src/audio/music_information.cpp b/src/audio/music_information.cpp index 7f07c2b38..fde25fc8f 100644 --- a/src/audio/music_information.cpp +++ b/src/audio/music_information.cpp @@ -85,7 +85,6 @@ MusicInformation::MusicInformation(const XMLNode *root, m_faster_time = 1.0f; m_max_pitch = 0.1f; m_gain = 1.0f; - m_adjusted_gain = 1.0f; // Otherwise read config file @@ -101,8 +100,6 @@ MusicInformation::MusicInformation(const XMLNode *root, root->get("fast", &m_enable_fast ); root->get("fast-filename", &m_fast_filename ); - m_adjusted_gain = m_gain; - // Get the path from the filename and add it to the ogg filename std::string path = StringUtils::getPath(filename); m_normal_filename = path + "/" + m_normal_filename; @@ -172,7 +169,7 @@ void MusicInformation::startMusic() m_normal_filename.c_str()); return; } - m_normal_music->volumeMusic(m_adjusted_gain); + m_normal_music->setVolume(m_gain); m_normal_music->playMusic(); // Then (if available) load the music for the last track @@ -206,7 +203,7 @@ void MusicInformation::startMusic() "supported or not found.\n", m_fast_filename.c_str()); return; } - m_fast_music->volumeMusic(m_adjusted_gain); + m_fast_music->setVolume(m_gain); } // startMusic //----------------------------------------------------------------------------- @@ -298,21 +295,22 @@ void MusicInformation::resumeMusic() } // resumeMusic //----------------------------------------------------------------------------- -void MusicInformation::volumeMusic(float gain) +void MusicInformation::setDefaultVolume() { - m_adjusted_gain = m_gain * gain; if (m_normal_music && m_normal_music->isPlaying()) - m_normal_music->volumeMusic(m_adjusted_gain); + m_normal_music->setVolume(m_gain); if (m_fast_music && m_fast_music->isPlaying()) - m_fast_music->volumeMusic(m_adjusted_gain); -} // volumeMusic + m_fast_music->setVolume(m_gain); +} // setVolume //----------------------------------------------------------------------------- - -void MusicInformation::setTemporaryVolume(float gain) +/** Overwrites the current volume with a temporary value (used e.g. to fade + * from normal music to last lap music). + */ +void MusicInformation::setTemporaryVolume(float volume) { - if (m_normal_music != NULL) m_normal_music->volumeMusic(gain); - if (m_fast_music != NULL) m_fast_music->volumeMusic(gain); + if (m_normal_music != NULL) m_normal_music->setVolume(volume); + if (m_fast_music != NULL) m_fast_music->setVolume(volume); } //----------------------------------------------------------------------------- diff --git a/src/audio/music_information.hpp b/src/audio/music_information.hpp index 21b622d30..7203451bc 100644 --- a/src/audio/music_information.hpp +++ b/src/audio/music_information.hpp @@ -57,7 +57,6 @@ private: bool m_enable_fast; float m_gain; - float m_adjusted_gain; /** Either time for fading faster music in, or time to change pitch. */ float m_faster_time; @@ -87,12 +86,9 @@ private: void stopMusic(); void pauseMusic(); void resumeMusic(); - void volumeMusic(float gain); + void setDefaultVolume(); void switchToFastMusic(); - void setTemporaryVolume(float gain); - // ------------------------------------------------------------------------ - /** Resets a temporary volume change. */ - void resetTemporaryVolume() { volumeMusic(m_adjusted_gain); } + void setTemporaryVolume(float volume); // ------------------------------------------------------------------------ /** Sets the music to be waiting, i.e. startMusic still needs to be * called. Used to pre-load track music during track loading time. */ diff --git a/src/audio/music_manager.cpp b/src/audio/music_manager.cpp index 12ddb70d2..46fbeaa14 100644 --- a/src/audio/music_manager.cpp +++ b/src/audio/music_manager.cpp @@ -252,7 +252,7 @@ void MusicManager::setTemporaryVolume(float gain) void MusicManager::resetTemporaryVolume() { if (m_current_music) - SFXManager::get()->queue(SFXManager::SFX_MUSIC_RESET_TMP_VOLUME, + SFXManager::get()->queue(SFXManager::SFX_MUSIC_DEFAULT_VOLUME, m_current_music); } // resetTemporaryVolume @@ -271,7 +271,7 @@ void MusicManager::setMasterMusicVolume(float gain) if (m_current_music) { // Sets the music volume to m_master_gain - SFXManager::get()->queue(SFXManager::SFX_MUSIC_VOLUME, + SFXManager::get()->queue(SFXManager::SFX_MUSIC_DEFAULT_VOLUME, m_current_music); } @@ -296,7 +296,7 @@ MusicInformation* MusicManager::getMusicInformation(const std::string& filename) MusicInformation *mi = MusicInformation::create(filename); if(mi) { - SFXManager::get()->queue(SFXManager::SFX_MUSIC_VOLUME, mi); + SFXManager::get()->queue(SFXManager::SFX_MUSIC_DEFAULT_VOLUME, mi); m_all_music[basename] = mi; } return mi; diff --git a/src/audio/music_ogg.cpp b/src/audio/music_ogg.cpp index 68678a6cf..e49a293b9 100644 --- a/src/audio/music_ogg.cpp +++ b/src/audio/music_ogg.cpp @@ -247,14 +247,15 @@ bool MusicOggStream::resumeMusic() } // resumeMusic //----------------------------------------------------------------------------- -void MusicOggStream::volumeMusic(float gain) +void MusicOggStream::setVolume(float volume) { - if (gain > 1.0f) gain = 1.0f; - if (gain < 0.0f) gain = 0.0f; + volume *= music_manager->getMasterMusicVolume(); + if (volume > 1.0f) volume = 1.0f; + if (volume < 0.0f) volume = 0.0f; - alSourcef(m_soundSource, AL_GAIN, gain); + alSourcef(m_soundSource, AL_GAIN, volume); check("volume music"); // clear errors -} // volumeMusic +} // setVolume //----------------------------------------------------------------------------- void MusicOggStream::updateFading(float percent) diff --git a/src/audio/music_ogg.hpp b/src/audio/music_ogg.hpp index 96ed7b62d..c8f2ec63e 100644 --- a/src/audio/music_ogg.hpp +++ b/src/audio/music_ogg.hpp @@ -60,7 +60,7 @@ public: virtual bool stopMusic(); virtual bool pauseMusic(); virtual bool resumeMusic(); - virtual void volumeMusic (float gain); + virtual void setVolume(float volume); virtual bool isPlaying(); protected: diff --git a/src/audio/sfx_manager.cpp b/src/audio/sfx_manager.cpp index edb5fd05b..5f9910e54 100644 --- a/src/audio/sfx_manager.cpp +++ b/src/audio/sfx_manager.cpp @@ -326,8 +326,7 @@ void* SFXManager::mainLoop(void *obj) 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->setDefaultVolume(); current->m_music_information->startMusic(); break; } case SFX_MUSIC_STOP: @@ -335,7 +334,10 @@ void* SFXManager::mainLoop(void *obj) case SFX_MUSIC_PAUSE: current->m_music_information->pauseMusic(); break; case SFX_MUSIC_RESUME: - current->m_music_information->resumeMusic(); break; + current->m_music_information->resumeMusic(); + // This might be necessasary if the volume was changed + // in the in-game menu + current->m_music_information->setDefaultVolume(); break; case SFX_MUSIC_SWITCH_FAST: current->m_music_information->switchToFastMusic(); break; case SFX_MUSIC_SET_TMP_VOLUME: @@ -343,17 +345,11 @@ void* SFXManager::mainLoop(void *obj) MusicInformation *mi = current->m_music_information; mi->setTemporaryVolume(current->m_parameter.getX()); break; } - case SFX_MUSIC_RESET_TMP_VOLUME: - { - MusicInformation *mi = current->m_music_information; - mi->resetTemporaryVolume(); break; - } case SFX_MUSIC_WAITING: current->m_music_information->setMusicWaiting(); break; - case SFX_MUSIC_VOLUME: + case SFX_MUSIC_DEFAULT_VOLUME: { - float gain = music_manager->getMasterMusicVolume(); - current->m_music_information->volumeMusic(gain); + current->m_music_information->setDefaultVolume(); } default: assert("Not yet supported."); } diff --git a/src/audio/sfx_manager.hpp b/src/audio/sfx_manager.hpp index 5f99a3aeb..3bd8788df 100644 --- a/src/audio/sfx_manager.hpp +++ b/src/audio/sfx_manager.hpp @@ -83,9 +83,8 @@ public: SFX_MUSIC_RESUME, SFX_MUSIC_SWITCH_FAST, SFX_MUSIC_SET_TMP_VOLUME, - SFX_MUSIC_RESET_TMP_VOLUME, SFX_MUSIC_WAITING, - SFX_MUSIC_VOLUME, + SFX_MUSIC_DEFAULT_VOLUME, SFX_EXIT, }; // SFXCommands diff --git a/src/audio/sfx_openal.cpp b/src/audio/sfx_openal.cpp index 8f86afb9d..1207f51b2 100644 --- a/src/audio/sfx_openal.cpp +++ b/src/audio/sfx_openal.cpp @@ -38,7 +38,7 @@ #include #include -SFXOpenAL::SFXOpenAL(SFXBuffer* buffer, bool positional, float gain, +SFXOpenAL::SFXOpenAL(SFXBuffer* buffer, bool positional, float volume, bool owns_buffer) : SFXBase() { @@ -46,7 +46,7 @@ SFXOpenAL::SFXOpenAL(SFXBuffer* buffer, bool positional, float gain, m_sound_source = 0; m_status = SFX_NOT_INITIALISED; m_positional = positional; - m_default_gain = gain; + m_default_gain = volume; m_loop = false; m_gain = -1.0f; m_master_gain = 1.0f; @@ -179,22 +179,22 @@ void SFXOpenAL::reallySetSpeed(float factor) //----------------------------------------------------------------------------- /** Changes the volume of a sound effect. - * \param gain Volume adjustment between 0.0 (mute) and 1.0 (full volume). + * \param volume Volume adjustment between 0.0 (mute) and 1.0 (full volume). */ -void SFXOpenAL::setVolume(float gain) +void SFXOpenAL::setVolume(float volume) { if(m_status==SFX_UNKNOWN || !SFXManager::get()->sfxAllowed()) return; - assert(!isnan(gain)) ; - SFXManager::get()->queue(SFXManager::SFX_VOLUME, this, gain); + assert(!isnan(volume)) ; + SFXManager::get()->queue(SFXManager::SFX_VOLUME, this, volume); } // setVolume //----------------------------------------------------------------------------- /** Changes the volume of a sound effect. - * \param gain Volume adjustment between 0.0 (mute) and 1.0 (full volume). + * \param volume Volume adjustment between 0.0 (mute) and 1.0 (full volume). */ -void SFXOpenAL::reallySetVolume(float gain) +void SFXOpenAL::reallySetVolume(float volume) { - m_gain = m_default_gain * gain; + m_gain = m_default_gain * volume; if(m_status==SFX_UNKNOWN) return; @@ -210,23 +210,23 @@ void SFXOpenAL::reallySetVolume(float gain) //----------------------------------------------------------------------------- /** Schedules setting of the master volume. - * \param gain Gain value. + * \param volume Volume value. */ -void SFXOpenAL::setMasterVolume(float gain) +void SFXOpenAL::setMasterVolume(float volume) { // This needs to be called even if sfx are disabled atm, so only exit // in case that the sfx could not be loaded in the first place. if(m_status==SFX_UNKNOWN) return; - SFXManager::get()->queue(SFXManager::SFX_MASTER_VOLUME, this, gain); + SFXManager::get()->queue(SFXManager::SFX_MASTER_VOLUME, this, volume); } // setMasterVolume //----------------------------------------------------------------------------- /** Sets the master volume. - * \param gain Master volume. + * \param volume Master volume. */ -void SFXOpenAL::reallySetMasterVolumeNow(float gain) +void SFXOpenAL::reallySetMasterVolumeNow(float volume) { - m_master_gain = gain; + m_master_gain = volume; if(m_status==SFX_UNKNOWN || m_status == SFX_NOT_INITIALISED) return; diff --git a/src/audio/sfx_openal.hpp b/src/audio/sfx_openal.hpp index 87a3c7d3b..1642970b5 100644 --- a/src/audio/sfx_openal.hpp +++ b/src/audio/sfx_openal.hpp @@ -76,7 +76,7 @@ private: float m_play_time; public: - SFXOpenAL(SFXBuffer* buffer, bool positional, float gain, + SFXOpenAL(SFXBuffer* buffer, bool positional, float volume, bool owns_buffer = false); virtual ~SFXOpenAL(); @@ -97,10 +97,10 @@ public: virtual void reallySetSpeed(float factor); virtual void setPosition(const Vec3 &position); virtual void reallySetPosition(const Vec3 &p); - virtual void setVolume(float gain); - virtual void reallySetVolume(float gain); - virtual void setMasterVolume(float gain); - virtual void reallySetMasterVolumeNow(float gain); + virtual void setVolume(float volume); + virtual void reallySetVolume(float volume); + virtual void setMasterVolume(float volume); + virtual void reallySetMasterVolumeNow(float volue); virtual void onSoundEnabledBack(); virtual void setRolloff(float rolloff); // ------------------------------------------------------------------------