HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
audio_system.hpp
1// Copyright Take Vos 2020-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "audio_device.hpp"
8#include "../utility/utility.hpp"
9#include "../coroutine/module.hpp"
10#include "../macros.hpp"
11#include <vector>
12#include <memory>
13#include <concepts>
14
15hi_export_module(hikogui.audio.audio_system);
16
17namespace hi { inline namespace v1 {
18
22hi_export class audio_system {
23public:
24 using notifier_type = notifier<>;
25 using callback_token = notifier_type::callback_token;
26 using callback_proto = notifier_type::callback_proto;
27
30 [[nodiscard]] static audio_system &global() noexcept;
31
32 audio_system() = default;
33 virtual ~audio_system() = default;
34 audio_system(audio_system const&) = delete;
35 audio_system(audio_system&&) = delete;
36 audio_system& operator=(audio_system const&) = delete;
37 audio_system& operator=(audio_system&&) = delete;
38
47
52 callback_token subscribe(forward_of<callback_proto> auto&& func, callback_flags flags = callback_flags::synchronous) noexcept
53 {
54 return _notifier.subscribe(hi_forward(func), flags);
55 }
56
57 auto operator co_await() noexcept
58 {
59 return _notifier.operator co_await();
60 }
61
62protected:
63 inline static std::unique_ptr<audio_system> _global;
64
65 notifier_type _notifier;
66};
67
68template<typename Context>
69concept audio_device_filter = std::same_as<Context, audio_device_state> or std::same_as<Context, audio_direction>;
70
71[[nodiscard]] constexpr bool match_audio_device(audio_device const &device) noexcept
72{
73 return true;
74}
75
77[[nodiscard]] inline bool match_audio_device(audio_device const &device, FirstFilter &&first_filter, Filters &&...filters) noexcept
78{
79 if constexpr (std::same_as<FirstFilter, audio_device_state>) {
80 if (device.state() != first_filter) {
81 return false;
82 }
83 } else if constexpr (std::same_as<FirstFilter, audio_direction>) {
84 if (not to_bool(device.direction() & first_filter)) {
85 return false;
86 }
87 } else {
88 hi_static_no_default();
89 }
90
91 return match_audio_device(device, std::forward<Filters>(filters)...);
92}
93
101template<audio_device_filter... Filters>
103{
104 for (auto &device: audio_system::global().devices()) {
105 if (match_audio_device(device, std::forward<Filters>(filters)...)) {
106 co_yield device;
107 }
108 }
109}
110
111}} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:16
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
generator< audio_device & > audio_devices(Filters &&...filters) noexcept
Get audio devices matching the filter arguments.
Definition audio_system.hpp:102
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
A set of audio channels which can be rendered and/or captures at the same time.
Definition audio_device.hpp:34
Definition audio_system.hpp:22
static audio_system & global() noexcept
Create an audio system object specific for the current operating system.
Definition audio_system_win32.hpp:229
callback_token subscribe(forward_of< callback_proto > auto &&func, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a function to be called when the device list changes.
Definition audio_system.hpp:52
virtual generator< audio_device & > devices() noexcept=0
The devices that are part of the audio system.
Definition audio_system.hpp:69
True if T is a forwarded type of Forward.
Definition concepts.hpp:131