Apply clang-format across the whole tree
Run clang-format (the repo's .clang-format: LLVM base, 120 cols, tabs, Allman functions) over every source file so the tree is formatter-clean. Whitespace only -- no behavior change; full x64 + x86 suites pass. Also set SortIncludes: false in .clang-format. Windows include order is load-bearing (windows.h must precede tlhelp32.h / mmreg.h / xinput.h / dinput.h; winsock2.h must precede windows.h), and the default alphabetical sort reorders tlhelp32.h ahead of windows.h -- a build break. Leaving order alone keeps the manual, correct grouping.
This commit is contained in:
@@ -17,14 +17,12 @@
|
||||
|
||||
using namespace coop;
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace {
|
||||
int g_failures = 0;
|
||||
void check(bool ok, const char* what)
|
||||
{
|
||||
std::printf("%s %s\n", ok ? " ok:" : "FAIL:", what);
|
||||
if (!ok)
|
||||
{
|
||||
if (!ok) {
|
||||
++g_failures;
|
||||
}
|
||||
}
|
||||
@@ -36,8 +34,7 @@ constexpr double kPi = 3.14159265358979323846;
|
||||
double source(double t)
|
||||
{
|
||||
const double chirp = std::sin(2.0 * kPi * (300.0 * t + 140.0 * t * t));
|
||||
return 0.5 * std::sin(2.0 * kPi * 221.0 * t) + 0.28 * std::sin(2.0 * kPi * 437.0 * t + 0.6) +
|
||||
0.22 * chirp;
|
||||
return 0.5 * std::sin(2.0 * kPi * 221.0 * t) + 0.28 * std::sin(2.0 * kPi * 437.0 * t + 0.6) + 0.22 * chirp;
|
||||
}
|
||||
|
||||
// Sample s(t) at `rate` for `seconds`, starting at t0 (capture-latency skew), optionally adding
|
||||
@@ -48,8 +45,7 @@ std::vector<float> capture(unsigned rate, double seconds, double t0, double nois
|
||||
std::vector<float> out(n);
|
||||
std::mt19937 rng(seed);
|
||||
std::uniform_real_distribution<float> jitter(-1.0f, 1.0f);
|
||||
for (std::size_t i = 0; i < n; ++i)
|
||||
{
|
||||
for (std::size_t i = 0; i < n; ++i) {
|
||||
const double t = t0 + static_cast<double>(i) / static_cast<double>(rate);
|
||||
out[i] = static_cast<float>(source(t)) + static_cast<float>(noise) * jitter(rng);
|
||||
}
|
||||
@@ -64,15 +60,13 @@ double multi(double t, unsigned channel)
|
||||
{
|
||||
const double k = 1.0 + 0.37 * static_cast<double>(channel); // distinct frequency scale per channel
|
||||
const double chirp = std::sin(2.0 * kPi * (300.0 * k * t + 140.0 * t * t));
|
||||
return 0.5 * std::sin(2.0 * kPi * 221.0 * k * t) + 0.28 * std::sin(2.0 * kPi * 437.0 * k * t + 0.6) +
|
||||
0.22 * chirp;
|
||||
return 0.5 * std::sin(2.0 * kPi * 221.0 * k * t) + 0.28 * std::sin(2.0 * kPi * 437.0 * k * t + 0.6) + 0.22 * chirp;
|
||||
}
|
||||
|
||||
double multi_mono(double t, unsigned channels)
|
||||
{
|
||||
double sum = 0.0;
|
||||
for (unsigned c = 0; c < channels; ++c)
|
||||
{
|
||||
for (unsigned c = 0; c < channels; ++c) {
|
||||
sum += multi(t, c);
|
||||
}
|
||||
return sum / channels;
|
||||
@@ -84,20 +78,15 @@ std::vector<std::uint8_t> encode(unsigned rate, unsigned channels, unsigned bits
|
||||
const unsigned bps = bits / 8;
|
||||
const std::size_t frames = static_cast<std::size_t>(rate * seconds);
|
||||
std::vector<std::uint8_t> out(frames * channels * bps);
|
||||
for (std::size_t i = 0; i < frames; ++i)
|
||||
{
|
||||
for (std::size_t i = 0; i < frames; ++i) {
|
||||
const double t = static_cast<double>(i) / rate;
|
||||
for (unsigned c = 0; c < channels; ++c)
|
||||
{
|
||||
for (unsigned c = 0; c < channels; ++c) {
|
||||
const double s = multi(t, c);
|
||||
std::uint8_t* p = out.data() + (i * channels + c) * bps;
|
||||
if (tag == coop::kWaveFormatFloat)
|
||||
{
|
||||
if (tag == coop::kWaveFormatFloat) {
|
||||
const float f = static_cast<float>(s);
|
||||
std::memcpy(p, &f, 4);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
const std::int16_t v = static_cast<std::int16_t>(s * 30000.0);
|
||||
std::memcpy(p, &v, 2);
|
||||
}
|
||||
@@ -121,16 +110,14 @@ coop::ChunkedCapture make_chunks(unsigned rate, unsigned ch, unsigned bits, unsi
|
||||
std::mt19937 rng(123);
|
||||
std::uniform_int_distribution<int> garbage(0, 255);
|
||||
std::size_t f = 0;
|
||||
while (f < frames)
|
||||
{
|
||||
while (f < frames) {
|
||||
const unsigned count = static_cast<unsigned>(std::min<std::size_t>(480, frames - f));
|
||||
cap.counts.push_back(count);
|
||||
// The hook reads count*stride contiguous bytes: the count real frames first
|
||||
// (count*real_block bytes), then count*(stride-real_block) bytes of stale over-read.
|
||||
const std::uint8_t* src = clean.data() + f * real_block;
|
||||
cap.bytes.insert(cap.bytes.end(), src, src + static_cast<std::size_t>(count) * real_block);
|
||||
for (std::size_t p = 0; p < static_cast<std::size_t>(count) * (stride - real_block); ++p)
|
||||
{
|
||||
for (std::size_t p = 0; p < static_cast<std::size_t>(count) * (stride - real_block); ++p) {
|
||||
cap.bytes.push_back(static_cast<std::uint8_t>(garbage(rng)));
|
||||
}
|
||||
f += count;
|
||||
@@ -140,11 +127,11 @@ coop::ChunkedCapture make_chunks(unsigned rate, unsigned ch, unsigned bits, unsi
|
||||
|
||||
// One layout scenario: the hook bytes are at (true_*) and still being measured; the loopback is the
|
||||
// post-mix mono of the same audio at device_rate. Assert correlate_format recovers the full layout.
|
||||
void test_layout(unsigned true_rate, unsigned true_ch, unsigned true_bits, unsigned true_tag,
|
||||
unsigned device_rate, const char* label)
|
||||
void test_layout(unsigned true_rate, unsigned true_ch, unsigned true_bits, unsigned true_tag, unsigned device_rate,
|
||||
const char* label)
|
||||
{
|
||||
std::printf("== layout: %s (%u Hz / %u ch / %u-bit %s -> device %u Hz) ==\n", label, true_rate, true_ch,
|
||||
true_bits, true_tag == coop::kWaveFormatFloat ? "float" : "pcm", device_rate);
|
||||
std::printf("== layout: %s (%u Hz / %u ch / %u-bit %s -> device %u Hz) ==\n", label, true_rate, true_ch, true_bits,
|
||||
true_tag == coop::kWaveFormatFloat ? "float" : "pcm", device_rate);
|
||||
// Device block 32 (8ch float) is the largest stride; every test layout's real block is <= 32.
|
||||
const coop::ChunkedCapture hook = make_chunks(true_rate, true_ch, true_bits, true_tag, /*stride=*/32, 0.6);
|
||||
// Loopback: the post-mix mono of the same audio, at the device rate, started ~18 ms later + noise.
|
||||
@@ -152,16 +139,15 @@ void test_layout(unsigned true_rate, unsigned true_ch, unsigned true_bits, unsig
|
||||
std::vector<float> loop(loop_frames);
|
||||
std::mt19937 rng(5);
|
||||
std::uniform_real_distribution<float> j(-1.0f, 1.0f);
|
||||
for (std::size_t m = 0; m < loop_frames; ++m)
|
||||
{
|
||||
loop[m] = static_cast<float>(multi_mono(0.018 + static_cast<double>(m) / device_rate, true_ch)) +
|
||||
0.02f * j(rng);
|
||||
for (std::size_t m = 0; m < loop_frames; ++m) {
|
||||
loop[m] =
|
||||
static_cast<float>(multi_mono(0.018 + static_cast<double>(m) / device_rate, true_ch)) + 0.02f * j(rng);
|
||||
}
|
||||
|
||||
const FormatCorrelation r =
|
||||
correlate_format(hook, loop, device_rate, standard_audio_rates(), standard_audio_layouts());
|
||||
std::printf(" picked %u Hz / %u ch / %u-bit %s score=%.3f runner_up=%.3f ok=%d\n", r.rate, r.channels,
|
||||
r.bits, r.tag == coop::kWaveFormatFloat ? "float" : "pcm", r.score, r.runner_up, r.ok ? 1 : 0);
|
||||
std::printf(" picked %u Hz / %u ch / %u-bit %s score=%.3f runner_up=%.3f ok=%d\n", r.rate, r.channels, r.bits,
|
||||
r.tag == coop::kWaveFormatFloat ? "float" : "pcm", r.score, r.runner_up, r.ok ? 1 : 0);
|
||||
check(r.ok, "layout pick is confident");
|
||||
check(r.rate == true_rate, "recovered the true rate");
|
||||
check(r.channels == true_ch, "recovered the true channel count");
|
||||
@@ -179,8 +165,7 @@ void test_case(unsigned true_rate, unsigned device_rate, const char* label)
|
||||
const std::vector<float> loop = capture(device_rate, 0.55, 0.022, 0.02, 7);
|
||||
|
||||
const RateCorrelation r = correlate_rate(hook, loop, device_rate, standard_audio_rates());
|
||||
std::printf(" picked %u Hz score=%.3f runner_up=%.3f ok=%d\n", r.rate, r.score, r.runner_up,
|
||||
r.ok ? 1 : 0);
|
||||
std::printf(" picked %u Hz score=%.3f runner_up=%.3f ok=%d\n", r.rate, r.score, r.runner_up, r.ok ? 1 : 0);
|
||||
check(r.rate == true_rate, "correlator picked the true rate");
|
||||
check(r.ok, "pick is confident (clears threshold + beats runner-up)");
|
||||
check(r.score > r.runner_up, "winner scores above the runner-up");
|
||||
@@ -210,8 +195,8 @@ int main()
|
||||
std::vector<float> stereo = {1.0f, 3.0f, 2.0f, 4.0f, -1.0f, 1.0f};
|
||||
std::vector<float> mono;
|
||||
correlate_detail::downmix(stereo.data(), 3, 2, mono);
|
||||
check(mono.size() == 3 && std::fabs(mono[0] - 2.0f) < 1e-6 && std::fabs(mono[1] - 3.0f) < 1e-6 &&
|
||||
std::fabs(mono[2] - 0.0f) < 1e-6,
|
||||
check(mono.size() == 3 && std::fabs(mono[0] - 2.0f) < 1e-6 && std::fabs(mono[1] - 3.0f) < 1e-6
|
||||
&& std::fabs(mono[2] - 0.0f) < 1e-6,
|
||||
"stereo frames average to mono");
|
||||
}
|
||||
|
||||
@@ -222,8 +207,7 @@ int main()
|
||||
std::vector<float> noise(static_cast<std::size_t>(48000 * 0.5));
|
||||
std::mt19937 rng(99);
|
||||
std::uniform_real_distribution<float> d(-1.0f, 1.0f);
|
||||
for (float& x : noise)
|
||||
{
|
||||
for (float& x : noise) {
|
||||
x = d(rng);
|
||||
}
|
||||
const RateCorrelation r = correlate_rate(hook, noise, 48000, standard_audio_rates());
|
||||
@@ -231,8 +215,7 @@ int main()
|
||||
check(!r.ok, "unrelated loopback is not a confident match");
|
||||
}
|
||||
|
||||
if (g_failures == 0)
|
||||
{
|
||||
if (g_failures == 0) {
|
||||
std::printf("PASS audio_correlation_test\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user