rpodcast / r_dev_projects

Boilerplate and configs for my R development projects
76 stars 31 forks source link

sounds on errors #25

Open rpodcast opened 2 years ago

rpodcast commented 2 years ago

Key is to get pulse audio working in container and mounted to host processes. Post at https://joonas.fi/2020/12/audio-in-docker-containers-linux-audio-subsystems-spotifyd/ gives a nice overview. Here's what i got working:

More links:

Implementation

  1. Add these ubuntu packages to the Dockerfile: libpulse0 libasound2 libasound2-plugins pulseaudio-utils
  2. Add more mounts and custom environment vars to devcontainer.json
// Add more local mounts to container
    "mounts": [
        "source=/opt/local/renv/cache,target=/renv/cache,type=bind,consistency=cached",
        "source=/etc/alsa,target=/etc/alsa,type=bind,consistency=cached",
        "source=/usr/share/alsa,target=/usr/share/alsa,type=bind,consistency=cached",
        "source=/run/user/1000/pulse/native,target=/run/user/1000/pulse/native,type=bind,consistency=cached"
    ],

"containerEnv": {
        "PULSE_SERVER": "unix:/run/user/1000/pulse/native"
    }
  1. Audio can be played in a container system process via paplay (which the {beepr} package wraps:
paplay some_file.wav

To route the sound to a custom pulse audio sink:

paplay some_file.wav -d SoundBoard

where SoundBoard is an example of the name

  1. To play in R:
system2("paplay", args = c("some_file.wav", "-d", "SoundBoard"), wait = FALSE)