From 1de064ee2c5ac3620f47a256ec03966aae71625c Mon Sep 17 00:00:00 2001 From: Max Horn Date: Tue, 5 Aug 2003 01:14:27 +0000 Subject: optimized MP3InputStream::readBuffer (once more, I won't gurantee for correctness although it seems to work fine over here) svn-id: r9475 --- sound/audiostream.cpp | 64 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 21 deletions(-) diff --git a/sound/audiostream.cpp b/sound/audiostream.cpp index 7a1a537ebb..b206c00fc4 100644 --- a/sound/audiostream.cpp +++ b/sound/audiostream.cpp @@ -68,28 +68,33 @@ public: if (stereo) // Stereo requires even sized data assert(len % 2 == 0); } - int readBuffer(int16 *buffer, int numSamples) { - int samples = 0; - while (samples < numSamples && !eosIntern()) { - const int len = MIN(numSamples, (_end - _ptr) / (is16Bit ? 2 : 1)); - while (samples < len) { - *buffer++ = readSample(_ptr); - _ptr += (is16Bit ? 2 : 1); - samples++; - } - if (_loopPtr && _ptr == _end) { - _ptr = _loopPtr; - _end = _loopEnd; - } - } - return samples; - } + int readBuffer(int16 *buffer, int numSamples); int16 read() { return readIntern(); } bool eos() const { return eosIntern(); } bool isStereo() const { return stereo; } }; +template +int LinearMemoryStream::readBuffer(int16 *buffer, int numSamples) { + int samples = 0; + while (samples < numSamples && !eosIntern()) { + const int len = MIN(numSamples, (_end - _ptr) / (is16Bit ? 2 : 1)); + while (samples < len) { + *buffer++ = readSample(_ptr); + _ptr += (is16Bit ? 2 : 1); + samples++; + } + // Loop, if looping was specified + if (_loopPtr && _ptr == _end) { + _ptr = _loopPtr; + _end = _loopEnd; + } + } + return samples; +} + + #pragma mark - #pragma mark --- WrappedMemoryStream --- @@ -149,7 +154,6 @@ int WrappedMemoryStream::readBuffer(int16 *buffer, while (samples < numSamples && !eosIntern()) { const byte *endMarker = (_pos > _end) ? _bufferEnd : _end; const int len = MIN(numSamples, (endMarker - _pos) / (is16Bit ? 2 : 1)); - while (samples < len) { *buffer++ = readSample(_pos); _pos += (is16Bit ? 2 : 1); @@ -386,9 +390,7 @@ static inline int scale_sample(mad_fixed_t sample) { } inline int16 MP3InputStream::readIntern() { - if (_size < 0 || _posInFrame >= _synth.pcm.length) { // EOF - return 0; - } + assert(!eosIntern()); int16 sample; if (_isStereo) { @@ -412,10 +414,30 @@ inline int16 MP3InputStream::readIntern() { } int MP3InputStream::readBuffer(int16 *buffer, int numSamples) { - int samples; + int samples = 0; +#if 1 + if (_isStereo) + assert(_curChannel == 0); + while (samples < numSamples && !eosIntern()) { + const int len = MIN(numSamples, (int)(_synth.pcm.length - _posInFrame) * (_isStereo ? 2 : 1)); + while (samples < len) { + *buffer++ = (int16)scale_sample(_synth.pcm.samples[0][_posInFrame]); + samples++; + if (_isStereo) { + *buffer++ = (int16)scale_sample(_synth.pcm.samples[1][_posInFrame]); + samples++; + } + _posInFrame++; + } + if (_posInFrame >= _synth.pcm.length) { + refill(); + } + } +#else for (samples = 0; samples < numSamples && !eosIntern(); samples++) { *buffer++ = readIntern(); } +#endif return samples; } -- cgit v1.2.3