SDL Audio callback thread not running!

Hello, I just used SDL for only few days, but got a problem using SDL
sound function to play sounds.
I got two codes from SDL documentation and one worked well but another
didnt.

I show these codes below :

code 1 :
static Uint8 *audio_chunk;
static Uint32 audio_len;
static Uint8 *audio_pos;

/* The audio function callback takes the following parameters:
   stream:  A pointer to the audio buffer to be filled
   len:     The length (in bytes) of the audio buffer
*/
void fill_audio(void *udata, Uint8 *stream, int len)
{
    /* Only play if we have data left */
    if ( audio_len == 0 )
        return;

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

/* Load the audio data ... */

;;;;;

audio_pos = audio_chunk;

/* Let the callback function play the audio chunk */
SDL_PauseAudio(0);

/* Do some processing */

;;;;;

/* Wait for sound to complete */
while ( audio_len > 0 ) {
    SDL_Delay(100);         /* Sleep 1/10 second */
}
SDL_CloseAudio();________________________________________________________________________________

code 2:

#define NUM_SOUNDS 2
struct sample {
Uint8 *data;
Uint32 dpos;
Uint32 dlen;
} sounds[NUM_SOUNDS];

void mixaudio(void *unused, Uint8 *stream, int len)
{
int i;
Uint32 amount;

for ( i=0; i<NUM_SOUNDS; ++i ) {
    amount = (sounds[i].dlen-sounds[i].dpos);
    if ( amount > len ) {
        amount = len;
    }
    SDL_MixAudio(stream, &sounds[i].data[sounds[i].dpos], amount,

SDL_MIX_MAXVOLUME);
sounds[i].dpos += amount;
}
}

void PlaySound(char *file)
{
int index;
SDL_AudioSpec wave;
Uint8 *data;
Uint32 dlen;
SDL_AudioCVT cvt;

/* Look for an empty (or finished) sound slot */
for ( index=0; index<NUM_SOUNDS; ++index ) {
    if ( sounds[index].dpos == sounds[index].dlen ) {
        break;
    }
}
if ( index == NUM_SOUNDS )
    return;

/* Load the sound file and convert it to 16-bit stereo at 22kHz */
if ( SDL_LoadWAV(file, &wave, &data, &dlen) == NULL ) {
    fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
    return;
}
SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq,
                        AUDIO_S16,   2,             22050);
cvt.buf = malloc(dlen*cvt.len_mult);
memcpy(cvt.buf, data, dlen);
cvt.len = dlen;
SDL_ConvertAudio(&cvt);
SDL_FreeWAV(data);

/* Put the sound data in the slot (it starts playing immediately) */
if ( sounds[index].data ) {
    free(sounds[index].data);
}
SDL_LockAudio();
sounds[index].data = cvt.buf;
sounds[index].dlen = cvt.len_cvt;
sounds[index].dpos = 0;
SDL_UnlockAudio();

}


code 1 WORKED well but Code 2 DIDN’T. I dont understend what’s the
difference?
in documentation it says the callback function is a seperate running
thread,
but in code 2 the thread seems not running at all.

and, why the while (audio_len>0) loop needed in the code 1 ?

thanks for your helps!

code 1 WORKED well but Code 2 DIDN’T. I dont understend what’s the
difference?
in documentation it says the callback function is a seperate running
thread,
but in code 2 the thread seems not running at all.

Somewhere in code 2 there’s a successful call to SDL_OpenAudio(), right?

–ryan.

code 1 WORKED well but Code 2 DIDN’T. I dont understend what’s the
difference?
in documentation it says the callback function is a seperate running
thread,
but in code 2 the thread seems not running at all.

Somewhere in code 2 there’s a successful call to SDL_OpenAudio(), right?

–ryan._________________________________________________________________________
yes, but the audio output still failed .

I’ve traced the program, and can trace into mixaudio() once and excute
SDL_MixAudio(), then the program exited without entering mixaudio() again.

is the callback function runnging all the time and get data if data exits
, right?

it seems callback function only excute once.

is there something I should notice when writing program witj SDL audio ?

best regards!

[…]

code 1 WORKED well but Code 2 DIDN’T. I dont understend what’s the
difference?
in documentation it says the callback function is a seperate running
thread,
but in code 2 the thread seems not running at all.

and, why the while (audio_len>0) loop needed in the code 1 ?

I’ve followed too the example with code 2 and it didn’t work.
I hadded an SDL_PauseAudio(0); at the bottom of code 2 and before calling
PlaySound I “initialized” the audio with SDL_OpenAudio.
Then it worked.On Tue, Nov 11, 2003 at 03:04:21PM +0800, ttwu at itri.org.tw wrote:


Paulo