From cf97705203ee47c56a00ea1d3526fbf36f823a86 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Mon, 19 Jan 2004 22:21:57 +0000 Subject: [PATCH] 64-bit frame counts remove DSoundBuf::Reset --- stepmania/src/arch/Sound/DSoundHelpers.cpp | 22 +++++++------------ stepmania/src/arch/Sound/DSoundHelpers.h | 9 ++++---- .../src/arch/Sound/RageSoundDriver_DSound.cpp | 19 ++++++++-------- .../src/arch/Sound/RageSoundDriver_DSound.h | 4 ++-- .../Sound/RageSoundDriver_DSound_Software.cpp | 22 ++++++++----------- .../Sound/RageSoundDriver_DSound_Software.h | 4 ++-- .../src/arch/Sound/RageSoundDriver_Null.cpp | 4 ++-- .../src/arch/Sound/RageSoundDriver_Null.h | 2 +- .../arch/Sound/RageSoundDriver_WaveOut.cpp | 14 ++++++------ .../src/arch/Sound/RageSoundDriver_WaveOut.h | 6 ++--- 10 files changed, 47 insertions(+), 59 deletions(-) diff --git a/stepmania/src/arch/Sound/DSoundHelpers.cpp b/stepmania/src/arch/Sound/DSoundHelpers.cpp index 65ce8a04a7..382c556c36 100644 --- a/stepmania/src/arch/Sound/DSoundHelpers.cpp +++ b/stepmania/src/arch/Sound/DSoundHelpers.cpp @@ -136,7 +136,8 @@ DSoundBuf::DSoundBuf(DSound &ds, DSoundBuf::hw hardware, samplebits = samplebits_; writeahead = writeahead_; buffer_locked = false; - last_cursor_pos = write_cursor = LastPosition = buffer_bytes_filled = 0; + last_cursor_pos = write_cursor = buffer_bytes_filled = 0; + LastPosition = 0; /* The size of the actual DSound buffer. This can be large; we generally * won't fill it completely. */ @@ -395,7 +396,6 @@ bool DSoundBuf::get_output_buf(char **buffer, unsigned *bufsiz, int chunksize) /* Increment last_cursor_pos to point at where the data we're about to * ask for will actually be played. */ last_cursor_pos += num_bytes_empty / bytes_per_frame(); - buffer_locked = true; return true; @@ -407,7 +407,7 @@ void DSoundBuf::release_output_buf(char *buffer, unsigned bufsiz) buffer_locked = false; } -int DSoundBuf::GetPosition() const +int64_t DSoundBuf::GetPosition() const { DWORD cursor, junk; buf->GetCurrentPosition(&cursor, &junk); @@ -415,10 +415,12 @@ int DSoundBuf::GetPosition() const int write_cursor_frames = write_cursor / bytes_per_frame(); int frames_behind = write_cursor_frames - cursor_frames; - if(frames_behind <= 0) + /* frames_behind will be 0 if we're called before the buffer starts playing: + * both write_cursor_frames and cursor_frames will be 0. */ + if( frames_behind < 0 ) frames_behind += buffersize_frames(); /* unwrap */ - int ret = last_cursor_pos - frames_behind; + int64_t ret = last_cursor_pos - frames_behind; /* Failsafe: never return a value smaller than we've already returned. * This can happen once in a while in underrun conditions. */ @@ -437,15 +439,7 @@ void DSoundBuf::Stop() { buf->Stop(); buf->SetCurrentPosition(0); - last_cursor_pos = LastPosition = write_cursor = buffer_bytes_filled = 0; -} - - -void DSoundBuf::Reset() -{ - /* Nothing is playing. Reset the sample count; this is just to - * prevent eventual overflow. */ - last_cursor_pos = LastPosition = 0; + last_cursor_pos = write_cursor = buffer_bytes_filled = 0; } /* diff --git a/stepmania/src/arch/Sound/DSoundHelpers.h b/stepmania/src/arch/Sound/DSoundHelpers.h index 484d1577b9..e05d7a07af 100644 --- a/stepmania/src/arch/Sound/DSoundHelpers.h +++ b/stepmania/src/arch/Sound/DSoundHelpers.h @@ -37,8 +37,8 @@ class DSoundBuf int bytes_per_frame() const { return channels*samplebits/8; } int write_cursor, buffer_bytes_filled; /* bytes */ - int last_cursor_pos; /* frames */ - mutable int LastPosition; + int64_t last_cursor_pos; /* frames */ + mutable int64_t LastPosition; bool buffer_locked; char *locked_buf; @@ -56,7 +56,6 @@ public: bool get_output_buf(char **buffer, unsigned *bufsiz, int chunksize); void release_output_buf(char *buffer, unsigned bufsiz); - void Reset(); void Play(); void Stop(); void SetVolume(float vol); @@ -64,8 +63,8 @@ public: int GetSampleRate() { return samplerate; } ~DSoundBuf(); - int GetPosition() const; - int GetOutputPosition() const { return last_cursor_pos; } + int64_t GetPosition() const; + int64_t GetOutputPosition() const { return last_cursor_pos; } }; #endif diff --git a/stepmania/src/arch/Sound/RageSoundDriver_DSound.cpp b/stepmania/src/arch/Sound/RageSoundDriver_DSound.cpp index 3f1b40473f..fae1d924b2 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_DSound.cpp +++ b/stepmania/src/arch/Sound/RageSoundDriver_DSound.cpp @@ -82,7 +82,7 @@ void RageSound_DSound::Update(float delta) { if(str[i]->state != str[i]->STOPPING) continue; - int ps = str[i]->pcm->GetPosition(); + const int64_t ps = str[i]->pcm->GetPosition(); if(ps < str[i]->flush_pos) continue; /* stopping but still flushing */ @@ -105,8 +105,8 @@ bool RageSound_DSound::stream::GetData(bool init) char *locked_buf; unsigned len; - const int play_pos = pcm->GetOutputPosition(); - const int cur_play_pos = pcm->GetPosition(); + const int64_t play_pos = pcm->GetOutputPosition(); + const int64_t cur_play_pos = pcm->GetPosition(); if(init) { /* We're initializing; fill the entire buffer. The buffer is supposed to @@ -130,17 +130,18 @@ bool RageSound_DSound::stream::GetData(bool init) if( !start_time.IsZero() ) { /* If the sound is supposed to start at a time past this buffer, insert silence. */ - const int iFramesUntilThisBuffer = play_pos - cur_play_pos; + const int64_t iFramesUntilThisBuffer = play_pos - cur_play_pos; const float fSecondsBeforeStart = -start_time.Ago(); - const int iFramesBeforeStart = int(fSecondsBeforeStart * pcm->GetSampleRate()); - const int iSilentFramesInThisBuffer = iFramesBeforeStart-iFramesUntilThisBuffer; - const int iSilentBytesInThisBuffer = clamp( iSilentFramesInThisBuffer * bytes_per_frame, 0, bytes_left ); + const int64_t iFramesBeforeStart = int64_t(fSecondsBeforeStart * pcm->GetSampleRate()); + const int64_t iSilentFramesInThisBuffer = iFramesBeforeStart-iFramesUntilThisBuffer; + const int iSilentBytesInThisBuffer = clamp( int(iSilentFramesInThisBuffer * bytes_per_frame), 0, bytes_left ); memset( locked_buf, 0, iSilentBytesInThisBuffer ); bytes_read += iSilentBytesInThisBuffer; bytes_left -= iSilentBytesInThisBuffer; - if( !iSilentBytesInThisBuffer ) + /* If we didn't completely fill the buffer, then we've written all of the silence. */ + if( bytes_left ) start_time.SetZero(); } @@ -314,7 +315,7 @@ void RageSound_DSound::StopMixing( RageSoundBase *snd ) stream_pool[i]->snd = NULL; } -int RageSound_DSound::GetPosition( const RageSoundBase *snd ) const +int64_t RageSound_DSound::GetPosition( const RageSoundBase *snd ) const { LockMutex L(SOUNDMAN->lock); diff --git a/stepmania/src/arch/Sound/RageSoundDriver_DSound.h b/stepmania/src/arch/Sound/RageSoundDriver_DSound.h index 17116e579a..28b35dc718 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_DSound.h +++ b/stepmania/src/arch/Sound/RageSoundDriver_DSound.h @@ -25,7 +25,7 @@ class RageSound_DSound: public RageSoundDriver STOPPING } state; - int flush_pos; /* state == STOPPING only */ + int64_t flush_pos; /* state == STOPPING only */ RageTimer start_time; bool GetData(bool init); @@ -47,7 +47,7 @@ class RageSound_DSound: public RageSoundDriver /* virtuals: */ void StartMixing( RageSoundBase *snd ); /* used by RageSound */ void StopMixing( RageSoundBase *snd ); /* used by RageSound */ - int GetPosition( const RageSoundBase *snd ) const; + int64_t GetPosition( const RageSoundBase *snd ) const; void Update(float delta); void VolumeChanged(); diff --git a/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.cpp b/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.cpp index dcc432f545..add5aea25e 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.cpp +++ b/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.cpp @@ -63,12 +63,11 @@ bool RageSound_DSound_Software::GetData() char *locked_buf; unsigned len; - const int play_pos = pcm->GetOutputPosition(); - const int cur_play_pos = pcm->GetPosition(); + const int64_t play_pos = pcm->GetOutputPosition(); + int64_t cur_play_pos = -1; if(!pcm->get_output_buf(&locked_buf, &len, chunksize)) return false; - /* Silence the buffer. */ memset(locked_buf, 0, len); @@ -94,11 +93,13 @@ bool RageSound_DSound_Software::GetData() if( !sounds[i]->start_time.IsZero() ) { /* If the sound is supposed to start at a time past this buffer, insert silence. */ - const int iFramesUntilThisBuffer = play_pos - cur_play_pos; + if( cur_play_pos == -1 ) + cur_play_pos = pcm->GetPosition(); + const int64_t iFramesUntilThisBuffer = play_pos - cur_play_pos; const float fSecondsBeforeStart = -sounds[i]->start_time.Ago(); - const int iFramesBeforeStart = int(fSecondsBeforeStart * samplerate); - const int iSilentFramesInThisBuffer = iFramesBeforeStart-iFramesUntilThisBuffer; - const int iSilentBytesInThisBuffer = clamp( iSilentFramesInThisBuffer * bytes_per_frame, 0, bytes_left ); + const int64_t iFramesBeforeStart = int64_t(fSecondsBeforeStart * samplerate); + const int64_t iSilentFramesInThisBuffer = iFramesBeforeStart-iFramesUntilThisBuffer; + const int iSilentBytesInThisBuffer = clamp( int(iSilentFramesInThisBuffer * bytes_per_frame), 0, bytes_left ); memset( buf+bytes_read, 0, iSilentBytesInThisBuffer ); bytes_read += iSilentBytesInThisBuffer; @@ -177,14 +178,9 @@ void RageSound_DSound_Software::StopMixing( RageSoundBase *snd ) delete sounds[i]; sounds.erase(sounds.begin()+i, sounds.begin()+i+1); - - /* If nothing is playing, reset the frame count; this is just to - * prevent eventual overflow. */ - if(sounds.empty()) - pcm->Reset(); } -int RageSound_DSound_Software::GetPosition( const RageSoundBase *snd ) const +int64_t RageSound_DSound_Software::GetPosition( const RageSoundBase *snd ) const { LockMut(SOUNDMAN->lock); return pcm->GetPosition(); diff --git a/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.h b/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.h index 4dd7584e85..426edd6d74 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.h +++ b/stepmania/src/arch/Sound/RageSoundDriver_DSound_Software.h @@ -15,7 +15,7 @@ class RageSound_DSound_Software: public RageSoundDriver RageSoundBase *snd; RageTimer start_time; bool stopping; - int flush_pos; /* when stopping only */ + int64_t flush_pos; /* when stopping only */ sound() { snd = NULL; stopping=false; } }; @@ -38,7 +38,7 @@ class RageSound_DSound_Software: public RageSoundDriver /* virtuals: */ void StartMixing( RageSoundBase *snd ); /* used by RageSound */ void StopMixing( RageSoundBase *snd ); /* used by RageSound */ - int GetPosition( const RageSoundBase *snd ) const; + int64_t GetPosition( const RageSoundBase *snd ) const; float GetPlayLatency() const; int GetSampleRate( int rate ) const; diff --git a/stepmania/src/arch/Sound/RageSoundDriver_Null.cpp b/stepmania/src/arch/Sound/RageSoundDriver_Null.cpp index 3ce0aa56ac..f69f7ad4b5 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_Null.cpp +++ b/stepmania/src/arch/Sound/RageSoundDriver_Null.cpp @@ -131,9 +131,9 @@ void RageSound_Null::StopMixing( RageSoundBase *snd ) sounds.erase(sounds.begin()+i, sounds.begin()+i+1); } -int RageSound_Null::GetPosition( const RageSoundBase *snd ) const +int64_t RageSound_Null::GetPosition( const RageSoundBase *snd ) const { - return int(RageTimer::GetTimeSinceStart() * samplerate); + return int64_t(RageTimer::GetTimeSinceStart() * samplerate); } RageSound_Null::RageSound_Null() diff --git a/stepmania/src/arch/Sound/RageSoundDriver_Null.h b/stepmania/src/arch/Sound/RageSoundDriver_Null.h index eee963470e..4e6faadbd0 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_Null.h +++ b/stepmania/src/arch/Sound/RageSoundDriver_Null.h @@ -39,7 +39,7 @@ protected: /* virtuals: */ virtual void StartMixing( RageSoundBase *snd ); virtual void StopMixing( RageSoundBase *snd ); - virtual int GetPosition( const RageSoundBase *snd ) const; + virtual int64_t GetPosition( const RageSoundBase *snd ) const; virtual float GetPlayLatency() const; virtual void Update(float delta); virtual bool GetData(); diff --git a/stepmania/src/arch/Sound/RageSoundDriver_WaveOut.cpp b/stepmania/src/arch/Sound/RageSoundDriver_WaveOut.cpp index e99316c406..4cda343aec 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_WaveOut.cpp +++ b/stepmania/src/arch/Sound/RageSoundDriver_WaveOut.cpp @@ -80,8 +80,8 @@ bool RageSound_WaveOut::GetData() static SoundMixBuffer mix; mix.SetVolume( SOUNDMAN->GetMixVolume() ); - const int play_pos = last_cursor_pos; - const int cur_play_pos = GetPosition( NULL ); + const int64_t play_pos = last_cursor_pos; + const int64_t cur_play_pos = GetPosition( NULL ); for(unsigned i = 0; i < sounds.size(); ++i) { @@ -94,11 +94,11 @@ bool RageSound_WaveOut::GetData() if( !sounds[i]->start_time.IsZero() ) { /* If the sound is supposed to start at a time past this buffer, insert silence. */ - const int iFramesUntilThisBuffer = play_pos - cur_play_pos; + const int64_t iFramesUntilThisBuffer = play_pos - cur_play_pos; const float fSecondsBeforeStart = -sounds[i]->start_time.Ago(); - const int iFramesBeforeStart = int(fSecondsBeforeStart * samplerate); - const int iSilentFramesInThisBuffer = iFramesBeforeStart-iFramesUntilThisBuffer; - const int iSilentBytesInThisBuffer = clamp( iSilentFramesInThisBuffer * bytes_per_frame, 0, bytes_left ); + const int64_t iFramesBeforeStart = int64_t(fSecondsBeforeStart * samplerate); + const int64_t iSilentFramesInThisBuffer = iFramesBeforeStart-iFramesUntilThisBuffer; + const int iSilentBytesInThisBuffer = clamp( int(iSilentFramesInThisBuffer * bytes_per_frame), 0, bytes_left ); memset( buf+bytes_read, 0, iSilentBytesInThisBuffer ); bytes_read += iSilentBytesInThisBuffer; @@ -181,7 +181,7 @@ void RageSound_WaveOut::StopMixing( RageSoundBase *snd ) sounds.erase(sounds.begin()+i, sounds.begin()+i+1); } -int RageSound_WaveOut::GetPosition( const RageSoundBase *snd ) const +int64_t RageSound_WaveOut::GetPosition( const RageSoundBase *snd ) const { LockMutex L(SOUNDMAN->lock); diff --git a/stepmania/src/arch/Sound/RageSoundDriver_WaveOut.h b/stepmania/src/arch/Sound/RageSoundDriver_WaveOut.h index d932e5dbd4..cd0fcd6444 100644 --- a/stepmania/src/arch/Sound/RageSoundDriver_WaveOut.h +++ b/stepmania/src/arch/Sound/RageSoundDriver_WaveOut.h @@ -12,10 +12,8 @@ class RageSound_WaveOut: public RageSoundDriver struct sound { RageSoundBase *snd; RageTimer start_time; - bool stopping; - - int flush_pos; /* state == STOPPING only */ + int64_t flush_pos; /* state == STOPPING only */ sound() { snd = NULL; stopping=false; } }; @@ -39,7 +37,7 @@ class RageSound_WaveOut: public RageSoundDriver /* virtuals: */ void StartMixing( RageSoundBase *snd ); /* used by RageSound */ void StopMixing( RageSoundBase *snd ); /* used by RageSound */ - int GetPosition( const RageSoundBase *snd ) const; + int64_t GetPosition( const RageSoundBase *snd ) const; float GetPlayLatency() const; public: