MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxvk_sound.hpp
Go to the documentation of this file.
1/**
2 * @file mxvk_sound.hpp
3 * @brief SDL3_mixer audio subsystem wrapper.
4 */
5#pragma once
6
7#if defined(MXVK_WITH_MIXER) || defined(WITH_MIXER)
8
9#include <SDL3/SDL.h>
10#include <SDL3_mixer/SDL_mixer.h>
11
12#include <cstddef>
13#include <memory>
14#include <string>
15#include <vector>
16
17namespace mxvk {
18
19 /**
20 * @class VK_Mixer
21 * @brief SDL3_mixer-based audio manager.
22 *
23 * Provides facilities for loading WAV sound chunks and streamed music
24 * tracks, playing them, and querying playback state.
25 * Only available when the library is built with MIXER enabled.
26 */
27 class VK_Mixer {
28 public:
29 /** @brief Initialise SDL3_mixer and open the audio device. */
30 VK_Mixer();
31
32 /** @brief Halt all playback and free all loaded audio resources. */
33 ~VK_Mixer() noexcept;
34
35 VK_Mixer(const VK_Mixer &) = delete;
36 VK_Mixer &operator=(const VK_Mixer &) = delete;
37 VK_Mixer(VK_Mixer &&) = delete;
39
40 /** @brief Open the audio device (called automatically by constructor). */
41 void init();
42
43 /**
44 * @brief Load a WAV file as a sound effect chunk.
45 * @param filename Path to the WAV file.
46 * @return Index used to reference this chunk in playWav().
47 */
48 int loadWav(const std::string &filename);
49
50 /**
51 * @brief Load an audio file as streaming background music.
52 * @param filename Path to the music file (OGG, MP3, WAV, etc.).
53 * @return Index used to reference this track in playMusic().
54 */
55 int loadMusic(const std::string &filename);
56
57 /**
58 * @brief Start playing a previously loaded music track.
59 * @param id Music index returned by loadMusic().
60 * @param value Number of additional loops (0 = play once, -1 = infinite).
61 * @return 0 on success, negative on failure.
62 */
63 int playMusic(int id, int value = 0);
64
65 /**
66 * @brief Play a previously loaded WAV chunk.
67 * @param id Chunk index returned by loadWav().
68 * @param value Number of additional loops (0 = play once).
69 * @param channel Track index to use (-1 = use track for @p id).
70 * @return The channel used for playback, or -1 on failure.
71 */
72 int playWav(int id, int value = 0, int channel = -1);
73
74 /**
75 * @brief Query whether a sound track is currently playing.
76 * @param channel Track index.
77 * @return @c true if the channel is active.
78 */
79 [[nodiscard]] bool isPlaying(int channel) const;
80
81 /**
82 * @brief Query whether a loaded music track is currently playing.
83 * @param id Music index returned by loadMusic().
84 * @return @c true if the music track is active.
85 */
86 [[nodiscard]] bool isMusicPlaying(int id) const;
87
88 /** @brief Free all loaded audio chunks and music tracks. */
89 void cleanup();
90
91 /** @brief Stop background music playback immediately. */
92 void stopMusic();
93
94 private:
95 using MixerHandle = std::unique_ptr<MIX_Mixer, decltype(&MIX_DestroyMixer)>;
96 using AudioHandle = std::unique_ptr<MIX_Audio, decltype(&MIX_DestroyAudio)>;
97 using TrackHandle = std::unique_ptr<MIX_Track, decltype(&MIX_DestroyTrack)>;
98
99 bool initialized = false; ///< Whether the mixer is initialized.
100 MixerHandle mixer{nullptr, MIX_DestroyMixer};
101 std::vector<AudioHandle> music_files{}; ///< Loaded music audio files.
102 std::vector<AudioHandle> wav_files{}; ///< Loaded sound effect audio files.
103 std::vector<TrackHandle> music_tracks{}; ///< Tracks assigned to music files.
104 std::vector<TrackHandle> wav_tracks{}; ///< Tracks assigned to wav files.
105
106 [[nodiscard]] static std::size_t toIndex(int value);
107 };
108
109 // Backward-compatible alias.
111
112} // namespace mxvk
113
114namespace mx {
117} // namespace mx
118
119#endif
SDL3_mixer-based audio manager.
int loadWav(const std::string &filename)
Load a WAV file as a sound effect chunk.
int playWav(int id, int value=0, int channel=-1)
Play a previously loaded WAV chunk.
void stopMusic()
Stop background music playback immediately.
void cleanup()
Free all loaded audio chunks and music tracks.
bool isPlaying(int channel) const
Query whether a sound track is currently playing.
void init()
Open the audio device (called automatically by constructor).
int loadMusic(const std::string &filename)
Load an audio file as streaming background music.
VK_Mixer(VK_Mixer &&)=delete
bool isMusicPlaying(int id) const
Query whether a loaded music track is currently playing.
VK_Mixer & operator=(VK_Mixer &&)=delete
VK_Mixer()
Initialise SDL3_mixer and open the audio device.
VK_Mixer & operator=(const VK_Mixer &)=delete
int playMusic(int id, int value=0)
Start playing a previously loaded music track.
VK_Mixer(const VK_Mixer &)=delete
~VK_Mixer() noexcept
Halt all playback and free all loaded audio resources.
mxvk::VK_Mixer VK_Mixer
mxvk::VK_Mixer Mixer
Utilities for loading and saving PNG images.
Definition mxvk.hpp:30
VK_Mixer Mixer