Docker Community Forums

Share and learn in the Docker community.

Failed recording sound on Arm docker with X86 Host - pyaudio

The Problem:

Trying to record audio on Arm container with a X86 Host using python and PyAudio

In depth:

Hi, ive been trying to record audio in a docker container
I’m running Ubuntu 20.04 X86 as my main os
and I’ve built a Dockerfile to create an environment

when I’m building from an X86 image, recording works just fine

when building an Arm image, the script fails to connect to audio device
with the following statement

“OSError: [Errno -9996] Invalid input device (no default output device)”

using lsusb - both containers see all of the devices.

I couldn’t find any source to solve my problem.

Env:

I’ve used these commands to create Arm Containers on X86:

sudo apt-get install qemu binfmt-support qemu-user-static
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
docker run --rm -t arm64v8/ubuntu uname -m

on docker run - both containers run with --privileged

The arm container works great, running uname -m on it confirms that its arm based

any ideas? maybe defining somehow a default device?

DockerFile

# when using arm
FROM arm64v8/ubuntu

# when using ubuntu  
# FROM ubuntu
COPY req.txt /req.txt

# installing
# when using arm
RUN dpkg --add-architecture armhf
RUN apt update -y && apt upgrade -y

# isntalling python 
RUN apt install software-properties-common -y && add-apt-repository ppa:deadsnakes/ppa -y && apt update -y
RUN apt install python3.8 -y && apt install python3-pip -y
# RUN ln -s /usr/bin/python3.8 /usr/bin/python3

# installing requierments
RUN apt-get install libasound-dev -y && apt-get install portaudio19-dev -y \
	&& pip install pyaudio --user && apt-get install python3-pyaudio

RUN apt-get install pkg-config -y && apt-get install libcairo2-dev -y
RUN pip install -r /req.txt
RUN apt install git -y
RUN apt-get install -y pulseaudio && apt install -y alsa

Here is The Recording code if anyone needs it:
import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "voice.wav"

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

print("* recording")

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()