Search code examples
c++linuxalsa

Linux/ALSA: Callback when frame is written to sound card


I'm new to sound programming and ALSA. I'd like to create a little application, that for example prints out to the console when a frame of data is written to ALSA with snd_pcm_writei(...). Is that possible and if so, how? Currently I'm thinking of registering a callback to ALSA so when an application calls snd_pcm_writei(...) the callback is executed. But I'm not sure that is how it works.


Solution

  • You can either use

    • blocking mode (the default), in which snd_pcm_write*() returns only when all the data has been written into the ring buffer (or when an error has occured), or
    • non-blocking mode (enabled with SND_PCM_NONBLOCK when opening, or snd_pcm_nonblock()), in which you can use poll()/epoll() etc. to get a notification.

    Using an ALSA async handler works only with certain devices, and has all the drawbacks of signal handlers; it is deprecated.