27#include "engine_audio.hpp"
29#include "SDL3/SDL_init.h"
30#include "engine_logger.hpp"
34using namespace Brenta;
35using namespace Brenta::Utils;
38std::unordered_map<Types::StreamName, SDL_AudioStream *>
Audio::streams;
42 if (SDL_Init(SDL_INIT_AUDIO))
44 auto error = SDL_GetError();
45 ERROR(
"SDL Audio failed to initialize: ", error);
50 INFO(
"SDL Audio initialized");
56 SDL_DestroyAudioStream(stream.second);
60 SDL_free(audiofile.second.audio_buf);
64 INFO(
"SDL Audio destroyed");
70 audiofile.
path = path;
72 if (SDL_LoadWAV(path.c_str(), &audiofile.
spec, &audiofile.
audio_buf,
75 auto error = SDL_GetError();
76 ERROR(
"SDL Audio failed to load WAV file: ", error);
81 INFO(
"Loaded audio at ", path);
85 Types::StreamName stream_name)
88 if (stream ==
nullptr)
90 ERROR(
"Could not play audio: Audio stream not found");
96 if (SDL_PutAudioStreamData(stream, audiofile.audio_buf,
103 SDL_AudioStream *stream = SDL_OpenAudioDeviceStream(
104 SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, NULL, NULL, NULL);
107 const char *error = SDL_GetError();
108 ERROR(
"SDL Audio failed to create stream: ", error);
114 INFO(
"SDL Audio stream created");
121 ERROR(
"Audio file not found with name: ", name);
131 ERROR(
"Audio stream not found with name: " + name);
140 if (stream ==
nullptr)
142 ERROR(
"Could not set volume: Audio stream not found");
146 if (SDL_SetAudioStreamGain(stream, volume))
148 INFO(
"Volume set to ", volume);
151void Audio::CheckError()
153 auto error = SDL_GetError();
154 ERROR(
"SDL Audio error: ", error);
160 if (stream ==
nullptr)
162 ERROR(
"Could not clear stream: Audio stream not found");
166 if (SDL_ClearAudioStream(stream))
168 INFO(
"Stream cleared");
174 if (stream ==
nullptr)
176 ERROR(
"Could not pause stream: Audio stream not found");
180 if (SDL_PauseAudioStreamDevice(stream))
182 INFO(
"Stream paused");
188 if (stream ==
nullptr)
190 ERROR(
"Could not resume stream: Audio stream not found");
194 if (SDL_ResumeAudioStreamDevice(stream))
196 INFO(
"Stream resumed");
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.