0

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.

1 Answer 1

0

I am not sure if this will completely fix your issue but I found this post: https://stackoverflow.com/questions/70344884/pyaudio-write-systemerror-py-ssize-t-clean-macro-must-be-defined-for-format

It looks like there is an issue with python3.10 and the pyaudio==0.2.11.

I was able to run a similar set of code in a docker container on my raspberry pi 5. I am attempting to get a ROS2 Node working with pyaudio... I am still a couple steps away from getting that to work exactly right but the update of pyaudio should get you past your issue... hopefully!

1
  • Thanks! What ended up helping me was to mount Pulseaudio into the docker container, which was a bit tricky to setup correctly. This ended up working for me on a Raspberry Pi 4 and 5 also running via a ROS2 Node.
    – K. Meyer
    Commented Jun 24 at 7:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.