27#include "engine_audio.hpp"
29#include "SDL3/SDL_init.h"
30#include "engine_logger.hpp"
34using namespace brenta;
37std::unordered_map<types::stream_name_t, SDL_AudioStream *>
audio::streams;
41 if (SDL_Init(SDL_INIT_AUDIO))
43 auto error = SDL_GetError();
44 ERROR(
"SDL Audio failed to initialize: {}", error);
49 INFO(
"SDL Audio initialized");
55 SDL_DestroyAudioStream(stream.second);
59 SDL_free(audiofile.second.audio_buf);
63 INFO(
"SDL Audio destroyed");
69 audiofile.
path = path;
71 if (SDL_LoadWAV(path.c_str(), &audiofile.
spec, &audiofile.
audio_buf,
74 auto error = SDL_GetError();
75 ERROR(
"SDL Audio failed to load WAV file: {}", error);
80 INFO(
"Loaded audio at {}", path);
84 types::stream_name_t stream_name)
87 if (stream ==
nullptr)
89 ERROR(
"Could not play audio: Audio stream not found");
95 if (SDL_PutAudioStreamData(stream, audiofile.audio_buf,
102 SDL_AudioStream *stream = SDL_OpenAudioDeviceStream(
103 SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, NULL, NULL, NULL);
106 const char *error = SDL_GetError();
107 ERROR(
"SDL Audio failed to create stream: {}", error);
113 INFO(
"SDL Audio stream created");
120 ERROR(
"Audio file not found with name: {}", name);
130 ERROR(
"Audio stream not found with name: {}", name);
139 if (stream ==
nullptr)
141 ERROR(
"Could not set volume: Audio stream not found");
145 if (SDL_SetAudioStreamGain(stream, volume))
147 INFO(
"Volume set to {}", volume);
150void audio::check_error_audio()
152 auto error = SDL_GetError();
153 ERROR(
"SDL Audio error: {}", error);
159 if (stream ==
nullptr)
161 ERROR(
"Could not clear stream: Audio stream not found");
165 if (SDL_ClearAudioStream(stream))
167 INFO(
"Stream cleared");
173 if (stream ==
nullptr)
175 ERROR(
"Could not pause stream: Audio stream not found");
179 if (SDL_PauseAudioStreamDevice(stream))
181 INFO(
"Stream paused");
187 if (stream ==
nullptr)
189 ERROR(
"Could not resume stream: Audio stream not found");
193 if (SDL_ResumeAudioStreamDevice(stream))
195 INFO(
"Stream resumed");
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.