Flint Engine
Modular C++20 2D game engine powered by SDL3
Loading...
Searching...
No Matches
audio.h
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2025-2026 Manuel Osorio
3#pragma once
4#include <SDL3_mixer/SDL_mixer.h>
5
6#include <memory>
7#include <unordered_map>
8
9namespace Flint {
18 class Audio {
19 public:
20 Audio() = default;
21
22 ~Audio() {
23 cleanup();
24 }
25
32 bool init();
33
40 void cleanup();
41
50 int loadSound(const void *data, const size_t size);
51
61 bool playSound(int id, bool loop = false, float volume = 1.0f, float pitch = 1.0f);
62
70 void pauseSound(int id);
71
77 void resumeSound(int id);
78
85 bool isPaused(const int id) const;
86
92 void stopSound(int id);
93
98
105 void setVolume(int id, float volume);
106
113 void setPitch(int id, float pitch);
114
124 void setLooping(int id, bool loop);
125
132 bool isPlaying(int id) const;
133
137 void pauseAll() const;
138
142 void resumeAll() const;
143
149 void setMasterVolume(float volume) const;
150
151 private:
158 MIX_Track *getOrCreateTrack(int soundId);
159
160 bool m_initialized = false;
161 MIX_Mixer *m_mixer = nullptr;
162 int m_nextSoundId = 1;
163
164 std::unordered_map<int, MIX_Audio *> m_audioData;
165 std::unordered_map<int, MIX_Track *> m_tracks;
166 };
167}
void stopSound(int id)
Stop playback of a sound immediately.
void cleanup()
Release all audio resources and shut down SDL_mixer.
bool init()
Initialize the audio subsystem and open the default playback device.
void setVolume(int id, float volume)
Set the playback volume for a specific sound.
bool playSound(int id, bool loop=false, float volume=1.0f, float pitch=1.0f)
Begin playback of a previously loaded sound.
void resumeSound(int id)
Resume a paused sound.
void setLooping(int id, bool loop)
Change the looping state of a currently playing sound.
bool isPaused(const int id) const
Query whether a sound is currently paused.
bool isPlaying(int id) const
Query whether a sound is currently playing (not stopped or finished).
void setPitch(int id, float pitch)
Set the playback pitch (frequency ratio) for a specific sound.
void pauseSound(int id)
Pause playback of the given sound.
int loadSound(const void *data, const size_t size)
Load a sound from an in-memory audio file.
void setMasterVolume(float volume) const
Set the master output volume for the entire mixer.
Manages audio playback via SDL3_mixer.
Definition audio.h:18
void resumeAll() const
Resume all tracks on the mixer device.
void pauseAll() const
Pause all tracks on the mixer device.
void stopAllSounds()
Stop all currently playing sounds immediately.