How to access USB speaker from within a docker container?

I am trying to play a wave file from within a Docker container. I found the following issue that is closed:

I am using a custom build of the jetson-dockers where I am mounting my project source and running it in the container. The container builds fine with my dependencies. In the container, I am able to record audio no problem, run the speaker-test, but whenever I try to play anything, I get Playback open error: -2,No such file or directory.

I have added the following to my docker run:

./jetson-containers run --volume /mnt/nvme/git/mimzy:/opt/mimzy --device /dev/snd --device /dev/bus/usb --volume /etc/timezone:/etc/timezone:ro --volume /etc/localtime:/etc/localtime:ro mimzy:r36.2.0 

and does not work. Please note that the jetson-containers script is provided by the repository.

Hi,

Could you check if the driver below helps?

Thanks.

An update on my investigations. This is on a freshly re-flashed instance with nothing installed except python3-pyaudio which is needed for Dusty’s script. This is also running directly on the host from the Desktop.

Here is the output from Dusty’s code:

 $ python list_audio_devices.py 
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib confmisc.c:1369:(snd_func_refer) Unable to find definition 'cards.2.pcm.hdmi.0:CARD=2,AES0=4,AES1=130,AES2=0,AES3=2'
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5701:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM hdmi
ALSA lib confmisc.c:1369:(snd_func_refer) Unable to find definition 'cards.2.pcm.hdmi.0:CARD=2,AES0=4,AES1=130,AES2=0,AES3=2'
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5701:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM hdmi
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp
ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp
ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card
ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 'card'
ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card
ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 'card'
ALSA lib pcm_dmix.c:1005:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock

AUDIO DEVICES:

 0: NVIDIA Jetson AGX Orin HDA: HDMI 1 (hw:0,7)        (inputs=0   outputs=8   sampleRate=44100)
 1: NVIDIA Jetson AGX Orin HDA: HDMI 2 (hw:0,8)        (inputs=0   outputs=8   sampleRate=44100)
 2: NVIDIA Jetson AGX Orin HDA: HDMI 3 (hw:0,9)        (inputs=0   outputs=8   sampleRate=44100)
 3: NVIDIA Jetson AGX Orin APE: - (hw:1,0)             (inputs=16  outputs=16  sampleRate=44100)
 4: NVIDIA Jetson AGX Orin APE: - (hw:1,1)             (inputs=16  outputs=16  sampleRate=44100)
 5: NVIDIA Jetson AGX Orin APE: - (hw:1,2)             (inputs=16  outputs=16  sampleRate=44100)
 6: NVIDIA Jetson AGX Orin APE: - (hw:1,3)             (inputs=16  outputs=16  sampleRate=44100)
 7: NVIDIA Jetson AGX Orin APE: - (hw:1,4)             (inputs=16  outputs=16  sampleRate=44100)
 8: NVIDIA Jetson AGX Orin APE: - (hw:1,5)             (inputs=16  outputs=16  sampleRate=44100)
 9: NVIDIA Jetson AGX Orin APE: - (hw:1,6)             (inputs=16  outputs=16  sampleRate=44100)
10: NVIDIA Jetson AGX Orin APE: - (hw:1,7)             (inputs=16  outputs=16  sampleRate=44100)
11: NVIDIA Jetson AGX Orin APE: - (hw:1,8)             (inputs=16  outputs=16  sampleRate=44100)
12: NVIDIA Jetson AGX Orin APE: - (hw:1,9)             (inputs=16  outputs=16  sampleRate=44100)
13: NVIDIA Jetson AGX Orin APE: - (hw:1,10)            (inputs=16  outputs=16  sampleRate=44100)
14: NVIDIA Jetson AGX Orin APE: - (hw:1,11)            (inputs=16  outputs=16  sampleRate=44100)
15: NVIDIA Jetson AGX Orin APE: - (hw:1,12)            (inputs=16  outputs=16  sampleRate=44100)
16: NVIDIA Jetson AGX Orin APE: - (hw:1,13)            (inputs=16  outputs=16  sampleRate=44100)
17: NVIDIA Jetson AGX Orin APE: - (hw:1,14)            (inputs=16  outputs=16  sampleRate=44100)
18: NVIDIA Jetson AGX Orin APE: - (hw:1,15)            (inputs=16  outputs=16  sampleRate=44100)
19: NVIDIA Jetson AGX Orin APE: - (hw:1,16)            (inputs=16  outputs=16  sampleRate=44100)
20: NVIDIA Jetson AGX Orin APE: - (hw:1,17)            (inputs=16  outputs=16  sampleRate=44100)
21: NVIDIA Jetson AGX Orin APE: - (hw:1,18)            (inputs=16  outputs=16  sampleRate=44100)
22: NVIDIA Jetson AGX Orin APE: - (hw:1,19)            (inputs=16  outputs=16  sampleRate=44100)
23: PowerConf S3: USB Audio (hw:2,0)                   (inputs=1   outputs=2   sampleRate=48000)
24: sysdefault                                         (inputs=128 outputs=128 sampleRate=48000)
25: front                                              (inputs=0   outputs=2   sampleRate=48000)
26: surround40                                         (inputs=0   outputs=2   sampleRate=48000)
27: iec958                                             (inputs=0   outputs=2   sampleRate=48000)
28: spdif                                              (inputs=1   outputs=2   sampleRate=48000)
29: samplerate                                         (inputs=128 outputs=128 sampleRate=44100)
30: speexrate                                          (inputs=128 outputs=128 sampleRate=44100)
31: pulse                                              (inputs=32  outputs=32  sampleRate=44100)
32: upmix                                              (inputs=8   outputs=8   sampleRate=48000)
33: vdownmix                                           (inputs=6   outputs=6   sampleRate=48000)
34: demixer                                            (inputs=0   outputs=128 sampleRate=48000)
35: dmix                                               (inputs=0   outputs=2   sampleRate=48000)
36: default                                            (inputs=32  outputs=32  sampleRate=44100)

