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;
original = cpy.original;
full_buf = cpy.full_buf;
big = cpy.big;
m_StartSample = cpy.m_StartSample;
m_LengthSamples = cpy.m_LengthSamples;
StopMode = cpy.StopMode;
@@ -112,11 +110,8 @@ RageSound::RageSound(const RageSound &cpy)
speed_input_samples = cpy.speed_input_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
* copy this down here. */
@@ -136,8 +131,6 @@ void RageSound::Unload()
m_sFilePath = "";
stream.buf.clear();
full_buf.erase();
}
/* This is called upon fatal failure. Replace the sound with silence. */
@@ -146,13 +139,10 @@ void RageSound::Fail(CString reason)
delete stream.Sample;
stream.Sample = NULL;
big = false;
full_buf.erase();
/* XXX untested
/* XXX
* full_buf.append(0, 1024); should be OK, but VC6 is broken ... */
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;
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",
sSoundFilePath.GetString(), NewSample->GetError().c_str());
/* Try to decode into full_buf. */
big = true;
/* Try to precache. */
if(precache)
{
SoundReader_Preload *Preload = new SoundReader_Preload;
@@ -196,7 +184,6 @@ bool RageSound::Load(CString sSoundFilePath, int precache)
delete Preload;
}
stream.Sample = NewSample;
// stream.Sample->SetPosition_Accurate(0);
return true;
}
@@ -220,21 +207,14 @@ void RageSound::SetLengthSeconds(float secs)
void RageSound::Update(float delta)
{
if(playing && big && delta)
if(playing && delta)
FillBuf(int(delta * samplerate * samplesize));
}
/* Return the number of bytes available in the input buffer. */
int RageSound::Bytes_Available() const
{
if(big)
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;
return stream.buf.size();
}
#include <math.h>
@@ -303,9 +283,6 @@ int RageSound::FillBuf(int bytes)
{
LockMut(SOUNDMAN->lock);
if(!big)
return 0; /* prebuffer is already fully loaded */
ASSERT(stream.Sample);
bool got_something = false;
@@ -381,19 +358,12 @@ int RageSound::GetData(char *buffer, int size)
got = min(got, size);
if(buffer)
memset(buffer, 0, got);
} else if(big) {
} else {
/* Feed data out of our streaming buffer. */
ASSERT(stream.Sample);
got = min(int(stream.buf.size()), size);
if(buffer)
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;
@@ -562,22 +532,17 @@ void RageSound::Stop()
float RageSound::GetLengthSeconds()
{
if(big) {
ASSERT(stream.Sample);
int len = stream.Sample->GetLength();
ASSERT(stream.Sample);
int len = stream.Sample->GetLength();
if(len < 0)
{
LOG->Warn("GetLengthSeconds failed on %s: %s",
GetLoadedFilePath().GetString(), stream.Sample->GetError().c_str() );
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);
if(len < 0)
{
LOG->Warn("GetLengthSeconds failed on %s: %s",
GetLoadedFilePath().GetString(), stream.Sample->GetError().c_str() );
return -1;
}
return len / 1000.f; /* ms -> secs */
}
float RageSound::GetPositionSeconds() const
@@ -685,19 +650,6 @@ bool RageSound::SetPositionSamples( int samples )
int ms = int(float(samples) * 1000.f / samplerate);
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();
ASSERT(stream.Sample);
-10
View File
@@ -82,9 +82,6 @@ public:
float GetPlaybackRate() const { return float(speed_input_samples) / speed_output_samples; }
bool IsPlaying() const { return playing; }
CString GetLoadedFilePath() const { return m_sFilePath; }
/* Query only: */
bool IsStreaming() const { return big; }
private:
/* If we were copied from another RageSound, this will point to it; otherwise
@@ -98,13 +95,6 @@ private:
} stream;
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
* of each block for the last four calls of GetPCM. */
struct pos_map_t {
@@ -5,6 +5,7 @@ const int channels = 2;
const int samplesize = 2 * channels; /* 16-bit */
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;
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+1, fn.GetString(),
s[n].s.IsPlaying()? "Playing":"Stopped",
@@ -100,7 +99,6 @@ void ScreenTestSound::UpdateText(int n)
"Continue until stopped":
"Loop",
pos.size()? pos.GetString(): "none playing",
s[n].s.IsStreaming()? "Streaming":"Preloaded",
selected == n? "^^^^^^":""
));
}