diff options
author | athrxx | 2019-04-14 21:50:26 +0200 |
---|---|---|
committer | athrxx | 2019-04-15 21:51:47 +0200 |
commit | 9144b8894e53aa12ecd4f313d52aaeed604a40b2 (patch) | |
tree | 68550eff40b5db17a3ea14df4aadd4562e9eb473 /engines/kyra/sound/sound_digital_mr.cpp | |
parent | 9d527463343c627eac305d8b17a273c0583ffa63 (diff) | |
download | scummvm-rg350-9144b8894e53aa12ecd4f313d52aaeed604a40b2.tar.gz scummvm-rg350-9144b8894e53aa12ecd4f313d52aaeed604a40b2.tar.bz2 scummvm-rg350-9144b8894e53aa12ecd4f313d52aaeed604a40b2.zip |
KYRA: sound files/classes reorganization step #4
Separate drivers from their wrapper classes and move them into their own files
Diffstat (limited to 'engines/kyra/sound/sound_digital_mr.cpp')
-rw-r--r-- | engines/kyra/sound/sound_digital_mr.cpp | 298 |
1 files changed, 1 insertions, 297 deletions
diff --git a/engines/kyra/sound/sound_digital_mr.cpp b/engines/kyra/sound/sound_digital_mr.cpp index cf94eb0ff7..ffcfcd128d 100644 --- a/engines/kyra/sound/sound_digital_mr.cpp +++ b/engines/kyra/sound/sound_digital_mr.cpp @@ -25,7 +25,6 @@ #include "kyra/engine/kyra_mr.h" #include "audio/audiostream.h" - #include "audio/decoders/mp3.h" #include "audio/decoders/vorbis.h" #include "audio/decoders/flac.h" @@ -103,294 +102,6 @@ int KyraAudioStream::readBuffer(int16 *buffer, const int numSamples) { return samplesRead; } -// Thanks to Torbjorn Andersson (eriktorbjorn) for his aud player on which -// this code is based on - -// TODO: cleanup of whole AUDStream - -class AUDStream : public Audio::SeekableAudioStream { -public: - AUDStream(Common::SeekableReadStream *stream); - ~AUDStream(); - - int readBuffer(int16 *buffer, const int numSamples); - - bool isStereo() const { return false; } - bool endOfData() const { return _endOfData; } - - int getRate() const { return _rate; } - - bool seek(const Audio::Timestamp &where); - Audio::Timestamp getLength() const { return _length; } -private: - Common::SeekableReadStream *_stream; - uint32 _streamStart; - bool _endOfData; - int _rate; - uint _processedSize; - uint _totalSize; - Audio::Timestamp _length; - - int _bytesLeft; - - byte *_outBuffer; - int _outBufferOffset; - uint _outBufferSize; - - byte *_inBuffer; - uint _inBufferSize; - - int readChunk(int16 *buffer, const int maxSamples); - - static const int8 WSTable2Bit[]; - static const int8 WSTable4Bit[]; -}; - -const int8 AUDStream::WSTable2Bit[] = { -2, -1, 0, 1 }; -const int8 AUDStream::WSTable4Bit[] = { - -9, -8, -6, -5, -4, -3, -2, -1, - 0, 1, 2, 3, 4, 5, 6, 8 -}; - -AUDStream::AUDStream(Common::SeekableReadStream *stream) : _stream(stream), _endOfData(true), _rate(0), - _processedSize(0), _totalSize(0), _length(0, 1), _bytesLeft(0), _outBuffer(0), - _outBufferOffset(0), _outBufferSize(0), _inBuffer(0), _inBufferSize(0) { - - _rate = _stream->readUint16LE(); - _totalSize = _stream->readUint32LE(); - - // TODO?: add checks - int flags = _stream->readByte(); // flags - int type = _stream->readByte(); // type - - _streamStart = stream->pos(); - - debugC(5, kDebugLevelSound, "AUD Info: rate: %d, totalSize: %d, flags: %d, type: %d, streamStart: %d", _rate, _totalSize, flags, type, _streamStart); - - _length = Audio::Timestamp(0, _rate); - for (uint32 i = 0; i < _totalSize;) { - uint16 size = _stream->readUint16LE(); - uint16 outSize = _stream->readUint16LE(); - - _length = _length.addFrames(outSize); - stream->seek(size + 4, SEEK_CUR); - i += size + 8; - } - - stream->seek(_streamStart, SEEK_SET); - - if (type == 1 && !flags) - _endOfData = false; - else - warning("No AUD file (rate: %d, size: %d, flags: 0x%X, type: %d)", _rate, _totalSize, flags, type); -} - -AUDStream::~AUDStream() { - delete[] _outBuffer; - delete[] _inBuffer; - delete _stream; -} - -int AUDStream::readBuffer(int16 *buffer, const int numSamples) { - int samplesRead = 0, samplesLeft = numSamples; - - while (samplesLeft > 0 && !_endOfData) { - int samples = readChunk(buffer, samplesLeft); - samplesRead += samples; - samplesLeft -= samples; - buffer += samples; - } - - return samplesRead; -} - -inline int16 clip8BitSample(int16 sample) { - return CLIP<int16>(sample, 0, 255); -} - -int AUDStream::readChunk(int16 *buffer, const int maxSamples) { - int samplesProcessed = 0; - - // if no bytes of the old chunk are left, read the next one - if (_bytesLeft <= 0) { - if (_processedSize >= _totalSize) { - _endOfData = true; - return 0; - } - - uint16 size = _stream->readUint16LE(); - uint16 outSize = _stream->readUint16LE(); - uint32 id = _stream->readUint32LE(); - - assert(id == 0x0000DEAF); - - _processedSize += 8 + size; - - _outBufferOffset = 0; - if (size == outSize) { - if (outSize > _outBufferSize) { - _outBufferSize = outSize; - delete[] _outBuffer; - _outBuffer = new uint8[_outBufferSize]; - assert(_outBuffer); - } - - _bytesLeft = size; - - _stream->read(_outBuffer, _bytesLeft); - } else { - _bytesLeft = outSize; - - if (outSize > _outBufferSize) { - _outBufferSize = outSize; - delete[] _outBuffer; - _outBuffer = new uint8[_outBufferSize]; - assert(_outBuffer); - } - - if (size > _inBufferSize) { - _inBufferSize = size; - delete[] _inBuffer; - _inBuffer = new uint8[_inBufferSize]; - assert(_inBuffer); - } - - if (_stream->read(_inBuffer, size) != size) { - _endOfData = true; - return 0; - } - - int16 curSample = 0x80; - byte code = 0; - int8 count = 0; - uint16 input = 0; - int j = 0; - int i = 0; - - while (outSize > 0) { - input = _inBuffer[i++] << 2; - code = (input >> 8) & 0xFF; - count = (input & 0xFF) >> 2; - - switch (code) { - case 2: - if (count & 0x20) { - /* NOTE: count is signed! */ - count <<= 3; - curSample += (count >> 3); - _outBuffer[j++] = curSample & 0xFF; - outSize--; - } else { - for (; count >= 0; count--) { - _outBuffer[j++] = _inBuffer[i++]; - outSize--; - } - curSample = _inBuffer[i - 1]; - } - break; - case 1: - for (; count >= 0; count--) { - code = _inBuffer[i++]; - - curSample += WSTable4Bit[code & 0x0F]; - curSample = clip8BitSample(curSample); - _outBuffer[j++] = curSample; - - curSample += WSTable4Bit[code >> 4]; - curSample = clip8BitSample(curSample); - _outBuffer[j++] = curSample; - - outSize -= 2; - } - break; - case 0: - for (; count >= 0; count--) { - code = (uint8)_inBuffer[i++]; - - curSample += WSTable2Bit[code & 0x03]; - curSample = clip8BitSample(curSample); - _outBuffer[j++] = curSample & 0xFF; - - curSample += WSTable2Bit[(code >> 2) & 0x03]; - curSample = clip8BitSample(curSample); - _outBuffer[j++] = curSample & 0xFF; - - curSample += WSTable2Bit[(code >> 4) & 0x03]; - curSample = clip8BitSample(curSample); - _outBuffer[j++] = curSample & 0xFF; - - curSample += WSTable2Bit[(code >> 6) & 0x03]; - curSample = clip8BitSample(curSample); - _outBuffer[j++] = curSample & 0xFF; - - outSize -= 4; - } - break; - default: - for (; count >= 0; count--) { - _outBuffer[j++] = curSample & 0xFF; - outSize--; - } - } - } - } - } - - // copies the chunk data to the output buffer - if (_bytesLeft > 0) { - int samples = MIN(_bytesLeft, maxSamples); - samplesProcessed += samples; - _bytesLeft -= samples; - - while (samples--) { - int16 sample = (_outBuffer[_outBufferOffset++] << 8) ^ 0x8000; - - *buffer++ = sample; - } - } - - return samplesProcessed; -} - -bool AUDStream::seek(const Audio::Timestamp &where) { - const uint32 seekSample = Audio::convertTimeToStreamPos(where, getRate(), isStereo()).totalNumberOfFrames(); - - _stream->seek(_streamStart); - _processedSize = 0; - _bytesLeft = 0; - _endOfData = false; - - uint32 curSample = 0; - - while (!endOfData()) { - uint16 size = _stream->readUint16LE(); - uint16 outSize = _stream->readUint16LE(); - - if (curSample + outSize > seekSample) { - _stream->seek(-4, SEEK_CUR); - - uint32 samples = seekSample - curSample; - int16 *temp = new int16[samples]; - assert(temp); - - readChunk(temp, samples); - delete[] temp; - curSample += samples; - break; - } else { - curSample += outSize; - _processedSize += 8 + size; - _stream->seek(size + 4, SEEK_CUR); - } - } - - _endOfData = (_processedSize >= _totalSize); - - return (curSample == seekSample); -} - -#pragma mark - - SoundDigital::SoundDigital(KyraEngine_MR *vm, Audio::Mixer *mixer) : _vm(vm), _mixer(mixer) { for (uint i = 0; i < ARRAYSIZE(_sounds); ++i) _sounds[i].stream = 0; @@ -518,13 +229,7 @@ void SoundDigital::beginFadeOut(int channel, int ticks) { // static res -namespace { - -Audio::SeekableAudioStream *makeAUDStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse) { - return new AUDStream(stream); -} - -} // end of anonymous namespace +Audio::SeekableAudioStream *makeAUDStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse); const SoundDigital::AudioCodecs SoundDigital::_supportedCodecs[] = { #ifdef USE_FLAC @@ -540,5 +245,4 @@ const SoundDigital::AudioCodecs SoundDigital::_supportedCodecs[] = { { 0, 0 } }; - } // End of namespace Kyra |