Using SDL_AudioCVT inside audio callback

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

Yeah, replying to my own question. I moved the resampling code out of the
callback and into the audio decoder and everything works fine now.

–GabrielOn Wed, Apr 23, 2008 at 9:02 PM, Gabriel Gambetta wrote:

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