libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
sound.hpp
Go to the documentation of this file.
1/**
2 * @file sound.hpp
3 * @brief SDL_mixer audio subsystem wrapper (requires WITH_MIXER build flag).
4 *
5 * The Mixer class manages loading and playback of both background music
6 * (Mix_Music) and short sound effects (Mix_Chunk).
7 */
8#ifndef __SOUND__HPP_H__
9#define __SOUND__HPP_H__
10#ifdef __EMSCRIPTEN__
11#include"config.hpp"
12#else
13#include"config.h"
14#endif
15#ifdef WITH_MIXER
16#include"SDL.h"
17#include"SDL_mixer.h"
18#include<vector>
19#include<string>
20
21namespace mx {
22
23/**
24 * @class Mixer
25 * @brief SDL_mixer–based audio manager.
26 *
27 * Provides facilities for loading WAV sound chunks and streamed music
28 * tracks, playing them on SDL_mixer channels, and querying playback state.
29 * Only available when the library is built with WITH_MIXER defined.
30 */
31 class Mixer {
32 public:
33 /** @brief Initialise SDL_mixer and open the audio device. */
34 Mixer();
35
36 /** @brief Halt all playback and free all loaded audio resources. */
37 ~Mixer();
38
39 Mixer(const Mixer&) = delete;
40 Mixer& operator=(const Mixer&) = delete;
41 Mixer(Mixer&&) = delete;
42 Mixer& operator=(Mixer&&) = delete;
43
44 /** @brief Open the audio device (called automatically by constructor). */
45 void init();
46
47 /**
48 * @brief Load a WAV file as a sound effect chunk.
49 * @param filename Path to the WAV file.
50 * @return Index used to reference this chunk in playWav().
51 */
52 int loadWav(const std::string &filename);
53
54 /**
55 * @brief Load an audio file as streaming background music.
56 * @param filename Path to the music file (OGG, MP3, WAV, etc.).
57 * @return Index used to reference this track in playMusic().
58 */
59 int loadMusic(const std::string &filename);
60
61 /**
62 * @brief Start playing a previously loaded music track.
63 * @param id Music index returned by loadMusic().
64 * @param value Number of additional loops (0 = play once, -1 = infinite).
65 * @return 0 on success, negative on failure.
66 */
67 int playMusic(int id, int value = 0);
68
69 /**
70 * @brief Play a previously loaded WAV chunk.
71 * @param id Chunk index returned by loadWav().
72 * @param value Number of additional loops (0 = play once).
73 * @param channel SDL_mixer channel to play on (0 = first available).
74 * @return The channel used for playback, or -1 on failure.
75 */
76 int playWav(int id, int value = 0, int channel = 0);
77
78 /**
79 * @brief Query whether a mixer channel is currently playing.
80 * @param channel SDL_mixer channel number.
81 * @return @c true if the channel is active.
82 */
83 bool isPlaying(int channel) const;
84
85 /** @brief Free all loaded audio chunks and music tracks. */
86 void cleanup();
87
88 /** @brief Stop background music playback immediately. */
89 void stopMusic();
90 private:
91 bool init_ = false; ///< Whether the audio device is open.
92 std::vector<Mix_Music *> files; ///< Loaded music tracks.
93 std::vector<Mix_Chunk *> wav; ///< Loaded WAV chunks.
94 };
95}
96
97#endif
98#endif
void init()
Open the audio device (called automatically by constructor).
Definition sound.cpp:56
int playWav(int id, int value=0, int channel=0)
Play a previously loaded WAV chunk.
Definition sound.cpp:98
std::vector< Mix_Chunk * > wav
Loaded WAV chunks.
Definition sound.hpp:93
Mixer(Mixer &&)=delete
Mixer(const Mixer &)=delete
void cleanup()
Free all loaded audio chunks and music tracks.
Definition sound.cpp:23
bool isPlaying(int channel) const
Query whether a mixer channel is currently playing.
Definition sound.cpp:105
Mixer & operator=(const Mixer &)=delete
bool init_
Whether the audio device is open.
Definition sound.hpp:91
Mixer & operator=(Mixer &&)=delete
int playMusic(int id, int value=0)
Start playing a previously loaded music track.
Definition sound.cpp:74
Mixer()
Initialise SDL_mixer and open the audio device.
Definition sound.cpp:16
std::vector< Mix_Music * > files
Loaded music tracks.
Definition sound.hpp:92
void stopMusic()
Stop background music playback immediately.
Definition sound.cpp:47
int loadWav(const std::string &filename)
Load a WAV file as a sound effect chunk.
Definition sound.cpp:87
int loadMusic(const std::string &filename)
Load an audio file as streaming background music.
Definition sound.cpp:64
~Mixer()
Halt all playback and free all loaded audio resources.
Definition sound.cpp:19
Compile-time feature flags and version macros for libmx.
Definition cfg.cpp:11