[SDL] Problem playing audio packets

mithilesh kumar mithi_lesh at hotmail.com
Sun Jul 13 08:23:01 PDT 2003


Hi all,
I want to play a wav file using the sdl library. I have succeded in playing 
local files but when I take input from the network, I get a pause between 
every packet played.
Can anyone please tell me how I could get a continuous output???
Follows below is the code. The input to this is a wav audio file. Can anyone 
tell me what mistake I'm making?
Thanks in advance.
Mithilesh


#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#include "SDL.h"
#include "SDL_audio.h"

static struct {
	SDL_AudioSpec spec;
	Uint8 *audio_chunk;
	Uint32 audio_len;
	Uint8 *audio_pos;
	int done;
	Uint8 buffer[16671880];
	//Uint8   *sound;			/* Pointer to wave data */
	//Uint32   soundlen;		/* Length of wave data */
	//int      soundpos;		/* Current play position */
} wave;

void fillerup(void *unused, Uint8 *stream, int len)
{
        /* Only play if we have data left */
        if ( wave.audio_len == 0 )
	{   wave.done = 1;
            return;
	}


        /* Mix as much data as possible */
        len = ( len > wave.audio_len ? wave.audio_len : len );
        SDL_MixAudio(stream, wave.audio_pos, len, SDL_MIX_MAXVOLUME);
        wave.audio_pos += len;
        wave.audio_len -= len;

}

int sdl_initialize()
{
	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
                fprintf(stderr, "Couldn't initialize SDL: 
%s\n",SDL_GetError());
                exit(1);
        }
        atexit(SDL_Quit);

	wave.done = 0;
	wave.spec.freq = 44100;
    	wave.spec.channels = 2;
    	wave.spec.samples = 4096;        /* A good value for games */
    	wave.spec.userdata = NULL;
	wave.spec.format = AUDIO_S16;
        wave.spec.callback = fillerup;

        /* Initialize fillerup() variables */
        if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
                fprintf(stderr, "Couldn't open audio: %s\n", 
SDL_GetError());
                exit(2);
        }
	return 0;
}
int flag;
int play(Uint8 *buf, int len)
{
	char name[32];


	if ( buf == NULL ) {
		fprintf(stderr, "Usage: a.out <wavefile>\n");
		exit(1);
	}

	wave.audio_chunk = buf;
	wave.audio_len = len;
	wave.audio_pos = wave.audio_chunk;


	printf("freq = %d\n", wave.spec.freq);
        printf("channels = %d\n", wave.spec.channels);
        printf("samples = %d\n", wave.spec.samples);        /* A good value 
for games */
        printf("userdata = %s\n", wave.spec.userdata);
        printf("format = %d\n", wave.spec.format);

	if(flag == 0) {
		SDL_PauseAudio(0);
		fprintf(stderr, "OK prints\n");
		flag = 1;
	}

	/* Let the audio run */
	//printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32));
	while ( ! wave.done )
		SDL_Delay(10);
	wave.done = 0;

	/* Clean up on signal */
	return(0);
}

int closeAudio()
{
	SDL_PauseAudio( 1 );
        SDL_CloseAudio();
}

int main(int argc, char *argv[])
{
	//int ret = sdl_initialize();
	int len;
	flag =0;
	FILE *fp = fopen(argv[1], "r");
	printf("ok \n");
	int ret = sdl_initialize();


	while(len = fread(wave.buffer, 1, 50*4608, /*1667188,*/ fp)){
	if (len <= 0)
	{	printf("error in reading file\n"); exit(1);
	}
	printf("ok \n");
        //SDL_Delay(33);
	play(wave.buffer, len);
	}

	closeAudio();
}

_________________________________________________________________
Are you a geek? Do gizmos make you grin? 
http://www.msn.co.in/Computing/Gizmos/ Click here!





More information about the SDL mailing list