Specifically

24: PowerConf S3: USB Audio (hw:2,0)                   (inputs=1   outputs=0   sampleRate=48000)

Here are more details:

$ cat /proc/asound/cards
 0 [HDA            ]: tegra-hda - NVIDIA Jetson AGX Orin HDA
                      NVIDIA Jetson AGX Orin HDA at 0x3518000 irq 122
 1 [APE            ]: tegra-ape - NVIDIA Jetson AGX Orin APE
                      Unknown-NVIDIAJetsonAGXOrinDeveloperKit-NotSpecified
 2 [S3             ]: USB-Audio - PowerConf S3
                      Anker PowerConf S3 at usb-3610000.usb-4.1, full speed

Why does is state Unknown-NVIDIAJetsonAGXOrinDeveloperKit-NotSpecified for the APE device? Looking at this documentation shows the output on a Jetson AGX Xavier. I would expect it to show the APE dvice relative to the Orin AGX, and not Unknown-NVIDIAJetsonAGXOrinDeveloperKit-NotSpecified.

When I try to play the audio specifying the device on the host (not in a container):

$ aplay -Dhw:S3,0 test-mic.wav 
Playing WAVE 'test-mic.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
aplay: set_params:1358: Channels count non available

$ aplay -l
...
card 2: S3 [PowerConf S3], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

$ aplay -Dhw:2,0 test-mic.wav 
Playing WAVE 'test-mic.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
aplay: set_params:1358: Channels count non available

$ aplay -Dhw:S3,0 -c 1 test-mic.wav
Playing WAVE 'test-mic.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
aplay: set_params:1358: Channels count non available

Playing the audio using the default card as set from the Desktop Settings → Sound, it works:

$ aplay test-mic.wav 
Playing WAVE 'test-mic.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
[PLAYS AUDIO]

When the default is set through Desktop Settings, I get a resource busy when I specify the device:

$ aplay -Dhw:S3,0 -c 1 test-mic.wav
aplay: main:831: audio open error: Device or resource busy

More debug infomation:

$ amixer -c 2 contents
numid=3,iface=MIXER,name='PCM Playback Switch'
  ; type=BOOLEAN,access=rw------,values=1
  : values=on
numid=4,iface=MIXER,name='PCM Playback Volume'
  ; type=INTEGER,access=rw---R--,values=1,min=0,max=7248,step=0
  : values=5943
  | dBminmax-min=-28.37dB,max=-0.06dB
numid=5,iface=MIXER,name='Mic Capture Switch'
  ; type=BOOLEAN,access=rw------,values=1
  : values=on
numid=6,iface=MIXER,name='Mic Capture Volume'
  ; type=INTEGER,access=rw---R--,values=1,min=0,max=7248,step=0
  : values=7248
  | dBminmax-min=-28.37dB,max=-0.06dB
numid=2,iface=PCM,name='Capture Channel Map'
  ; type=INTEGER,access=r----R--,values=1,min=0,max=36,step=0
  : values=2
  | container
    | chmap-fixed=MONO
numid=1,iface=PCM,name='Playback Channel Map'
  ; type=INTEGER,access=r----R--,values=2,min=0,max=36,step=0
  : values=0,0
  | container
    | chmap-fixed=FL,FR

Finally, I found a tone.py script. When I run it:

