From 4fca64d1b049d5d94b0ca844c7dad301ddaf8718 Mon Sep 17 00:00:00 2001 From: Johannes Schickel Date: Sat, 6 Feb 2010 16:41:27 +0000 Subject: Move test AudioStream creation to a new file. svn-id: r47933 --- test/sound/helper.h | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test/sound/raw.h | 62 +----------------------------------------------- 2 files changed, 69 insertions(+), 61 deletions(-) create mode 100644 test/sound/helper.h diff --git a/test/sound/helper.h b/test/sound/helper.h new file mode 100644 index 0000000000..c389a9c576 --- /dev/null +++ b/test/sound/helper.h @@ -0,0 +1,68 @@ +#ifndef TEST_SOUND_HELPER_H +#define TEST_SOUND_HELPER_H + +#include "sound/decoders/raw.h" + +#include "common/stream.h" +#include "common/endian.h" + +#include +#include + +template +static T *createSine(const int sampleRate, const int time) { + T *sine = (T *)malloc(sizeof(T) * time * sampleRate); + + const bool isUnsigned = !std::numeric_limits::is_signed; + const T xorMask = isUnsigned ? (1 << (std::numeric_limits::digits - 1)) : 0; + const T maxValue = std::numeric_limits::max() ^ xorMask; + + for (int i = 0; i < time * sampleRate; ++i) + sine[i] = ((T)(sin((double)i / sampleRate * 2 * PI) * maxValue)) ^ xorMask; + + return sine; +} + +template +static Audio::SeekableAudioStream *createSineStream(const int sampleRate, const int time, int16 **comp, bool le, bool isStereo) { + T *sine = createSine(sampleRate, time * (isStereo ? 2 : 1)); + + const bool isUnsigned = !std::numeric_limits::is_signed; + const T xorMask = isUnsigned ? (1 << (std::numeric_limits::digits - 1)) : 0; + const bool is16Bits = (sizeof(T) == 2); + assert(sizeof(T) == 2 || sizeof(T) == 1); + + const int samples = sampleRate * time * (isStereo ? 2 : 1); + + if (comp) { + *comp = new int16[samples]; + for (int i = 0; i < samples; ++i) { + if (is16Bits) + (*comp)[i] = sine[i] ^ xorMask; + else + (*comp)[i] = (sine[i] ^ xorMask) << 8; + } + } + + if (is16Bits) { + if (le) { + for (int i = 0; i < samples; ++i) + WRITE_LE_UINT16(&sine[i], sine[i]); + } else { + for (int i = 0; i < samples; ++i) + WRITE_BE_UINT16(&sine[i], sine[i]); + } + } + + Common::SeekableReadStream *sD = new Common::MemoryReadStream((const byte *)sine, sizeof(T) * samples, DisposeAfterUse::YES); + Audio::SeekableAudioStream *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; +} + +#endif + diff --git a/test/sound/raw.h b/test/sound/raw.h index 33f000566b..564ea2b266 100644 --- a/test/sound/raw.h +++ b/test/sound/raw.h @@ -2,70 +2,10 @@ #include "sound/decoders/raw.h" -#include "common/stream.h" -#include "common/endian.h" - -#include -#include +#include "helper.h" class RawStreamTestSuite : public CxxTest::TestSuite { -public: - template - static T *createSine(const int sampleRate, const int time) { - T *sine = (T *)malloc(sizeof(T) * time * sampleRate); - - const bool isUnsigned = !std::numeric_limits::is_signed; - const T xorMask = isUnsigned ? (1 << (std::numeric_limits::digits - 1)) : 0; - const T maxValue = std::numeric_limits::max() ^ xorMask; - - for (int i = 0; i < time * sampleRate; ++i) - sine[i] = ((T)(sin((double)i / sampleRate * 2 * PI) * maxValue)) ^ xorMask; - - return sine; - } - - template - static Audio::SeekableAudioStream *createSineStream(const int sampleRate, const int time, int16 **comp, bool le, bool isStereo) { - T *sine = createSine(sampleRate, time * (isStereo ? 2 : 1)); - - const bool isUnsigned = !std::numeric_limits::is_signed; - const T xorMask = isUnsigned ? (1 << (std::numeric_limits::digits - 1)) : 0; - const bool is16Bits = (sizeof(T) == 2); - assert(sizeof(T) == 2 || sizeof(T) == 1); - - const int samples = sampleRate * time * (isStereo ? 2 : 1); - - if (comp) { - *comp = new int16[samples]; - for (int i = 0; i < samples; ++i) { - if (is16Bits) - (*comp)[i] = sine[i] ^ xorMask; - else - (*comp)[i] = (sine[i] ^ xorMask) << 8; - } - } - - if (is16Bits) { - if (le) { - for (int i = 0; i < samples; ++i) - WRITE_LE_UINT16(&sine[i], sine[i]); - } else { - for (int i = 0; i < samples; ++i) - WRITE_BE_UINT16(&sine[i], sine[i]); - } - } - - Common::SeekableReadStream *sD = new Common::MemoryReadStream((const byte *)sine, sizeof(T) * samples, DisposeAfterUse::YES); - Audio::SeekableAudioStream *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; - } - private: template void readBufferTestTemplate(const int sampleRate, const int time, const bool le, const bool isStereo) { -- cgit v1.2.3