cleanup, fix

This commit is contained in:
Glenn Maynard
2002-12-20 02:32:23 +00:00
parent 878ebe7aae
commit 576d2ba4aa
+9 -13
View File
@@ -219,6 +219,10 @@ void RageSound::SetStartSeconds( float secs )
void RageSound::SetLengthSeconds(float secs) void RageSound::SetLengthSeconds(float secs)
{ {
ASSERT(secs == -1 || secs >= 0);
if(secs == -1)
m_LengthSamples = -1;
else
m_LengthSamples = int(secs*samplerate); m_LengthSamples = int(secs*samplerate);
} }
@@ -314,24 +318,17 @@ int RageSound::GetPCM(char *buffer, int size, int sampleno)
while(size) while(size)
{ {
int got; int got;
int MaxBytes; int MaxBytes = size;
if(m_LengthSamples != -1) if(m_LengthSamples != -1)
{ {
/* We have a length; only read up to the end. MaxPosition is the /* We have a length; only read up to the end. MaxPosition is the
* sample position of the end. */ * sample position of the end. */
int MaxPosition = m_StartSample + m_LengthSamples; int SamplesToRead = m_StartSample + m_LengthSamples - position;
/* Number of bytes until MaxPosition. */ /* If it's negative, we're past the end, so cap it at 0. Don't read
MaxBytes = (MaxPosition - position) * samplesize; * more than size. */
MaxBytes = clamp(SamplesToRead * samplesize, 0, size);
/* If it's negative, we're past the end, so cap it at 0. */
MaxBytes = max(0, MaxBytes);
/* Don't read more than size. */
MaxBytes = min(MaxBytes, size);
} }
else
MaxBytes = size;
if(position < 0) { if(position < 0) {
/* We havn't *really* started playing yet, so just feed silence. How /* We havn't *really* started playing yet, so just feed silence. How
@@ -349,7 +346,6 @@ int RageSound::GetPCM(char *buffer, int size, int sampleno)
int byte_pos = position * samplesize; int byte_pos = position * samplesize;
got = min(int(full_buf.size())-byte_pos, MaxBytes); got = min(int(full_buf.size())-byte_pos, MaxBytes);
got = max(got, 0); got = max(got, 0);
if(got)
memcpy(buffer, full_buf.data()+byte_pos, got); memcpy(buffer, full_buf.data()+byte_pos, got);
} }