ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

raw.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/endian.h"
00024 #include "common/memstream.h"
00025 #include "common/textconsole.h"
00026 #include "common/util.h"
00027 
00028 #include "audio/audiostream.h"
00029 #include "audio/decoders/raw.h"
00030 
00031 namespace Audio {
00032 
00033 // This used to be an inline template function, but
00034 // buggy template function handling in MSVC6 forced
00035 // us to go with the macro approach. So far this is
00036 // the only template function that MSVC6 seemed to
00037 // compile incorrectly. Knock on wood.
00038 #define READ_ENDIAN_SAMPLE(is16Bit, isUnsigned, ptr, isLE) \
00039     ((is16Bit ? (isLE ? READ_LE_UINT16(ptr) : READ_BE_UINT16(ptr)) : (*ptr << 8)) ^ (isUnsigned ? 0x8000 : 0))
00040 
00041 
00042 #pragma mark -
00043 #pragma mark --- RawStream ---
00044 #pragma mark -
00045 
00049 template<bool is16Bit, bool isUnsigned, bool isLE>
00050 class RawStream : public SeekableAudioStream {
00051 public:
00052     RawStream(int rate, bool stereo, DisposeAfterUse::Flag disposeStream, Common::SeekableReadStream *stream)
00053         : _rate(rate), _isStereo(stereo), _playtime(0, rate), _stream(stream, disposeStream), _endOfData(false), _buffer(0) {
00054         // Setup our buffer for readBuffer
00055         _buffer = new byte[kSampleBufferLength * (is16Bit ? 2 : 1)];
00056         assert(_buffer);
00057 
00058         // Calculate the total playtime of the stream
00059         _playtime = Timestamp(0, _stream->size() / (_isStereo ? 2 : 1) / (is16Bit ? 2 : 1), rate);
00060     }
00061 
00062     ~RawStream() {
00063         delete[] _buffer;
00064     }
00065 
00066     int readBuffer(int16 *buffer, const int numSamples);
00067 
00068     bool isStereo() const  { return _isStereo; }
00069     bool endOfData() const { return _endOfData; }
00070 
00071     int getRate() const         { return _rate; }
00072     Timestamp getLength() const { return _playtime; }
00073 
00074     bool seek(const Timestamp &where);
00075 private:
00076     const int _rate;                                           
00077     const bool _isStereo;                                      
00078     Timestamp _playtime;                                       
00079     Common::DisposablePtr<Common::SeekableReadStream> _stream; 
00080     bool _endOfData;                                           
00081 
00082     byte *_buffer;                                             
00083     enum {
00090         kSampleBufferLength = 2048
00091     };
00092 
00099     int fillBuffer(int maxSamples);
00100 };
00101 
00102 template<bool is16Bit, bool isUnsigned, bool isLE>
00103 int RawStream<is16Bit, isUnsigned, isLE>::readBuffer(int16 *buffer, const int numSamples) {
00104     int samplesLeft = numSamples;
00105 
00106     while (samplesLeft > 0) {
00107         // Try to read up to "samplesLeft" samples.
00108         int len = fillBuffer(samplesLeft);
00109 
00110         // In case we were not able to read any samples
00111         // we will stop reading here.
00112         if (!len)
00113             break;
00114 
00115         // Adjust the samples left to read.
00116         samplesLeft -= len;
00117 
00118         // Copy the data to the caller's buffer.
00119         const byte *src = _buffer;
00120         while (len-- > 0) {
00121             *buffer++ = READ_ENDIAN_SAMPLE(is16Bit, isUnsigned, src, isLE);
00122             src += (is16Bit ? 2 : 1);
00123         }
00124     }
00125 
00126     return numSamples - samplesLeft;
00127 }
00128 
00129 template<bool is16Bit, bool isUnsigned, bool isLE>
00130 int RawStream<is16Bit, isUnsigned, isLE>::fillBuffer(int maxSamples) {
00131     int bufferedSamples = 0;
00132     byte *dst = _buffer;
00133 
00134     // We can only read up to "kSampleBufferLength" samples
00135     // so we take this into consideration, when trying to
00136     // read up to maxSamples.
00137     maxSamples = MIN<int>(kSampleBufferLength, maxSamples);
00138 
00139     // We will only read up to maxSamples
00140     while (maxSamples > 0 && !endOfData()) {
00141         // Try to read all the sample data and update the
00142         // destination pointer.
00143         const int bytesRead = _stream->read(dst, maxSamples * (is16Bit ? 2 : 1));
00144         dst += bytesRead;
00145 
00146         // Calculate how many samples we actually read.
00147         const int samplesRead = bytesRead / (is16Bit ? 2 : 1);
00148 
00149         // Update all status variables
00150         bufferedSamples += samplesRead;
00151         maxSamples -= samplesRead;
00152 
00153         // We stop stream playback, when we reached the end of the data stream.
00154         // We also stop playback when an error occures.
00155         if (_stream->pos() == _stream->size() || _stream->err() || _stream->eos())
00156             _endOfData = true;
00157     }
00158 
00159     return bufferedSamples;
00160 }
00161 
00162 template<bool is16Bit, bool isUnsigned, bool isLE>
00163 bool RawStream<is16Bit, isUnsigned, isLE>::seek(const Timestamp &where) {
00164     _endOfData = true;
00165 
00166     if (where > _playtime)
00167         return false;
00168 
00169     const uint32 seekSample = convertTimeToStreamPos(where, getRate(), isStereo()).totalNumberOfFrames();
00170     _stream->seek(seekSample * (is16Bit ? 2 : 1), SEEK_SET);
00171 
00172     // In case of an error we will not continue stream playback.
00173     if (!_stream->err() && !_stream->eos() && _stream->pos() != _stream->size())
00174         _endOfData = false;
00175 
00176     return true;
00177 }
00178 
00179 #pragma mark -
00180 #pragma mark --- Raw stream factories ---
00181 #pragma mark -
00182 
00183 /* In the following, we use preprocessor / macro tricks to simplify the code
00184  * which instantiates the input streams. We used to use template functions for
00185  * this, but MSVC6 / EVC 3-4 (used for WinCE builds) are extremely buggy when it
00186  * comes to this feature of C++... so as a compromise we use macros to cut down
00187  * on the (source) code duplication a bit.
00188  * So while normally macro tricks are said to make maintenance harder, in this
00189  * particular case it should actually help it :-)
00190  */
00191 
00192 #define MAKE_RAW_STREAM(UNSIGNED) \
00193         if (is16Bit) { \
00194             if (isLE) \
00195                 return new RawStream<true, UNSIGNED, true>(rate, isStereo, disposeAfterUse, stream); \
00196             else  \
00197                 return new RawStream<true, UNSIGNED, false>(rate, isStereo, disposeAfterUse, stream); \
00198         } else \
00199             return new RawStream<false, UNSIGNED, false>(rate, isStereo, disposeAfterUse, stream)
00200 
00201 SeekableAudioStream *makeRawStream(Common::SeekableReadStream *stream,
00202                                    int rate, byte flags,
00203                                    DisposeAfterUse::Flag disposeAfterUse) {
00204     const bool isStereo   = (flags & Audio::FLAG_STEREO) != 0;
00205     const bool is16Bit    = (flags & Audio::FLAG_16BITS) != 0;
00206     const bool isUnsigned = (flags & Audio::FLAG_UNSIGNED) != 0;
00207     const bool isLE       = (flags & Audio::FLAG_LITTLE_ENDIAN) != 0;
00208 
00209     assert(stream->size() % ((is16Bit ? 2 : 1) * (isStereo ? 2 : 1)) == 0);
00210 
00211     if (isUnsigned) {
00212         MAKE_RAW_STREAM(true);
00213     } else {
00214         MAKE_RAW_STREAM(false);
00215     }
00216 }
00217 
00218 SeekableAudioStream *makeRawStream(const byte *buffer, uint32 size,
00219                                    int rate, byte flags,
00220                                    DisposeAfterUse::Flag disposeAfterUse) {
00221     return makeRawStream(new Common::MemoryReadStream(buffer, size, disposeAfterUse), rate, flags, DisposeAfterUse::YES);
00222 }
00223 
00224 class PacketizedRawStream : public StatelessPacketizedAudioStream {
00225 public:
00226     PacketizedRawStream(int rate, byte flags) :
00227         StatelessPacketizedAudioStream(rate, ((flags & FLAG_STEREO) != 0) ? 2 : 1), _flags(flags) {}
00228 
00229 protected:
00230     AudioStream *makeStream(Common::SeekableReadStream *data);
00231 
00232 private:
00233     byte _flags;
00234 };
00235 
00236 AudioStream *PacketizedRawStream::makeStream(Common::SeekableReadStream *data) {
00237     return makeRawStream(data, getRate(), _flags);
00238 }
00239 
00240 PacketizedAudioStream *makePacketizedRawStream(int rate, byte flags) {
00241     return new PacketizedRawStream(rate, flags);
00242 }
00243 
00244 } // End of namespace Audio


Generated on Sat Feb 23 2019 05:01:15 for ResidualVM by doxygen 1.7.1
curved edge   curved edge