feat(System): bounded PCM playback with owned samples and voice lifetimes (#18)
This commit is contained in:
1 parent
df2ed107e2
commit
caebb7f7f1
1 file changed
+126
@@ -0,0 +1,126 @@
|
||||
#include <Void/System/Audio.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
using Void::System::AudioDevice;
|
||||
using Void::System::AudioStatus;
|
||||
using Void::System::PcmBuffer;
|
||||
using Void::System::VoiceState;
|
||||
|
||||
[[nodiscard]] std::shared_ptr<const PcmBuffer>
|
||||
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<const PcmBuffer>(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<const PcmBuffer> 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<const PcmBuffer> 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<const PcmBuffer> 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<const PcmBuffer>(std::move(empty)));
|
||||
Void::Testing::require(!empty_buffer.has_value());
|
||||
Void::Testing::expect(empty_buffer.error().status ==
|
||||
AudioStatus::invalid_buffer);
|
||||
}
|
||||
Reference in new issue
Block a user