Brenta Engine 1.1
Loading...
Searching...
No Matches
engine_audio.hpp
1/*
2 * MIT License
3 *
4 * Copyright (c) 2024 Giovanni Santini
5
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27#pragma once
28
29#include <SDL3/SDL_audio.h>
30#include <string>
31#include <unordered_map>
32#include <vector>
33
34namespace brenta
35{
36
37namespace types
38{
39
40typedef std::string stream_name_t;
41
42typedef std::string audio_name_t;
43
53{
57 std::string path;
61 Uint8 *audio_buf;
65 Uint32 audio_len;
69 SDL_AudioSpec spec;
70};
71
72/*
73 * typedef struct SDL_AudioSpec
74 * {
75 * SDL_AudioFormat format; // Audio data format
76 * int channels; // Number of channels: 1 mono, 2 stereo, etc
77 * int freq; // sample rate: sample frames per second
78 * } SDL_AudioSpec;
79 *
80 */
81
82} // namespace types
83
93class audio
94{
95 public:
105 static std::unordered_map<types::audio_name_t, types::audio_file_t>
118 static std::unordered_map<types::stream_name_t, SDL_AudioStream *> streams;
119
120 audio() = delete;
128 static void init();
135 static void destroy();
136
146 static types::audio_file_t get_audio_file(types::audio_name_t name);
156 static SDL_AudioStream *get_stream(types::stream_name_t name);
157
167 static void load_audio(types::audio_name_t name, std::string path);
176 static void create_stream(types::stream_name_t);
188 static void play_audio(types::audio_name_t,
189 types::stream_name_t = "default");
199 static void set_volume(types::stream_name_t name, int volume);
208 static void pause_stream(types::stream_name_t name);
217 static void resume_stream(types::stream_name_t name);
226 static void clear_stream(types::stream_name_t name);
227
228 private:
229 static void check_error_audio();
230};
231
232} // namespace brenta
Audio subsystem.
static void destroy()
Destroy the audio system.
static std::unordered_map< types::stream_name_t, SDL_AudioStream * > streams
Map of audio streams.
static void load_audio(types::audio_name_t name, std::string path)
Load an audio file.
static void resume_stream(types::stream_name_t name)
Resume a stream.
static std::unordered_map< types::audio_name_t, types::audio_file_t > audio_files
Map of audio files.
static void play_audio(types::audio_name_t, types::stream_name_t="default")
Play an audio file.
static types::audio_file_t get_audio_file(types::audio_name_t name)
Get an audio file.
static void clear_stream(types::stream_name_t name)
Stop a stream.
static void create_stream(types::stream_name_t)
Create an audio stream.
static void set_volume(types::stream_name_t name, int volume)
Set the volume of a stream.
static void pause_stream(types::stream_name_t name)
Pause a stream.
static void init()
Initialize the audio system.
static SDL_AudioStream * get_stream(types::stream_name_t name)
Get an audio stream.
Struct containing information about an audio file.
SDL_AudioSpec spec
Information about the audio format.
Uint8 * audio_buf
Pointer to audio buffer.
std::string path
The path to the audio file.
Uint32 audio_len
Length of audio buffer.