
Untitled
By: a guest on
Jun 26th, 2012 | syntax:
None | size: 1.81 KB | hits: 9 | expires: Never
#include <SDL/SDL_mixer.h>
#include <SDL/SDL.h>
typedef enum {
SOUND_NONE = 0, // Just to be safe
SOUND_YAY,
SOUND_WOOT
} soundTrack;
#define NUM_CHUNKS 3
#define SOUND_PANNINGSCOPE 0.5
Mix_Chunk* chunks[NUM_CHUNKS];
unsigned char enabled = 0;
void initSound(void){
SDL_RWops* handle;
if(Mix_OpenAudio(SOUND_FREQUENCY, AUDIO_S16SYS, 2, SOUND_CHUNKSIZE) == -1){
;// Give the user a warning that sound isn't available
return;
}
enabled = 1;
chunks[0] = NULL;
handle = SDL_RWFromFile("path/to/sound.wav", "r");
if(handle == NULL) ;// Throw an error that the file couldn't be loaded
chunks[1] = Mix_LoadWAV_RW(handle, 0);
if(chunks[1] == NULL) ;// Throw an error that the file couldn't be read
SDL_RWclose(handle);
handle = SDL_RWFromFile("path/to/anothersound.wav", "r");
if(handle == NULL) ;// Throw an error that the file couldn't be loaded
chunks[2] = Mix_LoadWAV_RW(handle, 0);
if(chunks[2] == NULL) ;// Throw an error that the file couldn't be read
SDL_RWclose(handle);
}
void cleanupSound(void){
int i;
if(enabled == 0) return;
Mix_HaltChannel(-1);
for(i = 1; i < NUM_CHUNKS; ++i){
Mix_FreeChunk(chunks[i]);
}
Mix_CloseAudio();
}
void playSound(soundTrack track){
int channel;
if(enabled == 0) return;
channel = Mix_PlayChannel(-1, chunks[track], 0);
Mix_SetPanning(channel, 255, 255); // Resets the panning in case it was changed on this channel
}
void playSoundPan(soundTrack track, int panning){
int channel;
float realPanning;
if(enabled == 0) return;
realPanning = panning;
realPanning -= 255.0/2;
realPanning *= SOUND_PANNINGSCOPE;
realPanning += 255.0/2;
channel = Mix_PlayChannel(-1, chunks[track], 0);
Mix_SetPanning(channel, 255.0-realPanning, realPanning); // Panned sound is about 50% more silent
}