Brenta Engine 1.0
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 StreamName;
41
42typedef std::string AudioName;
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::AudioName, Types::AudioFile> audiofiles;
117 static std::unordered_map<Types::StreamName, SDL_AudioStream *> streams;
118
119 Audio() = delete;
127 static void Init();
134 static void Destroy();
135
145 static Types::AudioFile GetAudioFile(Types::AudioName name);
155 static SDL_AudioStream *GetStream(Types::StreamName name);
156
166 static void LoadAudio(Types::AudioName name, std::string path);
175 static void CreateStream(Types::StreamName);
187 static void PlayAudio(Types::AudioName, Types::StreamName = "default");
197 static void SetVolume(Types::StreamName name, int volume);
206 static void PauseStream(Types::StreamName name);
215 static void ResumeStream(Types::StreamName name);
224 static void ClearStream(Types::StreamName name);
225
226 private:
227 static void CheckError();
228};
229
230} // namespace Brenta
Audio subsystem.
static void Init()
Initialize the audio system.
static std::unordered_map< Types::AudioName, Types::AudioFile > audiofiles
Map of audio files.
static void Destroy()
Destroy the audio system.
static void ClearStream(Types::StreamName name)
Stop a stream.
static void SetVolume(Types::StreamName name, int volume)
Set the volume of a stream.
static void CreateStream(Types::StreamName)
Create an audio stream.
static void PlayAudio(Types::AudioName, Types::StreamName="default")
Play an audio file.
static SDL_AudioStream * GetStream(Types::StreamName name)
Get an audio stream.
static void LoadAudio(Types::AudioName name, std::string path)
Load an audio file.
static Types::AudioFile GetAudioFile(Types::AudioName name)
Get an audio file.
static void PauseStream(Types::StreamName name)
Pause a stream.
static void ResumeStream(Types::StreamName name)
Resume a stream.
static std::unordered_map< Types::StreamName, SDL_AudioStream * > streams
Map of audio streams.
Struct containing information about an audio file.
std::string path
The path to the audio file.
Uint32 audio_len
Length of audio buffer.
SDL_AudioSpec spec
Information about the audio format.
Uint8 * audio_buf
Pointer to audio buffer.