Help me modify my Sound Server
Ok, I have a "sound server" written in SDL, heres its code:
sndserv.h:
Code:
#include <SDL/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#define null NULL
#define MAX_PLAYING_SOUNDS 10
#define VOLUME_PER_SOUND SDL_MIX_MAXVOLUME / 2
//Desired Audio Specs
#define FREQUENCY 44100;
#define FORMAT AUDIO_S16
#define SAMPLES 4096
#define CHANNELS 2
#define CALLBACK AudioCallback
#define USERDATA NULL
static SDL_AudioSpec desired, obtained;
typedef struct Sound_s {
Uint8 *samples;
Uint32 length;
} Sound, *Sound_p;
typedef struct playing_s {
int active;
Sound_p sound;
Uint32 position;
} playing_t, *playing_p;
static playing_t playing[MAX_PLAYING_SOUNDS];
void AudioCallback(void *user_data, Uint8 *audio, int length){
int i;
memset(audio, 0, length);
for (i = 0; i < MAX_PLAYING_SOUNDS; i++){
if (playing[i].active){
Uint8 *sound_buf;
Uint32 sound_len;
sound_buf = playing[i].sound->samples;
sound_buf += playing[i].position;
if ((playing[i].position + length) > playing[i].sound->length){
sound_len = playing[i].sound->length - playing[i].position;
} else {
sound_len = length;
}
SDL_MixAudio(audio, sound_buf, sound_len, VOLUME_PER_SOUND);
playing[i].position += length;
if (playing[i].position >= playing[i].sound->length){
playing[i].active = 0;
}
}
}
}
int LoadSound(char *filename, Sound_p sound){
SDL_AudioSpec *spec = &obtained;
SDL_AudioCVT cvt;
SDL_AudioSpec loaded;
Uint8 *new_buf;
if (SDL_LoadWAV(filename, &loaded, &sound->samples, &sound->length) == null){
printf("Unable to load sound: %s\n", SDL_GetError());
return 1;
}
if (SDL_BuildAudioCVT(&cvt, loaded.format, loaded.channels, loaded.freq, spec->format, spec->channels, spec->freq) < 0){
printf("Unable to convert sound: %s\n", SDL_GetError());
return 1;
}
cvt.len = sound->length;
new_buf = (Uint8 *)malloc(cvt.len * cvt.len_mult);
if (new_buf == null){
printf("Out of memory!\n");
SDL_FreeWAV(sound->samples);
return 1;
}
memcpy(new_buf, sound->samples, sound->length);
cvt.buf = new_buf;
if (SDL_ConvertAudio(&cvt) < 0){
printf("Audio conversion error:%s\n", SDL_GetError());
free(new_buf);
SDL_FreeWAV(sound->samples);
return 1;
}
SDL_FreeWAV(sound->samples);
sound->samples = new_buf;
sound->length = sound->length * cvt.len_mult;
printf("%s was loaded and converted successfully.\n", filename);
return 0;
}
void ClearPlayingSounds(void){
int i;
for (i = 0; i < MAX_PLAYING_SOUNDS; i++){
playing[i].active = 0;
}
}
int PlaySound(Sound_p sound){
int i;
for (i = 0; i < MAX_PLAYING_SOUNDS; i++){
if (playing[i].active == 0){
break;
}
}
if (i == MAX_PLAYING_SOUNDS){
return 1;
}
SDL_LockAudio();
playing[i].active = 1;
playing[i].sound = sound;
playing[i].position = 0;
SDL_UnlockAudio();
return 0;
}
int sndserv_init(){
desired.freq = FREQUENCY;
desired.format = FORMAT;
desired.samples = SAMPLES;
desired.channels = CHANNELS;
desired.callback = CALLBACK;
desired.userdata = USERDATA;
if (SDL_OpenAudio(&desired, &obtained) < 0){
printf("Unable to open Audio Device");
return 1;
}
atexit(SDL_CloseAudio);
ClearPlayingSounds();
SDL_PauseAudio(0);
}
int sndserv_end(){
SDL_PauseAudio(1);
SDL_LockAudio();
}
int FreeSound(Sound_p snd){
SDL_LockAudio();
free(snd->samples);
SDL_UnlockAudio();
}
ok heres how a simple example works:
sndtest.c:
Code:
#include <SDL/SDL.h>
#include "sndserv.h"
int main(int argc, char *argv[]){
Sound explosion;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
atexit(SDL_Quit);
sndserv_init();
LoadSound("sound/explosion.wav", &explosion);
PlaySound(&explosion);
SDL_Delay(5000);
sndserv_end();
FreeSound(&explosion);
return 0;
}
As you can see you load sounds with LoadSound
How could I modify the function so that LoadSound works like this:
explosion = LoadSound("sound/explosion.wav");
Any help would be greatly appreaciated.