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 "flint/core/api.h"
5
6#include <SDL3_mixer/SDL_mixer.h>
7
8#include <memory>
9#include <unordered_map>
10
11namespace Flint {
20 class Audio {
21 public:
22 Audio() = default;
23
24 ~Audio() {
25 cleanup();
26 }
27
34 FLINT_API bool init();
35
42 FLINT_API void cleanup();
43
52 FLINT_API int loadSound(const void *data, const size_t size);
53
63 FLINT_API bool playSound(int id, bool loop = false, float volume = 1.0f, float pitch = 1.0f);
64
72 FLINT_API void pauseSound(int id);
73
79 FLINT_API void resumeSound(int id);
80
87 FLINT_API bool isPaused(const int id) const;
88
94 FLINT_API void stopSound(int id);
95
99 FLINT_API void stopAllSounds();
100
107 FLINT_API void setVolume(int id, float volume);
108
115 FLINT_API void setPitch(int id, float pitch);
116
126 FLINT_API void setLooping(int id, bool loop);
127
134 FLINT_API bool isPlaying(int id) const;
135
139 FLINT_API void pauseAll() const;
140
144 FLINT_API void resumeAll() const;
145
151 FLINT_API void setMasterVolume(float volume) const;
152
153 private:
160 MIX_Track *getOrCreateTrack(int soundId);
161
162 bool m_initialized = false;
163 MIX_Mixer *m_mixer = nullptr;
164 int m_nextSoundId = 1;
165
166 std::unordered_map<int, MIX_Audio *> m_audioData;
167 std::unordered_map<int, MIX_Track *> m_tracks;
168 };
169}
Manages audio playback via SDL3_mixer.
Definition audio.h:20
FLINT_API bool isPlaying(int id) const
Query whether a sound is currently playing (not stopped or finished).
FLINT_API void stopSound(int id)
Stop playback of a sound immediately.
FLINT_API void setMasterVolume(float volume) const
Set the master output volume for the entire mixer.
FLINT_API bool init()
Initialize the audio subsystem and open the default playback device.
FLINT_API void resumeSound(int id)
Resume a paused sound.
FLINT_API int loadSound(const void *data, const size_t size)
Load a sound from an in-memory audio file.
FLINT_API void pauseSound(int id)
Pause playback of the given sound.
FLINT_API void setVolume(int id, float volume)
Set the playback volume for a specific sound.
FLINT_API void resumeAll() const
Resume all tracks on the mixer device.
FLINT_API void cleanup()
Release all audio resources and shut down SDL_mixer.
FLINT_API bool isPaused(const int id) const
Query whether a sound is currently paused.
FLINT_API void stopAllSounds()
Stop all currently playing sounds immediately.
FLINT_API bool playSound(int id, bool loop=false, float volume=1.0f, float pitch=1.0f)
Begin playback of a previously loaded sound.
FLINT_API void pauseAll() const
Pause all tracks on the mixer device.
FLINT_API void setLooping(int id, bool loop)
Change the looping state of a currently playing sound.
FLINT_API void setPitch(int id, float pitch)
Set the playback pitch (frequency ratio) for a specific sound.