Skip to content

Commit

Permalink
refactor: refactor null backend to satisfy p1386 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghost-LZW committed May 5, 2023
1 parent 630a0bc commit 7a04bd8
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 60 deletions.
31 changes: 16 additions & 15 deletions examples/level_meter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,22 @@ int main() {
if (!device)
return 1;

device->connect([&](audio_device &, audio_device_io<float> &io) noexcept {
if (!io.input_buffer.has_value())
return;

auto &in = *io.input_buffer;

for (int frame = 0; frame < in.size_frames(); ++frame) {
for (int channel = 0; channel < in.size_channels(); ++channel) {
float abs_value = std::abs(in(channel, frame));

if (abs_value > max_abs_value)
max_abs_value.store(abs_value);
}
}
});
device->connect<float>(
[&](audio_device &, audio_device_io<float> &io) noexcept {
if (!io.input_buffer.has_value())
return;

auto &in = *io.input_buffer;

for (int frame = 0; frame < in.size_frames(); ++frame) {
for (int channel = 0; channel < in.size_channels(); ++channel) {
float abs_value = std::abs(in(channel, frame));

if (abs_value > max_abs_value)
max_abs_value.store(abs_value);
}
}
});

device->start();
while (device->is_running()) {
Expand Down
2 changes: 1 addition & 1 deletion examples/melody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ int main() {
auto synth = synthesiser();
synth.set_sample_rate(float(device->get_sample_rate()));

device->connect(
device->connect<float>(
[=](audio_device &, audio_device_io<float> &io) mutable noexcept {
if (!io.output_buffer.has_value())
return;
Expand Down
6 changes: 3 additions & 3 deletions examples/print_devices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,19 @@ int main() {
print_all_devices();

set_audio_device_list_callback(
audio_device_list_event::device_list_changed, [] noexcept {
audio_device_list_event::device_list_changed, []() noexcept {
std::cout << "\n=== Audio device list changed! ===\n\n";
print_all_devices();
});

set_audio_device_list_callback(
audio_device_list_event::default_input_device_changed, [] noexcept {
audio_device_list_event::default_input_device_changed, []() noexcept {
std::cout << "\n=== Default input device changed! ===\n\n";
print_all_devices();
});

set_audio_device_list_callback(
audio_device_list_event::default_output_device_changed, [] noexcept {
audio_device_list_event::default_output_device_changed, []() noexcept {
std::cout << "\n=== Default output device changed! ===\n\n";
print_all_devices();
});
Expand Down
2 changes: 1 addition & 1 deletion examples/sine_wave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int main() {
float delta = 2.0f * frequency_hz * float(M_PI / device->get_sample_rate());
float phase = 0;

device->connect(
device->connect<float>(
[=](audio_device &, audio_device_io<float> &io) mutable noexcept {
if (!io.output_buffer.has_value())
return;
Expand Down
17 changes: 9 additions & 8 deletions examples/white_noise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ int main() {
std::uniform_real_distribution<float> white_noise(-1.0f, 1.0f);

if (auto device = get_default_audio_output_device()) {
device->connect([&](audio_device &, audio_device_io<float> &io) noexcept {
if (!io.output_buffer.has_value())
return;
device->connect<float>(
[&](audio_device &, audio_device_io<float> &io) noexcept {
if (!io.output_buffer.has_value())
return;

auto &out = *io.output_buffer;
auto &out = *io.output_buffer;

for (int frame = 0; frame < out.size_frames(); ++frame)
for (int channel = 0; channel < out.size_channels(); ++channel)
out(channel, frame) = white_noise(gen);
});
for (int frame = 0; frame < out.size_frames(); ++frame)
for (int channel = 0; channel < out.size_channels(); ++channel)
out(channel, frame) = white_noise(gen);
});

device->start();
std::this_thread::sleep_for(std::chrono::seconds(5));
Expand Down
4 changes: 2 additions & 2 deletions include/experimental/__p1386/audio_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enum class audio_device_list_event {
default_output_device_changed,
};

template <AudioDeviceListCallback F>
void set_audio_device_list_callback(audio_device_list_event, F &&cb);
void set_audio_device_list_callback(audio_device_list_event,
AudioDeviceListCallback auto &&cb);

_LIBSTDAUDIO_NAMESPACE_END
4 changes: 2 additions & 2 deletions include/experimental/audio_backend/__coreaudio_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,8 @@ struct __coreaudio_device_config_listener {
}
};

template <AudioDeviceListCallback F>
void set_audio_device_list_callback(audio_device_list_event event, F &&cb) {
void set_audio_device_list_callback(audio_device_list_event event,
AudioDeviceListCallback auto &&cb) {
__coreaudio_device_config_listener::register_callback(event,
function<void()>(cb));
}
Expand Down
31 changes: 14 additions & 17 deletions include/experimental/audio_backend/__null_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "experimental/__p1386/audio_buffer.h"
#include "experimental/__p1386/audio_device.h"
#include "experimental/__p1386/audio_event.h"
#include "experimental/__p1386/concepts.h"

_LIBSTDAUDIO_NAMESPACE_BEGIN

Expand Down Expand Up @@ -50,29 +51,24 @@ class audio_device {
return false;
}

template <typename _SampleType>
template <typename SampleType>
constexpr bool supports_sample_type() const noexcept {
return false;
}

constexpr bool can_connect() const noexcept { return false; }

template <typename _CallbackType> void connect(_CallbackType callback) {}
template <typename SampleType>
void connect(AudioIOCallback<SampleType> auto &&io_callback) {}

constexpr bool can_process() const noexcept { return false; }

using no_op_t = std::function<void(audio_device &)>;

template <typename _StartCallbackType = no_op_t,
typename _StopCallbackType = no_op_t,
// TODO: is_nothrow_invocable_t does not compile, temporarily
// replaced with is_invocable_t
typename = enable_if_t<
is_invocable_v<_StartCallbackType, audio_device &> &&
is_invocable_v<_StopCallbackType, audio_device &>>>
bool start(
_StartCallbackType &&start_callback = [](audio_device &) noexcept {},
_StopCallbackType &&stop_callback = [](audio_device &) noexcept {}) {
using no_op = decltype([](audio_device &) noexcept {});

template <AudioDeviceCallback StartCallback = no_op,
AudioDeviceCallback StopCallback = no_op>
bool start(StartCallback &&start_callback = {},
StopCallback &&stop_callback = {}) {
return false;
}

Expand All @@ -82,7 +78,8 @@ class audio_device {

void wait() const { assert(false); }

template <typename _CallbackType> void process(_CallbackType &) {
template <typename SampleType>
void process(AudioIOCallback<SampleType> auto &&io_callback) {
assert(false);
}

Expand All @@ -99,7 +96,7 @@ audio_device_list get_audio_input_device_list() { return {}; }

audio_device_list get_audio_output_device_list() { return {}; }

template <AudioDeviceListCallback F>
void set_audio_device_list_callback(audio_device_list_event event, F &&cb) {}
void set_audio_device_list_callback(audio_device_list_event event,
AudioDeviceListCallback auto &&cb) {}

_LIBSTDAUDIO_NAMESPACE_END
3 changes: 1 addition & 2 deletions include/experimental/audio_backend/__wasapi_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -915,9 +915,8 @@ class __audio_device_monitor {
array<unique_ptr<WASAPINotificationClient>, 3> _callback_monitors;
};

template <AudioDeviceListCallback F>
void set_audio_device_list_callback(audio_device_list_event event,
F &&callback) {
AudioDeviceListCallback auto &&callback) {
__audio_device_monitor::instance().register_callback(event,
std::move(callback));
}
Expand Down
18 changes: 9 additions & 9 deletions test/audio_device_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ TEST_CASE("Repeatedly stopping an output device is not an error") {
TEST_CASE("Starting an input device and registering a start callback") {
auto devices = get_audio_input_device_list();
for (auto &device : devices) {
auto start_cb = [](audio_device &) {};
auto start_cb = [](audio_device &) noexcept {};
device.start(std::move(start_cb));
}

Expand All @@ -321,7 +321,7 @@ TEST_CASE("Starting an input device and registering a start callback") {
TEST_CASE("Starting an output device and registering a start callback") {
auto devices = get_audio_output_device_list();
for (auto &device : devices) {
auto start_cb = [](audio_device &) {};
auto start_cb = [](audio_device &) noexcept {};
device.start(std::move(start_cb));
}

Expand All @@ -332,8 +332,8 @@ TEST_CASE("Starting an input device and registering both a start and a stop "
"callback") {
auto devices = get_audio_input_device_list();
for (auto &device : devices) {
auto start_cb = [](audio_device &) {};
auto stop_cb = [](audio_device &) {};
auto start_cb = [](audio_device &) noexcept {};
auto stop_cb = [](audio_device &) noexcept {};
device.start(std::move(start_cb), std::move(stop_cb));
}

Expand All @@ -344,8 +344,8 @@ TEST_CASE("Starting an output device and registering both a start and a stop "
"callback") {
auto devices = get_audio_output_device_list();
for (auto &device : devices) {
auto start_cb = [](audio_device &) {};
auto stop_cb = [](audio_device &) {};
auto start_cb = [](audio_device &) noexcept {};
auto stop_cb = [](audio_device &) noexcept {};
device.start(std::move(start_cb), std::move(stop_cb));
}

Expand All @@ -367,19 +367,19 @@ TEST_CASE("Stopping an output device that is not running must succeed") {
}

TEST_CASE("Register device list change callback") {
auto cb = [] noexcept {};
auto cb = []() noexcept {};
set_audio_device_list_callback(audio_device_list_event::device_list_changed,
cb);
}

TEST_CASE("Register default input device change callback") {
auto cb = [] noexcept {};
auto cb = []() noexcept {};
set_audio_device_list_callback(
audio_device_list_event::default_input_device_changed, cb);
}

TEST_CASE("Register default output device change callback") {
auto cb = [] noexcept {};
auto cb = []() noexcept {};
set_audio_device_list_callback(
audio_device_list_event::default_output_device_changed, cb);
}

0 comments on commit 7a04bd8

Please sign in to comment.