Formatting
publish / publish (push) Successful in 14m33s

This commit is contained in:
khodowany
2026-09-14 23:56:16 -05:00
parent 3816eb96c9
commit 59b96ddf95
16 changed files with 127 additions and 232 deletions
+7 -48
View File
@@ -15,6 +15,7 @@
#include <sonus.hpp>
namespace {
constexpr auto pi = 3.141592653589793238462643383279502884;
@@ -29,7 +30,7 @@ auto test_file(std::string_view name) -> std::filesystem::path {
return std::filesystem::temp_directory_path() / name;
}
auto peak(std::vector<std::float32_t> const& samples, std::size_t begin_frame, std::size_t frame_count, std::size_t channels) -> std::float32_t {
auto peak(std::vector<std::float32_t> const &samples, std::size_t begin_frame, std::size_t frame_count, std::size_t channels) -> std::float32_t {
auto result = std::float32_t{};
auto const begin = begin_frame * channels;
auto const end = std::min(samples.size(), begin + frame_count * channels);
@@ -39,11 +40,11 @@ auto peak(std::vector<std::float32_t> const& samples, std::size_t begin_frame, s
return result;
}
auto write_bytes(std::ofstream& output, std::string_view value) -> void {
auto write_bytes(std::ofstream &output, std::string_view value) -> void {
output.write(value.data(), static_cast<std::streamsize>(value.size()));
}
auto write_u16_le(std::ofstream& output, std::uint16_t value) -> void {
auto write_u16_le(std::ofstream &output, std::uint16_t value) -> void {
auto bytes = std::array<char, 2>{
static_cast<char>(value & 0xFFU),
static_cast<char>((value >> 8U) & 0xFFU),
@@ -51,7 +52,7 @@ auto write_u16_le(std::ofstream& output, std::uint16_t value) -> void {
output.write(bytes.data(), static_cast<std::streamsize>(bytes.size()));
}
auto write_u32_le(std::ofstream& output, std::uint32_t value) -> void {
auto write_u32_le(std::ofstream &output, std::uint32_t value) -> void {
auto bytes = std::array<char, 4>{
static_cast<char>(value & 0xFFU),
static_cast<char>((value >> 8U) & 0xFFU),
@@ -61,16 +62,14 @@ auto write_u32_le(std::ofstream& output, std::uint32_t value) -> void {
output.write(bytes.data(), static_cast<std::streamsize>(bytes.size()));
}
auto write_test_wav(std::filesystem::path const& path, std::uint32_t sample_rate, std::uint16_t channels, std::uint32_t frame_count, double frequency_hz) -> void {
auto write_test_wav(std::filesystem::path const &path, std::uint32_t sample_rate, std::uint16_t channels, std::uint32_t frame_count, double frequency_hz) -> void {
auto output = std::ofstream{path, std::ios::binary | std::ios::trunc};
BOOST_REQUIRE(output.good());
auto const bits_per_sample = std::uint16_t{16};
auto const bytes_per_sample = bits_per_sample / 8U;
auto const block_align = static_cast<std::uint16_t>(channels * bytes_per_sample);
auto const byte_rate = sample_rate * block_align;
auto const data_size = frame_count * block_align;
write_bytes(output, "RIFF");
write_u32_le(output, 36U + data_size);
write_bytes(output, "WAVE");
@@ -84,7 +83,6 @@ auto write_test_wav(std::filesystem::path const& path, std::uint32_t sample_rate
write_u16_le(output, bits_per_sample);
write_bytes(output, "data");
write_u32_le(output, data_size);
for (auto frame = std::uint32_t{}; frame < frame_count; ++frame) {
auto const t = static_cast<double>(frame) / static_cast<double>(sample_rate);
auto const sample = static_cast<std::int16_t>(std::sin(2.0 * pi * frequency_hz * t) * 12000.0);
@@ -92,7 +90,6 @@ auto write_test_wav(std::filesystem::path const& path, std::uint32_t sample_rate
write_u16_le(output, static_cast<std::uint16_t>(sample));
}
}
BOOST_REQUIRE(output.good());
}
@@ -101,7 +98,6 @@ auto write_test_wav(std::filesystem::path const& path, std::uint32_t sample_rate
BOOST_AUTO_TEST_CASE(sonus_validates_configs) {
auto audio = sonus::audio_config{};
BOOST_TEST(static_cast<bool>(sonus::validate(audio)));
audio.sample_rate = 0;
BOOST_TEST(!sonus::validate(audio));
audio = {};
@@ -136,7 +132,6 @@ BOOST_AUTO_TEST_CASE(sonus_validates_configs) {
audio = {};
audio.playback_device_name.assign("a\0b", 3);
BOOST_TEST(!sonus::validate(audio));
auto metronome = sonus::metronome_config{};
BOOST_TEST(static_cast<bool>(sonus::validate(metronome)));
metronome.bpm = 0;
@@ -155,13 +150,11 @@ BOOST_AUTO_TEST_CASE(sonus_playback_devices_can_be_enumerated) {
BOOST_TEST(!enumerated.error().message().empty());
return;
}
for (auto const& device : *enumerated) {
for (auto const &device : *enumerated) {
BOOST_TEST(!device.id.empty());
BOOST_TEST(device.id.find('\0') == std::string::npos);
BOOST_TEST(device.name.find('\0') == std::string::npos);
}
if (!enumerated->empty()) {
auto config = sonus::audio_config{};
config.playback_device_id = enumerated->front().id;
@@ -172,11 +165,9 @@ BOOST_AUTO_TEST_CASE(sonus_playback_devices_can_be_enumerated) {
BOOST_AUTO_TEST_CASE(sonus_no_device_engine_renders_silence_when_idle) {
auto created = sonus::engine::create(no_device_config());
BOOST_REQUIRE(created);
auto samples = std::vector<std::float32_t>(2048U * created->channels(), 1.0F);
auto rendered = created->render(samples.data(), 2048U);
BOOST_REQUIRE(rendered);
BOOST_TEST(std::all_of(samples.begin(), samples.end(), [](auto sample) {
return sample == 0.0F;
}));
@@ -185,7 +176,6 @@ BOOST_AUTO_TEST_CASE(sonus_no_device_engine_renders_silence_when_idle) {
BOOST_AUTO_TEST_CASE(sonus_master_volume_can_be_changed_concurrently) {
auto created = sonus::engine::create(no_device_config());
BOOST_REQUIRE(created);
auto failed = std::atomic_bool{false};
auto volume_worker = [&created, &failed](std::uint32_t offset) {
for (auto index = std::uint32_t{}; index < 250U; ++index) {
@@ -203,14 +193,12 @@ BOOST_AUTO_TEST_CASE(sonus_master_volume_can_be_changed_concurrently) {
}
}
};
auto first = std::thread{volume_worker, 0U};
auto second = std::thread{volume_worker, 37U};
auto renderer = std::thread{render_worker};
first.join();
second.join();
renderer.join();
BOOST_TEST(!failed.load(std::memory_order_acquire));
BOOST_TEST(!created->set_master_volume(-0.01F));
BOOST_TEST(!created->set_master_volume(1.01F));
@@ -222,21 +210,17 @@ BOOST_AUTO_TEST_CASE(sonus_metronome_renders_accented_clicks) {
config.channels = 2;
auto created = sonus::engine::create(config);
BOOST_REQUIRE(created);
auto metro = sonus::metronome_config{};
metro.bpm = 120;
metro.beats_per_bar = 4;
metro.volume = 0.70F;
metro.click_duration = std::chrono::milliseconds{25};
BOOST_REQUIRE(created->start_metronome(metro));
auto samples = std::vector<std::float32_t>(config.sample_rate * config.channels);
BOOST_REQUIRE(created->render(samples.data(), config.sample_rate));
auto downbeat_peak = peak(samples, 0, 2400, config.channels);
auto beat_peak = peak(samples, config.sample_rate / 2, 2400, config.channels);
auto between_peak = peak(samples, config.sample_rate / 4, 2400, config.channels);
BOOST_TEST(downbeat_peak > 0.2F);
BOOST_TEST(beat_peak > 0.1F);
BOOST_TEST(downbeat_peak > beat_peak * 1.2F);
@@ -246,12 +230,10 @@ BOOST_AUTO_TEST_CASE(sonus_metronome_renders_accented_clicks) {
BOOST_AUTO_TEST_CASE(sonus_metronome_update_keeps_inactive_metronome_stopped) {
auto created = sonus::engine::create(no_device_config());
BOOST_REQUIRE(created);
auto metro = sonus::metronome_config{};
metro.bpm = 140;
BOOST_REQUIRE(created->update_metronome(metro));
BOOST_TEST(!created->metronome_active());
BOOST_REQUIRE(created->start_metronome(metro));
BOOST_TEST(created->metronome_active());
metro.bpm = 180;
@@ -265,7 +247,6 @@ BOOST_AUTO_TEST_CASE(sonus_stopped_metronome_updates_stay_silent_and_restart) {
config.channels = 2;
auto created = sonus::engine::create(config);
BOOST_REQUIRE(created);
for (auto index = std::uint32_t{}; index < 500U; ++index) {
auto metro = sonus::metronome_config{};
metro.bpm = 60U + (index % 180U);
@@ -274,13 +255,11 @@ BOOST_AUTO_TEST_CASE(sonus_stopped_metronome_updates_stay_silent_and_restart) {
BOOST_REQUIRE(created->update_metronome(metro));
BOOST_TEST(!created->metronome_active());
}
auto samples = std::vector<std::float32_t>(4096U * config.channels, 1.0F);
BOOST_REQUIRE(created->render(samples.data(), 4096U));
BOOST_TEST(std::all_of(samples.begin(), samples.end(), [](auto sample) {
return sample == 0.0F;
}));
auto metro = sonus::metronome_config{};
metro.bpm = 120;
BOOST_REQUIRE(created->start_metronome(metro));
@@ -292,7 +271,6 @@ BOOST_AUTO_TEST_CASE(sonus_stopped_metronome_updates_stay_silent_and_restart) {
BOOST_AUTO_TEST_CASE(sonus_metronome_can_be_updated_concurrently) {
auto created = sonus::engine::create(no_device_config());
BOOST_REQUIRE(created);
auto worker = [&created](std::uint32_t offset) {
for (auto index = std::uint32_t{}; index < 100U; ++index) {
auto metro = sonus::metronome_config{};
@@ -304,14 +282,12 @@ BOOST_AUTO_TEST_CASE(sonus_metronome_can_be_updated_concurrently) {
}
}
};
auto first = std::thread{worker, 0U};
auto second = std::thread{worker, 11U};
auto third = std::thread{worker, 23U};
first.join();
second.join();
third.join();
auto samples = std::vector<std::float32_t>(4096U * created->channels());
BOOST_TEST(static_cast<bool>(created->render(samples.data(), 4096U)));
}
@@ -319,7 +295,6 @@ BOOST_AUTO_TEST_CASE(sonus_metronome_can_be_updated_concurrently) {
BOOST_AUTO_TEST_CASE(sonus_file_playback_reports_missing_files) {
auto created = sonus::engine::create(no_device_config());
BOOST_REQUIRE(created);
auto missing = std::filesystem::path{"/tmp/sonus-missing-file.wav"};
auto played = created->play_file(missing);
BOOST_TEST(!played);
@@ -331,18 +306,14 @@ BOOST_AUTO_TEST_CASE(sonus_file_playback_renders_audio) {
config.channels = 2;
auto created = sonus::engine::create(config);
BOOST_REQUIRE(created);
auto path = test_file("sonus-tone-long.wav");
write_test_wav(path, config.sample_rate, 1, config.sample_rate, 440.0);
auto played = created->play_file(path);
BOOST_REQUIRE(played);
BOOST_TEST(created->active_sound_count() == 1U);
auto samples = std::vector<std::float32_t>(4096U * config.channels);
BOOST_REQUIRE(created->render(samples.data(), 4096U));
BOOST_TEST(peak(samples, 0, 4096U, config.channels) > 0.05F);
auto playing = created->is_playing(*played);
BOOST_REQUIRE(playing);
BOOST_TEST(*playing);
@@ -354,16 +325,12 @@ BOOST_AUTO_TEST_CASE(sonus_finished_file_playback_can_be_collected) {
config.channels = 2;
auto created = sonus::engine::create(config);
BOOST_REQUIRE(created);
auto path = test_file("sonus-tone-short.wav");
write_test_wav(path, config.sample_rate, 1, 256U, 880.0);
auto played = created->play_file(path);
BOOST_REQUIRE(played);
auto samples = std::vector<std::float32_t>(2048U * config.channels);
BOOST_REQUIRE(created->render(samples.data(), 2048U));
auto playing = created->is_playing(*played);
BOOST_REQUIRE(playing);
BOOST_TEST(!*playing);
@@ -376,10 +343,8 @@ BOOST_AUTO_TEST_CASE(sonus_repeated_short_sounds_are_retired) {
config.channels = 2;
auto created = sonus::engine::create(config);
BOOST_REQUIRE(created);
auto path = test_file("sonus-tone-repeated.wav");
write_test_wav(path, config.sample_rate, 1, 64U, 660.0);
auto samples = std::vector<std::float32_t>(512U * config.channels);
auto collected = std::uint64_t{};
for (auto index = 0; index < 20; ++index) {
@@ -388,7 +353,6 @@ BOOST_AUTO_TEST_CASE(sonus_repeated_short_sounds_are_retired) {
BOOST_REQUIRE(created->render(samples.data(), 512U));
collected += created->collect_finished();
}
BOOST_TEST(collected == 20U);
BOOST_TEST(created->active_sound_count() == 0U);
}
@@ -399,10 +363,8 @@ BOOST_AUTO_TEST_CASE(sonus_no_device_render_serializes_with_control_paths) {
config.channels = 2;
auto created = sonus::engine::create(config);
BOOST_REQUIRE(created);
auto path = test_file("sonus-tone-concurrent.wav");
write_test_wav(path, config.sample_rate, 1, 512U, 330.0);
auto render_worker = [&created, channels = config.channels] {
auto samples = std::vector<std::float32_t>(512U * channels);
for (auto index = 0; index < 150; ++index) {
@@ -420,7 +382,6 @@ BOOST_AUTO_TEST_CASE(sonus_no_device_render_serializes_with_control_paths) {
} else {
static_cast<void>(created->start_metronome(metro));
}
auto played = created->play_file(path);
if (played && index % 3 == 0) {
static_cast<void>(created->stop(*played));
@@ -428,12 +389,10 @@ BOOST_AUTO_TEST_CASE(sonus_no_device_render_serializes_with_control_paths) {
static_cast<void>(created->collect_finished());
}
};
auto renderer = std::thread{render_worker};
auto controller = std::thread{control_worker};
renderer.join();
controller.join();
created->stop_all();
created->stop_metronome();
BOOST_TEST(created->active_sound_count() == 0U);