aboutsummaryrefslogtreecommitdiff
path: root/audio/audiostream.cpp
diff options
context:
space:
mode:
authorEinar Johan Trøan Sømåen2012-08-31 13:11:31 +0200
committerEinar Johan Trøan Sømåen2012-08-31 13:11:31 +0200
commit3fe7f2cbe2b70eaa824b7159d94d40c2280006a3 (patch)
treeea592da00ad567c75008137f79eeba16c3ba40c4 /audio/audiostream.cpp
parent246109839b9c196e9181a6f619c15694456b9aec (diff)
parent10a947a0be80ea8c5c88bd3493a5057b1223ce45 (diff)
downloadscummvm-rg350-3fe7f2cbe2b70eaa824b7159d94d40c2280006a3.tar.gz
scummvm-rg350-3fe7f2cbe2b70eaa824b7159d94d40c2280006a3.tar.bz2
scummvm-rg350-3fe7f2cbe2b70eaa824b7159d94d40c2280006a3.zip
Merge remote-tracking branch 'origin/master' into wintermute
Diffstat (limited to 'audio/audiostream.cpp')
-rw-r--r--audio/audiostream.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/audio/audiostream.cpp b/audio/audiostream.cpp
index 1c5c435359..6e185702f0 100644
--- a/audio/audiostream.cpp
+++ b/audio/audiostream.cpp
@@ -386,4 +386,42 @@ Timestamp convertTimeToStreamPos(const Timestamp &where, int rate, bool isStereo
return Timestamp(result.secs(), result.numberOfFrames(), result.framerate());
}
+/**
+ * An AudioStream wrapper that cuts off the amount of samples read after a
+ * given time length is reached.
+ */
+class LimitingAudioStream : public AudioStream {
+public:
+ LimitingAudioStream(AudioStream *parentStream, const Audio::Timestamp &length, DisposeAfterUse::Flag disposeAfterUse) :
+ _parentStream(parentStream), _samplesRead(0), _disposeAfterUse(disposeAfterUse),
+ _totalSamples(length.convertToFramerate(getRate()).totalNumberOfFrames() * getChannels()) {}
+
+ ~LimitingAudioStream() {
+ if (_disposeAfterUse == DisposeAfterUse::YES)
+ delete _parentStream;
+ }
+
+ int readBuffer(int16 *buffer, const int numSamples) {
+ // Cap us off so we don't read past _totalSamples
+ int samplesRead = _parentStream->readBuffer(buffer, MIN<int>(numSamples, _totalSamples - _samplesRead));
+ _samplesRead += samplesRead;
+ return samplesRead;
+ }
+
+ bool endOfData() const { return _parentStream->endOfData() || _samplesRead >= _totalSamples; }
+ bool isStereo() const { return _parentStream->isStereo(); }
+ int getRate() const { return _parentStream->getRate(); }
+
+private:
+ int getChannels() const { return isStereo() ? 2 : 1; }
+
+ AudioStream *_parentStream;
+ DisposeAfterUse::Flag _disposeAfterUse;
+ uint32 _totalSamples, _samplesRead;
+};
+
+AudioStream *makeLimitingAudioStream(AudioStream *parentStream, const Timestamp &length, DisposeAfterUse::Flag disposeAfterUse) {
+ return new LimitingAudioStream(parentStream, length, disposeAfterUse);
+}
+
} // End of namespace Audio