Skip to content

PulseAudio

Aneel Nazareth edited this page Aug 28, 2023 · 3 revisions

Using PulseAudio

First, you need to get PulseAudio working on the host machine. Then you need to enable the network interface for the PulseAudio server. Then using it in the app is pretty easy.

This is what I did to get it working on an Ubuntu 22.04 host machine. I imagine other platforms will be similar.

  • Install pulseaudio. It came by default in my OS installation so that was easy. An easy way to check if you already have it is to run pulseaudio --version from the command line. If you don't, you'll want to sudo apt install pulseaudio
  • Get a command line session as the user that runs the docker containers. Add the user account the audio user group - sudo usermod -a -G audio your_username - then log out and log in again to get it to stick.
  • Restart pulseaudio by executing pulseaudio --kill followed by pulseaudio --start
  • Run pactl list cards to make sure you see your audio device. If you don't you're on your own to figure that out. If you do, check the listed Active Profile to make sure it lists something that indicates it will take sound input. Mine was initially output:analog-stereo which does not allow for recording. If you are in the same boat, change with a command like pactl set-card-profile 1 output:analog-stereo+input:analog-stereo. The 1 in the command is the number of the device assigned by pulseaudio that you would have seen in the output of pactl list cards
  • Next, run pactl list sources to find the name of your source. Mine was alsa_input.pci-0000_00_1b.0.analog-stereo - maybe it'll be something like that. If it says "monitor" in the name, that is not what you want. That provides a way of listening to what is on one of the outputs. In the output of pcatl list sources you should see something like
        Ports:
                analog-input-mic: Microphone (type: Mic, priority: 8700, not available)
                analog-input-linein: Line In (type: Line, priority: 8100, not available)
        Active Port: analog-input-mic

Make sure active port in the input you want. You can change it with pactl set-source-port <source_index> <port_name>

  • Make sure the volume is set to an appropriate non-0 level. It is also listed in the putput of pactl list sources. It might require some messing about to get it to a point where you get sound but not distortion. You can set it with pactl set-source-port <source_index> <volume_level>
  • Try recording something. If you can't record in the host, you won't be able to record in the container. Try...
ffmpeg -f pulse -i <pulse_source_name> -t 10 output.wav

and sing a little tune into your microphone. Play back the wav and if you hear the mellifluous tones of your own voice, proceed. If not, time to do some troubleshooting. Maybe you need to turn the volume of the mic up? Maybe you plugged it into the wrong port? Etc. etc.

  • Now we will be turning on the network interface of the PulseAudio server. Go to /etc/pulse and edit default.pa. Down near the bottom add the following:
# Load the TCP module for PulseAudio  
load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1;172.16.0.0/12 auth-anonymous=1  

If your docker network is configured different from the default, you'll need to change 172.16.0.0/12 to whatever is appropriate in your case.

  • Then restart pulseaudio again by executing pulseaudio --kill followed by pulseaudio --start
  • Shutdown BirdCAGE with a docker-compose down in you birdcage directory. Make sure you have the environment variable PULSE_SERVER set in your docker-compose file. The value in the docker-compose.yml in the repository PULSE_SERVER=172.17.0.1 should be fine unless you have changed the defaults for how docker does its networking.
  • Start BirdCAGE back up with docker-compose up -d
  • Go into Stream Settings and add a stream with the PulseAudio Source protocol type. In the address field, put the same name you used in your ffmpeg test above. Transport doesn't matter.
  • Hit the Apply Preferences and Stream Settings button, and if you've lived a good life and your ancestors are proud of you, in a few seconds you'll be capturing audio from your sound card.

Whew.

Oh. No? Doesn't work? Here are some more things to try:

  • See if things basically work inside of the docker container. While it's running, use this to get a shell:
docker exec -it birdcage-birdcage_backend-1 bash
# inside the container...
pcatl list sources
  • If it seems to work for a while and then stop, try commenting this out from /etc/pulse/default.pa:
load-module module-suspend-on-idle
Clone this wiki locally