26 for(
auto &i :
files) {
48 if (Mix_QuerySpec(
nullptr,
nullptr,
nullptr) != 0) {
49 if (Mix_PlayingMusic()) {
57 if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) {
58 throw mx::Exception(
"Could not initalize: " + std::string(Mix_GetError()));
65 Mix_Music *m = Mix_LoadMUS(filename.c_str());
68 throw mx::Exception(
"Error loading music file: " + filename +
": " +std::string(Mix_GetError()));
71 return static_cast<int>(
files.size()-1);
75 if(
id >= 0 &&
id <
static_cast<int>(
files.size())) {
76 if (Mix_PlayMusic(
files.at(
id), value) == -1) {
77 Mix_FreeMusic(
files.at(
id));
79 throw mx::Exception(
"Play music failed: " + std::string(Mix_GetError()));
88 Mix_Chunk *m = Mix_LoadWAV(filename.c_str());
91 throw mx::Exception(
"Error loading file: " + std::string(Mix_GetError()));
95 return static_cast<int>(
wav.size()-1);
99 if(
id >= 0 &&
id <
static_cast<int>(
wav.size())) {
100 return Mix_PlayChannel(channel,
wav.at(
id), value);
106 if(Mix_Playing(channel))
General-purpose exception class for the mx library.
void init()
Open the audio device (called automatically by constructor).
int playWav(int id, int value=0, int channel=0)
Play a previously loaded WAV chunk.
std::vector< Mix_Chunk * > wav
Loaded WAV chunks.
void cleanup()
Free all loaded audio chunks and music tracks.
bool isPlaying(int channel) const
Query whether a mixer channel is currently playing.
bool init_
Whether the audio device is open.
int playMusic(int id, int value=0)
Start playing a previously loaded music track.
Mixer()
Initialise SDL_mixer and open the audio device.
std::vector< Mix_Music * > files
Loaded music tracks.
void stopMusic()
Stop background music playback immediately.
int loadWav(const std::string &filename)
Load a WAV file as a sound effect chunk.
int loadMusic(const std::string &filename)
Load an audio file as streaming background music.
~Mixer()
Halt all playback and free all loaded audio resources.
Compile-time feature flags and version macros for libmx.
Central SDL2 application window class and related includes.
SDL_mixer audio subsystem wrapper (requires WITH_MIXER build flag).