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 "../macros.hpp"
10#include <vector>
11#include <memory>
12#include <concepts>
13#include <coroutine>
14
15hi_export_module(hikogui.audio.audio_system);
16
17hi_export namespace hi { inline namespace v1 {
18
22hi_export class audio_system {
23public:
26 [[nodiscard]] static audio_system &global() noexcept;
27
28 audio_system() = default;
29 virtual ~audio_system() = default;
30 audio_system(audio_system const&) = delete;
31 audio_system(audio_system&&) = delete;
32 audio_system& operator=(audio_system const&) = delete;
33 audio_system& operator=(audio_system&&) = delete;
34
42 [[nodiscard]] virtual generator<audio_device &> devices() noexcept = 0;
43
48 template<forward_of<void()> Func>
49 [[nodiscard]] callback<void()> subscribe(Func&& func, callback_flags flags = callback_flags::synchronous) noexcept
50 {
51 return _notifier.subscribe(std::forward<Func>(func), flags);
52 }
53
54 auto operator co_await() const noexcept
55 {
56 return _notifier.operator co_await();
57 }
58
59protected:
60 notifier<void()> _notifier;
61};
62
63namespace detail {
64inline std::unique_ptr<audio_system> audio_system_global;
65}
66
67template<typename Context>
68concept audio_device_filter = std::convertible_to<Context, audio_device_state> or std::convertible_to<Context, audio_direction>;
69
70[[nodiscard]] constexpr bool match_audio_device(audio_device const &device) noexcept
71{
72 return true;
73}
74
75template<audio_device_filter FirstFilter, audio_device_filter... Filters>
76[[nodiscard]] inline bool match_audio_device(audio_device const &device, FirstFilter &&first_filter, Filters &&...filters) noexcept
77{
78 if constexpr (std::convertible_to<FirstFilter, audio_device_state>) {
79 if (device.state() != first_filter) {
80 return false;
81 }
82 } else if constexpr (std::convertible_to<FirstFilter, audio_direction>) {
83 if (not to_bool(device.direction() & first_filter)) {
84 return false;
85 }
86 } else {
87 hi_static_no_default();
88 }
89
90 return match_audio_device(device, std::forward<Filters>(filters)...);
91}
92
100template<audio_device_filter... Filters>
101[[nodiscard]] generator<audio_device &> audio_devices(Filters &&...filters) noexcept
102{
103 for (auto &device: audio_system::global().devices()) {
104 if (match_audio_device(device, std::forward<Filters>(filters)...)) {
105 co_yield device;
106 }
107 }
108}
109
110}} // namespace hi::inline v1
The HikoGUI namespace.
Definition array_generic.hpp:20
generator< audio_device & > audio_devices(Filters &&...filters) noexcept
Get audio devices matching the filter arguments.
Definition audio_system.hpp:101
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
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:230
callback< void()> subscribe(Func &&func, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a function to be called when the device list changes.
Definition audio_system.hpp:49
virtual generator< audio_device & > devices() noexcept=0
The devices that are part of the audio system.
Definition callback.hpp:77
Definition audio_system.hpp:68
True if T is a forwarded type of Forward.
Definition concepts.hpp:137