From bba2c8ce952b5242e3ff80054f1ac9cc48bcd097 Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Fri, 1 Jan 2010 16:57:23 +0000 Subject: Fix getTotalPlayTime for MP3, FLAC, Vorbis and LinearMemoryStream after the latest loop related changes. svn-id: r46838 --- sound/audiostream.cpp | 18 +++++++++++++++--- sound/audiostream.h | 10 +++++----- sound/flac.cpp | 8 ++++++-- sound/mp3.cpp | 10 ++++++---- sound/vorbis.cpp | 8 +++----- 5 files changed, 35 insertions(+), 19 deletions(-) (limited to 'sound') diff --git a/sound/audiostream.cpp b/sound/audiostream.cpp index 2915b38eec..795953c3a8 100644 --- a/sound/audiostream.cpp +++ b/sound/audiostream.cpp @@ -138,8 +138,11 @@ public: : _ptr(ptr), _end(ptr+len), _loopPtr(0), _loopEnd(0), _rate(rate), _playtime(calculatePlayTime(rate, len / (is16Bit ? 2 : 1) / (stereo ? 2 : 1))) { if (loopLen) { + _numLoops = 0; _loopPtr = _ptr + loopOffset; _loopEnd = _loopPtr + loopLen; + } else { + _numLoops = 1; } _origPtr = autoFreeMemory ? ptr : 0; @@ -153,7 +156,11 @@ public: bool endOfData() const { return _ptr >= _end; } int getRate() const { return _rate; } - int32 getTotalPlayTime() const { return _playtime; } + int32 getTotalPlayTime() const { + if (!_numLoops) + return kUnknownPlayTime; + return _playtime * _numLoops; + } void setNumLoops(uint numLoops) { _numLoops = numLoops; @@ -181,6 +188,7 @@ int LinearMemoryStream::readBuffer(int16 *buf _ptr += (is16Bit ? 2 : 1); } while (--len); // Loop, if looping was specified + // TODO: Handle non-infinite loops if (_loopPtr && _ptr >= _end) { _ptr = _loopPtr; _end = _loopEnd; @@ -242,7 +250,7 @@ protected: public: LinearDiskStream(int rate, uint beginLoop, uint endLoop, bool disposeStream, Common::SeekableReadStream *stream, LinearDiskStreamAudioBlock *block, uint numBlocks, bool loop) : _rate(rate), _stream(stream), _beginLoop(beginLoop), _endLoop(endLoop), _disposeAfterUse(disposeStream), - _audioBlockCount(numBlocks), _loop(loop), _numPlayedLoops(0) { + _audioBlockCount(numBlocks), _loop(loop), _numLoops(loop ? 0 : 1), _numPlayedLoops(0) { assert(numBlocks > 0); @@ -290,7 +298,11 @@ public: bool endOfData() const { return (_currentBlock == _audioBlockCount - 1) && (_diskLeft == 0) && (_bufferLeft == 0); } int getRate() const { return _rate; } - int32 getTotalPlayTime() const { return _playtime; } + int32 getTotalPlayTime() const { + if (!_numLoops) + return kUnknownPlayTime; + return _playtime * _numLoops; + } }; template diff --git a/sound/audiostream.h b/sound/audiostream.h index b0f49caaea..b5c95079f3 100644 --- a/sound/audiostream.h +++ b/sound/audiostream.h @@ -98,10 +98,6 @@ public: uint32 duration = 0, uint numLoops = 1); - enum { - kUnknownPlayTime = -1 - }; - /** * Sets number of times the stream is supposed to get looped * @param numLoops number of loops to play, 0 - infinite @@ -114,9 +110,13 @@ public: */ virtual uint getNumPlayedLoops() { return 0; } + enum { + kUnknownPlayTime = -1 + }; + /** * Returns total playtime of the AudioStream object. - * Note that this does not require to return an playtime, if the + * Note that this does not require to return any playtime, if the * playtime of the AudioStream is unknown it returns 'kUnknownPlayTime'. * @see kUnknownPlayTime * diff --git a/sound/flac.cpp b/sound/flac.cpp index 8fc7440ff5..09c7cce7c0 100644 --- a/sound/flac.cpp +++ b/sound/flac.cpp @@ -143,7 +143,11 @@ public: return _streaminfo.channels == 0 || (_lastSampleWritten && _sampleCache.bufFill == 0); } - int32 getTotalPlayTime() const { return _totalPlayTime; } + int32 getTotalPlayTime() const { + if (!_numLoops) + return kUnknownPlayTime; + return _totalPlayTime * _numLoops; + } bool isStreamDecoderReady() const { return getStreamDecoderState() == FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC ; } @@ -260,7 +264,7 @@ FlacInputStream::FlacInputStream(Common::SeekableReadStream *inStream, bool disp int32 seconds = samples / rate; int32 milliseconds = (1000 * (samples % rate)) / rate; - _totalPlayTime = (seconds * 1000 + milliseconds) * numLoops; + _totalPlayTime = (seconds * 1000 + milliseconds); } else { _totalPlayTime = kUnknownPlayTime; } diff --git a/sound/mp3.cpp b/sound/mp3.cpp index 02c6634999..6614dc944c 100644 --- a/sound/mp3.cpp +++ b/sound/mp3.cpp @@ -92,7 +92,11 @@ public: bool endOfData() const { return _state == MP3_STATE_EOS; } bool isStereo() const { return MAD_NCHANNELS(&_frame.header) == 2; } int getRate() const { return _frame.header.samplerate; } - int32 getTotalPlayTime() const { return _totalPlayTime; } + int32 getTotalPlayTime() const { + if (!_numLoops) + return kUnknownPlayTime; + return _totalPlayTime * _numLoops; + } void setNumLoops(uint numLoops = 1) { _numLoops = numLoops; } uint getNumPlayedLoops() { return _numPlayedLoops; } @@ -182,9 +186,7 @@ MP3InputStream::MP3InputStream(Common::SeekableReadStream *inStream, bool dispos _totalPlayTime = mad_timer_count(length, MAD_UNITS_MILLISECONDS); - if (numLoops && mad_timer_sign(length) >= 0) - _totalPlayTime *= numLoops; - else + if (mad_timer_sign(length) < 0) _totalPlayTime = kUnknownPlayTime; // Decode the first chunk of data. This is necessary so that _frame diff --git a/sound/vorbis.cpp b/sound/vorbis.cpp index 1d265e6025..c80bc700fb 100644 --- a/sound/vorbis.cpp +++ b/sound/vorbis.cpp @@ -92,7 +92,6 @@ protected: bool _isStereo; int _rate; - const uint _totalNumLoops; uint _numLoops; ///< Number of loops to play uint _numPlayedLoops; ///< Number of loops which have been played @@ -126,13 +125,13 @@ public: uint getNumPlayedLoops() { return _numPlayedLoops; } int32 getTotalPlayTime() const { - if (!_totalNumLoops) + if (!_numLoops) return AudioStream::kUnknownPlayTime; #ifdef USE_TREMOR - return (_endTime - _startTime) * _totalNumLoops; + return (_endTime - _startTime) * _numLoops; #else - return (int32)((_endTime - _startTime) * 1000.0) * _totalNumLoops; + return (int32)((_endTime - _startTime) * 1000.0) * _numLoops; #endif } @@ -144,7 +143,6 @@ VorbisInputStream::VorbisInputStream(Common::SeekableReadStream *inStream, bool _inStream(inStream), _disposeAfterUse(dispose), _numLoops(numLoops), - _totalNumLoops(numLoops), _bufferEnd(_buffer + ARRAYSIZE(_buffer)) { int res = ov_open_callbacks(inStream, &_ovFile, NULL, 0, g_stream_wrap); -- cgit v1.2.3