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

xa.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 "audio/decoders/xa.h"
00024 #include "audio/audiostream.h"
00025 #include "common/stream.h"
00026 
00027 namespace Audio {
00028 
00029 class XAStream : public Audio::RewindableAudioStream {
00030 public:
00031     XAStream(Common::SeekableReadStream *stream, int rate, DisposeAfterUse::Flag disposeAfterUse);
00032     ~XAStream();
00033 
00034     bool isStereo() const { return false; }
00035     bool endOfData() const { return _endOfData && _samplesRemaining == 0; }
00036     int getRate() const { return _rate; }
00037     int readBuffer(int16 *buffer, const int numSamples);
00038 
00039     bool rewind();
00040 private:
00041     Common::SeekableReadStream *_stream;
00042     DisposeAfterUse::Flag _disposeAfterUse;
00043 
00044     void seekToPos(uint pos);
00045 
00046     byte _predictor;
00047     double _samples[28];
00048     byte _samplesRemaining;
00049     int _rate;
00050     double _s1, _s2;
00051     uint _loopPoint;
00052     bool _endOfData;
00053 };
00054 
00055 XAStream::XAStream(Common::SeekableReadStream *stream, int rate, DisposeAfterUse::Flag disposeAfterUse)
00056         : _stream(stream), _disposeAfterUse(disposeAfterUse) {
00057     _samplesRemaining = 0;
00058     _predictor = 0;
00059     _s1 = _s2 = 0.0;
00060     _rate = rate;
00061     _loopPoint = 0;
00062     _endOfData = false;
00063 }
00064 
00065 
00066 XAStream::~XAStream() {
00067     if (_disposeAfterUse == DisposeAfterUse::YES)
00068         delete _stream;
00069 }
00070 
00071 static const double s_xaDataTable[5][2] = {
00072     {  0.0, 0.0 },
00073     {  60.0 / 64.0,  0.0 },
00074     {  115.0 / 64.0, -52.0 / 64.0 },
00075     {  98.0 / 64.0, -55.0 / 64.0 },
00076     {  122.0 / 64.0, -60.0 / 64.0 }
00077 };
00078 
00079 int XAStream::readBuffer(int16 *buffer, const int numSamples) {
00080     int32 samplesDecoded = 0;
00081 
00082     for (int i = 28 - _samplesRemaining; i < 28 && samplesDecoded < numSamples; i++) {
00083         _samples[i] = _samples[i] + _s1 * s_xaDataTable[_predictor][0] + _s2 * s_xaDataTable[_predictor][1];
00084         _s2 = _s1;
00085         _s1 = _samples[i];
00086         int16 d = (int)(_samples[i] + 0.5);
00087         buffer[samplesDecoded] = d;
00088         samplesDecoded++;
00089         _samplesRemaining--;
00090     }
00091 
00092     if (endOfData())
00093         return samplesDecoded;
00094 
00095     while (samplesDecoded < numSamples) {
00096         byte i = 0;
00097 
00098         _predictor = _stream->readByte();
00099         byte shift = _predictor & 0xf;
00100         _predictor >>= 4;
00101 
00102         byte flags = _stream->readByte();
00103         if (flags == 3) {
00104             // Loop
00105             seekToPos(_loopPoint);
00106             continue;
00107         } else if (flags == 6) {
00108             // Set loop point
00109             _loopPoint = _stream->pos() - 2;
00110         } else if (flags == 7) {
00111             // End of stream
00112             _endOfData = true;
00113             return samplesDecoded;
00114         }
00115 
00116         for (i = 0; i < 28; i += 2) {
00117             byte d = _stream->readByte();
00118             int16 s = (d & 0xf) << 12;
00119             if (s & 0x8000)
00120                 s |= 0xffff0000;
00121             _samples[i] = (double)(s >> shift);
00122             s = (d & 0xf0) << 8;
00123             if (s & 0x8000)
00124                 s |= 0xffff0000;
00125             _samples[i + 1] = (double)(s >> shift);
00126         }
00127 
00128         for (i = 0; i < 28 && samplesDecoded < numSamples; i++) {
00129             _samples[i] = _samples[i] + _s1 * s_xaDataTable[_predictor][0] + _s2 * s_xaDataTable[_predictor][1];
00130             _s2 = _s1;
00131             _s1 = _samples[i];
00132             int16 d = (int)(_samples[i] + 0.5);
00133             buffer[samplesDecoded] = d;
00134             samplesDecoded++;
00135         }
00136 
00137         if (i != 28)
00138             _samplesRemaining = 28 - i;
00139 
00140         if (_stream->pos() >= _stream->size())
00141             _endOfData = true;
00142     }
00143 
00144     return samplesDecoded;
00145 }
00146 
00147 bool XAStream::rewind() {
00148     seekToPos(0);
00149     return true;
00150 }
00151 
00152 void XAStream::seekToPos(uint pos) {
00153     _stream->seek(pos);
00154     _samplesRemaining = 0;
00155     _predictor = 0;
00156     _s1 = _s2 = 0.0;
00157     _endOfData = false;
00158 }
00159 
00160 RewindableAudioStream *makeXAStream(Common::SeekableReadStream *stream, int rate, DisposeAfterUse::Flag disposeAfterUse) {
00161     return new XAStream(stream, rate, disposeAfterUse);
00162 }
00163 
00164 } // End of namespace Audio


Generated on Sat Mar 16 2019 05:02:03 for ResidualVM by doxygen 1.7.1
curved edge   curved edge