remove prebuffering from RageSound; let Preload handle it

This commit is contained in:
Glenn Maynard
2003-03-15 23:55:24 +00:00
parent 187aee0a05
commit bbf8067551
4 changed files with 18 additions and 77 deletions
+17 -65
View File
@@ -100,8 +100,6 @@ RageSound::RageSound(const RageSound &cpy)
stream.Sample = NULL; stream.Sample = NULL;
original = cpy.original; original = cpy.original;
full_buf = cpy.full_buf;
big = cpy.big;
m_StartSample = cpy.m_StartSample; m_StartSample = cpy.m_StartSample;
m_LengthSamples = cpy.m_LengthSamples; m_LengthSamples = cpy.m_LengthSamples;
StopMode = cpy.StopMode; StopMode = cpy.StopMode;
@@ -112,11 +110,8 @@ RageSound::RageSound(const RageSound &cpy)
speed_input_samples = cpy.speed_input_samples; speed_input_samples = cpy.speed_input_samples;
speed_output_samples = cpy.speed_output_samples; speed_output_samples = cpy.speed_output_samples;
if(big) stream.buf.reserve(internal_buffer_size);
{ stream.Sample = cpy.stream.Sample->Copy();
stream.buf.reserve(internal_buffer_size);
stream.Sample = cpy.stream.Sample->Copy();
}
/* Load() won't work on a copy if m_sFilePath is already set, so /* Load() won't work on a copy if m_sFilePath is already set, so
* copy this down here. */ * copy this down here. */
@@ -136,8 +131,6 @@ void RageSound::Unload()
m_sFilePath = ""; m_sFilePath = "";
stream.buf.clear(); stream.buf.clear();
full_buf.erase();
} }
/* This is called upon fatal failure. Replace the sound with silence. */ /* This is called upon fatal failure. Replace the sound with silence. */
@@ -146,13 +139,10 @@ void RageSound::Fail(CString reason)
delete stream.Sample; delete stream.Sample;
stream.Sample = NULL; stream.Sample = NULL;
big = false; /* XXX
full_buf.erase();
/* XXX untested
* full_buf.append(0, 1024); should be OK, but VC6 is broken ... */ * full_buf.append(0, 1024); should be OK, but VC6 is broken ... */
basic_string<char> empty(1024, 0); basic_string<char> empty(1024, 0);
full_buf.insert(full_buf.end(), empty.begin(), empty.end()); // full_buf.insert(full_buf.end(), empty.begin(), empty.end());
position = 0; position = 0;
LOG->Warn("Decoding %s failed: %s", LOG->Warn("Decoding %s failed: %s",
@@ -181,9 +171,7 @@ bool RageSound::Load(CString sSoundFilePath, int precache)
RageException::Throw( "RageSoundManager::RageSoundManager: error opening sound %s: %s", RageException::Throw( "RageSoundManager::RageSoundManager: error opening sound %s: %s",
sSoundFilePath.GetString(), NewSample->GetError().c_str()); sSoundFilePath.GetString(), NewSample->GetError().c_str());
/* Try to decode into full_buf. */ /* Try to precache. */
big = true;
if(precache) if(precache)
{ {
SoundReader_Preload *Preload = new SoundReader_Preload; SoundReader_Preload *Preload = new SoundReader_Preload;
@@ -196,7 +184,6 @@ bool RageSound::Load(CString sSoundFilePath, int precache)
delete Preload; delete Preload;
} }
stream.Sample = NewSample; stream.Sample = NewSample;
// stream.Sample->SetPosition_Accurate(0);
return true; return true;
} }
@@ -220,21 +207,14 @@ void RageSound::SetLengthSeconds(float secs)
void RageSound::Update(float delta) void RageSound::Update(float delta)
{ {
if(playing && big && delta) if(playing && delta)
FillBuf(int(delta * samplerate * samplesize)); FillBuf(int(delta * samplerate * samplesize));
} }
/* Return the number of bytes available in the input buffer. */ /* Return the number of bytes available in the input buffer. */
int RageSound::Bytes_Available() const int RageSound::Bytes_Available() const
{ {
if(big) return stream.buf.size();
return stream.buf.size();
unsigned byte_pos = position * samplesize; /* samples -> bytes */
if(byte_pos > full_buf.size())
return 0; /* eof */
return full_buf.size() - byte_pos;
} }
#include <math.h> #include <math.h>
@@ -303,9 +283,6 @@ int RageSound::FillBuf(int bytes)
{ {
LockMut(SOUNDMAN->lock); LockMut(SOUNDMAN->lock);
if(!big)
return 0; /* prebuffer is already fully loaded */
ASSERT(stream.Sample); ASSERT(stream.Sample);
bool got_something = false; bool got_something = false;
@@ -381,19 +358,12 @@ int RageSound::GetData(char *buffer, int size)
got = min(got, size); got = min(got, size);
if(buffer) if(buffer)
memset(buffer, 0, got); memset(buffer, 0, got);
} else if(big) { } else {
/* Feed data out of our streaming buffer. */ /* Feed data out of our streaming buffer. */
ASSERT(stream.Sample); ASSERT(stream.Sample);
got = min(int(stream.buf.size()), size); got = min(int(stream.buf.size()), size);
if(buffer) if(buffer)
stream.buf.read(buffer, got); stream.buf.read(buffer, got);
} else {
/* Feed data out of our full buffer. */
int byte_pos = position * samplesize;
got = min(int(full_buf.size())-byte_pos, size);
got = max(got, 0);
if(buffer)
memcpy(buffer, full_buf.data()+byte_pos, got);
} }
return got; return got;
@@ -562,22 +532,17 @@ void RageSound::Stop()
float RageSound::GetLengthSeconds() float RageSound::GetLengthSeconds()
{ {
if(big) { ASSERT(stream.Sample);
ASSERT(stream.Sample); int len = stream.Sample->GetLength();
int len = stream.Sample->GetLength();
if(len < 0) if(len < 0)
{ {
LOG->Warn("GetLengthSeconds failed on %s: %s", LOG->Warn("GetLengthSeconds failed on %s: %s",
GetLoadedFilePath().GetString(), stream.Sample->GetError().c_str() ); GetLoadedFilePath().GetString(), stream.Sample->GetError().c_str() );
return -1; return -1;
}
return len / 1000.f; /* ms -> secs */
} else {
/* We have the whole file loaded; just return the position. */
return full_buf.size() / (float(samplerate)*samplesize);
} }
return len / 1000.f; /* ms -> secs */
} }
float RageSound::GetPositionSeconds() const float RageSound::GetPositionSeconds() const
@@ -685,19 +650,6 @@ bool RageSound::SetPositionSamples( int samples )
int ms = int(float(samples) * 1000.f / samplerate); int ms = int(float(samples) * 1000.f / samplerate);
ms = max(ms, 0); ms = max(ms, 0);
if(!big) {
/* Just make sure the position is in range. */
if(position*samplesize < int(full_buf.size()))
return true;
/* We were told to seek beyond EOF. This could be a truncated file
* or invalid data. Warn about it and jump back to the beginning. */
LOG->Warn("SetPositionSamples: %i ms is beyond EOF in %s",
ms, GetLoadedFilePath().GetString());
position = 0;
return false;
}
stream.buf.clear(); stream.buf.clear();
ASSERT(stream.Sample); ASSERT(stream.Sample);
-10
View File
@@ -83,9 +83,6 @@ public:
bool IsPlaying() const { return playing; } bool IsPlaying() const { return playing; }
CString GetLoadedFilePath() const { return m_sFilePath; } CString GetLoadedFilePath() const { return m_sFilePath; }
/* Query only: */
bool IsStreaming() const { return big; }
private: private:
/* If we were copied from another RageSound, this will point to it; otherwise /* If we were copied from another RageSound, this will point to it; otherwise
* this is ourself. */ * this is ourself. */
@@ -98,13 +95,6 @@ private:
} stream; } stream;
int FillBuf(int bytes); int FillBuf(int bytes);
/* These are only used when big == false: */
basic_string<char> full_buf;
/* If true, we're streaming data through sample and buf. If false, the entire
* file is pre-loaded into full_buf. */
bool big;
/* Sound blocks we've sent out recently through GetPCM. We keep track /* Sound blocks we've sent out recently through GetPCM. We keep track
* of each block for the last four calls of GetPCM. */ * of each block for the last four calls of GetPCM. */
struct pos_map_t { struct pos_map_t {
@@ -5,6 +5,7 @@ const int channels = 2;
const int samplesize = 2 * channels; /* 16-bit */ const int samplesize = 2 * channels; /* 16-bit */
const int samplerate = 44100; const int samplerate = 44100;
/* If a sound is smaller than this, we'll load it entirely into memory. */
const int max_prebuf_size = 1024*256; const int max_prebuf_size = 1024*256;
int SoundReader_Preload::total_samples() const int SoundReader_Preload::total_samples() const
-2
View File
@@ -90,7 +90,6 @@ void ScreenTestSound::UpdateText(int n)
"%s\n" "%s\n"
"%s\n" "%s\n"
"(%s)\n" "(%s)\n"
"%s\n"
"%s", "%s",
n+1, fn.GetString(), n+1, fn.GetString(),
s[n].s.IsPlaying()? "Playing":"Stopped", s[n].s.IsPlaying()? "Playing":"Stopped",
@@ -100,7 +99,6 @@ void ScreenTestSound::UpdateText(int n)
"Continue until stopped": "Continue until stopped":
"Loop", "Loop",
pos.size()? pos.GetString(): "none playing", pos.size()? pos.GetString(): "none playing",
s[n].s.IsStreaming()? "Streaming":"Preloaded",
selected == n? "^^^^^^":"" selected == n? "^^^^^^":""
)); ));
} }