$ python tone.py 
init_audio: Create PyAudio object
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib confmisc.c:1369:(snd_func_refer) Unable to find definition 'cards.2.pcm.hdmi.0:CARD=2,AES0=4,AES1=130,AES2=0,AES3=2'
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5701:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM hdmi
ALSA lib confmisc.c:1369:(snd_func_refer) Unable to find definition 'cards.2.pcm.hdmi.0:CARD=2,AES0=4,AES1=130,AES2=0,AES3=2'
ALSA lib conf.c:5178:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5701:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM hdmi
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2664:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp
ALSA lib pcm_oss.c:397:(_snd_pcm_oss_open) Cannot open device /dev/dsp
ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card
ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 'card'
ALSA lib confmisc.c:160:(snd_config_get_card) Invalid field card
ALSA lib pcm_usb_stream.c:482:(_snd_pcm_usb_stream_open) Invalid card 'card'
ALSA lib pcm_dmix.c:1005:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
JackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock
init_audio: Open stream
Expression 'paInvalidSampleRate' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2048
Expression 'PaAlsaStreamComponent_InitialConfigure( &self->playback, outParams, self->primeBuffers, hwParamsPlayback, &realSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2721
Expression 'PaAlsaStream_Configure( stream, inputParameters, outputParameters, sampleRate, framesPerBuffer, &inputLatency, &outputLatency, &hostBufferSizeMode )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2842
Traceback (most recent call last):
  File "/mnt/nvme/git/mimzy/tone.py", line 73, in <module>
    main()
  File "/mnt/nvme/git/mimzy/tone.py", line 55, in main
    init_audio()
  File "/mnt/nvme/git/mimzy/tone.py", line 20, in init_audio
    s = pa.open(output=True,
  File "/mnt/nvme/git/mimzy/mimzy-env/lib/python3.10/site-packages/pyaudio/__init__.py", line 639, in open
    stream = PyAudio.Stream(self, *args, **kwargs)
  File "/mnt/nvme/git/mimzy/mimzy-env/lib/python3.10/site-packages/pyaudio/__init__.py", line 441, in __init__
    self._stream = pa.open(**arguments)
OSError: [Errno -9997] Invalid sample rate

Here are the logs from the linked document:

kernel.log (85.9 KB)

dt.log (390.8 KB)

tone.py.txt (1.6 KB)

I am at my wits end at this stage and am suspect that the issue is with the Orin AGX which I purchased new a couple of months ago and came with Jetpack 6.0. Perhaps this is something that is broken in the this pre-release?

At this point, I am dead in the water.

Should I open up a new issue since after a lot of investigations this is an issue on the host device.

@mimzy ALSA produces that error when the audio device doesn’t support the number of channels requested, or the audio file does not have that number of channels (i.e. trying to play a mono wav file through a stereo device). As per this StackExchange thread, ALSA does not do any sample or format conversion for you when hw: devices are specified.

To confirm this, I installed alsa-utils in container (this was using nano_llm container, but it shouldn’t matter) on the same Anker PowerConf mic/speaker and tried recording with aplay (in mono) and playing it back (in stereo). The PowerConf is a mono microphone and stereo speaker. So while it was successful recording (in mono), it failed playing it back in stereo:

# install arecord and aplay
apt-get update && apt-get install -y alsa-utils

# list audio devices
arecord --list-devices
...
card 2: S3 [PowerConf S3], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

# record in mono
arecord --format S16_LE --rate 48000 --channels 1 --device "hw:2,0" /data/audio/test_mono.wav

# attempt playback
aplay --device "hw:2,0" --rate 48000 --channels 2 /data/audio/test_mono.wav

Playing WAVE '/data/audio/test_mono.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Mono
aplay: set_params:1358: Channels count non available

However then I manually converted this wav file to stereo (in Audacity, but again that doesn’t matter), and it then played fine:

aplay --device "hw:2,0" --rate 48000 --channels 2 /data/audio/test_stereo.wav
Playing WAVE '/data/audio/test_stereo.wav' : Signed 16 bit Little Endian, Rate 48100 Hz, Stereo

So basically I think this is unrelated to Jetson but rather cryptic nuances inside the ALSA utilities (and in general, Linux audio support like ALSA, JACK, PulseAudio, ect is not straightforward to navigate, so much so there exist many guides like this one to the myriad of APIs)

If you are developing a Python application, I personally would just use PyAudio, in my experience it’s simple and reliable. The Invalid sample rate error you were getting from that PyAudio tone test script was likely because it wasn’t setting the sample rate to one that the Anker PowerConf supports (which is 48000 KHz)

Do that by setting the rate kwarg when you open the PyAudio device, like this code does: