libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
mx::Mixer Class Reference

SDL_mixer–based audio manager. More...

#include <sound.hpp>

Collaboration diagram for mx::Mixer:
[legend]

Public Member Functions

 Mixer ()
 Initialise SDL_mixer and open the audio device.
 ~Mixer ()
 Halt all playback and free all loaded audio resources.
 Mixer (const Mixer &)=delete
Mixer & operator= (const Mixer &)=delete
 Mixer (Mixer &&)=delete
Mixer & operator= (Mixer &&)=delete
void init ()
 Open the audio device (called automatically by constructor).
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.
int playMusic (int id, int value=0)
 Start playing a previously loaded music track.
int playWav (int id, int value=0, int channel=0)
 Play a previously loaded WAV chunk.
bool isPlaying (int channel) const
 Query whether a mixer channel is currently playing.
void cleanup ()
 Free all loaded audio chunks and music tracks.
void stopMusic ()
 Stop background music playback immediately.

Private Attributes

bool init_ = false
 Whether the audio device is open.
std::vector< Mix_Music * > files
 Loaded music tracks.
std::vector< Mix_Chunk * > wav
 Loaded WAV chunks.

Detailed Description

SDL_mixer–based audio manager.

Provides facilities for loading WAV sound chunks and streamed music tracks, playing them on SDL_mixer channels, and querying playback state. Only available when the library is built with WITH_MIXER defined.

Definition at line 31 of file sound.hpp.

Constructor & Destructor Documentation

◆ Mixer() [1/3]

mx::Mixer::Mixer ( )

Initialise SDL_mixer and open the audio device.

Definition at line 16 of file sound.cpp.

References init_.

Referenced by Mixer(), Mixer(), operator=(), and operator=().

◆ ~Mixer()

mx::Mixer::~Mixer ( )

Halt all playback and free all loaded audio resources.

Definition at line 19 of file sound.cpp.

References cleanup().

◆ Mixer() [2/3]

mx::Mixer::Mixer ( const Mixer & )
delete

References Mixer().

◆ Mixer() [3/3]

mx::Mixer::Mixer ( Mixer && )
delete

References Mixer().

Member Function Documentation

◆ cleanup()

void mx::Mixer::cleanup ( )

Free all loaded audio chunks and music tracks.

Definition at line 23 of file sound.cpp.

References files, init_, stopMusic(), and wav.

Referenced by ~Mixer().

◆ init()

void mx::Mixer::init ( )

Open the audio device (called automatically by constructor).

Definition at line 56 of file sound.cpp.

References init_.

◆ isPlaying()

bool mx::Mixer::isPlaying ( int channel) const

Query whether a mixer channel is currently playing.

Parameters
channelSDL_mixer channel number.
Returns
true if the channel is active.

Definition at line 105 of file sound.cpp.

◆ loadMusic()

int mx::Mixer::loadMusic ( const std::string & filename)

Load an audio file as streaming background music.

Parameters
filenamePath to the music file (OGG, MP3, WAV, etc.).
Returns
Index used to reference this track in playMusic().

Definition at line 64 of file sound.cpp.

References files.

◆ loadWav()

int mx::Mixer::loadWav ( const std::string & filename)

Load a WAV file as a sound effect chunk.

Parameters
filenamePath to the WAV file.
Returns
Index used to reference this chunk in playWav().

Definition at line 87 of file sound.cpp.

References wav.

◆ operator=() [1/2]

Mixer & mx::Mixer::operator= ( const Mixer & )
delete

References Mixer().

◆ operator=() [2/2]

Mixer & mx::Mixer::operator= ( Mixer && )
delete

References Mixer().

◆ playMusic()

int mx::Mixer::playMusic ( int id,
int value = 0 )

Start playing a previously loaded music track.

Parameters
idMusic index returned by loadMusic().
valueNumber of additional loops (0 = play once, -1 = infinite).
Returns
0 on success, negative on failure.

Definition at line 74 of file sound.cpp.

References files.

◆ playWav()

int mx::Mixer::playWav ( int id,
int value = 0,
int channel = 0 )

Play a previously loaded WAV chunk.

Parameters
idChunk index returned by loadWav().
valueNumber of additional loops (0 = play once).
channelSDL_mixer channel to play on (0 = first available).
Returns
The channel used for playback, or -1 on failure.

Definition at line 98 of file sound.cpp.

References wav.

◆ stopMusic()

void mx::Mixer::stopMusic ( )

Stop background music playback immediately.

Definition at line 47 of file sound.cpp.

Referenced by cleanup().

Member Data Documentation

◆ files

std::vector<Mix_Music *> mx::Mixer::files
private

Loaded music tracks.

Definition at line 92 of file sound.hpp.

Referenced by cleanup(), loadMusic(), and playMusic().

◆ init_

bool mx::Mixer::init_ = false
private

Whether the audio device is open.

Definition at line 91 of file sound.hpp.

Referenced by cleanup(), init(), and Mixer().

◆ wav

std::vector<Mix_Chunk *> mx::Mixer::wav
private

Loaded WAV chunks.

Definition at line 93 of file sound.hpp.

Referenced by cleanup(), loadWav(), and playWav().


The documentation for this class was generated from the following files: