Repository navigation
Expand file tree
/
Copy pathaudio_capture_test.cpp
More file actions
140 lines (132 loc) · 5.43 KB
/
Copy pathaudio_capture_test.cpp
File metadata and controls
140 lines (132 loc) · 5.43 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
#include "test_harness.hpp"
#include <sync/audio_capture.hpp>
#include <array>
#include <limits>
#include <thread>
namespace audio = noisefactor::sync::audio;
SYNC_TEST(audio_capture_keeps_all_32_channels_and_frame_order) {
audio::CaptureBuffer input(48000, 32, 1024);
std::vector<float> samples(640 * 32);
for (std::size_t frame = 0; frame < 640; ++frame)
for (std::size_t channel = 0; channel < 32; ++channel)
samples[frame * 32 + channel] = static_cast<float>(frame * 32 + channel);
input.push(samples);
auto first = input.read();
SYNC_REQUIRE(first.channels == 32);
SYNC_REQUIRE(first.sample_rate == 48000);
SYNC_REQUIRE(first.first_frame == 0);
SYNC_REQUIRE(first.samples.size() == 480 * 32);
SYNC_REQUIRE(first.samples[31] == 31);
SYNC_REQUIRE(first.samples.back() == 15359);
auto second = input.read();
SYNC_REQUIRE(second.first_frame == 480);
SYNC_REQUIRE(second.samples.size() == 160 * 32);
SYNC_REQUIRE(second.samples.front() == 15360);
SYNC_REQUIRE(second.samples.back() == 20479);
SYNC_REQUIRE(input.read().samples.empty());
}
SYNC_TEST(audio_capture_preserves_sample_identity_during_concurrent_ring_reuse) {
constexpr std::size_t channels = 32;
constexpr std::size_t frames_per_push = 64;
constexpr std::size_t pushes = 4096;
audio::CaptureBuffer input(48000, channels, frames_per_push);
std::atomic<bool> start{false};
std::atomic<bool> done{false};
std::thread producer([&] {
std::array<float, frames_per_push * channels> samples{};
while (!start.load(std::memory_order_acquire)) std::this_thread::yield();
for (std::size_t batch = 0; batch < pushes; ++batch) {
for (std::size_t i = 0; i < samples.size(); ++i)
samples[i] = static_cast<float>(batch * samples.size() + i);
input.push(samples);
std::this_thread::yield();
}
done.store(true, std::memory_order_release);
});
start.store(true, std::memory_order_release);
std::size_t received = 0;
std::uint64_t previous_end = 0;
std::uint64_t dropped = 0;
bool finished = false;
bool ordered = true;
bool intact = true;
do {
// Read after acquiring completion too, so the final buffered frames drain.
finished = done.load(std::memory_order_acquire);
const auto packet = input.read();
dropped = packet.dropped_frames;
if (packet.samples.empty()) {
std::this_thread::yield();
continue;
}
ordered = ordered && packet.first_frame >= previous_end;
for (std::size_t i = 0; i < packet.samples.size(); ++i)
if (packet.samples[i] != static_cast<float>(packet.first_frame * channels + i))
intact = false;
const auto frames = packet.samples.size() / channels;
received += frames;
previous_end = packet.first_frame + frames;
} while (!finished);
producer.join();
SYNC_REQUIRE(ordered);
SYNC_REQUIRE(intact);
SYNC_REQUIRE(received > 0);
// Deliberate contention may drop frames, but must never corrupt a packet.
SYNC_REQUIRE(received + dropped == pushes * frames_per_push);
}
SYNC_TEST(audio_capture_discards_oldest_frames_at_its_fixed_capacity) {
audio::CaptureBuffer input(44100, 2, 3);
input.push(std::array<float, 4>{1, 11, 2, 12});
input.push(std::array<float, 4>{3, 13, 4, 14});
const auto packet = input.read();
SYNC_REQUIRE(packet.first_frame == 1);
SYNC_REQUIRE(packet.dropped_frames == 1);
SYNC_REQUIRE((packet.samples == std::vector<float>{2, 12, 3, 13, 4, 14}));
}
SYNC_TEST(audio_capture_default_capacity_survives_a_one_second_reader_stall_at_32_channels) {
audio::CaptureBuffer input(44100, 32);
std::array<float, 256 * 32> samples{};
for (std::size_t batch = 0; batch < 200; ++batch) {
for (std::size_t frame = 0; frame < 256; ++frame)
for (std::size_t channel = 0; channel < 32; ++channel)
samples[frame * 32 + channel] = static_cast<float>(batch * 256 + frame);
input.push(samples);
}
std::size_t received = 0;
for (;;) {
const auto packet = input.read();
SYNC_REQUIRE(packet.dropped_frames == 0);
if (packet.samples.empty()) break;
SYNC_REQUIRE(packet.first_frame == received);
received += packet.samples.size() / 32;
}
SYNC_REQUIRE(received == 200 * 256);
}
SYNC_TEST(audio_capture_bounds_oversized_callbacks_and_sanitizes_nonfinite_samples) {
audio::CaptureBuffer input(48000, 1, 2);
input.push(std::array<float, 4>{1, 2, -0.5f, std::numeric_limits<float>::quiet_NaN()});
const auto packet = input.read();
SYNC_REQUIRE(packet.first_frame == 2);
SYNC_REQUIRE(packet.dropped_frames == 2);
SYNC_REQUIRE((packet.samples == std::vector<float>{-0.5f, 0}));
}
SYNC_TEST(audio_wire_packet_has_little_endian_header_and_signed_pcm) {
audio::CaptureBuffer input(48000, 2);
input.push(std::array<float, 4>{0.5f, -0.5f, 1.0f, -1.0f});
const auto bytes = audio::encode_packet(input.read());
SYNC_REQUIRE(bytes.size() == 48);
const std::array<unsigned char, 16> header{'N', 'A', 'U', 'D', 1, 0, 2, 0,
0x80, 0xbb, 0, 0, 2, 0, 0, 0};
for (std::size_t i = 0; i < header.size(); ++i)
SYNC_REQUIRE(std::to_integer<unsigned char>(bytes[i]) == header[i]);
SYNC_REQUIRE(std::to_integer<unsigned char>(bytes[35]) == 0x3f);
SYNC_REQUIRE(std::to_integer<unsigned char>(bytes[39]) == 0xbf);
}
SYNC_TEST(audio_capture_rejects_invalid_channel_layouts_before_allocating) {
for (const auto channels : {0u, 33u}) {
bool rejected = false;
try { audio::CaptureBuffer input(48000, channels); }
catch (const std::invalid_argument&) { rejected = true; }
SYNC_REQUIRE(rejected);
}
}