#include "AudioPlayer.h" #include "SDL.h" #include "SDL_audio.h" #include #include #include #define NUM_SOUNDS 10 static SDL_AudioSpec fmt; static bool initiated = false; AudioPlayer::AudioPlayer(void) { } AudioPlayer::~AudioPlayer(void) { SDL_CloseAudio(); } void AudioPlayer::init() { initiated = true; extern void mixaudio(void *unused, Uint8 *stream, int len); fmt.freq = 22050; fmt.format = AUDIO_S16; fmt.channels = 2; fmt.samples = 1024; /* A good value for games */ fmt.callback = mixaudio; fmt.userdata = NULL; /* Open the audio device and start playing sound! */ if ( SDL_OpenAudio(&fmt, NULL) < 0 ) { fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError()); } SDL_PauseAudio(0); } static struct sample { Uint8 *data; Uint32 dpos; Uint32 dlen; } sounds[NUM_SOUNDS]; void mixaudio(void *unused, Uint8 *stream, int len) { int i; Uint32 amount; for ( i=0; i (Uint32)len ) { amount = len; } SDL_MixAudio(stream, &sounds[i].data[sounds[i].dpos], amount, SDL_MIX_MAXVOLUME); sounds[i].dpos += amount; } } void AudioPlayer::playSound(std::string fileString) { if(initiated) { char *file = new char[fileString.length() + 1]; strcpy(file, fileString.c_str()); int index; SDL_AudioSpec wave; Uint8 *data; Uint32 dlen; SDL_AudioCVT cvt; /* Look for an empty (or finished) sound slot */ for ( index=0; index