2010-02-03 10:36:14 +00:00
|
|
|
#include <cxxtest/TestSuite.h>
|
|
|
|
|
|
|
|
#include "sound/decoders/raw.h"
|
|
|
|
|
|
|
|
#include "common/stream.h"
|
|
|
|
#include "common/endian.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
class RawStreamTestSuite : public CxxTest::TestSuite
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template<typename T>
|
2010-02-03 15:25:06 +00:00
|
|
|
static T *createSine(const int sampleRate, const int time) {
|
2010-02-03 10:36:14 +00:00
|
|
|
T *sine = (T *)malloc(sizeof(T) * time * sampleRate);
|
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
const bool isUnsigned = !std::numeric_limits<T>::is_signed;
|
|
|
|
const T xorMask = isUnsigned ? (1 << (std::numeric_limits<T>::digits - 1)) : 0;
|
|
|
|
const T maxValue = std::numeric_limits<T>::max() ^ xorMask;
|
2010-02-03 10:36:14 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < time * sampleRate; ++i)
|
2010-02-03 15:25:06 +00:00
|
|
|
sine[i] = ((T)(sin((double)i / sampleRate * 2 * PI) * maxValue)) ^ xorMask;
|
2010-02-03 10:36:14 +00:00
|
|
|
|
|
|
|
return sine;
|
|
|
|
}
|
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
template<typename T>
|
2010-02-03 16:02:25 +00:00
|
|
|
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));
|
2010-02-03 15:25:06 +00:00
|
|
|
|
|
|
|
const bool isUnsigned = !std::numeric_limits<T>::is_signed;
|
|
|
|
const T xorMask = isUnsigned ? (1 << (std::numeric_limits<T>::digits - 1)) : 0;
|
|
|
|
const bool is16Bits = (sizeof(T) == 2);
|
|
|
|
assert(sizeof(T) == 2 || sizeof(T) == 1);
|
|
|
|
|
2010-02-03 16:02:25 +00:00
|
|
|
const int samples = sampleRate * time * (isStereo ? 2 : 1);
|
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
if (comp) {
|
2010-02-03 16:02:25 +00:00
|
|
|
*comp = new int16[samples];
|
|
|
|
for (int i = 0; i < samples; ++i) {
|
2010-02-03 15:25:06 +00:00
|
|
|
if (is16Bits)
|
|
|
|
(*comp)[i] = sine[i] ^ xorMask;
|
|
|
|
else
|
|
|
|
(*comp)[i] = (sine[i] ^ xorMask) << 8;
|
|
|
|
}
|
|
|
|
}
|
2010-02-03 15:24:24 +00:00
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
if (is16Bits) {
|
|
|
|
if (le) {
|
2010-02-03 16:02:25 +00:00
|
|
|
for (int i = 0; i < samples; ++i)
|
2010-02-03 15:25:06 +00:00
|
|
|
WRITE_LE_UINT16(&sine[i], sine[i]);
|
|
|
|
} else {
|
2010-02-03 16:02:25 +00:00
|
|
|
for (int i = 0; i < samples; ++i)
|
2010-02-03 15:25:06 +00:00
|
|
|
WRITE_BE_UINT16(&sine[i], sine[i]);
|
|
|
|
}
|
|
|
|
}
|
2010-02-03 15:24:24 +00:00
|
|
|
|
2010-02-03 16:02:25 +00:00
|
|
|
Common::SeekableReadStream *sD = new Common::MemoryReadStream((const byte *)sine, sizeof(T) * samples, DisposeAfterUse::YES);
|
2010-02-03 15:25:06 +00:00
|
|
|
Audio::SeekableAudioStream *s = Audio::makeRawStream(sD, sampleRate,
|
|
|
|
(is16Bits ? Audio::FLAG_16BITS : 0)
|
|
|
|
| (isUnsigned ? Audio::FLAG_UNSIGNED : 0)
|
2010-02-03 16:02:25 +00:00
|
|
|
| (le ? Audio::FLAG_LITTLE_ENDIAN : 0)
|
|
|
|
| (isStereo ? Audio::FLAG_STEREO : 0));
|
2010-02-03 10:36:14 +00:00
|
|
|
|
2010-02-03 15:24:24 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
void test_read_buffer_8_bit_signed_mono() {
|
|
|
|
const int sampleRate = 11025;
|
|
|
|
const int time = 2;
|
2010-02-03 15:24:24 +00:00
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
int16 *sine;
|
2010-02-03 16:02:25 +00:00
|
|
|
Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, &sine, false, false);
|
2010-02-03 10:36:14 +00:00
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
int16 *buffer = new int16[sampleRate * time];
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time);
|
|
|
|
TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time), 0);
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
2010-02-03 10:36:14 +00:00
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
delete[] sine;
|
|
|
|
delete[] buffer;
|
|
|
|
delete s;
|
2010-02-03 10:36:14 +00:00
|
|
|
}
|
|
|
|
|
2010-02-03 16:02:25 +00:00
|
|
|
void test_read_buffer_8_bit_signed_stereo() {
|
|
|
|
const int sampleRate = 11025;
|
|
|
|
const int time = 2;
|
|
|
|
|
|
|
|
int16 *sine;
|
|
|
|
Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, &sine, false, true);
|
|
|
|
|
|
|
|
int16 *buffer = new int16[sampleRate * time * 2];
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time * 2), sampleRate * time * 2);
|
|
|
|
TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time * 2), 0);
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
|
|
|
|
|
|
|
delete[] sine;
|
|
|
|
delete[] buffer;
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
void test_read_buffer_8_bit_unsigned_mono() {
|
2010-02-03 10:36:14 +00:00
|
|
|
const int sampleRate = 11025;
|
|
|
|
const int time = 2;
|
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
int16 *sine;
|
2010-02-03 16:02:25 +00:00
|
|
|
Audio::SeekableAudioStream *s = createSineStream<uint8>(sampleRate, time, &sine, false, false);
|
2010-02-03 10:36:14 +00:00
|
|
|
|
|
|
|
int16 *buffer = new int16[sampleRate * time];
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time);
|
2010-02-03 15:25:06 +00:00
|
|
|
TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time), 0);
|
2010-02-03 10:36:14 +00:00
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
delete[] sine;
|
2010-02-03 10:36:14 +00:00
|
|
|
delete[] buffer;
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
void test_read_buffer_16_bit_signed_be_mono() {
|
2010-02-03 10:36:14 +00:00
|
|
|
const int sampleRate = 11025;
|
|
|
|
const int time = 2;
|
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
int16 *sine;
|
2010-02-03 16:02:25 +00:00
|
|
|
Audio::SeekableAudioStream *s = createSineStream<int16>(sampleRate, time, &sine, false, false);
|
2010-02-03 10:36:14 +00:00
|
|
|
|
|
|
|
int16 *buffer = new int16[sampleRate * time];
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time);
|
2010-02-03 15:25:06 +00:00
|
|
|
TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time), 0);
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
2010-02-03 10:36:14 +00:00
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
delete[] sine;
|
|
|
|
delete[] buffer;
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
|
2010-02-03 16:02:25 +00:00
|
|
|
void test_read_buffer_16_bit_signed_be_stereo() {
|
|
|
|
const int sampleRate = 11025;
|
|
|
|
const int time = 2;
|
|
|
|
|
|
|
|
int16 *sine;
|
|
|
|
Audio::SeekableAudioStream *s = createSineStream<int16>(sampleRate, time, &sine, false, true);
|
|
|
|
|
|
|
|
int16 *buffer = new int16[sampleRate * time * 2];
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time * 2), sampleRate * time * 2);
|
|
|
|
TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time * 2), 0);
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
|
|
|
|
|
|
|
delete[] sine;
|
|
|
|
delete[] buffer;
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
void test_read_buffer_16_bit_unsigned_be_mono() {
|
|
|
|
const int sampleRate = 11025;
|
|
|
|
const int time = 2;
|
|
|
|
|
|
|
|
int16 *sine;
|
2010-02-03 16:02:25 +00:00
|
|
|
Audio::SeekableAudioStream *s = createSineStream<uint16>(sampleRate, time, &sine, false, false);
|
2010-02-03 10:36:14 +00:00
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
int16 *buffer = new int16[sampleRate * time];
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time);
|
|
|
|
TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time), 0);
|
2010-02-03 10:36:14 +00:00
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
delete[] sine;
|
2010-02-03 10:36:14 +00:00
|
|
|
delete[] buffer;
|
|
|
|
delete s;
|
|
|
|
}
|
2010-02-03 15:24:24 +00:00
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
void test_read_buffer_16_bit_signed_le_mono() {
|
2010-02-03 15:24:24 +00:00
|
|
|
const int sampleRate = 11025;
|
|
|
|
const int time = 2;
|
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
int16 *sine;
|
2010-02-03 16:02:25 +00:00
|
|
|
Audio::SeekableAudioStream *s = createSineStream<int16>(sampleRate, time, &sine, true, false);
|
2010-02-03 15:24:24 +00:00
|
|
|
|
|
|
|
int16 *buffer = new int16[sampleRate * time];
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time);
|
2010-02-03 15:25:06 +00:00
|
|
|
TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time), 0);
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
2010-02-03 15:24:24 +00:00
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
delete[] sine;
|
|
|
|
delete[] buffer;
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_buffer_16_bit_unsigned_le_mono() {
|
|
|
|
const int sampleRate = 11025;
|
|
|
|
const int time = 2;
|
|
|
|
|
|
|
|
int16 *sine;
|
2010-02-03 16:02:25 +00:00
|
|
|
Audio::SeekableAudioStream *s = createSineStream<uint16>(sampleRate, time, &sine, true, false);
|
2010-02-03 15:24:24 +00:00
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
int16 *buffer = new int16[sampleRate * time];
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time);
|
|
|
|
TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time), 0);
|
2010-02-03 15:24:24 +00:00
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
|
|
|
|
2010-02-03 15:25:06 +00:00
|
|
|
delete[] sine;
|
2010-02-03 15:24:24 +00:00
|
|
|
delete[] buffer;
|
|
|
|
delete s;
|
|
|
|
}
|
2010-02-03 15:44:43 +00:00
|
|
|
|
2010-02-03 16:02:25 +00:00
|
|
|
void test_read_buffer_16_bit_unsigned_le_stereo() {
|
|
|
|
const int sampleRate = 11025;
|
|
|
|
const int time = 2;
|
|
|
|
|
|
|
|
int16 *sine;
|
|
|
|
Audio::SeekableAudioStream *s = createSineStream<uint16>(sampleRate, time, &sine, true, true);
|
|
|
|
|
|
|
|
int16 *buffer = new int16[sampleRate * time * 2];
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time * 2), sampleRate * time * 2);
|
|
|
|
TS_ASSERT_EQUALS(memcmp(sine, buffer, sizeof(int16) * sampleRate * time * 2), 0);
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
|
|
|
|
|
|
|
delete[] sine;
|
|
|
|
delete[] buffer;
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
|
2010-02-03 15:45:08 +00:00
|
|
|
void test_partial_read() {
|
|
|
|
const int sampleRate = 11025;
|
|
|
|
const int time = 4;
|
|
|
|
|
|
|
|
int16 *sine;
|
2010-02-03 16:02:25 +00:00
|
|
|
Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, &sine, false, false);
|
2010-02-03 15:45:08 +00:00
|
|
|
int16 *buffer = new int16[sampleRate * time];
|
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate), sampleRate);
|
|
|
|
TS_ASSERT_EQUALS(memcmp(sine, buffer, sampleRate), 0);
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), false);
|
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * 2), sampleRate * 2);
|
|
|
|
TS_ASSERT_EQUALS(memcmp(sine + sampleRate, buffer, sampleRate * 2), 0);
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), false);
|
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate), sampleRate);
|
|
|
|
TS_ASSERT_EQUALS(memcmp(sine + sampleRate * 3, buffer, sampleRate), 0);
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
|
|
|
|
|
|
|
delete[] sine;
|
|
|
|
delete[] buffer;
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_after_end() {
|
|
|
|
const int sampleRate = 11025;
|
|
|
|
const int time = 1;
|
2010-02-03 16:02:25 +00:00
|
|
|
Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, 0, false, false);
|
2010-02-03 15:45:08 +00:00
|
|
|
int16 *buffer = new int16[sampleRate * time];
|
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time);
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), 0);
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
|
|
|
|
|
|
|
delete[] buffer;
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
|
2010-02-03 15:44:43 +00:00
|
|
|
void test_rewind() {
|
|
|
|
const int sampleRate = 11025;
|
|
|
|
const int time = 2;
|
2010-02-03 16:02:25 +00:00
|
|
|
Audio::SeekableAudioStream *s = createSineStream<int8>(sampleRate, time, 0, false, false);
|
2010-02-03 15:44:43 +00:00
|
|
|
int16 *buffer = new int16[sampleRate * time];
|
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time);
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
|
|
|
|
|
|
|
s->rewind();
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), false);
|
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time);
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
|
|
|
|
|
|
|
delete[] buffer;
|
|
|
|
delete s;
|
|
|
|
}
|
2010-02-03 15:45:29 +00:00
|
|
|
|
|
|
|
void test_length() {
|
2010-02-03 16:02:04 +00:00
|
|
|
int sampleRate = 0;
|
2010-02-03 15:45:29 +00:00
|
|
|
const int time = 4;
|
|
|
|
|
|
|
|
Audio::SeekableAudioStream *s = 0;
|
|
|
|
|
2010-02-03 16:02:04 +00:00
|
|
|
// 11025 Hz tests
|
|
|
|
sampleRate = 11025;
|
2010-02-03 16:02:25 +00:00
|
|
|
s = createSineStream<int8>(sampleRate, time, 0, false, false);
|
2010-02-03 16:02:04 +00:00
|
|
|
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
|
|
|
|
delete s;
|
|
|
|
|
2010-02-03 16:02:25 +00:00
|
|
|
s = createSineStream<uint16>(sampleRate, time, 0, false, false);
|
2010-02-03 16:02:04 +00:00
|
|
|
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
|
|
|
|
delete s;
|
|
|
|
|
|
|
|
// 48000 Hz tests
|
|
|
|
sampleRate = 48000;
|
2010-02-03 16:02:25 +00:00
|
|
|
s = createSineStream<int8>(sampleRate, time, 0, false, false);
|
2010-02-03 16:02:04 +00:00
|
|
|
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
|
|
|
|
delete s;
|
|
|
|
|
2010-02-03 16:02:25 +00:00
|
|
|
s = createSineStream<uint16>(sampleRate, time, 0, true, false);
|
2010-02-03 16:02:04 +00:00
|
|
|
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
|
|
|
|
delete s;
|
|
|
|
|
|
|
|
// 11840 Hz tests
|
|
|
|
sampleRate = 11840;
|
2010-02-03 16:02:25 +00:00
|
|
|
s = createSineStream<int8>(sampleRate, time, 0, false, false);
|
2010-02-03 16:02:04 +00:00
|
|
|
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
|
|
|
|
delete s;
|
|
|
|
|
2010-02-03 16:02:25 +00:00
|
|
|
s = createSineStream<uint16>(sampleRate, time, 0, false, false);
|
2010-02-03 16:02:04 +00:00
|
|
|
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
|
|
|
|
delete s;
|
|
|
|
|
|
|
|
// 11111 Hz tests
|
|
|
|
sampleRate = 11111;
|
2010-02-03 16:02:25 +00:00
|
|
|
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);
|
|
|
|
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
|
|
|
|
delete s;
|
|
|
|
|
|
|
|
// 22050 Hz stereo test
|
|
|
|
sampleRate = 22050;
|
|
|
|
s = createSineStream<int8>(sampleRate, time, 0, false, true);
|
2010-02-03 15:45:29 +00:00
|
|
|
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
|
|
|
|
delete s;
|
|
|
|
|
2010-02-03 16:02:25 +00:00
|
|
|
s = createSineStream<uint16>(sampleRate, time, 0, true, true);
|
2010-02-03 15:45:29 +00:00
|
|
|
TS_ASSERT_EQUALS(s->getLength().totalNumberOfFrames(), sampleRate * time);
|
|
|
|
delete s;
|
|
|
|
}
|
2010-02-03 10:36:14 +00:00
|
|
|
};
|