blob: d55227751f3846f7505f20a4ac447fe0fbe3dcf0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
#include "mad.hpp"
#include <algorithm>
#include <cstdint>
#include "catch2/catch.hpp"
#include "span.hpp"
#include "test.mp3.hpp"
void load_mp3(cpp::span<std::byte> dest) {
cpp::span<std::byte> src(reinterpret_cast<std::byte*>(test_mp3),
test_mp3_len);
std::copy(src.begin(), src.begin() + dest.size(), dest.begin());
}
namespace codecs {
TEST_CASE("libmad mp3 decoder", "[unit]") {
MadMp3Decoder decoder;
SECTION("handles only mp3 files") {
REQUIRE(decoder.CanHandleFile("cool.mp3") == true);
REQUIRE(decoder.CanHandleFile("bad.wma") == false);
}
SECTION("processes streams correctly") {
std::array<std::byte, 2048> input;
input.fill(std::byte{0});
decoder.ResetForNewStream();
decoder.SetInput(input);
SECTION("empty stream fails gracefully") {
auto result = decoder.ProcessNextFrame();
REQUIRE(result.has_value());
REQUIRE(result.value() == true);
}
SECTION("invalid stream fails") {
input.fill(std::byte{0x69});
auto result = decoder.ProcessNextFrame();
REQUIRE(result.has_error());
REQUIRE(result.error() == ICodec::MALFORMED_DATA);
}
SECTION("valid stream parses successfully") {
load_mp3(input);
auto result = decoder.ProcessNextFrame();
REQUIRE(result.has_value());
REQUIRE(result.value() == false);
SECTION("output samples synthesized") {
std::array<std::byte, 256> output;
output.fill(std::byte{0});
auto res = decoder.WriteOutputSamples(output);
REQUIRE(res.first > 0);
REQUIRE(res.second == false);
// Just check that some kind of data was written. We don't care
// about what.
bool wrote_something = false;
for (int i = 0; i < output.size(); i++) {
if (std::to_integer<uint8_t>(output[0]) != 0) {
wrote_something = true;
break;
}
}
REQUIRE(wrote_something == true);
}
SECTION("output format correct") {
auto format = decoder.GetOutputFormat();
// Matches the test data.
REQUIRE(format.num_channels == 1);
REQUIRE(format.sample_rate_hz == 44100);
// Matches libmad output
REQUIRE(format.bits_per_sample == 24);
}
}
}
}
} // namespace codecs
|