Added support for master music volume.

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@2709 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
stevo14 2008-12-17 15:51:35 +00:00
parent 18848fc04c
commit f2f5a20b43
7 changed files with 54 additions and 16 deletions

View File

@ -25,12 +25,12 @@
class Music
{
public:
virtual bool load (const std::string& filename,
float gain ) = 0;
virtual bool load (const std::string& filename) = 0;
virtual bool playMusic () = 0;
virtual bool stopMusic () = 0;
virtual bool pauseMusic () = 0;
virtual bool resumeMusic () = 0;
virtual bool volumeMusic (float gain) = 0;
virtual void updateFading(float percent) = 0;
virtual void updateFaster(float percent, float pitch) = 0;
virtual void update () = 0;

View File

@ -46,6 +46,7 @@ MusicInformation::MusicInformation(const std::string& filename)
m_faster_time = 1.0f;
m_max_pitch = 0.1f;
m_gain = 1.0f;
m_adjustedGain = 1.0f;
if(StringUtils::extension(filename)!="music")
{
@ -86,6 +87,7 @@ MusicInformation::MusicInformation(const std::string& filename)
LISP->get ("max-pitch", m_max_pitch );
LISP->getVector("tracks", m_all_tracks );
LISP->get ("gain", m_gain );
m_adjustedGain = m_gain;
// Get the path from the filename and add it to the ogg filename
std::string path=StringUtils::path(filename);
@ -129,7 +131,7 @@ void MusicInformation::startMusic()
}
m_normal_music = new MusicOggStream();
if((m_normal_music->load(m_normal_filename, m_gain)) == false)
if((m_normal_music->load(m_normal_filename)) == false)
{
delete m_normal_music;
m_normal_music=0;
@ -137,6 +139,7 @@ void MusicInformation::startMusic()
m_normal_filename.c_str());
return;
}
m_normal_music->volumeMusic(m_adjustedGain);
m_normal_music->playMusic();
// Then (if available) load the music for the last track
@ -156,7 +159,7 @@ void MusicInformation::startMusic()
}
m_fast_music= new MusicOggStream();
if((m_fast_music->load(m_fast_filename, m_gain)) == false)
if((m_fast_music->load(m_fast_filename)) == false)
{
delete m_fast_music;
m_fast_music=0;
@ -164,6 +167,7 @@ void MusicInformation::startMusic()
m_fast_filename.c_str());
return;
}
m_fast_music->volumeMusic(m_adjustedGain);
} // startMusic
//-----------------------------------------------------------------------------
@ -246,6 +250,14 @@ void MusicInformation::resumeMusic()
if (m_fast_music != NULL) m_fast_music->resumeMusic();
} // resumeMusic
//-----------------------------------------------------------------------------
void MusicInformation::volumeMusic(float gain)
{
m_adjustedGain = m_gain * gain;
if (m_normal_music != NULL) m_normal_music->volumeMusic(m_adjustedGain);
if (m_fast_music != NULL) m_fast_music->volumeMusic(m_adjustedGain);
} // volumeMusic
//-----------------------------------------------------------------------------
void MusicInformation::switchToFastMusic()
{

View File

@ -35,6 +35,7 @@ private:
std::vector<std::string> m_all_tracks;
int m_numLoops;
float m_gain;
float m_adjustedGain;
float m_faster_time; // Either time for fading faster
// music in, or time to change pitch
float m_max_pitch; // maximum pitch for faster music
@ -63,6 +64,7 @@ public:
void stopMusic ();
void pauseMusic ();
void resumeMusic ();
void volumeMusic (float gain);
void switchToFastMusic();
}; // MusicInformation
#endif

View File

