I am running a Raspberry Pi 4 with Ubuntu 22.04 connected to an Elecrow 7inch LCD Display-H 1024x600 that has an audio jack for headsets and comes with two speakers.
I am trying to setup a docker image that connects to the sound devices to record and play audio using a Python script with PyAudio. In my compose file, I am mounting:
/dev/snd:/dev/snd
and even tried running in privileged mode.
Running aplay -l
inside (and outside) the docker container shows following output:
$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: Headphones [bcm2835 Headphones], device 0: bcm2835 Headphones [bcm2835 Headphones]
Subdevices: 7/8
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
Subdevice #4: subdevice #4
Subdevice #5: subdevice #5
Subdevice #6: subdevice #6
Subdevice #7: subdevice #7
card 1: vc4hdmi0 [vc4-hdmi-0], device 0: MAI PCM i2s-hifi-0 [MAI PCM i2s-hifi-0]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 2: vc4hdmi1 [vc4-hdmi-1], device 0: MAI PCM i2s-hifi-0 [MAI PCM i2s-hifi-0]
Subdevices: 1/1
Subdevice #0: subdevice #0
I assume the issue is that Ubuntu on startup defaults to using the audio jack, despite no headset being connected to it instead of the two speakers. Switching to the speakers in the settings menu, will make audio work on the host system, however, inside the docker container it still defaults to the headphone jack. As I am neither familiar with Linux, nor ALSA I have been struggling to make the audio work. I tried connecting to every possible device in my Python script, however, despite connecting to some of them, no audio can be heard. The code snippet is as follows:
wf = wave.open(file_path, 'rb')
p = pyaudio.PyAudio()
device_count = p.get_device_count()
for i in range (0, device_count):
try:
stream = p.open(
format=p.get_format_from_width(wf.getsampwidth()),
channels=wf.getnchannels(),
rate=wf.getframerate(),
output=True,
output_device_index=i,
)
print("WORKING SOUND, device", i)
except Exception as e:
print(f"Exception {e}\n Device: {i}")
Any suggestions and help would be appreciated.