How to access USB speaker from within a docker container?

I have a USB speaker connected to an Xavier NX running JetPack 5.0.2. It works fine on the standard Ubuntu desktop, but cannot be accessed when I try to access it from within an Nvidia (Tensorflow) Docker Container. I have tried installing various audio packages in the container (like alsa-base, alsa-utils, mplayer, pavucontrol) and using the --device /dev/snd flag and --device /dev/bus/usb flag on the Docker run command, but no combination has successfully allowed me to play a .wav file through the speaker from within the container. What is the handy dandy secret to making this work?

Hi,

Please check the below topic to see if the command helps for your use case or not.

Thanks.

I tried the applicable settings in the referenced article and they helped me get closer, but when I try to play a wav file from within the container, I get the following:

$ aplay output.wav
ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
aplay: main:852: audio open error: No such device

I believe it may be related to settings in the ALSA config file (?) but it is unclear to me what to change to make it work.

1 Like

Hi,

If you run the command outside of the container, does it work?
Thanks.

Yes, it works fine outside the container using the default audio device, which is the USB speaker.

Inside the container, the contents of /proc/asound/cards shows:

0 [UACDemoV10 ]: USB-Audio - UACDemoV1.0
Jieli Technology UACDemoV1.0 at usb-3610000.xhci-2.2, full speed
1 [HDA ]: tegra-hda - NVIDIA Jetson Xavier NX HDA
NVIDIA Jetson Xavier NX HDA at 0x3518000 irq 48

So it knows the card is there (it’s device 0, the UACDemoV10 device)

Running the command: ‘$ aplay -D default output.wav’ or simply ‘$ aplay output.wav’ inside the container results in the following*:

ALSA lib pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
aplay: main:852: audio open error: No such device

(* Note: output.wav is the .wav file that I using to test)

Similarly, running the command ‘$ mplayer output.wav’ outside the container works fine. But inside the container results in the following:

$ mplayer output.wav
Creating config file: /home/tf2dc/.mplayer/config
MPlayer 1.3.0 (Debian), built with gcc-9 (C) 2000-2016 MPlayer Team
do_connect: could not connect to socket
connect: No such file or directory
Failed to open LIRC support. You will not be able to use your remote control.

Playing output.wav.
libavformat version 58.29.100 (external)
Audio only file format detected.
Load subtitles in ./

Opening audio decoder: [pcm] Uncompressed PCM audio decoder
AUDIO: 11025 Hz, 1 ch, floatle, 352.8 kbit/100.00% (ratio: 44100->44100)
Selected audio codec: [pcm] afm: pcm (Uncompressed PCM)

AO: [pulse] Init failed: Connection refused
Failed to initialize audio driver ‘pulse’
[AO_ALSA] alsa-lib: pcm_hw.c:1829:(_snd_pcm_hw_open) Invalid value for card
[AO_ALSA] Playback open error: No such device
Failed to initialize audio driver ‘alsa’
[AO SDL] Samplerate: 11025Hz Channels: Mono Format floatle
[AO SDL] using aalib audio driver.
[AO SDL] Unsupported audio format: 0x1d.
[AO SDL] Unable to open audio: No available audio device
Failed to initialize audio driver ‘sdl:aalib’
Could not open/initialize audio device → no sound.
Audio: no sound
Video: no video

Hi,

We can record a wav file within the container by adding --device /dev/snd --device /dev/bus/usb --volume /etc/timezone:/etc/timezone:ro --volume /etc/localtime:/etc/localtime:ro argument.
Below are our detailed steps. Please give it a try.

$ sudo docker run -it --rm --net=host --runtime nvidia -e DISPLAY=$DISPLAY --device /dev/snd --device /dev/bus/usb --volume /etc/timezone:/etc/timezone:ro --volume /etc/localtime:/etc/localtime:ro  -v /tmp/.X11-unix/:/tmp/.X11-unix nvcr.io/nvidia/l4t-base:r35.1.0
$ apt update
$ apt-get install alsa-utils
$ arecord -D hw:2,0 -c 2 -r 48000 -f S16_LE -d 10 cap.wav

Thanks.

I will give it a try, but I am trying to play a wav file (through an attached USB speaker), not record one.

Hi,

It’s expected that the audio decoder can work as well.
Thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.