Skip to content

Instantly share code, notes, and snippets.

@DirkWillem DirkWillem/audio.cpp
Created Jan 31, 2016

What would you like to do?
#include "audio.hpp"
#include <algorithm>
#include <iostream>
AudioHandler::AudioHandler() {
//Initialize SDL mixer
if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) {
std::cerr << "Mix_OpenAudio Error: " << Mix_GetError() << std::endl;
AudioHandler::~AudioHandler() {
//Free music
if(music) {
//Free all effects
for(std::map<std::string, Mix_Chunk*>::iterator it = sfx.begin(); it != sfx.end(); ++it) {
//De-init SDL mixer
void AudioHandler::InitOgg() {
//Initialize Ogg Vorbis module
int initted = Mix_Init(MIX_INIT_OGG);
if(initted != MIX_INIT_OGG) {
std::cerr << "Mix_Init Error: " << Mix_GetError() << std::endl;
void AudioHandler::ChangeMusic(const std::string& file) {
//If old music is loaded, free it
if(music) {
//Load new music and play it
music = Mix_LoadMUS(file.c_str());
if(!music) {
std::cerr << "Mix_LoadMUS Error: " << Mix_GetError() << std::endl;
Mix_PlayMusic(music, -1);
void AudioHandler::LoadEffects(const std::vector<std::string>& effects) {
Check whether there are redundant effects in the sfx map, and if so, remove them:
Iterate over all effects in the pool
for(std::map<std::string, Mix_Chunk*>::const_iterator it = sfx.begin();
it != sfx.end(); ++it) {
//If the list of effects that need to be loaded doesn't contain the effect, remove it from the pool
if(std::find(effects.begin(), effects.end(), it->first) == effects.end()) {
Check whether an effect is already loaded, otherwise, load it
Iterate over all effects that need to be loaded
for(std::vector<std::string>::const_iterator it = effects.begin();
it != effects.end(); ++it) {
//If the pool doesn't contain the effect, load it
if(sfx.find(*it) == sfx.end()) {
sfx[*it] = Mix_LoadWAV((*it).c_str());
if(!sfx[*it]) {
std::cerr << "Mix_LoadWAV Error: " << Mix_GetError() << std::endl;
void AudioHandler::PlayEffect(const std::string& effect) {
//Play sound
Mix_PlayChannel(-1, sfx[effect], 0);
int AudioHandler::StartLoopedEffect(const std::string& effect) {
//Play sound forever, and return it's channel
return Mix_PlayChannel(-1, sfx[effect], -1);
void AudioHandler::StopLoopedEffect(int chan) {
//Stop the channel the looped effect is playing on
#include <string>
#include <map>
#include <vector>
#include <SDL2/SDL_mixer.h>
class AudioHandler {
//Constructor and Destructor
//Separate module initialization (in this case just Ogg Vorbis)
void InitOgg();
//Change background music
void ChangeMusic(const std::string&);
//Load effects
void LoadEffects(const std::vector<std::string>&);
//Play effect
void PlayEffect(const std::string&);
//Play/stop looped effect
int StartLoopedEffect(const std::string&);
void StopLoopedEffect(int);
Mix_Music* music;
std::map<std::string, Mix_Chunk*> sfx;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.