diff options
author | Max Horn | 2003-07-28 16:55:43 +0000 |
---|---|---|
committer | Max Horn | 2003-07-28 16:55:43 +0000 |
commit | 7ea67048c1305bcf32a48fbe586e1bebf222a2b7 (patch) | |
tree | 0307d61752ba31cbe847ad6ffcf2384ad21c4be2 | |
parent | 7caa1a0c9e0438d154e209e47627e93181c2e969 (diff) | |
download | scummvm-rg350-7ea67048c1305bcf32a48fbe586e1bebf222a2b7.tar.gz scummvm-rg350-7ea67048c1305bcf32a48fbe586e1bebf222a2b7.tar.bz2 scummvm-rg350-7ea67048c1305bcf32a48fbe586e1bebf222a2b7.zip |
more work on WrappedMemoryStream (not yet tested)
svn-id: r9253
-rw-r--r-- | sound/audiostream.cpp | 76 | ||||
-rw-r--r-- | sound/audiostream.h | 17 |
2 files changed, 66 insertions, 27 deletions
diff --git a/sound/audiostream.cpp b/sound/audiostream.cpp index 465e1122e5..6fbf2acb1a 100644 --- a/sound/audiostream.cpp +++ b/sound/audiostream.cpp @@ -34,36 +34,58 @@ public: virtual bool isStereo() const { return stereo; } }; -#if 0 -TODO: Implement a wrapped memory stream, to be used by the ChannelStream class -(and possibly others?) + +#pragma mark - + template<bool stereo, int sampleSize> -class WrappedMemoryStream : public AudioInputStream { -protected: - byte *_bufferStart; - byte *_bufferEnd; - byte *_pos; - byte *_end; - - void advance() { - _ptr += sampleSize; - .. TODO: wrap - } -public: - WrappedMemoryStream(const byte *ptr, uint len) : _bufferStart(ptr), _bufferEnd(ptr+len) { } - virtual int size() const { - int size = _end - _pos; - if (size < 0) - size += _bufferEnd - _bufferStart - return size / sampleSize; - } - - void append(const byte *ptr, uint len) { - ... +WrappedMemoryStream<stereo, sampleSize>::WrappedMemoryStream(const byte *buffer, uint bufferSize) + : _bufferStart(buffer), _bufferEnd(buffer+bufferSize) { + if (stereo) // Stereo requires an even sized buffer + assert(bufferSize % 2 == 0); +} + +template<bool stereo, int sampleSize> +void WrappedMemoryStream<stereo, sampleSize>::advance() { + _pos += sampleSize; + // Wrap around? + if (_pos >= _bufferEnd) + _pos = _pos - (_bufferEnd - _bufferStart); +} + +template<bool stereo, int sampleSize> +int WrappedMemoryStream<stereo, sampleSize>::size() const { + int len = _end - _pos; + if (len < 0) + len += (_bufferEnd - _bufferStart); + return len / sampleSize; +} + +template<bool stereo, int sampleSize> +void WrappedMemoryStream<stereo, sampleSize>::append(const byte *data, uint32 len) { + if (_end + len > _bufferEnd) { + // Wrap-around case + uint32 size_to_end_of_buffer = _bufferEnd - _end; + len -= size_to_end_of_buffer; + if ((_end < _pos) || (_bufferStart + len >= _pos)) { + debug(2, "WrappedMemoryStream: buffer overflow (A)"); + return; + } + memcpy(_end, (byte*)data, size_to_end_of_buffer); + memcpy(_bufferStart, (byte *)data + size_to_end_of_buffer, len); + _end = _bufferStart + len; + } else { + if ((_end < _pos) && (_end + len >= _pos)) { + debug(2, "WrappedMemoryStream: buffer overflow (B)"); + return; + } + memcpy(_end, data, len); + _end += len; } -}; -#endif +} + + +#pragma mark - template<bool stereo, class T = class LinearMemoryStream<stereo, 1> > diff --git a/sound/audiostream.h b/sound/audiostream.h index b37a79b1e4..d0ac9d46c4 100644 --- a/sound/audiostream.h +++ b/sound/audiostream.h @@ -56,6 +56,23 @@ public: virtual bool isStereo() const { return false; } }; +// Wrapped memory stream, to be used by the ChannelStream class (and possibly others?) +template<bool stereo, int sampleSize> +class WrappedMemoryStream : public AudioInputStream { +protected: + byte *_bufferStart; + byte *_bufferEnd; + byte *_pos; + byte *_end; + + void advance(); +public: + WrappedMemoryStream(const byte *buffer, uint bufferSize); + virtual int size() const; + void append(const byte *data, uint32 len); +}; + + AudioInputStream *makeInputStream(byte _flags, const byte *ptr, uint32 len); #endif |