Bug#1096138: libsdl2-dev: audio capture with pulseaudio do not work
    Petter Reinholdtsen 
    pere at hungry.com
       
    Sun Feb 16 18:10:04 GMT 2025
    
    
  
Package: libsdl2-dev
Version: 2.32.0+dfsg-1
Forwarded: https://github.com/libsdl-org/SDL/issues/9706
As in <URL: https://github.com/libsdl-org/SDL/issues/9706 >, I
discovered this problem when trying to use whisper.cpp, and thanks to
the example program provided there, I can confirm that the issue is with
SDL 2.
This is the test program:
#include <SDL2/SDL.h>
#include <iostream>
const int SAMPLE_RATE = 16000;
const int NUM_CHANNELS = 1;
const int BUFFER_SIZE = 1024;
const int RECORDING_TIME = 2000; // 2 seconds in milliseconds
int callbackCount = 0;
// Audio callback function
void audioCallback(void* userdata, Uint8* stream, int len) {
    // Do nothing with the audio data, just count the callback invocations
    callbackCount++;
}
int main() {
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
        std::cerr << "SDL initialization failed: " << SDL_GetError() << std::endl;
        return 1;
    }
    SDL_AudioSpec wantedSpec;
    SDL_zero(wantedSpec);
    wantedSpec.freq = SAMPLE_RATE;
    wantedSpec.format = AUDIO_F32;
    wantedSpec.channels = NUM_CHANNELS;
    wantedSpec.samples = BUFFER_SIZE;
    wantedSpec.callback = audioCallback;
    SDL_AudioSpec obtainedSpec;
    SDL_zero(obtainedSpec);
    // Open the default audio capture device
    SDL_AudioDeviceID audioDevice = SDL_OpenAudioDevice(NULL, 1, &wantedSpec, &obtainedSpec, 0);
    if (audioDevice == 0) {
        std::cerr << "Failed to open audio: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }
    // Start recording
    SDL_PauseAudioDevice(audioDevice, 0);
    // Wait for recording time
    SDL_Delay(RECORDING_TIME);
    // Stop recording
    SDL_PauseAudioDevice(audioDevice, 1);
    // Close the audio device
    SDL_CloseAudioDevice(audioDevice);
    // Print out the number of callback invocations
    std::cout << "Callback invoked " << callbackCount << " times." << std::endl;
    SDL_Quit();
    return !callbackCount;
}
Tested using "g++ -o test test.cpp -lSDL2", and producing the
following output with pulseaudio:
  %./test
  Callback invoked 0 times.
  %
Here are some alternative results:
  % SDL_AUDIODRIVER=alsa ./test
  Callback invoked 35 times.
  % SDL_AUDIODRIVER=pipewire ./test
  [W][26087.476267] pw.conf      | [          conf.c: 1214 try_load_conf()] can't load config client.conf: No such file or directory
  [E][26087.476293] pw.conf      | [          conf.c: 1243 pw_conf_load_conf_for_context()] can't load config client.conf: No such file or directory
  SDL initialization failed: Pipewire: Failed to create hotplug detection context (2)
  %
-- 
Happy hacking
Petter Reinholdtsen
    
    
More information about the Pkg-sdl-maintainers
mailing list