From caebb7f7f112e8599e34d7f133d6ffee6dc6f679 Mon Sep 17 00:00:00 2001 From: Aleksey Selihov <1+faumaray@noreply@void.com> Date: Thu, 3 Dec 2026 07:03:09 +0000 Subject: [PATCH] feat(System): bounded PCM playback with owned samples and voice lifetimes (#18) --- tests/standalone/System/Audio.cpp | 126 ++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 tests/standalone/System/Audio.cpp diff --git a/tests/standalone/System/Audio.cpp b/tests/standalone/System/Audio.cpp new file mode 100644 index 0000000..be6bb16 --- /dev/null +++ b/tests/standalone/System/Audio.cpp @@ -0,0 +1,126 @@ +#include +#include +#include + +#include +#include +#include + +namespace +{ + +using Void::System::AudioDevice; +using Void::System::AudioStatus; +using Void::System::PcmBuffer; +using Void::System::VoiceState; + +[[nodiscard]] std::shared_ptr + buffer(std::uint32_t rate = 44100, std::uint16_t channels = 1, + std::size_t frames = 4) +{ + PcmBuffer value; + value.format = {.sample_rate = rate, .channels = channels}; + value.samples.assign(frames * channels, 100); + return std::make_shared(std::move(value)); +} + +} // namespace + +Void::Testing::Test audio_play_owns_samples_and_stop_releases() +{ + auto device = AudioDevice::open_silent(); + Void::Testing::require(device.has_value()); + + auto samples = buffer(); + std::weak_ptr weak = samples; + auto voice = device->play(samples, /*loop=*/true); + Void::Testing::require(voice.has_value()); + samples.reset(); + Void::Testing::expect(!weak.expired()); + + Void::Testing::expect(device->stop(*voice)); + Void::Testing::expect(weak.expired()); + Void::Testing::expect_eq(device->active_voices(), std::size_t{0}); +} + +Void::Testing::Test audio_voice_cap() +{ + auto device = AudioDevice::open_silent({.max_voices = 2}); + Void::Testing::require(device.has_value()); + + auto first = device->play(buffer(), true); + auto second = device->play(buffer(), true); + Void::Testing::require(first.has_value() && second.has_value()); + auto third = device->play(buffer(), true); + Void::Testing::require(!third.has_value()); + Void::Testing::expect(third.error().status == AudioStatus::voice_limit); + + Void::Testing::expect(device->stop(*first)); + auto reused = device->play(buffer(), true); + Void::Testing::expect(reused.has_value()); +} + +Void::Testing::Test audio_loop_stays_until_stopped() +{ + auto device = AudioDevice::open_silent(); + Void::Testing::require(device.has_value()); + auto voice = device->play(buffer(), true); + Void::Testing::require(voice.has_value()); + Void::Testing::expect(device->state(*voice) == VoiceState::playing); + Void::Testing::expect(device->stop(*voice)); + Void::Testing::expect(device->state(*voice) == VoiceState::unknown); +} + +Void::Testing::Test audio_non_loop_finishes_and_releases() +{ + auto device = AudioDevice::open_silent(); + Void::Testing::require(device.has_value()); + // A zero-length duration finishes on the next state query. + auto samples = buffer(1'000'000'000, 1, 1); + std::weak_ptr weak = samples; + auto voice = device->play(samples, /*loop=*/false); + Void::Testing::require(voice.has_value()); + samples.reset(); + Void::Testing::expect(device->state(*voice) == VoiceState::finished); + Void::Testing::expect(weak.expired()); +} + +Void::Testing::Test audio_close_stops_active_voices_and_reopens() +{ + auto device = AudioDevice::open_silent(); + Void::Testing::require(device.has_value()); + auto samples = buffer(); + std::weak_ptr weak = samples; + auto voice = device->play(samples, true); + Void::Testing::require(voice.has_value()); + samples.reset(); + + device->close(); + Void::Testing::expect(weak.expired()); + Void::Testing::expect_eq(device->active_voices(), std::size_t{0}); + auto rejected = device->play(buffer(), true); + Void::Testing::require(!rejected.has_value()); + Void::Testing::expect(rejected.error().status == AudioStatus::closed); + + auto reopened = AudioDevice::open_silent(); + Void::Testing::require(reopened.has_value()); + Void::Testing::expect(reopened->play(buffer(), true).has_value()); +} + +Void::Testing::Test audio_rejects_invalid_buffers() +{ + auto device = AudioDevice::open_silent(); + Void::Testing::require(device.has_value()); + auto null_buffer = device->play(nullptr); + Void::Testing::require(!null_buffer.has_value()); + Void::Testing::expect(null_buffer.error().status == + AudioStatus::invalid_buffer); + + PcmBuffer empty; + empty.format = {.sample_rate = 44100, .channels = 1}; + auto empty_buffer = device->play( + std::make_shared(std::move(empty))); + Void::Testing::require(!empty_buffer.has_value()); + Void::Testing::expect(empty_buffer.error().status == + AudioStatus::invalid_buffer); +}