aboutsummaryrefslogtreecommitdiff
path: root/audio/audiostream.cpp
diff options
context:
space:
mode:
authorMatthew Hoops2012-08-26 15:39:18 -0400
committerMatthew Hoops2012-08-26 15:41:56 -0400
commit857b92f8ffececa9c1f990d21a6a8d1630199a62 (patch)
tree420463df7bf481f8c4e0dcba9bd37e68d999b43d /audio/audiostream.cpp
parent1d58ebe133c274643a89f2f4f0d24a2a8ab343c3 (diff)
parent18e7573dafbffdd509943c8f90f91933b17b0435 (diff)
downloadscummvm-rg350-857b92f8ffececa9c1f990d21a6a8d1630199a62.tar.gz
scummvm-rg350-857b92f8ffececa9c1f990d21a6a8d1630199a62.tar.bz2
scummvm-rg350-857b92f8ffececa9c1f990d21a6a8d1630199a62.zip
Merge pull request #268 from clone2727/video-rewrite
VideoDecoder upgrade & partial rewrite
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