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:24:03 +00:00
|
|
|
static T *createSine(int sampleRate, int time) {
|
2010-02-03 10:36:14 +00:00
|
|
|
T *sine = (T *)malloc(sizeof(T) * time * sampleRate);
|
|
|
|
|
|
|
|
const T maxValue = std::numeric_limits<T>::max();
|
|
|
|
|
|
|
|
for (int i = 0; i < time * sampleRate; ++i)
|
|
|
|
sine[i] = (T)(sin((double)i / sampleRate * 2 * PI) * maxValue);
|
|
|
|
|
|
|
|
return sine;
|
|
|
|
}
|
|
|
|
|
2010-02-03 15:24:24 +00:00
|
|
|
static Audio::SeekableAudioStream *createSineStream8Bit(int sampleRate, int time, const int8 **sineP) {
|
|
|
|
int8 *sine = createSine<int8>(sampleRate, time);
|
|
|
|
|
|
|
|
Common::SeekableReadStream *sD = new Common::MemoryReadStream((const byte *)sine, sampleRate * time, DisposeAfterUse::YES);
|
|
|
|
Audio::SeekableAudioStream *s = Audio::makeRawStream(sD, sampleRate, 0);
|
|
|
|
|
|
|
|
if (sineP)
|
|
|
|
*sineP = sine;
|
2010-02-03 10:36:14 +00:00
|
|
|
|
2010-02-03 15:24:24 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Audio::SeekableAudioStream *createSineStream16Bit(int sampleRate, int time, bool le, const int16 **sineP) {
|
|
|
|
int16 *sine = createSine<int16>(sampleRate, time);
|
|
|
|
|
|
|
|
if (le) {
|
|
|
|
for (int i = 0; i < sampleRate * time; ++i)
|
|
|
|
WRITE_LE_UINT16(&sine[i], sine[i]);
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < sampleRate * time; ++i)
|
|
|
|
WRITE_BE_UINT16(&sine[i], sine[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Common::SeekableReadStream *sD = new Common::MemoryReadStream((const byte *)sine, sizeof(int16) * sampleRate * time, DisposeAfterUse::YES);
|
|
|
|
Audio::SeekableAudioStream *s = Audio::makeRawStream(sD, sampleRate, Audio::FLAG_16BITS | (le ? Audio::FLAG_LITTLE_ENDIAN : 0));
|
2010-02-03 10:36:14 +00:00
|
|
|
|
|
|
|
if (sineP)
|
|
|
|
*sineP = sine;
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_read_buffer_8_bit_mono() {
|
|
|
|
const int sampleRate = 11025;
|
|
|
|
const int time = 2;
|
|
|
|
|
2010-02-03 15:24:24 +00:00
|
|
|
const int8 *sine;
|
|
|
|
Audio::SeekableAudioStream *s = createSineStream8Bit(sampleRate, time, &sine);
|
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:24:44 +00:00
|
|
|
bool fine = true;
|
2010-02-03 10:36:14 +00:00
|
|
|
for (int i = 0; i < sampleRate * time; ++i)
|
2010-02-03 15:24:44 +00:00
|
|
|
fine &= (buffer[i] == (sine[i] << 8));
|
|
|
|
TS_ASSERT_EQUALS(fine, true);
|
2010-02-03 10:36:14 +00:00
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
|
|
|
|
|
|
|
delete[] buffer;
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
|
2010-02-03 15:24:03 +00:00
|
|
|
void test_read_buffer_16_bit_be_mono() {
|
2010-02-03 10:36:14 +00:00
|
|
|
const int sampleRate = 11025;
|
|
|
|
const int time = 2;
|
|
|
|
|
|
|
|
const int16 *sine = 0;
|
2010-02-03 15:24:24 +00:00
|
|
|
Audio::SeekableAudioStream *s = createSineStream16Bit(sampleRate, time, false, &sine);
|
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:24:44 +00:00
|
|
|
bool fine = true;
|
2010-02-03 10:36:14 +00:00
|
|
|
for (int i = 0; i < sampleRate * time; ++i)
|
2010-02-03 15:24:44 +00:00
|
|
|
fine &= (buffer[i] == (int16)READ_BE_UINT16(&sine[i]));
|
|
|
|
TS_ASSERT_EQUALS(fine, true);
|
2010-02-03 10:36:14 +00:00
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
|
|
|
|
|
|
|
delete[] buffer;
|
|
|
|
delete s;
|
|
|
|
}
|
2010-02-03 15:24:24 +00:00
|
|
|
|
|
|
|
void test_read_buffer_16_bit_le_mono() {
|
|
|
|
const int sampleRate = 11025;
|
|
|
|
const int time = 2;
|
|
|
|
|
|
|
|
const int16 *sine = 0;
|
|
|
|
Audio::SeekableAudioStream *s = createSineStream16Bit(sampleRate, time, true, &sine);
|
|
|
|
|
|
|
|
int16 *buffer = new int16[sampleRate * time];
|
|
|
|
TS_ASSERT_EQUALS(s->readBuffer(buffer, sampleRate * time), sampleRate * time);
|
|
|
|
|
2010-02-03 15:24:44 +00:00
|
|
|
bool fine = true;
|
2010-02-03 15:24:24 +00:00
|
|
|
for (int i = 0; i < sampleRate * time; ++i)
|
2010-02-03 15:24:44 +00:00
|
|
|
fine &= (buffer[i] == (int16)READ_LE_UINT16(&sine[i]));
|
|
|
|
TS_ASSERT_EQUALS(fine, true);
|
2010-02-03 15:24:24 +00:00
|
|
|
|
|
|
|
TS_ASSERT_EQUALS(s->endOfData(), true);
|
|
|
|
|
|
|
|
delete[] buffer;
|
|
|
|
delete s;
|
|
|
|
}
|
2010-02-03 10:36:14 +00:00
|
|
|
};
|