diff options
author | Christoph Mallon | 2011-08-06 16:30:52 +0200 |
---|---|---|
committer | Christoph Mallon | 2011-08-07 15:19:08 +0200 |
commit | a5a8833c059f28c85e392a3cd22c361d38ef95ff (patch) | |
tree | 3b3b69326019444c2c375c66f39cb3013a003591 /audio | |
parent | 2f23ff72c1d804d9d0e3ac09c46f52fd6b23a68c (diff) | |
download | scummvm-rg350-a5a8833c059f28c85e392a3cd22c361d38ef95ff.tar.gz scummvm-rg350-a5a8833c059f28c85e392a3cd22c361d38ef95ff.tar.bz2 scummvm-rg350-a5a8833c059f28c85e392a3cd22c361d38ef95ff.zip |
COMMON: Add DisposablePtr<T>, which replaces many repeated implementations of a dispose flag.
Diffstat (limited to 'audio')
-rw-r--r-- | audio/audiostream.cpp | 21 | ||||
-rw-r--r-- | audio/audiostream.h | 13 | ||||
-rw-r--r-- | audio/decoders/adpcm.cpp | 8 | ||||
-rw-r--r-- | audio/decoders/adpcm_intern.h | 5 | ||||
-rw-r--r-- | audio/decoders/mp3.cpp | 10 | ||||
-rw-r--r-- | audio/decoders/raw.cpp | 22 | ||||
-rw-r--r-- | audio/decoders/vorbis.cpp | 9 | ||||
-rw-r--r-- | audio/mixer.cpp | 9 |
8 files changed, 28 insertions, 69 deletions
diff --git a/audio/audiostream.cpp b/audio/audiostream.cpp index 547aa77526..1c63ce97bf 100644 --- a/audio/audiostream.cpp +++ b/audio/audiostream.cpp @@ -93,7 +93,7 @@ SeekableAudioStream *SeekableAudioStream::openStreamFile(const Common::String &b #pragma mark - LoopingAudioStream::LoopingAudioStream(RewindableAudioStream *stream, uint loops, DisposeAfterUse::Flag disposeAfterUse) - : _parent(stream), _disposeAfterUse(disposeAfterUse), _loops(loops), _completeIterations(0) { + : _parent(stream, disposeAfterUse), _loops(loops), _completeIterations(0) { assert(stream); if (!stream->rewind()) { @@ -102,11 +102,6 @@ LoopingAudioStream::LoopingAudioStream(RewindableAudioStream *stream, uint loops } } -LoopingAudioStream::~LoopingAudioStream() { - if (_disposeAfterUse == DisposeAfterUse::YES) - delete _parent; -} - int LoopingAudioStream::readBuffer(int16 *buffer, const int numSamples) { if ((_loops && _completeIterations == _loops) || !numSamples) return 0; @@ -169,7 +164,7 @@ SubLoopingAudioStream::SubLoopingAudioStream(SeekableAudioStream *stream, const Timestamp loopStart, const Timestamp loopEnd, DisposeAfterUse::Flag disposeAfterUse) - : _parent(stream), _disposeAfterUse(disposeAfterUse), _loops(loops), + : _parent(stream, disposeAfterUse), _loops(loops), _pos(0, getRate() * (isStereo() ? 2 : 1)), _loopStart(convertTimeToStreamPos(loopStart, getRate(), isStereo())), _loopEnd(convertTimeToStreamPos(loopEnd, getRate(), isStereo())), @@ -180,11 +175,6 @@ SubLoopingAudioStream::SubLoopingAudioStream(SeekableAudioStream *stream, _done = true; } -SubLoopingAudioStream::~SubLoopingAudioStream() { - if (_disposeAfterUse == DisposeAfterUse::YES) - delete _parent; -} - int SubLoopingAudioStream::readBuffer(int16 *buffer, const int numSamples) { if (_done) return 0; @@ -225,7 +215,7 @@ int SubLoopingAudioStream::readBuffer(int16 *buffer, const int numSamples) { #pragma mark - SubSeekableAudioStream::SubSeekableAudioStream(SeekableAudioStream *parent, const Timestamp start, const Timestamp end, DisposeAfterUse::Flag disposeAfterUse) - : _parent(parent), _disposeAfterUse(disposeAfterUse), + : _parent(parent, disposeAfterUse), _start(convertTimeToStreamPos(start, getRate(), isStereo())), _pos(0, getRate() * (isStereo() ? 2 : 1)), _length(convertTimeToStreamPos(end, getRate(), isStereo()) - _start) { @@ -234,11 +224,6 @@ SubSeekableAudioStream::SubSeekableAudioStream(SeekableAudioStream *parent, cons _parent->seek(_start); } -SubSeekableAudioStream::~SubSeekableAudioStream() { - if (_disposeAfterUse) - delete _parent; -} - int SubSeekableAudioStream::readBuffer(int16 *buffer, const int numSamples) { int framesLeft = MIN(_length.frameDiff(_pos), numSamples); int framesRead = _parent->readBuffer(buffer, framesLeft); diff --git a/audio/audiostream.h b/audio/audiostream.h index 0ffaa241ce..9c28e4d67f 100644 --- a/audio/audiostream.h +++ b/audio/audiostream.h @@ -23,6 +23,7 @@ #ifndef SOUND_AUDIOSTREAM_H #define SOUND_AUDIOSTREAM_H +#include "common/ptr.h" #include "common/scummsys.h" #include "common/str.h" #include "common/types.h" @@ -114,7 +115,6 @@ public: * @param disposeAfterUse Destroy the stream after the LoopingAudioStream has finished playback. */ LoopingAudioStream(RewindableAudioStream *stream, uint loops, DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::YES); - ~LoopingAudioStream(); int readBuffer(int16 *buffer, const int numSamples); bool endOfData() const; @@ -129,8 +129,7 @@ public: */ uint getCompleteIterations() const { return _completeIterations; } private: - RewindableAudioStream *_parent; - DisposeAfterUse::Flag _disposeAfterUse; + Common::DisposablePtr<RewindableAudioStream> _parent; uint _loops; uint _completeIterations; @@ -246,7 +245,6 @@ public: const Timestamp loopStart, const Timestamp loopEnd, DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::YES); - ~SubLoopingAudioStream(); int readBuffer(int16 *buffer, const int numSamples); bool endOfData() const { return _done; } @@ -254,8 +252,7 @@ public: bool isStereo() const { return _parent->isStereo(); } int getRate() const { return _parent->getRate(); } private: - SeekableAudioStream *_parent; - DisposeAfterUse::Flag _disposeAfterUse; + Common::DisposablePtr<SeekableAudioStream> _parent; uint _loops; Timestamp _pos; @@ -283,7 +280,6 @@ public: * @param disposeAfterUse Whether the parent stream object should be destroyed on destruction of the SubSeekableAudioStream. */ SubSeekableAudioStream(SeekableAudioStream *parent, const Timestamp start, const Timestamp end, DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::YES); - ~SubSeekableAudioStream(); int readBuffer(int16 *buffer, const int numSamples); @@ -297,8 +293,7 @@ public: Timestamp getLength() const { return _length; } private: - SeekableAudioStream *_parent; - DisposeAfterUse::Flag _disposeAfterUse; + Common::DisposablePtr<SeekableAudioStream> _parent; const Timestamp _start; const Timestamp _length; diff --git a/audio/decoders/adpcm.cpp b/audio/decoders/adpcm.cpp index 116f2f776a..535652a0b3 100644 --- a/audio/decoders/adpcm.cpp +++ b/audio/decoders/adpcm.cpp @@ -41,8 +41,7 @@ namespace Audio { // <http://wiki.multimedia.cx/index.php?title=Microsoft_IMA_ADPCM>. ADPCMStream::ADPCMStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse, uint32 size, int rate, int channels, uint32 blockAlign) - : _stream(stream), - _disposeAfterUse(disposeAfterUse), + : _stream(stream, disposeAfterUse), _startpos(stream->pos()), _endpos(_startpos + size), _channels(channels), @@ -52,11 +51,6 @@ ADPCMStream::ADPCMStream(Common::SeekableReadStream *stream, DisposeAfterUse::Fl reset(); } -ADPCMStream::~ADPCMStream() { - if (_disposeAfterUse == DisposeAfterUse::YES) - delete _stream; -} - void ADPCMStream::reset() { memset(&_status, 0, sizeof(_status)); _blockPos[0] = _blockPos[1] = _blockAlign; // To make sure first header is read diff --git a/audio/decoders/adpcm_intern.h b/audio/decoders/adpcm_intern.h index c9f894fb84..38514d7fca 100644 --- a/audio/decoders/adpcm_intern.h +++ b/audio/decoders/adpcm_intern.h @@ -33,6 +33,7 @@ #include "audio/audiostream.h" #include "common/endian.h" +#include "common/ptr.h" #include "common/stream.h" #include "common/textconsole.h" @@ -41,8 +42,7 @@ namespace Audio { class ADPCMStream : public RewindableAudioStream { protected: - Common::SeekableReadStream *_stream; - const DisposeAfterUse::Flag _disposeAfterUse; + Common::DisposablePtr<Common::SeekableReadStream> _stream; const int32 _startpos; const int32 _endpos; const int _channels; @@ -62,7 +62,6 @@ protected: public: ADPCMStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse, uint32 size, int rate, int channels, uint32 blockAlign); - ~ADPCMStream(); virtual bool endOfData() const { return (_stream->eos() || _stream->pos() >= _endpos); } virtual bool isStereo() const { return _channels == 2; } diff --git a/audio/decoders/mp3.cpp b/audio/decoders/mp3.cpp index 8d7f006ec7..00669945c2 100644 --- a/audio/decoders/mp3.cpp +++ b/audio/decoders/mp3.cpp @@ -25,6 +25,7 @@ #ifdef USE_MAD #include "common/debug.h" +#include "common/ptr.h" #include "common/stream.h" #include "common/textconsole.h" #include "common/util.h" @@ -52,8 +53,7 @@ protected: MP3_STATE_EOS // end of data reached (may need to loop) }; - Common::SeekableReadStream *_inStream; - DisposeAfterUse::Flag _disposeAfterUse; + Common::DisposablePtr<Common::SeekableReadStream> _inStream; uint _posInFrame; State _state; @@ -95,8 +95,7 @@ protected: }; MP3Stream::MP3Stream(Common::SeekableReadStream *inStream, DisposeAfterUse::Flag dispose) : - _inStream(inStream), - _disposeAfterUse(dispose), + _inStream(inStream, dispose), _posInFrame(0), _state(MP3_STATE_INIT), _length(0, 1000), @@ -134,9 +133,6 @@ MP3Stream::MP3Stream(Common::SeekableReadStream *inStream, DisposeAfterUse::Flag MP3Stream::~MP3Stream() { deinitStream(); - - if (_disposeAfterUse == DisposeAfterUse::YES) - delete _inStream; } void MP3Stream::decodeMP3Data() { diff --git a/audio/decoders/raw.cpp b/audio/decoders/raw.cpp index 4789fd0f36..881b8c1d6a 100644 --- a/audio/decoders/raw.cpp +++ b/audio/decoders/raw.cpp @@ -51,7 +51,7 @@ template<bool is16Bit, bool isUnsigned, bool isLE> class RawStream : public SeekableAudioStream { public: RawStream(int rate, bool stereo, DisposeAfterUse::Flag disposeStream, Common::SeekableReadStream *stream, const RawStreamBlockList &blocks) - : _rate(rate), _isStereo(stereo), _playtime(0, rate), _stream(stream), _disposeAfterUse(disposeStream), _blocks(blocks), _curBlock(_blocks.begin()), _blockLeft(0), _buffer(0) { + : _rate(rate), _isStereo(stereo), _playtime(0, rate), _stream(stream, disposeStream), _blocks(blocks), _curBlock(_blocks.begin()), _blockLeft(0), _buffer(0) { assert(_blocks.size() > 0); @@ -82,9 +82,6 @@ public: } ~RawStream() { - if (_disposeAfterUse == DisposeAfterUse::YES) - delete _stream; - delete[] _buffer; } @@ -98,15 +95,14 @@ public: bool seek(const Timestamp &where); private: - const int _rate; ///< Sample rate of stream - const bool _isStereo; ///< Whether this is an stereo stream - Timestamp _playtime; ///< Calculated total play time - Common::SeekableReadStream *_stream; ///< Stream to read data from - const DisposeAfterUse::Flag _disposeAfterUse; ///< Indicates whether the stream object should be deleted when this RawStream is destructed - const RawStreamBlockList _blocks; ///< Audio block list - - RawStreamBlockList::const_iterator _curBlock; ///< Current audio block number - int32 _blockLeft; ///< How many bytes are still left in the current block + const int _rate; ///< Sample rate of stream + const bool _isStereo; ///< Whether this is an stereo stream + Timestamp _playtime; ///< Calculated total play time + Common::DisposablePtr<Common::SeekableReadStream> _stream; ///< Stream to read data from + const RawStreamBlockList _blocks; ///< Audio block list + + RawStreamBlockList::const_iterator _curBlock; ///< Current audio block number + int32 _blockLeft; ///< How many bytes are still left in the current block /** * Advance one block in the stream in case diff --git a/audio/decoders/vorbis.cpp b/audio/decoders/vorbis.cpp index e10ec11a50..455803dc05 100644 --- a/audio/decoders/vorbis.cpp +++ b/audio/decoders/vorbis.cpp @@ -29,6 +29,7 @@ #ifdef USE_VORBIS +#include "common/ptr.h" #include "common/stream.h" #include "common/textconsole.h" #include "common/util.h" @@ -89,8 +90,7 @@ static ov_callbacks g_stream_wrap = { class VorbisStream : public SeekableAudioStream { protected: - Common::SeekableReadStream *_inStream; - DisposeAfterUse::Flag _disposeAfterUse; + Common::DisposablePtr<Common::SeekableReadStream> _inStream; bool _isStereo; int _rate; @@ -121,8 +121,7 @@ protected: }; VorbisStream::VorbisStream(Common::SeekableReadStream *inStream, DisposeAfterUse::Flag dispose) : - _inStream(inStream), - _disposeAfterUse(dispose), + _inStream(inStream, dispose), _length(0, 1000), _bufferEnd(_buffer + ARRAYSIZE(_buffer)) { @@ -150,8 +149,6 @@ VorbisStream::VorbisStream(Common::SeekableReadStream *inStream, DisposeAfterUse VorbisStream::~VorbisStream() { ov_clear(&_ovFile); - if (_disposeAfterUse == DisposeAfterUse::YES) - delete _inStream; } int VorbisStream::readBuffer(int16 *buffer, const int numSamples) { diff --git a/audio/mixer.cpp b/audio/mixer.cpp index 128224ae85..965766170d 100644 --- a/audio/mixer.cpp +++ b/audio/mixer.cpp @@ -163,9 +163,8 @@ private: uint32 _pauseStartTime; uint32 _pauseTime; - DisposeAfterUse::Flag _autofreeStream; RateConverter *_converter; - AudioStream *_stream; + Common::DisposablePtr<AudioStream> _stream; }; #pragma mark - @@ -492,8 +491,8 @@ Channel::Channel(Mixer *mixer, Mixer::SoundType type, AudioStream *stream, DisposeAfterUse::Flag autofreeStream, bool reverseStereo, int id, bool permanent) : _type(type), _mixer(mixer), _id(id), _permanent(permanent), _volume(Mixer::kMaxChannelVolume), _balance(0), _pauseLevel(0), _samplesConsumed(0), _samplesDecoded(0), _mixerTimeStamp(0), - _pauseStartTime(0), _pauseTime(0), _autofreeStream(autofreeStream), _converter(0), - _stream(stream) { + _pauseStartTime(0), _pauseTime(0), _converter(0), + _stream(stream, autofreeStream) { assert(mixer); assert(stream); @@ -503,8 +502,6 @@ Channel::Channel(Mixer *mixer, Mixer::SoundType type, AudioStream *stream, Channel::~Channel() { delete _converter; - if (_autofreeStream == DisposeAfterUse::YES) - delete _stream; } void Channel::setVolume(const byte volume) { |