diff options
-rw-r--r-- | audio/decoders/raw.cpp | 163 | ||||
-rw-r--r-- | audio/decoders/raw.h | 34 | ||||
-rw-r--r-- | test/audio/helper.h | 57 | ||||
-rw-r--r-- | test/audio/raw.h | 120 |
4 files changed, 57 insertions, 317 deletions
diff --git a/audio/decoders/raw.cpp b/audio/decoders/raw.cpp index 9a68401fdf..456d09cb1b 100644 --- a/audio/decoders/raw.cpp +++ b/audio/decoders/raw.cpp @@ -50,35 +50,14 @@ namespace Audio { 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, disposeStream), _blocks(blocks), _curBlock(_blocks.begin()), _blockLeft(0), _buffer(0) { - - assert(_blocks.size() > 0); - + RawStream(int rate, bool stereo, DisposeAfterUse::Flag disposeStream, Common::SeekableReadStream *stream) + : _rate(rate), _isStereo(stereo), _playtime(0, rate), _stream(stream, disposeStream), _endOfData(false), _buffer(0) { // Setup our buffer for readBuffer _buffer = new byte[kSampleBufferLength * (is16Bit ? 2 : 1)]; assert(_buffer); - // Set current buffer state, playing first block - _stream->seek(_curBlock->pos, SEEK_SET); - - // In case of an error we will stop (or rather - // not start) stream playback. - if (_stream->err()) { - _blockLeft = 0; - _curBlock = _blocks.end(); - } else { - _blockLeft = _curBlock->len; - } - - // Add up length of all blocks in order to caluclate total play time - int32 len = 0; - for (RawStreamBlockList::const_iterator i = _blocks.begin(); i != _blocks.end(); ++i) { - assert(i->len % (_isStereo ? 2 : 1) == 0); - len += i->len; - } - - _playtime = Timestamp(0, len / (_isStereo ? 2 : 1), rate); + // Calculate the total playtime of the stream + _playtime = Timestamp(0, _stream->size() / (_isStereo ? 2 : 1) / (is16Bit ? 2 : 1), rate); } ~RawStream() { @@ -87,8 +66,8 @@ public: int readBuffer(int16 *buffer, const int numSamples); - bool isStereo() const { return _isStereo; } - bool endOfData() const { return (_curBlock == _blocks.end()) && (_blockLeft == 0); } + bool isStereo() const { return _isStereo; } + bool endOfData() const { return _endOfData; } int getRate() const { return _rate; } Timestamp getLength() const { return _playtime; } @@ -99,18 +78,9 @@ private: 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 - * the current one is empty. - */ - void updateBlockIfNeeded(); + bool _endOfData; ///< Whether the stream end has been reached - byte *_buffer; ///< Buffer used in readBuffer + byte *_buffer; ///< Buffer used in readBuffer enum { /** * How many samples we can buffer at once. @@ -169,13 +139,9 @@ int RawStream<is16Bit, isUnsigned, isLE>::fillBuffer(int maxSamples) { // We will only read up to maxSamples while (maxSamples > 0 && !endOfData()) { - // Calculate how many samples we can safely read - // from the current block. - const int len = MIN<int>(maxSamples, _blockLeft); - // Try to read all the sample data and update the // destination pointer. - const int bytesRead = _stream->read(dst, len * (is16Bit ? 2 : 1)); + const int bytesRead = _stream->read(dst, maxSamples * (is16Bit ? 2 : 1)); dst += bytesRead; // Calculate how many samples we actually read. @@ -184,87 +150,31 @@ int RawStream<is16Bit, isUnsigned, isLE>::fillBuffer(int maxSamples) { // Update all status variables bufferedSamples += samplesRead; maxSamples -= samplesRead; - _blockLeft -= samplesRead; - - // In case of an error we will stop - // stream playback. - if (_stream->err()) { - _blockLeft = 0; - _curBlock = _blocks.end(); - } - // Advance to the next block in case the current - // one is already finished. - updateBlockIfNeeded(); + // 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; } return bufferedSamples; } template<bool is16Bit, bool isUnsigned, bool isLE> -void RawStream<is16Bit, isUnsigned, isLE>::updateBlockIfNeeded() { - // Have we now finished this block? If so, read the next block - if (_blockLeft == 0 && _curBlock != _blocks.end()) { - // Next block - ++_curBlock; - - // Check whether we reached the end of the stream - // yet. In case we did not do this, we will just - // setup the next block as new block. - if (_curBlock != _blocks.end()) { - _stream->seek(_curBlock->pos, SEEK_SET); - - // In case of an error we will stop - // stream playback. - if (_stream->err()) { - _blockLeft = 0; - _curBlock = _blocks.end(); - } else { - _blockLeft = _curBlock->len; - } - } - } -} - -template<bool is16Bit, bool isUnsigned, bool isLE> bool RawStream<is16Bit, isUnsigned, isLE>::seek(const Timestamp &where) { - _blockLeft = 0; - _curBlock = _blocks.end(); + _endOfData = true; if (where > _playtime) return false; const uint32 seekSample = convertTimeToStreamPos(where, getRate(), isStereo()).totalNumberOfFrames(); - uint32 curSample = 0; - - // Search for the disk block in which the specific sample is placed - for (_curBlock = _blocks.begin(); _curBlock != _blocks.end(); ++_curBlock) { - uint32 nextBlockSample = curSample + _curBlock->len; - - if (nextBlockSample > seekSample) - break; + _stream->seek(seekSample * (is16Bit ? 2 : 1), SEEK_SET); - curSample = nextBlockSample; - } - - if (_curBlock == _blocks.end()) { - return ((seekSample - curSample) == 0); - } else { - const uint32 offset = seekSample - curSample; - - _stream->seek(_curBlock->pos + offset * (is16Bit ? 2 : 1), SEEK_SET); - - // In case of an error we will stop - // stream playback. - if (_stream->err()) { - _blockLeft = 0; - _curBlock = _blocks.end(); - } else { - _blockLeft = _curBlock->len - offset; - } + // In case of an error we will not continue stream playback. + if (!_stream->err() && !_stream->eos() && _stream->pos() != _stream->size()) + _endOfData = false; - return true; - } + return true; } #pragma mark - @@ -283,28 +193,21 @@ bool RawStream<is16Bit, isUnsigned, isLE>::seek(const Timestamp &where) { #define MAKE_RAW_STREAM(UNSIGNED) \ if (is16Bit) { \ if (isLE) \ - return new RawStream<true, UNSIGNED, true>(rate, isStereo, disposeAfterUse, stream, blockList); \ + return new RawStream<true, UNSIGNED, true>(rate, isStereo, disposeAfterUse, stream); \ else \ - return new RawStream<true, UNSIGNED, false>(rate, isStereo, disposeAfterUse, stream, blockList); \ + return new RawStream<true, UNSIGNED, false>(rate, isStereo, disposeAfterUse, stream); \ } else \ - return new RawStream<false, UNSIGNED, false>(rate, isStereo, disposeAfterUse, stream, blockList) + return new RawStream<false, UNSIGNED, false>(rate, isStereo, disposeAfterUse, stream) SeekableAudioStream *makeRawStream(Common::SeekableReadStream *stream, - const RawStreamBlockList &blockList, - int rate, - byte flags, + int rate, byte flags, DisposeAfterUse::Flag disposeAfterUse) { const bool isStereo = (flags & Audio::FLAG_STEREO) != 0; const bool is16Bit = (flags & Audio::FLAG_16BITS) != 0; const bool isUnsigned = (flags & Audio::FLAG_UNSIGNED) != 0; const bool isLE = (flags & Audio::FLAG_LITTLE_ENDIAN) != 0; - if (blockList.empty()) { - warning("Empty block list passed to makeRawStream"); - if (disposeAfterUse == DisposeAfterUse::YES) - delete stream; - return 0; - } + assert(stream->size() % ((is16Bit ? 2 : 1) * (isStereo ? 2 : 1)) == 0); if (isUnsigned) { MAKE_RAW_STREAM(true); @@ -313,24 +216,6 @@ SeekableAudioStream *makeRawStream(Common::SeekableReadStream *stream, } } -SeekableAudioStream *makeRawStream(Common::SeekableReadStream *stream, - int rate, byte flags, - DisposeAfterUse::Flag disposeAfterUse) { - RawStreamBlockList blocks; - RawStreamBlock block; - block.pos = 0; - - const bool isStereo = (flags & Audio::FLAG_STEREO) != 0; - const bool is16Bit = (flags & Audio::FLAG_16BITS) != 0; - - assert(stream->size() % ((is16Bit ? 2 : 1) * (isStereo ? 2 : 1)) == 0); - - block.len = stream->size() / (is16Bit ? 2 : 1); - blocks.push_back(block); - - return makeRawStream(stream, blocks, rate, flags, disposeAfterUse); -} - SeekableAudioStream *makeRawStream(const byte *buffer, uint32 size, int rate, byte flags, DisposeAfterUse::Flag disposeAfterUse) { diff --git a/audio/decoders/raw.h b/audio/decoders/raw.h index 9d8884fa04..a06bebee10 100644 --- a/audio/decoders/raw.h +++ b/audio/decoders/raw.h @@ -60,21 +60,6 @@ enum RawFlags { FLAG_STEREO = 1 << 3 }; - -/** - * Struct used to define the audio data to be played by a RawStream. - */ -struct RawStreamBlock { - int32 pos; ///< Position in stream of the block (in bytes of course!) - int32 len; ///< Length of the block (in raw samples, not sample pairs!) -}; - -/** - * List containing all blocks of a raw stream. - * @see RawStreamBlock - */ -typedef Common::List<RawStreamBlock> RawStreamBlockList; - /** * Creates an audio stream, which plays from the given buffer. * @@ -104,25 +89,6 @@ SeekableAudioStream *makeRawStream(Common::SeekableReadStream *stream, int rate, byte flags, DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::YES); -/** - * Creates an audio stream, which plays from the given stream. - * - * @param stream Stream object to play from. - * @param blockList List of blocks to play. - * @see RawDiskStreamAudioBlock - * @see RawStreamBlockList - * @param rate Rate of the sound data. - * @param flags Audio flags combination. - * @see RawFlags - * @param disposeAfterUse Whether to delete the stream after use. - * @return The new SeekableAudioStream (or 0 on failure). - */ -SeekableAudioStream *makeRawStream(Common::SeekableReadStream *stream, - const RawStreamBlockList &blockList, - int rate, - byte flags, - DisposeAfterUse::Flag disposeAfterUse = DisposeAfterUse::YES); - } // End of namespace Audio #endif diff --git a/test/audio/helper.h b/test/audio/helper.h index 5080c79eef..77dc63b619 100644 --- a/test/audio/helper.h +++ b/test/audio/helper.h @@ -24,40 +24,7 @@ static T *createSine(const int sampleRate, const int time) { } template<typename T> -static Common::SeekableReadStream *createPartitionStream(T *sine, const int samples, Audio::RawStreamBlockList &blockList) { - const int block1Len = samples / 2; - const int block1Size = block1Len * sizeof(T); - const int block2Len = samples - block1Len; - const int block2Size = block2Len * sizeof(T); - - const int bufferLen = samples * 2; - const int bufferSize = bufferLen * sizeof(T); - T *partition = (T *)calloc(1, bufferSize); - - Audio::RawStreamBlock block; - - // The will layout the buffer like the following: - // [Zero], [Part2], [Zero], [Part1] - - // The first part of the stream is at the end of the memory buffer - block.pos = bufferSize - block1Size; - block.len = block1Len; - memcpy(partition + bufferLen - block1Len, sine, block1Size); - blockList.push_back(block); - - // The second part of the stream is near the beginning of the memory buffer - block.pos = block2Size; - block.len = block2Len; - memcpy(partition + block2Len, sine + block1Len, block2Size); - blockList.push_back(block); - - free(sine); - - return new Common::MemoryReadStream((const byte *)partition, bufferSize, DisposeAfterUse::YES); -} - -template<typename T> -static Audio::SeekableAudioStream *createSineStream(const int sampleRate, const int time, int16 **comp, bool le, bool isStereo, bool makePartition = false) { +static Audio::SeekableAudioStream *createSineStream(const int sampleRate, const int time, int16 **comp, bool le, bool isStereo) { T *sine = createSine<T>(sampleRate, time * (isStereo ? 2 : 1)); const bool isUnsigned = !std::numeric_limits<T>::is_signed; @@ -88,22 +55,12 @@ static Audio::SeekableAudioStream *createSineStream(const int sampleRate, const } Audio::SeekableAudioStream *s = 0; - if (makePartition) { - Audio::RawStreamBlockList blockList; - Common::SeekableReadStream *sD = createPartitionStream<T>(sine, samples, blockList); - s = Audio::makeRawStream(sD, blockList, sampleRate, - (is16Bits ? Audio::FLAG_16BITS : 0) - | (isUnsigned ? Audio::FLAG_UNSIGNED : 0) - | (le ? Audio::FLAG_LITTLE_ENDIAN : 0) - | (isStereo ? Audio::FLAG_STEREO : 0)); - } else { - Common::SeekableReadStream *sD = new Common::MemoryReadStream((const byte *)sine, sizeof(T) * samples, DisposeAfterUse::YES); - s = Audio::makeRawStream(sD, sampleRate, - (is16Bits ? Audio::FLAG_16BITS : 0) - | (isUnsigned ? Audio::FLAG_UNSIGNED : 0) - | (le ? Audio::FLAG_LITTLE_ENDIAN : 0) - | (isStereo ? Audio::FLAG_STEREO : 0)); - } + Common::SeekableReadStream *sD = new Common::MemoryReadStream((const byte *)sine, sizeof(T) * samples, DisposeAfterUse::YES); + s = Audio::makeRawStream(sD, sampleRate, + (is16Bits ? Audio::FLAG_16BITS : 0) + | (isUnsigned ? Audio::FLAG_UNSIGNED : 0) + | (le ? Audio::FLAG_LITTLE_ENDIAN : 0) + | (isStereo ? Audio::FLAG_STEREO : 0)); return s; } diff --git a/test/audio/raw.h b/test/audio/raw.h index 51ec067f7e..e7cb42ac44 100644 --- a/test/audio/raw.h +++ b/test/audio/raw.h @@ -8,9 +8,9 @@ class RawStreamTestSuite : public CxxTest::TestSuite { private: template<typename T> - void readBufferTestTemplate(const int sampleRate, const int time, const bool le, const bool isStereo, const bool makePartition = false) { + void readBufferTestTemplate(const int sampleRate, const int time, const bool le, const bool isStereo) { int16 *sine; - Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, &sine, le, isStereo, makePartition); + Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, &sine, le, isStereo); const int totalSamples = sampleRate * time * (isStereo ? 2 : 1); int16 *buffer = new int16[totalSamples]; @@ -68,57 +68,13 @@ public: readBufferTestTemplate<uint16>(11025, 2, true, true); } - void test_read_buffer_8_bit_signed_mono_parted() { - readBufferTestTemplate<int8>(11025, 2, false, false, true); - } - - void test_read_buffer_8_bit_signed_stereo_parted() { - readBufferTestTemplate<int8>(11025, 2, false, true, true); - } - - void test_read_buffer_8_bit_unsigned_mono_parted() { - readBufferTestTemplate<uint8>(11025, 2, false, false, true); - } - - void test_read_buffer_16_bit_signed_be_mono_parted() { - readBufferTestTemplate<int16>(11025, 2, false, false, true); - } - - void test_read_buffer_16_bit_signed_be_stereo_parted() { - readBufferTestTemplate<int16>(11025, 2, false, true, true); - } - - void test_read_buffer_16_bit_unsigned_be_mono_parted() { - readBufferTestTemplate<uint16>(11025, 2, false, false, true); - } - - void test_read_buffer_16_bit_unsigned_be_stereo_parted() { - readBufferTestTemplate<uint16>(11025, 2, false, true, true); - } - - void test_read_buffer_16_bit_signed_le_mono_parted() { - readBufferTestTemplate<int16>(11025, 2, true, false, true); - } - - void test_read_buffer_16_bit_signed_le_stereo_parted() { - readBufferTestTemplate<int16>(11025, 2, true, true, true); - } - - void test_read_buffer_16_bit_unsigned_le_mono_parted() { - readBufferTestTemplate<uint16>(11025, 2, true, false, true); - } - - void test_read_buffer_16_bit_unsigned_le_stereo_parted() { - readBufferTestTemplate<uint16>(11025, 2, true, true, true); - } - private: - void partialReadTest(const bool makePartition) { + void partialReadTest() { const int sampleRate = 11025; const int time = 4; int16 *sine; - Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, &sine, false, false, makePartition); + Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, &sine, false, false); int16 *buffer = new int16[sampleRate * time]; TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate), sampleRate); @@ -139,15 +95,11 @@ private: } public: void test_partial_read() { - partialReadTest(false); - } - - void test_partial_read_parted() { - partialReadTest(true); + partialReadTest(); } private: - void readAfterEndTest(const bool makePartition) { + void readAfterEndTest() { const int sampleRate = 11025; const int time = 1; Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, 0, false, false); @@ -165,18 +117,14 @@ private: public: void test_read_after_end() { - readAfterEndTest(false); - } - - void test_read_after_end_parted() { - readAfterEndTest(true); + readAfterEndTest(); } private: - void rewindTest(const bool makePartition) { + void rewindTest() { const int sampleRate = 11025; const int time = 2; - Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, 0, false, false, makePartition); + Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, 0, false, false); int16 *buffer = new int16[sampleRate * time]; TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time); @@ -193,15 +141,11 @@ private: } public: void test_rewind() { - rewindTest(false); - } - - void test_rewind_parted() { - rewindTest(true); + rewindTest(); } private: - void lengthTest(const bool makePartition) { + void lengthTest() { int sampleRate = 0; const int time = 4; @@ -209,66 +153,62 @@ private: // 11025 Hz tests sampleRate = 11025; - s = createSineStream<int8>(sampleRate, time, 0, false, false, makePartition); + s = createSineStream<int8>(sampleRate, time, 0, false, false); TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time); delete s; - s = createSineStream<uint16>(sampleRate, time, 0, false, false, makePartition); + s = createSineStream<uint16>(sampleRate, time, 0, false, false); TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time); delete s; // 48000 Hz tests sampleRate = 48000; - s = createSineStream<int8>(sampleRate, time, 0, false, false, makePartition); + s = createSineStream<int8>(sampleRate, time, 0, false, false); TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time); delete s; - s = createSineStream<uint16>(sampleRate, time, 0, true, false, makePartition); + s = createSineStream<uint16>(sampleRate, time, 0, true, false); TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time); delete s; // 11840 Hz tests sampleRate = 11840; - s = createSineStream<int8>(sampleRate, time, 0, false, false, makePartition); + s = createSineStream<int8>(sampleRate, time, 0, false, false); TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time); delete s; - s = createSineStream<uint16>(sampleRate, time, 0, false, false, makePartition); + s = createSineStream<uint16>(sampleRate, time, 0, false, false); TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time); delete s; // 11111 Hz tests sampleRate = 11111; - s = createSineStream<int8>(sampleRate, time, 0, false, false, makePartition); + s = createSineStream<int8>(sampleRate, time, 0, false, false); TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time); delete s; - s = createSineStream<uint16>(sampleRate, time, 0, false, false, makePartition); + s = createSineStream<uint16>(sampleRate, time, 0, false, false); TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time); delete s; // 22050 Hz stereo test sampleRate = 22050; - s = createSineStream<int8>(sampleRate, time, 0, false, true, makePartition); + s = createSineStream<int8>(sampleRate, time, 0, false, true); TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time); delete s; - s = createSineStream<uint16>(sampleRate, time, 0, true, true, makePartition); + s = createSineStream<uint16>(sampleRate, time, 0, true, true); TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time); delete s; } public: void test_length() { - lengthTest(false); - } - - void test_length_parted() { - lengthTest(true); + lengthTest(); } private: - void seekTest(const int sampleRate, const int time, const bool isStereo, const bool makePartition) { + void seekTest(const int sampleRate, const int time, const bool isStereo) { const int totalFrames = sampleRate * time * (isStereo ? 2 : 1); int readData = 0, offset = 0; @@ -276,7 +216,7 @@ private: Audio::SeekableAudioStream *s = 0; int16 *sine = 0; - s = createSineStream<int8>(sampleRate, time, &sine, false, isStereo, makePartition); + s = createSineStream<int8>(sampleRate, time, &sine, false, isStereo); // Seek to 500ms const Audio::Timestamp a(0, 1, 2); @@ -341,18 +281,10 @@ private: public: void test_seek_mono() { - seekTest(11025, 2, false, false); + seekTest(11025, 2, false); } void test_seek_stereo() { - seekTest(11025, 2, true, false); - } - - void test_seek_mono_parted() { - seekTest(11025, 2, false, true); - } - - void test_seek_stereo_parted() { - seekTest(11025, 2, true, true); + seekTest(11025, 2, true); } }; |