Removed unnecessary m_adjusted_gain variable and resetTempVolume

function. Fixed that music volume in race could not be changed.
This commit is contained in:
hiker 2015-02-10 08:32:05 +11:00
parent 13bfee5ce1
commit 58f0ce412e
11 changed files with 54 additions and 64 deletions

View File

@ -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;

View File

@ -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 () {}

View File

@ -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);
}
//-----------------------------------------------------------------------------

View File

@ -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. */

View File

@ -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;

View File

@ -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)

View File

@ -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:

View File

@ -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.");
}

View File

@ -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

View File

@ -38,7 +38,7 @@
#include <stdio.h>
#include <string>
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;

View File

@ -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);
// ------------------------------------------------------------------------