How to access USB speaker from within a docker container?

@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: