clean up and optimize

This commit is contained in:
Glenn Maynard
2005-10-24 02:00:19 +00:00
parent 9d32f5e9f5
commit 63cb85cafa
2 changed files with 70 additions and 73 deletions
+55 -57
View File
@@ -17,100 +17,98 @@ RageSoundResampler::RageSoundResampler()
void RageSoundResampler::reset() void RageSoundResampler::reset()
{ {
at_eof = false; m_bAtEof = false;
memset(prev, 0, sizeof(prev)); memset( m_iPrevSample, 0, sizeof(m_iPrevSample) );
memset(t, 0, sizeof(t)); m_iPos = 0;
outbuf.clear(); m_OutBuf.clear();
ipos = 0; m_iChannels = 2;
Channels = 2;
} }
/* Write data to be converted. */ /* Write data to be converted. */
void RageSoundResampler::write(const void *data_, int bytes) void RageSoundResampler::write(const void *data_, int bytes)
{ {
ASSERT(!at_eof); ASSERT(!m_bAtEof);
const int16_t *data = (const int16_t *) data_; const int16_t *data = (const int16_t *) data_;
const unsigned samples = bytes / sizeof(int16_t); const unsigned samples = bytes / sizeof(int16_t);
const unsigned frames = samples / Channels; const unsigned frames = samples / m_iChannels;
if(InputRate == OutputRate) if(m_iInputRate == m_iOutputRate)
{ {
/* Optimization. */ /* Optimization. */
outbuf.insert(outbuf.end(), data, data+samples); m_OutBuf.insert(m_OutBuf.end(), data, data+samples);
return; return;
} }
#if 0
/* Much higher quality for 44100->48000 resampling, but way too slow. */
const int upsamp = 8;
const float div = float(InputRate*upsamp) / OutputRate;
for(unsigned f = 0; f < frames; ++f)
{
for(int u = 0; u < upsamp; ++u)
{
int ipos_begin = (int) roundf(ipos / div);
int ipos_end = (int) roundf((ipos+1) / div);
for(int c = 0; c < Channels; ++c)
{
const float f = 0.5f;
prev[c] = int16_t(prev[c] * (f) + data[c] * (1-f));
for(int i = ipos_begin; i < ipos_end; ++i)
outbuf.push_back(prev[c]);
}
ipos++;
}
data += Channels;
}
#else
/* Lerp. */ /* Lerp. */
const float div = float(InputRate) / OutputRate; const int FIXED_SHIFT = 14;
const int FIXED_ONE = 1<<FIXED_SHIFT;
int iInputSamplesPerOutputSample =
(m_iInputRate<<FIXED_SHIFT) / m_iOutputRate;
m_OutBuf.resize( (frames*m_iChannels*m_iOutputRate)/m_iInputRate + 10 );
int iSize = 0;
for( int c = 0; c < m_iChannels; ++c )
{
int iPos = m_iPos;
const int16_t *pInBuf = &data[c];
int16_t *pOutBuf = &m_OutBuf[c];
int iSamplesInput = 0;
int iSamplesOutput = 0;
int16_t iPrevSample = m_iPrevSample[c];
for( unsigned f = 0; f < frames; ++f ) for( unsigned f = 0; f < frames; ++f )
{ {
for(int c = 0; c < Channels; ++c) while( iPos < FIXED_ONE )
{ {
while(t[c] < 1.0f) int iSamp = iPrevSample * (FIXED_ONE-iPos) +
{ pInBuf[iSamplesInput] * iPos;
int16_t s = int16_t(prev[c]*(1-t[c]) + data[c]*t[c]); pOutBuf[iSamplesOutput] = int16_t(iSamp >> FIXED_SHIFT);
outbuf.push_back(s); iSamplesOutput += m_iChannels;
t[c] += div; iPos += iInputSamplesPerOutputSample;
} }
t[c] -= 1; iPos -= FIXED_ONE;
prev[c] = data[c];
iPrevSample = pInBuf[iSamplesInput];
iSamplesInput += m_iChannels;
}
m_iPrevSample[c] = iPrevSample;
if( c == m_iChannels-1 )
{
iSize = iSamplesOutput;
m_iPos = iPos;
}
} }
ipos++; m_OutBuf.erase( m_OutBuf.begin()+iSize, m_OutBuf.end() );
data += Channels;
}
#endif
} }
void RageSoundResampler::eof() void RageSoundResampler::eof()
{ {
ASSERT(!at_eof); ASSERT(!m_bAtEof);
/* Write some silence to flush out the real data. If we don't have any sound, /* Write some silence to flush out the real data. If we don't have any sound,
* don't do this, so seeking past end of file doesn't write silence. */ * don't do this, so seeking past end of file doesn't write silence. */
bool bNeedsFlush = false; bool bNeedsFlush = false;
for( int c = 0; c < Channels; ++c ) for( int c = 0; c < m_iChannels; ++c )
if( prev[c] != 0 ) if( m_iPrevSample[c] != 0 )
bNeedsFlush = true; bNeedsFlush = true;
if( bNeedsFlush ) if( bNeedsFlush )
{ {
const int size = Channels*16; const int size = m_iChannels*16;
int16_t *data = new int16_t[size]; int16_t *data = new int16_t[size];
memset(data, 0, size * sizeof(int16_t)); memset(data, 0, size * sizeof(int16_t));
write(data, size * sizeof(int16_t)); write(data, size * sizeof(int16_t));
delete [] data; delete [] data;
} }
at_eof = true; m_bAtEof = true;
} }
@@ -119,19 +117,19 @@ int RageSoundResampler::read(void *data, unsigned bytes)
/* Don't be silly. */ /* Don't be silly. */
ASSERT( (bytes % sizeof(int16_t)) == 0); ASSERT( (bytes % sizeof(int16_t)) == 0);
/* If no data is available, and we're at_eof, return -1. */ /* If no data is available, and we're m_bAtEof, return -1. */
if(outbuf.size() == 0 && at_eof) if(m_OutBuf.size() == 0 && m_bAtEof)
return -1; return -1;
/* Fill as much as we have. */ /* Fill as much as we have. */
int Avail = min(outbuf.size()*sizeof(int16_t), bytes); int Avail = min(m_OutBuf.size()*sizeof(int16_t), bytes);
memcpy(data, &outbuf[0], Avail); memcpy(data, &m_OutBuf[0], Avail);
outbuf.erase(outbuf.begin(), outbuf.begin() + Avail/sizeof(int16_t)); m_OutBuf.erase(m_OutBuf.begin(), m_OutBuf.begin() + Avail/sizeof(int16_t));
return Avail; return Avail;
} }
/* /*
* Copyright (c) 2003 Glenn Maynard * Copyright (c) 2003-2005 Glenn Maynard
* All rights reserved. * All rights reserved.
* *
* Permission is hereby granted, free of charge, to any person obtaining a * Permission is hereby granted, free of charge, to any person obtaining a
+15 -16
View File
@@ -5,24 +5,13 @@
class RageSoundResampler class RageSoundResampler
{ {
int InputRate, OutputRate, Channels;
enum { MAX_CHANNELS = 15 };
int16_t prev[MAX_CHANNELS];
vector<int16_t> outbuf;
bool at_eof;
int ipos;
float t[MAX_CHANNELS];
public: public:
RageSoundResampler(); RageSoundResampler();
/* Configuration: */ /* Configuration: */
void SetChannels( int c ) { ASSERT( c < MAX_CHANNELS ); Channels = c; } void SetChannels( int c ) { ASSERT( c < MAX_CHANNELS ); m_iChannels = c; }
void SetInputSampleRate(int hz) { InputRate = hz; } void SetInputSampleRate(int hz) { m_iInputRate = hz; }
void SetOutputSampleRate(int hz) { OutputRate = hz; } void SetOutputSampleRate(int hz) { m_iOutputRate = hz; }
/* Write data to be converted. */ /* Write data to be converted. */
void write(const void *data, int bytes); void write(const void *data, int bytes);
@@ -33,18 +22,28 @@ public:
void reset(); void reset();
/* Return the number of bytes currently readable. */ /* Return the number of bytes currently readable. */
unsigned avail() const { return outbuf.size() / 2; } unsigned avail() const { return m_OutBuf.size() / 2; }
/* Read converted data. Returns the number of bytes filled. /* Read converted data. Returns the number of bytes filled.
* If eof() has been called and the output is completely * If eof() has been called and the output is completely
* flushed, returns -1. */ * flushed, returns -1. */
int read(void *data, unsigned bytes); int read(void *data, unsigned bytes);
private:
int m_iInputRate, m_iOutputRate, m_iChannels;
enum { MAX_CHANNELS = 15 };
int16_t m_iPrevSample[MAX_CHANNELS];
vector<int16_t> m_OutBuf;
bool m_bAtEof;
int m_iPos;
}; };
#endif #endif
/* /*
* Copyright (c) 2003 Glenn Maynard * Copyright (c) 2003-2005 Glenn Maynard
* All rights reserved. * All rights reserved.
* *
* Permission is hereby granted, free of charge, to any person obtaining a * Permission is hereby granted, free of charge, to any person obtaining a