summaryrefslogtreecommitdiff
path: root/src/audio_core/audio_manager.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/audio_core/audio_manager.h')
-rw-r--r--src/audio_core/audio_manager.h101
1 files changed, 101 insertions, 0 deletions
diff --git a/src/audio_core/audio_manager.h b/src/audio_core/audio_manager.h
new file mode 100644
index 000000000..70316e9cb
--- /dev/null
+++ b/src/audio_core/audio_manager.h
@@ -0,0 +1,101 @@
1// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#pragma once
5
6#include <array>
7#include <atomic>
8#include <functional>
9#include <mutex>
10#include <thread>
11
12#include "audio_core/audio_event.h"
13#include "core/hle/service/audio/errors.h"
14
15namespace Core {
16class System;
17}
18
19namespace AudioCore {
20
21namespace AudioOut {
22class Manager;
23}
24
25namespace AudioIn {
26class Manager;
27}
28
29/**
30 * The AudioManager's main purpose is to wait for buffer events for the audio in and out managers,
31 * and call an associated callback to release buffers.
32 *
33 * Execution pattern is:
34 * Buffers appended ->
35 * Buffers queued and played by the backend stream ->
36 * When consumed, set the corresponding manager event and signal the audio manager ->
37 * Consumed buffers are released, game is signalled ->
38 * Game appends more buffers.
39 *
40 * This is only used by audio in and audio out.
41 */
42class AudioManager {
43 using BufferEventFunc = std::function<void()>;
44
45public:
46 explicit AudioManager(Core::System& system);
47
48 /**
49 * Shutdown the audio manager.
50 */
51 void Shutdown();
52
53 /**
54 * Register the out manager, keeping a function to be called when the out event is signalled.
55 *
56 * @param buffer_func - Function to be called on signal.
57 * @return Result code.
58 */
59 Result SetOutManager(BufferEventFunc buffer_func);
60
61 /**
62 * Register the in manager, keeping a function to be called when the in event is signalled.
63 *
64 * @param buffer_func - Function to be called on signal.
65 * @return Result code.
66 */
67 Result SetInManager(BufferEventFunc buffer_func);
68
69 /**
70 * Set an event to signalled, and signal the thread.
71 *
72 * @param type - Manager type to set.
73 * @param signalled - Set the event to true or false?
74 */
75 void SetEvent(Event::Type type, bool signalled);
76
77private:
78 /**
79 * Main thread, waiting on a manager signal and calling the registered fucntion.
80 */
81 void ThreadFunc();
82
83 /// Core system
84 Core::System& system;
85 /// Have sessions started palying?
86 bool sessions_started{};
87 /// Is the main thread running?
88 std::atomic<bool> running{};
89 /// Unused
90 bool needs_update{};
91 /// Events to be set and signalled
92 Event events{};
93 /// Callbacks for each manager
94 std::array<BufferEventFunc, 3> buffer_events{};
95 /// General lock
96 std::mutex lock{};
97 /// Main thread for waiting and callbacks
98 std::jthread thread;
99};
100
101} // namespace AudioCore