Thread: Help me modify my Sound Server

  1. #1
    Registered User zdude's Avatar
    Join Date
    Sep 2002
    Posts
    32

    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.
    Those who live by the sword get shot by those who don't.

    I can C.

    Compiler: gcc

  2. #2
    Registered User zdude's Avatar
    Join Date
    Sep 2002
    Posts
    32
    > explosion = LoadSound("sound/explosion.wav");
    The first question is why are you ignoring the return result at present, and how do you propose to return error values with this new interface.

    I was ignoring the result because this was a simple demo of the "server" and because I forgot.

    I plan on giving an error by returning a null pointer.

    > #define FREQUENCY 44100;
    This is very poor - remove the trailing ;

    Heh, typo demon. Thanks
    Those who live by the sword get shot by those who don't.

    I can C.

    Compiler: gcc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Low latency sound effects
    By VirtualAce in forum Game Programming
    Replies: 0
    Last Post: 12-21-2004, 01:58 AM
  2. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  3. DirectSound - multiple sounds
    By Magos in forum Game Programming
    Replies: 9
    Last Post: 03-03-2004, 04:33 PM
  4. sounds?
    By BODYBUILDNERD in forum C++ Programming
    Replies: 6
    Last Post: 12-06-2002, 03:34 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM