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;
25typedef std::string sound_id_t;
26typedef ma_sound sound_t;
27typedef ma_sound_group stream_t;
28
29} // namespace types
30
41class audio : public subsystem
42{
43public:
44
51 static std::unordered_map<types::sound_id_t, types::sound_t> sounds;
52
60 static std::unordered_map<types::stream_id_t, types::stream_t> streams;
61
62 static const std::string subsystem_name;
63
64 class builder;
65 enum class error;
66
67 // Subsystem interface
74 std::expected<void, subsystem::error> initialize() override;
75
82 std::expected<void, subsystem::error> terminate() override;
83
84 std::string name() override;
85 bool is_initialized() override;
86
87 // Constructors / destructors
88
89 audio() = default;
90 ~audio() = default;
91
92 // Member functions
93
94 static audio &instance();
95
101 static std::expected<void, audio::error>
102 load(const types::sound_id_t &sound_id,
103 const std::string &path,
104 const types::stream_id_t &stream_id = "default");
105
109 static std::expected<void, audio::error>
110 play(const types::sound_id_t &id);
111
112 //
113 // Stream functions
114 //
115
116 static std::expected<void, audio::error>
117 create_stream(const types::stream_id_t &id);
118 static types::stream_t *get_stream(const types::stream_id_t &id);
119
120 static std::expected<void, audio::error>
121 stream_stop(const types::stream_id_t &id);
122
123 static std::expected<void, audio::error>
124 stream_start(const types::stream_id_t &id);
130 static std::expected<void, audio::error>
131 stream_set_volume(const types::stream_id_t &id, float volume);
132
133private:
134
139 static std::vector<std::pair<types::stream_id_t, float>> init_streams;
140
145 static std::vector<std::tuple<types::sound_id_t,
146 std::string,
147 types::stream_id_t>> init_sounds;
148
149 static bool initialized;
150
151 // Backend
152 static ma_engine engine;
153
154};
155
157{
158private:
159
160 std::vector<std::tuple<types::sound_id_t, std::string,
161 types::stream_id_t>> init_sounds;
162 std::vector<std::pair<types::stream_id_t, float>> init_streams;
163 float volume = 1.0;
164
165public:
166
167 builder() = default;
168 ~builder() = default;
169
170 builder &sound(const types::sound_id_t &sound_id,
171 const std::string &path,
172 const types::stream_id_t &stream_id = "default");
173 builder &stream(const types::stream_id_t &id,
174 float volume = 1.0);
175
176 brenta::subsystem &build() override;
177
178};
179
180enum class audio::error : int
181{
182 init_from_file,
183 stream_not_found,
184 sound_not_found,
185 stream_init,
186 stream_stop,
187 stream_start,
188};
189
190} // namespace brenta
Audio subsystem.
Definition audio.hpp:42
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:51
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:60
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:47
Builder interface.
Definition subsystem.hpp:49
Subsystem interface.
Definition subsystem.hpp:22