libmx2 2.2.1
C++20 multimedia and game-engine library (SDL2 / OpenGL / Vulkan)
Loading...
Searching...
No Matches
sound.cpp
Go to the documentation of this file.
1/**
2 * @file sound.cpp
3 * @brief Implementation of mx::Mixer SDL_mixer audio manager.
4 */
5#ifdef __EMSCRIPTEN__
6#include"config.hpp"
7#else
8#include"config.h"
9#endif
10
11#ifdef WITH_MIXER
12#include"sound.hpp"
13#include"mx.hpp"
14namespace mx {
15
16 Mixer::Mixer() : init_{false} {
17
18 }
20 cleanup();
21 }
22
24 if(init_ == true) {
25 stopMusic();
26 for(auto &i : files) {
27 if(i != nullptr) {
28 Mix_FreeMusic(i);
29 i = nullptr;
30 }
31 }
32 for(auto &i : wav) {
33 if(i != nullptr) {
34 Mix_FreeChunk(i);
35 i = nullptr;
36 }
37 }
38 files.clear();
39 wav.clear();
40 Mix_CloseAudio();
41 Mix_Quit();
42 init_ = false;
43 }
44
45 }
46
48 if (Mix_QuerySpec(nullptr, nullptr, nullptr) != 0) {
49 if (Mix_PlayingMusic()) {
50 Mix_HaltMusic();
51 }
52 Mix_HaltChannel(-1);
53 }
54 }
55
56 void Mixer::init() {
57 if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) {
58 throw mx::Exception("Could not initalize: " + std::string(Mix_GetError()));
59 }
60
61 init_ = true;
62 }
63
64 int Mixer::loadMusic(const std::string &filename) {
65 Mix_Music *m = Mix_LoadMUS(filename.c_str());
66 if(!m) {
67 Mix_CloseAudio();
68 throw mx::Exception("Error loading music file: " + filename + ": " +std::string(Mix_GetError()));
69 }
70 files.push_back(m);
71 return static_cast<int>(files.size()-1);
72 }
73
74 int Mixer::playMusic(int id, int value) {
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));
78 Mix_CloseAudio();
79 throw mx::Exception("Play music failed: " + std::string(Mix_GetError()));
80 return -1;
81 }
82 } else {
83 return -1;
84 }
85 return 0;
86 }
87 int Mixer::loadWav(const std::string &filename) {
88 Mix_Chunk *m = Mix_LoadWAV(filename.c_str());
89 if(!m) {
90 Mix_CloseAudio();
91 throw mx::Exception("Error loading file: " + std::string(Mix_GetError()));
92 return -1;
93 }
94 wav.push_back(m);
95 return static_cast<int>(wav.size()-1);
96 }
97
98 int Mixer::playWav(int id, int value, int channel) {
99 if(id >= 0 && id < static_cast<int>(wav.size())) {
100 return Mix_PlayChannel(channel, wav.at(id), value);
101 }
102 return -1;
103 }
104
105 bool Mixer::isPlaying(int channel) const {
106 if(Mix_Playing(channel))
107 return true;
108 return false;
109 }
110}
111
112#endif
General-purpose exception class for the mx library.
Definition exception.hpp:20
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
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
bool init_
Whether the audio device is open.
Definition sound.hpp:91
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.
Central SDL2 application window class and related includes.
Definition cfg.cpp:11
SDL_mixer audio subsystem wrapper (requires WITH_MIXER build flag).