Problem playing audio packets

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!

try copying the whole file in ram or on a temporary file on your hard disk
before playing!> ----- Original Message -----

From: mithi_lesh@hotmail.com (mithilesh kumar)
To:
Sent: Sunday, July 13, 2003 5:21 PM
Subject: [SDL] Problem playing audio packets

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];
file://Uint8 sound; / Pointer to wave data /
file://Uint32 soundlen; /
Length of wave data /
file://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 \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 */
file://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[])
{
file://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”);
file://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!


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

I think that “streaming” from network is REALTIME! Though you cannot copy
the whole file to disk and start playing after that. You have to push each
received packet to the decoder and then to the soundcard.> ----- Original Message -----

From: fhufsky@phorus.at (Florian Hufsky)
To:
Sent: Monday, July 14, 2003 8:04 PM
Subject: Re: [SDL] Problem playing audio packets

try copying the whole file in ram or on a temporary file on your hard disk
before playing!

----- Original Message -----
From: “mithilesh kumar” <mithi_lesh at hotmail.com>
To:
Sent: Sunday, July 13, 2003 5:21 PM
Subject: [SDL] Problem playing audio packets

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];
file://Uint8 sound; / Pointer to wave data /
file://Uint32 soundlen; /
Length of wave data /
file://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 \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 */
file://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[])
{
file://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”);
file://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!


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

try copying the whole file in ram or on a temporary file on your hard disk
before playing!

but I need to play it in real time … so I guess I cannot afford to do
this. Is there some other way??
mithilesh>----- Original Message -----

From: “mithilesh kumar” <@mithilesh_kumar>
To:
Sent: Sunday, July 13, 2003 5:21 PM
Subject: [SDL] Problem playing audio packets

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];
file://Uint8 sound; / Pointer to wave data /
file://Uint32 soundlen; /
Length of wave data /
file://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 \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 */
file://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[])
{
file://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”);
file://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!


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Astrology can be fun. Find out for yourself.
http://www.msn.co.in/Astrology/Astromagic/ With just a click!

Hi Mithilesh,

have you ever noticed when you watch videos on the internet or listen to
online radio stations that at the begining before it starts it says
"buffering" and buffers for atleast a couple seconds before it starts
playing?

Well whats its doing is it figures out your connection speed to the server
and it buffers enough data in memory so that it can start playing from RAM
while its downloading more behind the scenes so it makes for a more smooth
experience.

If you REALLY REALLY need to play this real time what you need to do is
decrease the sample rate of your file so theres less data to send and maybe
the network will be able to keep up w/ the sound more easily.> ----- Original Message -----

From: mithi_lesh@hotmail.com (mithilesh kumar)
To:
Sent: Tuesday, July 15, 2003 12:27 AM
Subject: Re: [SDL] Problem playing audio packets

try copying the whole file in ram or on a temporary file on your hard
disk

before playing!

but I need to play it in real time … so I guess I cannot afford to do
this. Is there some other way??
mithilesh

----- Original Message -----
From: “mithilesh kumar” <mithi_lesh at hotmail.com>
To:
Sent: Sunday, July 13, 2003 5:21 PM
Subject: [SDL] Problem playing audio packets

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];
file://Uint8 sound; / Pointer to wave data /
file://Uint32 soundlen; /
Length of wave data /
file://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 \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 */
file://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[])
{
file://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”);
file://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!


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl


Astrology can be fun. Find out for yourself.
http://www.msn.co.in/Astrology/Astromagic/ With just a click!


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

mithilesh kumar wrote:

try copying the whole file in ram or on a temporary file on your hard
disk
before playing!

but I need to play it in real time … so I guess I cannot afford to do
this. Is there some other way??
mithilesh

If you want it to play in realtime, the network must deliver the data at
least at the same rate you’re playing. If the network doesn’t keep up
with the minimum speed you get a pause. So I’d suggest two things,
buffering so you can queue up incomming packages and some sort of
compression…

like: http://www.vorbis.com/>

----- Original Message -----
From: “mithilesh kumar” <mithi_lesh at hotmail.com>
To:
Sent: Sunday, July 13, 2003 5:21 PM
Subject: [SDL] Problem playing audio packets

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???

Hi mithilesh,
Streaming information is tricky because there are so
many potential bottlenecks. It would help if you could
identify specificly what your bottleneck is.

There are 3 basic bottlenecks that come to my mind
with the type of program you are trying to write.

  1. The network may be too slow to send the amount of
    data you need quickly enough
  2. The CPU requirements to decode the file into pcm
    data is too much for your computer to keep up. (You
    need more CPU if the format is something like mp3 or
    ogg).
  3. The actual player needs more processing power than
    you can provide

(By the way, printf statements to stdout/stderr are
known to significantly degrade performance on some
systems. You might try removing them when you think
performance might be an issue.)

Anyway, I’m not terribly knowledgable with the
SDL_audio API so I can only share some general
concepts.

First, I am going to assume you have a powerful CPU so
I am going to rule out #2 and #3. Most likely your
network is too slow to stream the amount of data you
need to make the sound seem continuous.

A possibly quick and short term thing you can try is
to use an audio file that is at a lower recording
frequency so you don’t need as much data. You can also
try streaming a compressed file like mp3 or ogg if you
are familiar with one of the decoders. This will trade
off the number of bytes you need to send for higher
CPU requirements.

But in general, you may want to redesign your program
to avoid transmitting data at the most critical time.
If I am interpreting your code correctly you are
basically saying:

  1. Stream some data
  2. Decode/Load it into SDL_audio
  3. start playing chunk
  4. while(file_is_not_finished) {
    if(chunk_is_used_up) {
    Repeat steps 1-3
    }
    }

Keep in mind that when you run out of data in the
chunk, you’re goal is to provide a new chunk as
quickly as possible to avoid a pause in the playback.
Notice that when you run out of data, you have to go
through the network to fetch more data. Then you have
to feed it into SDL_audio. Then you finally start
playing again.

This is bad because at the most critical moment, you
are depending on a chain of slow things to happen. You
have to wait for the network transfer. You then have
to decode the data into pcm (more of an issue for
things like mp3/ogg). And then you have to feed it
into SDL_audio. Only after all of that can you start
playing again. If any one of these items is slow,
you’re likely to get pauses in your playback.

So the solution would be to figure out what things
must really be done at the critical moment, and what
things can be moved elsewhere.

I believe that only loading the pcm sample into
SDL_audio and playing need to be done at this critical
moment.

The following:

  1. network transfer
  2. decode into temporary buffer
    can be done sometime else, preferably before you
    actually need the data.

What I would recommend is to think along the lines of
a double buffering system. Keep two buffers around.
One buffer will always be in use for playing. The
other buffer is free to receive decoded more data
while the chunk is playing. Then when the playing
chunk runs out of data, feed SDL_audio the second
buffer (which should hopefully be filled by now), and
then start refilling the first buffer.

So the algorithm might look something like this

  1. initialize Buffer1
  2. initialize Buffer2
  3. Stream some data
  4. Decode/Load it into Buffer1
  5. Stream some data
  6. Decode/Load it into Buffer2
  7. Start playing buffer1
  8. while(file_is_not_finished) {
    if(chunk_is_used_up) {
    swap the buffer for the one with fresh data
    start playing the new buffer
    stream some data
    decode/load it into inactive buffer
    }

So hopefully you see that I push the network and
decoding away from the critical moment. The next
instance of streaming over the network comes after the
sound resumes playing.

Obviously, you will have to adapt this algorithm to
work with the SDL_Audio API. And obviously there are
corner cases you will eventually want to handle. You
may also find double buffering may not be enough
depending on your network speed. You may want many
buffers and you will want to try to fill as many as
you can while a chunk is still playing.

But this general idea does work. I implemented a
cheesy wrapper around SDL_mixer to do something like
this to allow multiple streaming channels (because
there can only be music channel) and it follows this
general idea. Also OpenAL is an interesting API
because they have this buffering idea built into the
API, and they support the many buffers concept.

Additional note:
You call SDL_MixAudio(). I’m not sure why you need
this. I thought this function is only needed if you
need to mix multiple audio sources to playback
simultaneously. If true, the nature of this function
may be realtively computationally expensive and
shouldn’t be used if you don’t really need it.

I hope this helps,
Eric> > From: “mithilesh kumar” <mithi_lesh at hotmail.com>

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



Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com