@ -49,7 +49,7 @@ MusicOggStream::~MusicOggStream()
} // ~MusicOggStream
//-----------------------------------------------------------------------------
bool MusicOggStream::load(const std::string& filename, float gain)
bool MusicOggStream::load(const std::string& filename)
{
m_error = true;
m_fileName = filename;
@ -94,7 +94,7 @@ bool MusicOggStream::load(const std::string& filename, float gain)
alSource3f(m_soundSource, AL_VELOCITY, 0.0, 0.0, 0.0);
alSource3f(m_soundSource, AL_DIRECTION, 0.0, 0.0, 0.0);
alSourcef (m_soundSource, AL_ROLLOFF_FACTOR, 0.0 );
alSourcef (m_soundSource, AL_GAIN, gain );
alSourcef (m_soundSource, AL_GAIN, 1.0 );
alSourcei (m_soundSource, AL_SOURCE_RELATIVE, AL_TRUE );
m_error=false;
@ -206,6 +206,12 @@ bool MusicOggStream::resumeMusic()
return true;
} // resumeMusic
//-----------------------------------------------------------------------------
bool MusicOggStream::volumeMusic(float gain)
{
alSourcef(m_soundSource, AL_GAIN, gain);
} // volumeMusic
//-----------------------------------------------------------------------------
void MusicOggStream::updateFading(float percent)
{

View File

@ -49,13 +49,13 @@ public:
virtual void updateFading(float percent);
virtual void updateFaster(float percent, float max_pitch);
virtual bool load(const std::string& filename, float gain);
virtual bool load(const std::string& filename);
virtual bool playMusic();
virtual bool stopMusic();
virtual bool pauseMusic();
virtual bool resumeMusic();
virtual bool volumeMusic (float gain);
protected:
bool empty();

View File

@ -47,6 +47,7 @@ SoundManager* sound_manager= NULL;
SoundManager::SoundManager()
{
m_current_music= NULL;
setMasterMusicVolume(0.7);
ALCdevice* device = alcOpenDevice ( NULL ); //The default sound device
if( device == NULL )
@ -145,6 +146,7 @@ void SoundManager::startMusic(MusicInformation* mi)
if(!mi || !user_config->doMusic() || !m_initialized) return;
mi->volumeMusic(m_masterGain);
mi->startMusic();
} // startMusic
@ -154,6 +156,18 @@ void SoundManager::stopMusic()
if(m_current_music) m_current_music->stopMusic();
} // stopMusic
//-----------------------------------------------------------------------------
void SoundManager::setMasterMusicVolume(float gain)
{
if(gain > 1.0)
gain = 1.0f;
if(gain < 0.0f)
gain = 0.0f;
m_masterGain = gain;
if(m_current_music) m_current_music->volumeMusic(m_masterGain);
}
//-----------------------------------------------------------------------------
MusicInformation* SoundManager::getMusicInformation(const std::string& filename)
{
@ -168,6 +182,7 @@ MusicInformation* SoundManager::getMusicInformation(const std::string& filename)
mi = new MusicInformation(filename);
m_allMusic[basename] = mi;
}
mi->volumeMusic(m_masterGain);
return mi;
} // SoundManager
@ -177,15 +192,15 @@ void SoundManager::positionListener(const Vec3 &position, const Vec3 &front)
if(!user_config->doSFX() || !m_initialized) return;
//forward vector
listenerVec[0] = front.getX();
listenerVec[1] = front.getY();
listenerVec[2] = front.getZ();
m_listenerVec[0] = front.getX();
m_listenerVec[1] = front.getY();
m_listenerVec[2] = front.getZ();
//up vector
listenerVec[3] = 0;
listenerVec[4] = 0;
listenerVec[5] = 1;
m_listenerVec[3] = 0;
m_listenerVec[4] = 0;
m_listenerVec[5] = 1;
alListener3f(AL_POSITION, position.getX(), position.getY(), position.getZ());
alListenerfv(AL_ORIENTATION, listenerVec);
alListenerfv(AL_ORIENTATION, m_listenerVec);
}

View File

@ -44,7 +44,9 @@ private:
m_allMusic;
void loadMusicInformation();
float listenerVec[6];
float m_listenerVec[6];
float m_masterGain;
public:
SoundManager();
virtual ~SoundManager();
@ -61,6 +63,7 @@ public:
m_current_music->resumeMusic(); }
void switchToFastMusic() {if(m_current_music)
m_current_music->switchToFastMusic();}
void setMasterMusicVolume(float gain);
MusicInformation *getCurrentMusic() {return m_current_music; }
MusicInformation *getMusicInformation(const std::string& filename);
void loadMusicFromOneDir(const std::string& dir);