From 68fd8b74b0a42279056a4143455bb8b40dc83ca9 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sat, 24 May 2008 22:41:15 +0000 Subject: Patch #1970427: AudioStream play length querying svn-id: r32258 --- sound/audiostream.cpp | 15 +++++++++++---- sound/audiostream.h | 14 ++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/sound/audiostream.cpp b/sound/audiostream.cpp index 0fba6affdb..4454a09fe6 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,10 +127,11 @@ 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) @@ -147,10 +153,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 diff --git a/sound/audiostream.h b/sound/audiostream.h index ed6b37e51c..45740ba2ae 100644 --- a/sound/audiostream.h +++ b/sound/audiostream.h @@ -93,6 +93,20 @@ public: * NULL in case of an error (e.g. invalid/nonexisting file) */ static AudioStream* openStreamFile(const Common::String &basename, uint32 startTime = 0, uint32 duration = 0, uint numLoops = 1); + + enum { + kUnknownPlayTime = -1 + }; + + /** + * Returns total playtime of the AudioStream object. + * Note that this does not require to return an playtime, if the + * playtime of the AudioStream is unknown it returns 'kUnknownPlayTime'. + * @see kUnknownPlayTime + * + * @return playtime in milliseconds + */ + int32 getTotalPlayTime() const { return kUnknownPlayTime; } }; /** -- cgit v1.2.3