2010-01-19 00:54:06 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
2021-12-26 17:47:58 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-02-18 01:34:17 +00:00
|
|
|
*
|
2010-01-19 00:54:06 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-02-18 01:34:17 +00:00
|
|
|
*
|
2010-01-19 00:54:06 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-12-26 17:47:58 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2010-01-19 00:54:06 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common/endian.h"
|
2010-11-19 17:03:07 +00:00
|
|
|
#include "common/memstream.h"
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
|
|
|
#include "common/util.h"
|
2010-01-19 00:54:06 +00:00
|
|
|
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/audiostream.h"
|
|
|
|
#include "audio/decoders/raw.h"
|
2010-01-19 00:54:06 +00:00
|
|
|
|
|
|
|
namespace Audio {
|
|
|
|
|
|
|
|
#pragma mark -
|
2010-02-03 09:19:16 +00:00
|
|
|
#pragma mark --- RawStream ---
|
2010-01-19 00:54:06 +00:00
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
/**
|
2010-01-30 15:29:48 +00:00
|
|
|
* This is a stream, which allows for playing raw PCM data from a stream.
|
2010-01-19 00:54:06 +00:00
|
|
|
*/
|
2021-04-11 01:23:38 +00:00
|
|
|
template<int bytesPerSample, bool isUnsigned, bool isLE>
|
2010-02-03 09:19:16 +00:00
|
|
|
class RawStream : public SeekableAudioStream {
|
2010-01-19 00:54:06 +00:00
|
|
|
public:
|
2011-11-06 13:19:17 +00:00
|
|
|
RawStream(int rate, bool stereo, DisposeAfterUse::Flag disposeStream, Common::SeekableReadStream *stream)
|
2021-11-13 19:23:55 +00:00
|
|
|
: _rate(rate), _isStereo(stereo), _playtime(0, rate), _stream(stream, disposeStream), _endOfData(false), _buffer(nullptr) {
|
2010-05-03 18:27:45 +00:00
|
|
|
// Setup our buffer for readBuffer
|
2021-04-11 01:23:38 +00:00
|
|
|
_buffer = new byte[kSampleBufferLength * bytesPerSample];
|
2010-05-03 18:27:45 +00:00
|
|
|
assert(_buffer);
|
2010-01-19 00:54:06 +00:00
|
|
|
|
2011-11-06 13:19:17 +00:00
|
|
|
// Calculate the total playtime of the stream
|
2021-04-11 01:23:38 +00:00
|
|
|
_playtime = Timestamp(0, _stream->size() / (_isStereo ? 2 : 1) / bytesPerSample, rate);
|
2010-01-19 00:54:06 +00:00
|
|
|
}
|
|
|
|
|
2010-05-03 18:27:45 +00:00
|
|
|
~RawStream() {
|
|
|
|
delete[] _buffer;
|
2010-01-19 00:54:06 +00:00
|
|
|
}
|
2010-01-30 15:16:55 +00:00
|
|
|
|
2021-11-13 20:26:42 +00:00
|
|
|
int readBuffer(int16 *buffer, const int numSamples) override;
|
2010-01-19 00:54:06 +00:00
|
|
|
|
2021-11-13 20:26:42 +00:00
|
|
|
bool isStereo() const override { return _isStereo; }
|
|
|
|
bool endOfData() const override { return _endOfData; }
|
2010-01-19 00:54:06 +00:00
|
|
|
|
2021-11-13 20:26:42 +00:00
|
|
|
int getRate() const override { return _rate; }
|
|
|
|
Timestamp getLength() const override { return _playtime; }
|
2010-01-19 00:54:06 +00:00
|
|
|
|
2021-11-13 20:26:42 +00:00
|
|
|
bool seek(const Timestamp &where) override;
|
2010-05-03 18:27:45 +00:00
|
|
|
private:
|
2011-08-06 14:30:52 +00:00
|
|
|
const int _rate; ///< Sample rate of stream
|
2023-03-09 16:37:11 +00:00
|
|
|
const bool _isStereo; ///< Whether this is a stereo stream
|
2011-08-06 14:30:52 +00:00
|
|
|
Timestamp _playtime; ///< Calculated total play time
|
|
|
|
Common::DisposablePtr<Common::SeekableReadStream> _stream; ///< Stream to read data from
|
2011-11-06 13:19:17 +00:00
|
|
|
bool _endOfData; ///< Whether the stream end has been reached
|
2010-05-03 18:27:45 +00:00
|
|
|
|
2011-11-06 13:19:17 +00:00
|
|
|
byte *_buffer; ///< Buffer used in readBuffer
|
2010-05-03 18:27:45 +00:00
|
|
|
enum {
|
|
|
|
/**
|
|
|
|
* How many samples we can buffer at once.
|
|
|
|
*
|
|
|
|
* TODO: Check whether this size suffices
|
|
|
|
* for systems with slow disk I/O.
|
|
|
|
*/
|
|
|
|
kSampleBufferLength = 2048
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fill the temporary sample buffer used in readBuffer.
|
|
|
|
*
|
|
|
|
* @param maxSamples Maximum samples to read.
|
|
|
|
* @return actual count of samples read.
|
|
|
|
*/
|
|
|
|
int fillBuffer(int maxSamples);
|
2010-01-19 00:54:06 +00:00
|
|
|
};
|
|
|
|
|
2021-04-11 01:23:38 +00:00
|
|
|
template<int bytesPerSample, bool isUnsigned, bool isLE>
|
|
|
|
int RawStream<bytesPerSample, isUnsigned, isLE>::readBuffer(int16 *buffer, const int numSamples) {
|
2010-05-03 18:27:45 +00:00
|
|
|
int samplesLeft = numSamples;
|
|
|
|
|
|
|
|
while (samplesLeft > 0) {
|
|
|
|
// Try to read up to "samplesLeft" samples.
|
|
|
|
int len = fillBuffer(samplesLeft);
|
|
|
|
|
|
|
|
// In case we were not able to read any samples
|
2010-05-03 18:32:26 +00:00
|
|
|
// we will stop reading here.
|
|
|
|
if (!len)
|
2010-05-03 18:27:45 +00:00
|
|
|
break;
|
2010-01-19 00:54:06 +00:00
|
|
|
|
2010-05-03 18:27:45 +00:00
|
|
|
// Adjust the samples left to read.
|
|
|
|
samplesLeft -= len;
|
2010-01-19 00:54:06 +00:00
|
|
|
|
2010-05-03 18:27:45 +00:00
|
|
|
// Copy the data to the caller's buffer.
|
|
|
|
const byte *src = _buffer;
|
|
|
|
while (len-- > 0) {
|
2021-04-11 11:31:30 +00:00
|
|
|
if (bytesPerSample == 1)
|
|
|
|
*buffer++ = (*src << 8) ^ (isUnsigned ? 0x8000 : 0);
|
|
|
|
else if (bytesPerSample == 2)
|
|
|
|
*buffer++ = ((isLE ? READ_LE_UINT16(src) : READ_BE_UINT16(src)) ^ (isUnsigned ? 0x8000 : 0));
|
|
|
|
else // if (bytesPerSample == 3)
|
|
|
|
*buffer++ = (((int16)((isLE ? READ_LE_UINT24(src) : READ_BE_UINT24(src)) >> 8)) ^ (isUnsigned ? 0x8000 : 0));
|
|
|
|
|
2021-04-11 01:23:38 +00:00
|
|
|
src += bytesPerSample;
|
2010-01-19 00:54:06 +00:00
|
|
|
}
|
2010-05-03 18:27:45 +00:00
|
|
|
}
|
2010-01-19 00:54:06 +00:00
|
|
|
|
2010-05-03 18:27:45 +00:00
|
|
|
return numSamples - samplesLeft;
|
|
|
|
}
|
2010-01-25 01:39:44 +00:00
|
|
|
|
2021-04-11 01:23:38 +00:00
|
|
|
template<int bytesPerSample, bool isUnsigned, bool isLE>
|
|
|
|
int RawStream<bytesPerSample, isUnsigned, isLE>::fillBuffer(int maxSamples) {
|
2010-05-03 18:27:45 +00:00
|
|
|
int bufferedSamples = 0;
|
|
|
|
byte *dst = _buffer;
|
|
|
|
|
|
|
|
// We can only read up to "kSampleBufferLength" samples
|
|
|
|
// so we take this into consideration, when trying to
|
|
|
|
// read up to maxSamples.
|
|
|
|
maxSamples = MIN<int>(kSampleBufferLength, maxSamples);
|
|
|
|
|
|
|
|
// We will only read up to maxSamples
|
|
|
|
while (maxSamples > 0 && !endOfData()) {
|
|
|
|
// Try to read all the sample data and update the
|
|
|
|
// destination pointer.
|
2021-04-11 01:23:38 +00:00
|
|
|
const int bytesRead = _stream->read(dst, maxSamples * bytesPerSample);
|
2010-05-03 18:27:45 +00:00
|
|
|
dst += bytesRead;
|
|
|
|
|
|
|
|
// Calculate how many samples we actually read.
|
2021-04-11 01:23:38 +00:00
|
|
|
const int samplesRead = bytesRead / bytesPerSample;
|
2010-05-03 18:27:45 +00:00
|
|
|
|
|
|
|
// Update all status variables
|
|
|
|
bufferedSamples += samplesRead;
|
|
|
|
maxSamples -= samplesRead;
|
|
|
|
|
2011-11-06 13:19:17 +00:00
|
|
|
// We stop stream playback, when we reached the end of the data stream.
|
|
|
|
// We also stop playback when an error occures.
|
|
|
|
if (_stream->pos() == _stream->size() || _stream->err() || _stream->eos())
|
|
|
|
_endOfData = true;
|
2010-01-19 00:54:06 +00:00
|
|
|
}
|
|
|
|
|
2010-05-03 18:27:45 +00:00
|
|
|
return bufferedSamples;
|
|
|
|
}
|
|
|
|
|
2021-04-11 01:23:38 +00:00
|
|
|
template<int bytesPerSample, bool isUnsigned, bool isLE>
|
|
|
|
bool RawStream<bytesPerSample, isUnsigned, isLE>::seek(const Timestamp &where) {
|
2011-11-06 13:19:17 +00:00
|
|
|
_endOfData = true;
|
2010-02-08 21:24:47 +00:00
|
|
|
|
|
|
|
if (where > _playtime)
|
|
|
|
return false;
|
|
|
|
|
2010-01-27 08:08:33 +00:00
|
|
|
const uint32 seekSample = convertTimeToStreamPos(where, getRate(), isStereo()).totalNumberOfFrames();
|
2021-04-11 01:23:38 +00:00
|
|
|
_stream->seek(seekSample * bytesPerSample, SEEK_SET);
|
2010-01-19 00:54:06 +00:00
|
|
|
|
2011-11-06 13:19:17 +00:00
|
|
|
// In case of an error we will not continue stream playback.
|
|
|
|
if (!_stream->err() && !_stream->eos() && _stream->pos() != _stream->size())
|
|
|
|
_endOfData = false;
|
2010-01-19 00:54:06 +00:00
|
|
|
|
2011-11-06 13:19:17 +00:00
|
|
|
return true;
|
2010-01-19 00:54:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark --- Raw stream factories ---
|
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
/* In the following, we use preprocessor / macro tricks to simplify the code
|
|
|
|
* which instantiates the input streams. We used to use template functions for
|
|
|
|
* this, but MSVC6 / EVC 3-4 (used for WinCE builds) are extremely buggy when it
|
|
|
|
* comes to this feature of C++... so as a compromise we use macros to cut down
|
|
|
|
* on the (source) code duplication a bit.
|
|
|
|
* So while normally macro tricks are said to make maintenance harder, in this
|
|
|
|
* particular case it should actually help it :-)
|
|
|
|
*/
|
|
|
|
|
2010-04-03 15:57:04 +00:00
|
|
|
#define MAKE_RAW_STREAM(UNSIGNED) \
|
2021-04-11 01:23:38 +00:00
|
|
|
if (bytesPerSample == 3) { \
|
2010-01-19 00:54:06 +00:00
|
|
|
if (isLE) \
|
2021-04-11 01:23:38 +00:00
|
|
|
return new RawStream<3, UNSIGNED, true>(rate, isStereo, disposeAfterUse, stream); \
|
2010-01-19 00:54:06 +00:00
|
|
|
else \
|
2021-04-11 01:23:38 +00:00
|
|
|
return new RawStream<3, UNSIGNED, false>(rate, isStereo, disposeAfterUse, stream); \
|
|
|
|
} else if (bytesPerSample == 2) { \
|
|
|
|
if (isLE) \
|
|
|
|
return new RawStream<2, UNSIGNED, true>(rate, isStereo, disposeAfterUse, stream); \
|
|
|
|
else \
|
|
|
|
return new RawStream<2, UNSIGNED, false>(rate, isStereo, disposeAfterUse, stream); \
|
2010-01-19 00:54:06 +00:00
|
|
|
} else \
|
2021-04-11 01:23:38 +00:00
|
|
|
return new RawStream<1, UNSIGNED, false>(rate, isStereo, disposeAfterUse, stream)
|
2010-01-19 00:54:06 +00:00
|
|
|
|
2010-01-30 15:17:57 +00:00
|
|
|
SeekableAudioStream *makeRawStream(Common::SeekableReadStream *stream,
|
2021-04-15 19:20:04 +00:00
|
|
|
int rate, byte flags,
|
|
|
|
DisposeAfterUse::Flag disposeAfterUse) {
|
2021-04-11 01:23:38 +00:00
|
|
|
const bool isStereo = (flags & Audio::FLAG_STEREO) != 0;
|
|
|
|
const int bytesPerSample = (flags & Audio::FLAG_24BITS ? 3 : (flags & Audio::FLAG_16BITS ? 2 : 1));
|
|
|
|
const bool isUnsigned = (flags & Audio::FLAG_UNSIGNED) != 0;
|
|
|
|
const bool isLE = (flags & Audio::FLAG_LITTLE_ENDIAN) != 0;
|
2010-01-19 00:54:06 +00:00
|
|
|
|
2021-04-11 01:23:38 +00:00
|
|
|
assert(stream->size() % (bytesPerSample * (isStereo ? 2 : 1)) == 0);
|
2010-01-30 15:16:55 +00:00
|
|
|
|
2010-04-03 15:57:04 +00:00
|
|
|
if (isUnsigned) {
|
|
|
|
MAKE_RAW_STREAM(true);
|
2010-01-19 00:54:06 +00:00
|
|
|
} else {
|
2010-04-03 15:57:04 +00:00
|
|
|
MAKE_RAW_STREAM(false);
|
2010-01-19 00:54:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-30 15:18:25 +00:00
|
|
|
SeekableAudioStream *makeRawStream(const byte *buffer, uint32 size,
|
2021-04-15 19:20:04 +00:00
|
|
|
int rate, byte flags,
|
|
|
|
DisposeAfterUse::Flag disposeAfterUse) {
|
2010-01-30 15:18:25 +00:00
|
|
|
return makeRawStream(new Common::MemoryReadStream(buffer, size, disposeAfterUse), rate, flags, DisposeAfterUse::YES);
|
|
|
|
}
|
|
|
|
|
2015-08-12 02:33:54 +00:00
|
|
|
class PacketizedRawStream : public StatelessPacketizedAudioStream {
|
|
|
|
public:
|
|
|
|
PacketizedRawStream(int rate, byte flags) :
|
|
|
|
StatelessPacketizedAudioStream(rate, ((flags & FLAG_STEREO) != 0) ? 2 : 1), _flags(flags) {}
|
|
|
|
|
|
|
|
protected:
|
2021-11-13 20:26:42 +00:00
|
|
|
AudioStream *makeStream(Common::SeekableReadStream *data) override;
|
2015-08-12 02:33:54 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
byte _flags;
|
|
|
|
};
|
|
|
|
|
|
|
|
AudioStream *PacketizedRawStream::makeStream(Common::SeekableReadStream *data) {
|
|
|
|
return makeRawStream(data, getRate(), _flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
PacketizedAudioStream *makePacketizedRawStream(int rate, byte flags) {
|
|
|
|
return new PacketizedRawStream(rate, flags);
|
|
|
|
}
|
|
|
|
|
2010-01-19 00:54:06 +00:00
|
|
|
} // End of namespace Audio
|