From c85de593f97bdb448d05b5402ae9a0710845f859 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 24 Oct 2004 19:25:21 +0000 Subject: [PATCH] s/SoundReader_Preload/RageSoundReader_Preload/ split RageSoundReader_Preload::PreloadSound split RageSound::LoadSoundReader --- stepmania/src/RageSound.cpp | 64 +++++++++++------------ stepmania/src/RageSound.h | 4 ++ stepmania/src/RageSoundReader_Preload.cpp | 33 ++++++++---- stepmania/src/RageSoundReader_Preload.h | 9 ++-- 4 files changed, 65 insertions(+), 45 deletions(-) diff --git a/stepmania/src/RageSound.cpp b/stepmania/src/RageSound.cpp index 366e037cbb..8679d75079 100644 --- a/stepmania/src/RageSound.cpp +++ b/stepmania/src/RageSound.cpp @@ -172,52 +172,50 @@ bool RageSound::Load( CString sSoundFilePath, int precache ) if(precache == 2) precache = false; - /* Don't load over copies. */ - ASSERT(original == this || m_sFilePath == ""); - - Unload(); - - m_sFilePath = sSoundFilePath; - decode_position = stopped_position = 0; - CString error; - Sample = SoundReader_FileReader::OpenFile( m_sFilePath, error ); - if( Sample == NULL ) + SoundReader *pSound = SoundReader_FileReader::OpenFile( sSoundFilePath, error ); + if( pSound == NULL ) { LOG->Warn( "RageSound::Load: error opening sound \"%s\": %s", - m_sFilePath.c_str(), error.c_str() ); + sSoundFilePath.c_str(), error.c_str() ); - Sample = new RageSoundReader_Silence; - } - - const int NeededRate = SOUNDMAN->GetDriverSampleRate( Sample->GetSampleRate() ); - if( NeededRate != Sample->GetSampleRate() ) - { - RageSoundReader_Resample *Resample = RageSoundReader_Resample::MakeResampler( (RageSoundReader_Resample::ResampleQuality) PREFSMAN->m_iSoundResampleQuality ); - Resample->Open(Sample); - Resample->SetSampleRate( NeededRate ); - Sample = Resample; + pSound = new RageSoundReader_Silence; } /* Try to precache. */ - if(precache) - { - SoundReader_Preload *Preload = new SoundReader_Preload; - if(Preload->Open(Sample)) { - Sample = Preload; - } else { - /* Preload failed. It read some data, so we need to rewind the - * reader. */ - Sample->SetPosition_Fast(0); - delete Preload; - } - } + if( precache ) + RageSoundReader_Preload::PreloadSound( pSound ); + + LoadSoundReader( pSound ); + + m_sFilePath = sSoundFilePath; m_Mutex.SetName( ssprintf("RageSound (%s)", Basename(sSoundFilePath).c_str() ) ); return true; } +void RageSound::LoadSoundReader( SoundReader *pSound ) +{ + /* Don't load over copies. */ + ASSERT( original == this || m_sFilePath == "" ); + + Unload(); + + decode_position = stopped_position = 0; + + const int NeededRate = SOUNDMAN->GetDriverSampleRate( pSound->GetSampleRate() ); + if( NeededRate != pSound->GetSampleRate() ) + { + RageSoundReader_Resample *Resample = RageSoundReader_Resample::MakeResampler( (RageSoundReader_Resample::ResampleQuality) PREFSMAN->m_iSoundResampleQuality ); + Resample->Open( pSound ); + Resample->SetSampleRate( NeededRate ); + pSound = Resample; + } + + Sample = pSound; +} + /* Return the number of bytes available in the input buffer. */ int RageSound::Bytes_Available() const { diff --git a/stepmania/src/RageSound.h b/stepmania/src/RageSound.h index 322125d652..ca9e515794 100644 --- a/stepmania/src/RageSound.h +++ b/stepmania/src/RageSound.h @@ -92,6 +92,10 @@ public: * they can be ignored most of the time, so we continue to work if a file * is broken or missing.) */ bool Load(CString fn, int precache = 2); + + /* Load a SoundReader that you've set up yourself. Sample rate conversion will + * be set up only if needed. Doesn't fail. */ + void LoadSoundReader( SoundReader *pSound ); void Unload(); void StartPlaying(); diff --git a/stepmania/src/RageSoundReader_Preload.cpp b/stepmania/src/RageSoundReader_Preload.cpp index dc7975b321..4061169ffc 100644 --- a/stepmania/src/RageSoundReader_Preload.cpp +++ b/stepmania/src/RageSoundReader_Preload.cpp @@ -10,12 +10,27 @@ /* If a sound is smaller than this, we'll load it entirely into memory. */ const unsigned max_prebuf_size = 1024*256; -int SoundReader_Preload::total_samples() const +bool RageSoundReader_Preload::PreloadSound( SoundReader *&pSound ) +{ + RageSoundReader_Preload *pPreload = new RageSoundReader_Preload; + if( !pPreload->Open(pSound) ) + { + /* Preload failed. It read some data, so we need to rewind the reader. */ + pSound->SetPosition_Fast( 0 ); + delete pPreload; + return false; + } + + pSound = pPreload; + return true; +} + +int RageSoundReader_Preload::total_samples() const { return buf.get().size() / samplesize; } -bool SoundReader_Preload::Open(SoundReader *source) +bool RageSoundReader_Preload::Open(SoundReader *source) { ASSERT(source); samplerate = source->GetSampleRate(); @@ -58,17 +73,17 @@ bool SoundReader_Preload::Open(SoundReader *source) return true; } -int SoundReader_Preload::GetLength() const +int RageSoundReader_Preload::GetLength() const { return int(float(total_samples()) * 1000.f / samplerate); } -int SoundReader_Preload::GetLength_Fast() const +int RageSoundReader_Preload::GetLength_Fast() const { return GetLength(); } -int SoundReader_Preload::SetPosition_Accurate(int ms) +int RageSoundReader_Preload::SetPosition_Accurate(int ms) { const int sample = int((ms / 1000.0f) * samplerate); position = sample * samplesize; @@ -82,12 +97,12 @@ int SoundReader_Preload::SetPosition_Accurate(int ms) return position; } -int SoundReader_Preload::SetPosition_Fast(int ms) +int RageSoundReader_Preload::SetPosition_Fast(int ms) { return SetPosition_Accurate(ms); } -int SoundReader_Preload::Read(char *buffer, unsigned len) +int RageSoundReader_Preload::Read(char *buffer, unsigned len) { const unsigned bytes_avail = buf.get().size() - position; @@ -98,9 +113,9 @@ int SoundReader_Preload::Read(char *buffer, unsigned len) return len; } -SoundReader *SoundReader_Preload::Copy() const +SoundReader *RageSoundReader_Preload::Copy() const { - return new SoundReader_Preload(*this); + return new RageSoundReader_Preload(*this); } rc_string::rc_string() diff --git a/stepmania/src/RageSoundReader_Preload.h b/stepmania/src/RageSoundReader_Preload.h index fce998e294..ad153c48ea 100644 --- a/stepmania/src/RageSoundReader_Preload.h +++ b/stepmania/src/RageSoundReader_Preload.h @@ -1,5 +1,5 @@ /* - * SoundReader_Preload - Preload sounds from another reader + * RageSoundReader_Preload - Preload sounds from another reader */ #ifndef RAGE_SOUND_READER_PRELOAD @@ -22,7 +22,7 @@ public: const string &get() const; }; -class SoundReader_Preload: public SoundReader +class RageSoundReader_Preload: public SoundReader { rc_string buf; @@ -49,7 +49,10 @@ public: bool IsStreamingFromDisk() const { return false; } SoundReader *Copy() const; - ~SoundReader_Preload() { } + ~RageSoundReader_Preload() { } + + /* Attempt to preload a sound. pSound must be rewound. */ + static bool PreloadSound( SoundReader *&pSound ); }; #endif