[SDL] Using SDL_AudioCVT inside audio callback

Gabriel Gambetta mystml at adinet.com.uy
Wed Apr 23 17:02:03 PDT 2008


I'm integrating the dranger example FFMPEG player with SDL_Mixer and other
things. I got everything working, except one thing. I open the mixer at
22KHz and the AVI provides audio at 44KHz. To compensate for this, I added
an SDL_AudioCVT which is supposed to do the right thing. The old callback
looked like this (simplified) :

void audio_callback (void *userdata, Uint8 * stream, int len)
{
    while (len > 0)
    {
        // Read audio data from the AVI and synchronize to video
        readAudioData();

        len1 = is->audio_buf_size - is->audio_buf_index;
        if (len1 > len)
            len1 = len;

        memcpy (stream, (uint8_t *) is->audio_buf + is->audio_buf_index,
nLen1);

        len -= len1;
        stream += nLen1;
        is->audio_buf_index += len1;
    }
}

The 44KHz audio played at 22KHz sounded slow, as expected. So I did this :

void audio_callback (void *userdata, Uint8 * stream, int len)
{
    while (len > 0)
    {
        // Read audio data from the AVI and synchronize to video
        readAudioData();

        len1 = is->audio_buf_size - is->audio_buf_index;
        if (len1 > len)
            len1 = len;

        int nOut = len1;
        if (is->bConvertAudio)
        {
            is->pAudioCVT.buf = is->audio_buf + is->audio_buf_index;
            is->pAudioCVT.len = len1;
            SDL_ConvertAudio(&is->pAudioCVT);
            nOut = len1/2;
        }

        memcpy (stream, (uint8_t *) is->audio_buf + is->audio_buf_index,
nOut);

        len -= len1;
        stream += nOut;
        is->audio_buf_index += len1;
    }
}

I did nOut = len1/2 because of the sampling rate difference. But with that
or without it, I get sound at the correct speed, but sounding skippy, as if
I was fulling only half of the buffer each time (as far as I can tell, I'm
filling the whole buffer)

Am I doing something obviously wrong here?

Thanks,
--Gabriel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20080423/716214d0/attachment.htm 


More information about the SDL mailing list