s/SoundReader_Preload/RageSoundReader_Preload/

split RageSoundReader_Preload::PreloadSound
split RageSound::LoadSoundReader
This commit is contained in:
Glenn Maynard
2004-10-24 19:25:21 +00:00
parent 6d8a38b6a5
commit c85de593f9
4 changed files with 65 additions and 45 deletions
+30 -32
View File
@@ -172,50 +172,48 @@ bool RageSound::Load( CString sSoundFilePath, int precache )
if(precache == 2) if(precache == 2)
precache = false; precache = false;
CString error;
SoundReader *pSound = SoundReader_FileReader::OpenFile( sSoundFilePath, error );
if( pSound == NULL )
{
LOG->Warn( "RageSound::Load: error opening sound \"%s\": %s",
sSoundFilePath.c_str(), error.c_str() );
pSound = new RageSoundReader_Silence;
}
/* Try to precache. */
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. */ /* Don't load over copies. */
ASSERT( original == this || m_sFilePath == "" ); ASSERT( original == this || m_sFilePath == "" );
Unload(); Unload();
m_sFilePath = sSoundFilePath;
decode_position = stopped_position = 0; decode_position = stopped_position = 0;
CString error; const int NeededRate = SOUNDMAN->GetDriverSampleRate( pSound->GetSampleRate() );
Sample = SoundReader_FileReader::OpenFile( m_sFilePath, error ); if( NeededRate != pSound->GetSampleRate() )
if( Sample == NULL )
{
LOG->Warn( "RageSound::Load: error opening sound \"%s\": %s",
m_sFilePath.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 ); RageSoundReader_Resample *Resample = RageSoundReader_Resample::MakeResampler( (RageSoundReader_Resample::ResampleQuality) PREFSMAN->m_iSoundResampleQuality );
Resample->Open(Sample); Resample->Open( pSound );
Resample->SetSampleRate( NeededRate ); Resample->SetSampleRate( NeededRate );
Sample = Resample; pSound = Resample;
} }
/* Try to precache. */ Sample = pSound;
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;
}
}
m_Mutex.SetName( ssprintf("RageSound (%s)", Basename(sSoundFilePath).c_str() ) );
return true;
} }
/* Return the number of bytes available in the input buffer. */ /* Return the number of bytes available in the input buffer. */
+4
View File
@@ -92,6 +92,10 @@ public:
* they can be ignored most of the time, so we continue to work if a file * they can be ignored most of the time, so we continue to work if a file
* is broken or missing.) */ * is broken or missing.) */
bool Load(CString fn, int precache = 2); 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 Unload();
void StartPlaying(); void StartPlaying();
+24 -9
View File
@@ -10,12 +10,27 @@
/* If a sound is smaller than this, we'll load it entirely into memory. */ /* If a sound is smaller than this, we'll load it entirely into memory. */
const unsigned max_prebuf_size = 1024*256; 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; return buf.get().size() / samplesize;
} }
bool SoundReader_Preload::Open(SoundReader *source) bool RageSoundReader_Preload::Open(SoundReader *source)
{ {
ASSERT(source); ASSERT(source);
samplerate = source->GetSampleRate(); samplerate = source->GetSampleRate();
@@ -58,17 +73,17 @@ bool SoundReader_Preload::Open(SoundReader *source)
return true; return true;
} }
int SoundReader_Preload::GetLength() const int RageSoundReader_Preload::GetLength() const
{ {
return int(float(total_samples()) * 1000.f / samplerate); return int(float(total_samples()) * 1000.f / samplerate);
} }
int SoundReader_Preload::GetLength_Fast() const int RageSoundReader_Preload::GetLength_Fast() const
{ {
return GetLength(); return GetLength();
} }
int SoundReader_Preload::SetPosition_Accurate(int ms) int RageSoundReader_Preload::SetPosition_Accurate(int ms)
{ {
const int sample = int((ms / 1000.0f) * samplerate); const int sample = int((ms / 1000.0f) * samplerate);
position = sample * samplesize; position = sample * samplesize;
@@ -82,12 +97,12 @@ int SoundReader_Preload::SetPosition_Accurate(int ms)
return position; return position;
} }
int SoundReader_Preload::SetPosition_Fast(int ms) int RageSoundReader_Preload::SetPosition_Fast(int ms)
{ {
return SetPosition_Accurate(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; const unsigned bytes_avail = buf.get().size() - position;
@@ -98,9 +113,9 @@ int SoundReader_Preload::Read(char *buffer, unsigned len)
return 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() rc_string::rc_string()
+6 -3
View File
@@ -1,5 +1,5 @@
/* /*
* SoundReader_Preload - Preload sounds from another reader * RageSoundReader_Preload - Preload sounds from another reader
*/ */
#ifndef RAGE_SOUND_READER_PRELOAD #ifndef RAGE_SOUND_READER_PRELOAD
@@ -22,7 +22,7 @@ public:
const string &get() const; const string &get() const;
}; };
class SoundReader_Preload: public SoundReader class RageSoundReader_Preload: public SoundReader
{ {
rc_string buf; rc_string buf;
@@ -49,7 +49,10 @@ public:
bool IsStreamingFromDisk() const { return false; } bool IsStreamingFromDisk() const { return false; }
SoundReader *Copy() const; SoundReader *Copy() const;
~SoundReader_Preload() { } ~RageSoundReader_Preload() { }
/* Attempt to preload a sound. pSound must be rewound. */
static bool PreloadSound( SoundReader *&pSound );
}; };
#endif #endif