How to capture and process audio?

Solved
DunogRamedu Posts 1 Registration date Thursday January 19, 2023 Status Member Last seen February 15, 2023 - Feb 14, 2023 at 11:11 PM
hamsheena Posts 8 Registration date Tuesday February 14, 2023 Status Member Last seen March 4, 2023 - Feb 15, 2023 at 02:29 AM

Hello,

I need to capture audio from an application I don't own and process it in real-time. There are guides on doing this with PulseAudio and Pipewire, but I want to run it in a docker container without privileged mode. I think creating a virtual ALSA device as an audio sink and source could work, but I don't know where to begin. Can I create a sink, make it the default output, create a source, connect them, and then have an application read from the source? Any suggestions on where to start?

1 reply

hamsheena Posts 8 Registration date Tuesday February 14, 2023 Status Member Last seen March 4, 2023 5
Updated on Feb 15, 2023 at 02:30 AM

Yes, you can create a virtual ALSA sink and source and use them to capture and process audio in a Docker container without privileged mode. Here's an outline of the steps you can take to accomplish this:

  1. Install the necessary ALSA packages in the Docker container. Depending on your operating system and package manager, the package names may vary. On Debian-based systems, you can use the following command to install the necessary packages:

    apt-get update && apt-get install -y alsa-utils libasound2-dev

  2. Create a virtual ALSA sink and source by creating a new .asoundrc file in the Docker container. Here's an example .asoundrc file that creates a sink and source:

    pcm.vir_sink { type null } pcm.vir_src { type dsnoop ipc_key 5678293 slave { pcm "vir_sink" } }

    This creates a virtual sink named vir_sink that discards all audio data, and a virtual source named vir_src that reads audio data from the vir_sink sink. You can modify this configuration to suit your needs.

  3. Set the virtual sink as the default output device in the Docker container. You can do this by setting the default option in the .asoundrc file:

    pcm.!default { type plug slave { pcm "vir_sink" } }

    This sets the vir_sink as the default output device for all ALSA applications in the Docker container.

  4. Start the application you want to capture audio from in the Docker container and configure it to use the virtual source as the input device. The exact steps to do this will depend on the application you're using. For example, you might need to specify the input device as vir_src in the application's audio settings.

  5. Once the application is running, you can capture and process the audio data from the virtual source (vir_src) using your own application or script.

0