Skip to content

Instantly share code, notes, and snippets.

@zachelko
Created April 11, 2010 00:04
Show Gist options
  • Save zachelko/362391 to your computer and use it in GitHub Desktop.
Save zachelko/362391 to your computer and use it in GitHub Desktop.
A simple sound manager for SDL
// SoundManager.cpp
//
// Zach Elko
// 2010
//
// A simple sound manager for SDL.
//
#include "SoundManager.h"
#include "SDL/SDL_mixer.h"
#include "../ResourceManager.h"
#include "../utilities.h"
#include <string>
using namespace soundmanager;
// Initialize our static variables
SoundManager* SoundManager::instance = 0;
SoundManager::AudioState SoundManager::currentState = ERROR;
std::string SoundManager::m_sCurrentMusicFilename = "";
void SoundManager::playMusic(const std::string& fileName)
{
if (currentState != ERROR)
{
// If no music is playing, play it
if (Mix_PlayingMusic() == 0)
{
// Load music
try
{
Mix_Music* music =
ResourceManager::getInstance()->acquireMusic(fileName.c_str());
//Play music
Mix_PlayMusic(music, -1);
currentState = PLAYING;
m_sCurrentMusicFilename = fileName.c_str();
}
catch (const Exception& e)
{
std::cerr << e.what() << std::endl;
}
}
else
{
// If music is playing, pause it
this->pauseMusic();
}
}
}
void SoundManager::pauseMusic()
{
if (currentState != ERROR)
{
// If music is playing, handle the pause request
if (Mix_PlayingMusic() == 1)
{
if (Mix_PausedMusic() == 1)
{
// If we receive a pause request and the music is already paused, resume it.
Mix_ResumeMusic();
currentState = PLAYING;
}
else
{
// Otherwise, pause the music
Mix_PauseMusic();
currentState = PAUSED;
}
}
}
}
void SoundManager::stopMusic()
{
if (currentState != ERROR)
{
Mix_HaltMusic();
currentState = STOPPED;
ResourceManager::getInstance()->releaseMusic(m_sCurrentMusicFilename);
m_sCurrentMusicFilename = "";
}
}
void SoundManager::playFX(const std::string& fileName) const
{
if (currentState != ERROR)
{
// TODO: Alter this to work for mp3 as well
try
{
Mix_Chunk* fx =
ResourceManager::getInstance()->acquireSFX(fileName.c_str());
Mix_PlayChannel(-1, fx, 0);
}
catch (const Exception& e)
{
std::cerr << e.what() << std::endl;
}
}
}
// SoundManager.h
//
// Zach Elko
// 2010
//
// A simple sound manager for SDL.
//
#ifndef _SOUNDMANAGER_H
#define _SOUNDMANAGER_H
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <string>
#include <iostream>
namespace soundmanager
{
class SoundManager
{
public:
// Singleton pattern
static SoundManager* getInstance()
{
if (instance == 0)
{
instance = new SoundManager;
SoundManager::initAudioDevice();
}
return instance;
}
void playMusic(const std::string& fileName);
void pauseMusic();
void stopMusic();
void playFX(const std::string& fileName) const;
bool isPaused() const
{
return currentState == PAUSED;
}
bool isStopped() const
{
return currentState == STOPPED;
}
bool isPlaying() const
{
return currentState == PLAYING;
}
bool inErrorState() const
{
return currentState == ERROR;
}
private:
static std::string m_sCurrentMusicFilename;
static SoundManager* instance;
enum AudioState
{
ERROR = 0,
WAITING,
PAUSED,
STOPPED,
PLAYING
};
static AudioState currentState;
static void initAudioDevice()
{
if (SDL_Init(SDL_INIT_AUDIO) != -1)
{
if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1)
{
std::cerr << "Error initializing audio device...\n";
currentState = ERROR;
}
else
{
currentState = WAITING;
}
}
else
{
std::cerr << "Error initializing SDL audio subsystem...\n";
currentState = ERROR;
}
}
// All of these are private due to the Singleton pattern
SoundManager()
{
}
SoundManager(const SoundManager&)
{
}
~SoundManager()
{
Mix_CloseAudio();
}
SoundManager & operator=(const SoundManager&)
{
}
};
}
#endif
@sam20908
Copy link

Do I really need SDL_Mixer for this?

@zachelko
Copy link
Author

Do I really need SDL_Mixer for this?

Yes - that is the API being used. At least it was 12 years ago, anyway :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment