Microphone recording from SDL2 is delayed by 2 seconds

I am recording my mic from SDL2. Somehow the callback gets called after about 2 seconds. I expected it to be called immediately.

If I created a callback for my speaker, it gets called immediately.

Here is the MWE where I get the delay:

#include <SDL2/SDL.h>

void cb(void *userdata, Uint8 *stream, int len) {
    printf("Callback at %u\n", SDL_GetTicks());
}

int main() {
    SDL_Init(SDL_INIT_AUDIO);

    SDL_AudioDeviceID dev;

    SDL_AudioSpec want, have;

    SDL_zero(want);
    want.freq = 44100;
    want.format = AUDIO_S16SYS;
    want.channels = 1;
    want.samples = 1024;
    want.callback = cb;
    dev = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(0, 1), 1, &want, &have, 0);

    if (have.format != want.format) {
        SDL_Log("We didn't get the wanted format.");
        return 1;
    }

    SDL_PauseAudioDevice(dev, 0);

    if (dev == 0) {
        SDL_Log("Failed to open audio: %s", SDL_GetError());
        return 1;
    }

    printf("Started at %u\n", SDL_GetTicks());
    SDL_Delay(5000);

    SDL_CloseAudioDevice(dev);
}

      

My conclusion

Starts at 21

Callback in 2001

Callback in 2001

Callback in 2001

etc.

I also changed want.freq

and want.samples

in smaller numbers without effect.

+3


source to share





All Articles