add separately controllable fade

This commit is contained in:
Glenn Maynard
2003-02-10 21:43:50 +00:00
parent f6ebc8a30a
commit 76e1f7b8ba
4 changed files with 22 additions and 10 deletions
+13 -7
View File
@@ -76,6 +76,7 @@ RageSound::RageSound()
m_StartSample = 0;
m_LengthSamples = -1;
AccurateSync = false;
fade_length = 0;
/* Register ourselves, so we receive Update()s. */
SOUNDMAN->all_sounds.insert(this);
}
@@ -109,6 +110,7 @@ RageSound::RageSound(const RageSound &cpy)
position = cpy.position;
playing = false;
AccurateSync = cpy.AccurateSync;
fade_length = cpy.fade_length;
speed_input_samples = cpy.speed_input_samples;
speed_output_samples = cpy.speed_output_samples;
@@ -530,22 +532,18 @@ int RageSound::GetPCM(char *buffer, int size, int sampleno)
/* Save this sampleno/position map. */
pos_map.push_back(pos_map_t(sampleno, position, got_samples));
const float FADE_TIME = 1.5f;
/* XXX: Loop shouldn't set fading; add a Fade_Time member?
*
* We want to fade when there's FADE_TIME seconds left, but if
/* We want to fade when there's FADE_TIME seconds left, but if
* m_LengthSamples is -1, we don't know the length we're playing.
* (m_LengthSamples is the length to play, not the length of the
* source.) If we don't know the length, don't fade. */
if(StopMode == M_LOOP && m_LengthSamples != -1) {
if(fade_length != 0 && m_LengthSamples != -1) {
Sint16 *p = (Sint16 *) buffer;
int this_position = position;
for(int samp = 0; samp < got_samples; ++samp)
{
float fSecsUntilSilent = float(m_StartSample + m_LengthSamples - this_position) / samplerate;
float fVolPercent = fSecsUntilSilent / FADE_TIME;
float fVolPercent = fSecsUntilSilent / fade_length;
fVolPercent = clamp(fVolPercent, 0.f, 1.f);
for(int i = 0; i < channels; ++i) {
@@ -582,6 +580,9 @@ void RageSound::StartPlaying()
void RageSound::StopPlaying()
{
if(!playing)
return;
stopped_position = GetPositionSeconds();
/* Tell the sound manager to stop mixing this sound. */
@@ -779,6 +780,11 @@ void RageSound::SetPlaybackRate( float NewSpeed )
}
}
void RageSound::SetFadeLength( float fSeconds )
{
fade_length = fSeconds;
}
void CircBuf::reserve(unsigned n)
{
clear();
+5
View File
@@ -77,6 +77,8 @@ public:
bool SetPositionSeconds( float fSeconds = -1);
void SetAccurateSync(bool yes=true) { AccurateSync = yes; }
void SetPlaybackRate( float fScale );
void SetFadeLength( float fSeconds );
void SetNoFade() { SetFadeLength(0); }
float GetPlaybackRate() const { return float(speed_input_samples) / speed_output_samples; }
bool IsPlaying() const { return playing; }
CString GetLoadedFilePath() const { return m_sFilePath; }
@@ -128,6 +130,9 @@ private:
* becomes positive. This is recorded in samples, to avoid rounding error. */
int position;
/* Amount of time to fade out at the end. */
float fade_length;
/* Hack: When we stop a playing sound, we can't ask the driver the position
* (we're not playing); and we can't seek back to the current playing position
* when we stop (too slow), but we want to be able to report the position we
+3 -2
View File
@@ -242,7 +242,7 @@ void RageSoundManager::MixAudio(Sint16 *dst, const Sint16 *src, Uint32 len, floa
}
}
void RageSoundManager::PlayMusic(CString file, bool loop, float start_sec, float length_sec)
void RageSoundManager::PlayMusic(CString file, bool loop, float start_sec, float length_sec, float fade_len)
{
// LOG->Trace("play '%s' (current '%s')", file.GetString(), music->GetLoadedFilePath().GetString());
if(music->IsPlaying())
@@ -270,7 +270,8 @@ void RageSoundManager::PlayMusic(CString file, bool loop, float start_sec, float
music->SetLengthSeconds();
else
music->SetLengthSeconds(length_sec);
music->SetFadeLength(fade_len);
music->SetPositionSeconds();
music->StartPlaying();
}
+1 -1
View File
@@ -59,7 +59,7 @@ public:
* itself to this. */
set<RageSound *> all_sounds;
void PlayMusic(CString file, bool loop = true, float start_sec = -1, float length_sec = -1);
void PlayMusic(CString file, bool loop = true, float start_sec = -1, float length_sec = -1, float fade_len = 0);
void StopMusic() { PlayMusic(""); }
static void MixAudio(Sint16 *dst, const Sint16 *src, Uint32 len, float volume);
};