diff options
Diffstat (limited to 'sound/audiostream.cpp')
-rw-r--r-- | sound/audiostream.cpp | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/sound/audiostream.cpp b/sound/audiostream.cpp index 771baf351a..588c49ebec 100644 --- a/sound/audiostream.cpp +++ b/sound/audiostream.cpp @@ -102,6 +102,11 @@ AudioStream* AudioStream::openStreamFile(const Common::String &basename, uint32 #pragma mark --- LinearMemoryStream --- #pragma mark - +inline int32 calculatePlayTime(int rate, int samples) { + int32 seconds = samples / rate; + int32 milliseconds = (1000 * (samples % rate)) / rate; + return seconds * 1000 + milliseconds; +} /** * A simple raw audio stream, purely memory based. It operates on a single @@ -122,23 +127,26 @@ protected: const byte *_loopEnd; const int _rate; const byte *_origPtr; + const int32 _playtime; public: LinearMemoryStream(int rate, const byte *ptr, uint len, uint loopOffset, uint loopLen, bool autoFreeMemory) - : _ptr(ptr), _end(ptr+len), _loopPtr(0), _loopEnd(0), _rate(rate) { + : _ptr(ptr), _end(ptr+len), _loopPtr(0), _loopEnd(0), _rate(rate), _playtime(calculatePlayTime(rate, len / (is16Bit ? 2 : 1) / (stereo ? 2 : 1))) { // Verify the buffer sizes are sane - if (is16Bit && stereo) + if (is16Bit && stereo) { assert((len & 3) == 0 && (loopLen & 3) == 0); - else if (is16Bit || stereo) + } else if (is16Bit || stereo) { assert((len & 1) == 0 && (loopLen & 1) == 0); + } if (loopLen) { _loopPtr = _ptr + loopOffset; _loopEnd = _loopPtr + loopLen; } - if (stereo) // Stereo requires even sized data + if (stereo) { // Stereo requires even sized data assert(len % 2 == 0); + } _origPtr = autoFreeMemory ? ptr : 0; } @@ -147,10 +155,11 @@ public: } int readBuffer(int16 *buffer, const int numSamples); - bool isStereo() const { return stereo; } - bool endOfData() const { return _ptr >= _end; } + bool isStereo() const { return stereo; } + bool endOfData() const { return _ptr >= _end; } - int getRate() const { return _rate; } + int getRate() const { return _rate; } + int32 getTotalPlayTime() const { return _playtime; } }; template<bool stereo, bool is16Bit, bool isUnsigned, bool isLE> @@ -300,7 +309,7 @@ int AppendableMemoryStream<stereo, is16Bit, isUnsigned, isLE>::readBuffer(int16 assert(buf.start <= _pos && _pos <= buf.end); const int samplesLeftInCurBuffer = buf.end - _pos; if (samplesLeftInCurBuffer == 0) { - delete [] buf.start; + delete[] buf.start; _bufferQueue.erase(_bufferQueue.begin()); _pos = 0; continue; @@ -322,10 +331,11 @@ void AppendableMemoryStream<stereo, is16Bit, isUnsigned, isLE>::queueBuffer(byte Common::StackLock lock(_mutex); // Verify the buffer size is sane - if (is16Bit && stereo) + if (is16Bit && stereo) { assert((size & 3) == 0); - else if (is16Bit || stereo) + } else if (is16Bit || stereo) { assert((size & 1) == 0); + } // Verify that the stream has not yet been finalized (by a call to finish()) assert(!_finalized); |