Skip to content

Commit

Permalink
Improvements in the last lap music transition
Browse files Browse the repository at this point in the history
- Group up the code to trigger the fast music and the last lap SFX in the same area
- Remove all the code meant to play a music at higher pitch, it was unused and would have been annoying if it was.
- Apply the 'last lap SFX' reduced music volume when switching between a normal music and a fast music too
- Increase the switch duration between normal and fast music from 1.0s to 1.5s
  • Loading branch information
Alayan-stk-2 committed Dec 22, 2024
1 parent 9f4a190 commit 4a479ed
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 57 deletions.
1 change: 0 additions & 1 deletion src/audio/music.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class Music : public NoCopy
virtual bool pauseMusic () = 0;
virtual bool resumeMusic () = 0;
virtual void setVolume (float volume) = 0;
virtual void updateFaster(float percent, float pitch) = 0;
virtual void update () = 0;
virtual bool isPlaying () = 0;

Expand Down
40 changes: 12 additions & 28 deletions src/audio/music_information.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ MusicInformation::MusicInformation(const XMLNode *root,
m_fast_loop_end = -1.0f;
m_enable_fast = false;
m_music_waiting = false;
m_faster_time = 1.0f;
m_max_pitch = 0.1f;
m_faster_time = 1.5f;
m_gain = 1.0f;


Expand All @@ -107,6 +106,8 @@ MusicInformation::MusicInformation(const XMLNode *root,
root->get("fast-loop-start", &m_fast_loop_start );
root->get("fast-loop-end", &m_fast_loop_end );

m_temporary_volume = 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;
Expand Down Expand Up @@ -273,31 +274,15 @@ void MusicInformation::update(float dt)
{
m_mode=SOUND_FAST;
m_normal_music->stopMusic();
m_fast_music->setVolume(m_temporary_volume);
m_fast_music->update();
return;
}
float fraction=m_time_since_faster/m_faster_time;
m_normal_music->setVolume(1-fraction);
m_fast_music->setVolume(fraction);
m_normal_music->setVolume(m_temporary_volume * (1-fraction));
m_fast_music->setVolume(m_temporary_volume * fraction);
m_normal_music->update();
m_fast_music->update();
break;
}
case SOUND_FASTER: {
if ( m_normal_music == NULL ) break;

m_time_since_faster +=dt;
if(m_time_since_faster>=m_faster_time)
{
// Once the pitch is adjusted, just switch back to normal
// mode. We can't switch to fast music mode, since this would
// play m_fast_music, which isn't available.
m_mode=SOUND_NORMAL;
return;
}
float fraction=m_time_since_faster/m_faster_time;
m_normal_music->updateFaster(fraction, m_max_pitch);

break;
}
case SOUND_NORMAL:
Expand Down Expand Up @@ -354,6 +339,7 @@ void MusicInformation::resumeMusic()
//-----------------------------------------------------------------------------
void MusicInformation::setDefaultVolume()
{
m_temporary_volume = m_gain;
if (m_normal_music && m_normal_music->isPlaying())
m_normal_music->setVolume(m_gain);
if (m_fast_music && m_fast_music->isPlaying())
Expand All @@ -366,11 +352,14 @@ void MusicInformation::setDefaultVolume()
*/
void MusicInformation::setTemporaryVolume(float volume)
{
if (m_normal_music != NULL) m_normal_music->setVolume(volume);
if (m_fast_music != NULL) m_fast_music->setVolume(volume);
m_temporary_volume = m_gain * volume;
if (m_normal_music != NULL) m_normal_music->setVolume(m_temporary_volume);
if (m_fast_music != NULL) m_fast_music->setVolume(m_temporary_volume);
}

//-----------------------------------------------------------------------------
/** If there is a fast music available, switch to it.
* */
void MusicInformation::switchToFastMusic()
{
if(!m_enable_fast) return;
Expand All @@ -381,11 +370,6 @@ void MusicInformation::switchToFastMusic()
m_fast_music->playMusic();
m_fast_music->setVolume(0);
}
else
{
// FIXME: for now this music is too annoying,
m_mode = SOUND_FASTER;
}
} // switchToFastMusic

//-----------------------------------------------------------------------------
Expand Down
16 changes: 7 additions & 9 deletions src/audio/music_information.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ class MusicInformation : public NoCopy
* was told not to start right away). */
bool m_music_waiting;

/** If faster music is enabled at all (either separate file or using
* the pitch shift approach). */
/** If faster music is enabled at all. */
bool m_enable_fast;

float m_gain;
Expand All @@ -64,18 +63,19 @@ class MusicInformation : public NoCopy
float m_normal_loop_end;
float m_fast_loop_end;

/** Either time for fading faster music in, or time to change pitch. */
/** Time for fading faster music in. */
float m_faster_time;
/** Maximum pitch for faster music. */
float m_max_pitch;

/* Avoid the temporary volume being forgotten */
float m_temporary_volume;

static const int LOOP_FOREVER=-1;
mutable std::mutex m_music_mutex;
Music *m_normal_music,
*m_fast_music;
enum {SOUND_NORMAL, //!< normal music is played
SOUND_FADING, //!< normal music fading out, faster fading in
SOUND_FASTER, //!< change pitch of normal music
SOUND_FAST} //!< playing faster music or max pitch reached
SOUND_FAST} //!< playing faster music
m_mode;
float m_time_since_faster;

Expand Down Expand Up @@ -127,8 +127,6 @@ class MusicInformation : public NoCopy
// ------------------------------------------------------------------------
/** If available, returns the file name of the faster/last-lap music. */
const std::string& getFastFilename() const { return m_fast_filename; }
// ------------------------------------------------------------------------
float getMaxPitch() const { return m_max_pitch; }

}; // MusicInformation
#endif
7 changes: 0 additions & 7 deletions src/audio/music_ogg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,13 +255,6 @@ void MusicOggStream::setVolume(float volume)
check("volume music"); // clear errors
} // setVolume

//-----------------------------------------------------------------------------
void MusicOggStream::updateFaster(float percent, float max_pitch)
{
alSourcef(m_soundSource,AL_PITCH,1+max_pitch*percent);
update();
} // updateFaster

//-----------------------------------------------------------------------------
void MusicOggStream::update()
{
Expand Down
1 change: 0 additions & 1 deletion src/audio/music_ogg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class MusicOggStream : public Music
virtual ~MusicOggStream();

virtual void update();
virtual void updateFaster(float percent, float max_pitch);

virtual bool load(const std::string& filename);

Expand Down
19 changes: 8 additions & 11 deletions src/modes/linear_world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,14 @@ void LinearWorld::newLap(unsigned int kart_index)
m_last_lap_sfx_playing = false;
}
}
// Switch on faster music if not already done so, if the
// first kart is doing its last lap.
if(!m_faster_music_active &&
useFastMusicNearEnd())
{
music_manager->switchToFastMusic();
m_faster_music_active=true;
}
}
else if (raceHasLaps() && kart_info.m_finished_laps > 0 &&
kart_info.m_finished_laps+1 < lap_count && !isLiveJoinWorld() && m_race_gui)
Expand Down Expand Up @@ -955,17 +963,6 @@ void LinearWorld::updateRacePosition()
assert(false);
}
#endif

// Switch on faster music if not already done so, if the
// first kart is doing its last lap.
if(!m_faster_music_active &&
p == 1 &&
kart_info.m_finished_laps == RaceManager::get()->getNumLaps() - 1 &&
useFastMusicNearEnd() )
{
music_manager->switchToFastMusic();
m_faster_music_active=true;
}
} // for i<kart_amount

// Define this to get a detailled analyses each time a race position
Expand Down

0 comments on commit 4a479ed

Please sign in to comment.