-
Notifications
You must be signed in to change notification settings - Fork 0
/
flac_decode_unit_test.cpp
128 lines (110 loc) · 3.87 KB
/
flac_decode_unit_test.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// SPDX-License-Identifier: MIT
#include "audio_buffer.h"
#include "stream.h"
#include <gtest/gtest.h>
namespace plac {
namespace {
class FlacDecodeTest : public ::testing::Test {
protected:
AudioBuffer<228000> audio_buffer_;
};
struct DummyWriter16Bps {
ssize_t operator()(const AudioFormat format, const u_char *const data, const size_t count) {
EXPECT_EQ(16, format.bits);
for (int i = 0; i < count; ++i) {
EXPECT_EQ(total & 0xFF, data[4 * i + 0]);
EXPECT_EQ((total >> 8) & 0xFF, data[4 * i + 1]);
EXPECT_EQ((total >> 16) & 0xFF, data[4 * i + 2]);
EXPECT_EQ(total & 0xFF, data[4 * i + 3]);
++total;
}
return count;
}
size_t total = 0;
};
struct DummyWriter24Bps {
ssize_t operator()(const AudioFormat format, const u_char *const data, const size_t count) {
EXPECT_EQ(24, format.bits);
for (int i = 0; i < count; ++i) {
EXPECT_EQ((total >> 0) & 0xFF, data[6 * i + 0]);
EXPECT_EQ((total >> 8) & 0xFF, data[6 * i + 1]);
EXPECT_EQ((total >> 16) & 0xFF, data[6 * i + 2]);
EXPECT_EQ((total >> 16) & 0xFF, data[6 * i + 3]);
EXPECT_EQ((total >> 8) & 0xFF, data[6 * i + 4]);
EXPECT_EQ((total >> 0) & 0xFF, data[6 * i + 5]);
++total;
}
return count;
}
size_t total = 0;
};
TEST_F(FlacDecodeTest, Play16Bps) {
FlowControl flow{};
Stream stream{audio_buffer_, flow};
DummyWriter16Bps writer{};
for (const char *name : {
"../assets/16bps_part0.flac",
"../assets/16bps_part1.flac",
"../assets/16bps_part2.flac",
"../assets/16bps_part3.flac",
"../assets/16bps_part4.flac",
"../assets/16bps_part5.flac",
"../assets/16bps_part6.flac",
"../assets/16bps_part7.flac",
"../assets/16bps_part8.flac",
"../assets/16bps_part9.flac",
"../assets/16bps_part10.flac",
"../assets/16bps_part11.flac",
"../assets/16bps_part12.flac",
"../assets/16bps_part13.flac",
"../assets/16bps_part14.flac",
}) {
ASSERT_TRUE(stream.Reset(name));
FLAC__StreamDecoderState state = FLAC__stream_decoder_get_state(stream.decoder_);
while (state != FLAC__STREAM_DECODER_END_OF_STREAM) {
audio_buffer_.Read(stream.format_, 5513, writer);
ASSERT_TRUE(FLAC__stream_decoder_process_single(stream.decoder_));
state = FLAC__stream_decoder_get_state(stream.decoder_);
usleep(100);
}
}
audio_buffer_.Drain(stream.format_, writer);
EXPECT_EQ(150000, writer.total);
EXPECT_TRUE(audio_buffer_.IsEmpty());
}
TEST_F(FlacDecodeTest, Play24Bps) {
FlowControl flow{};
Stream stream{audio_buffer_, flow};
DummyWriter24Bps writer{};
for (const char *name : {
"../assets/24bps_part0.flac",
"../assets/24bps_part1.flac",
"../assets/24bps_part2.flac",
"../assets/24bps_part3.flac",
"../assets/24bps_part4.flac",
"../assets/24bps_part5.flac",
"../assets/24bps_part6.flac",
"../assets/24bps_part7.flac",
"../assets/24bps_part8.flac",
"../assets/24bps_part9.flac",
"../assets/24bps_part10.flac",
"../assets/24bps_part11.flac",
"../assets/24bps_part12.flac",
"../assets/24bps_part13.flac",
"../assets/24bps_part14.flac",
}) {
ASSERT_TRUE(stream.Reset(name));
FLAC__StreamDecoderState state = FLAC__stream_decoder_get_state(stream.decoder_);
while (state != FLAC__STREAM_DECODER_END_OF_STREAM) {
audio_buffer_.Read(stream.format_, 5513, writer);
ASSERT_TRUE(FLAC__stream_decoder_process_single(stream.decoder_));
state = FLAC__stream_decoder_get_state(stream.decoder_);
usleep(100);
}
}
audio_buffer_.Drain(stream.format_, writer);
EXPECT_EQ(150000, writer.total);
EXPECT_TRUE(audio_buffer_.IsEmpty());
}
} // namespace
} // namespace plac