81 lines
1.9 KiB
C++
81 lines
1.9 KiB
C++
#include <doctest/doctest.h>
|
|
|
|
#include <echoion/core/AudioDecoder.hpp>
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
std::string fixture(const char* name)
|
|
{
|
|
return std::string(ECHOION_TEST_FIXTURE_DIR) + "/" + name;
|
|
}
|
|
|
|
// Drains every chunk out of the decoder until EOF; returns total samples.
|
|
std::size_t drain(Echoion::Core::AudioDecoder& decoder)
|
|
{
|
|
std::size_t total = 0;
|
|
std::vector<int16_t> chunk;
|
|
while (true) {
|
|
if (decoder.getNextChunk(chunk, true)) {
|
|
total += chunk.size();
|
|
} else if (decoder.isFinished()) {
|
|
break;
|
|
}
|
|
}
|
|
return total;
|
|
}
|
|
} // namespace
|
|
|
|
TEST_CASE("AudioDecoder decodes a WAV fixture completely")
|
|
{
|
|
Echoion::Core::AudioDecoder decoder;
|
|
REQUIRE(decoder.initialize().has_value());
|
|
decoder.setOutputFormat(48000, 2);
|
|
|
|
auto open = decoder.openSource(fixture("sine1.wav"));
|
|
REQUIRE_MESSAGE(open.has_value(),
|
|
(open ? std::string{} : open.error().message));
|
|
|
|
const std::size_t total = drain(decoder);
|
|
|
|
// 2 seconds @ 48kHz stereo. WAV is exact (no codec delay/padding).
|
|
CHECK(total == 2u * 48000u * 2u);
|
|
|
|
auto info = decoder.getStreamInfo();
|
|
REQUIRE(info.has_value());
|
|
CHECK(info->sampleRate == 48000);
|
|
|
|
decoder.closeSource();
|
|
decoder.shutdown();
|
|
}
|
|
|
|
TEST_CASE("AudioDecoder survives close/reopen cycles")
|
|
{
|
|
Echoion::Core::AudioDecoder decoder;
|
|
REQUIRE(decoder.initialize().has_value());
|
|
decoder.setOutputFormat(48000, 2);
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
auto open = decoder.openSource(fixture("sine2.wav"));
|
|
REQUIRE(open.has_value());
|
|
const std::size_t total = drain(decoder);
|
|
CHECK(total == 1u * 48000u * 2u); // 1 second fixture
|
|
decoder.closeSource();
|
|
}
|
|
|
|
decoder.shutdown();
|
|
}
|
|
|
|
TEST_CASE("AudioDecoder reports failure for a missing source")
|
|
{
|
|
Echoion::Core::AudioDecoder decoder;
|
|
REQUIRE(decoder.initialize().has_value());
|
|
|
|
auto open = decoder.openSource(fixture("does-not-exist.wav"));
|
|
CHECK_FALSE(open.has_value());
|
|
|
|
decoder.shutdown();
|
|
}
|