Instantly share code, notes, and snippets.

Embed
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;
return;
}
}
AudioHandler::~AudioHandler() {
//Free music
if(music) {
Mix_FreeMusic(music);
}
//Free all effects
for(std::map<std::string, Mix_Chunk*>::iterator it = sfx.begin(); it != sfx.end(); ++it) {
Mix_FreeChunk(it->second);
}
//De-init SDL mixer
Mix_CloseAudio();
}
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) {
Mix_FreeMusic(music);
}
//Load new music and play it
music = Mix_LoadMUS(file.c_str());
if(!music) {
std::cerr << "Mix_LoadMUS Error: " << Mix_GetError() << std::endl;
return;
}
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()) {
sfx.erase(it->first);
}
}
/*
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
Mix_HaltChannel(chan);
}
#ifndef ARCADE_AUDIO_HPP
#define ARCADE_AUDIO_HPP
#include <string>
#include <map>
#include <vector>
#include <SDL2/SDL_mixer.h>
class AudioHandler {
public:
//Constructor and Destructor
AudioHandler();
~AudioHandler();
//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);
private:
Mix_Music* music;
std::map<std::string, Mix_Chunk*> sfx;
};
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment