Brenta Engine 1.2
Loading...
Searching...
No Matches
audio.hpp
1// SPDX-License-Identifier: MIT
2// Author: Giovanni Santini
3// Mail: giovanni.santini@proton.me
4// Github: @San7o
5
6#pragma once
7
8#include <brenta/subsystem.hpp>
9
10// Backend
11#include <miniaudio/miniaudio.h>
12
13#include <string>
14#include <unordered_map>
15#include <vector>
16#include <tuple>
17
18namespace brenta
19{
20
21namespace types
22{
23
24typedef std::string stream_id_t;
25
26typedef std::string sound_id_t;
27
28typedef ma_sound sound_t;
29typedef ma_sound_group stream_t;
30
31} // namespace types
32
43class audio : public subsystem
44{
45public:
46
53 static std::unordered_map<types::sound_id_t, types::sound_t> sounds;
54
62 static std::unordered_map<types::stream_id_t, types::stream_t> streams;
63
64 static const std::string subsystem_name;
65
66 class builder;
67 enum class error;
68
69 // Subsystem interface
76 std::expected<void, subsystem::error> initialize() override;
77
84 std::expected<void, subsystem::error> terminate() override;
85
86 std::string name() override;
87 bool is_initialized() override;
88
89 // Constructors / destructors
90
91 audio() = default;
92 ~audio() = default;
93
94 // Member functions
95
96 static audio &instance();
97
103 static std::expected<void, audio::error>
104 load(const types::sound_id_t &sound_id,
105 const std::string &path,
106 const types::stream_id_t &stream_id = "default");
107
111 static std::expected<void, audio::error>
112 play(const types::sound_id_t &id);
113
114 //
115 // Stream functions
116 //
117
118 static std::expected<void, audio::error>
119 create_stream(const types::stream_id_t &id);
120 static types::stream_t *get_stream(const types::stream_id_t &id);
121
122 static std::expected<void, audio::error>
123 stream_stop(const types::stream_id_t &id);
124 static std::expected<void, audio::error>
125 stream_start(const types::stream_id_t &id);
131 static std::expected<void, audio::error>
132 stream_set_volume(const types::stream_id_t &id, float volume);
133
134private:
135
140 static std::vector<std::pair<types::stream_id_t, float>> init_streams;
141
146 static std::vector<std::tuple<types::sound_id_t,
147 std::string,
148 types::stream_id_t>> init_sounds;
149
150 static bool initialized;
151
152 // Backend
153 static ma_engine engine;
154
155};
156
158{
159private:
160
161 std::vector<std::tuple<types::sound_id_t, std::string,
162 types::stream_id_t>> init_sounds;
163 std::vector<std::pair<types::stream_id_t, float>> init_streams;
164 float volume = 1.0;
165
166public:
167
168 builder() = default;
169 ~builder() = default;
170
171 builder &sound(const types::sound_id_t &sound_id,
172 const std::string &path,
173 const types::stream_id_t &stream_id = "default");
174 builder &stream(const types::stream_id_t &id,
175 float volume = 1.0);
176
177 brenta::subsystem &build() override;
178
179};
180
181enum class audio::error : int
182{
183 init_from_file,
184 stream_not_found,
185 sound_not_found,
186 stream_init,
187 stream_stop,
188 stream_start,
189};
190
191} // namespace brenta
Audio subsystem.
Definition audio.hpp:44
std::expected< void, subsystem::error > terminate() override
Terminate the audio system.
Definition audio.cpp:65
static std::unordered_map< types::sound_id_t, types::sound_t > sounds
Map of sound files.
Definition audio.hpp:53
std::expected< void, subsystem::error > initialize() override
Initialize the audio subsystem.
Definition audio.cpp:28
static std::expected< void, audio::error > load(const types::sound_id_t &sound_id, const std::string &path, const types::stream_id_t &stream_id="default")
Load a sound from path on a stream.
Definition audio.cpp:103
bool is_initialized() override
Returns true if the subsystem is initialized.
Definition audio.cpp:87
static std::unordered_map< types::stream_id_t, types::stream_t > streams
Map of audio streams.
Definition audio.hpp:62
static std::expected< void, audio::error > stream_set_volume(const types::stream_id_t &id, float volume)
Set the volume of a stream.
Definition audio.cpp:180
static std::expected< void, audio::error > play(const types::sound_id_t &id)
Play a sound on its stream.
Definition audio.cpp:136
std::string name() override
Returns the name of the sybsystem.
Definition audio.cpp:82
Engine class.
Definition engine.hpp:46
Builder interface.
Definition subsystem.hpp:49
Subsystem interface.
Definition subsystem.hpp:22