We use pulseaudio server as default Reference
Install audio drivers and dependencies
RUN apt-get -y install libportaudio2 RUN apt-get -y install alsa-base RUN apt-get -y install alsa-utils #for playing sound files with aplay RUN apt-get -y install pulseaudio
Set PULSE_SERVER environment variable, mount the corresponding directory. Mount the cookie directory as well, without which we get alsa permission denied error
echo $XDG_RUNTIME_DIR '/run/user/1000' docker run -it \ -e PULSE_SERVER=unix:${XDG_RUNTIME_DIR}/pulse/native \ #Set environment variable -v ${XDG_RUNTIME_DIR}/pulse/native:${XDG_RUNTIME_DIR}/pulse/native \ #Mount directory -v ~/.config/pulse/cookie:/root/.config/pulse/cookie #Set up cookieCheck setup (in the container)
aplay -L | head -n9Output :
default Playback/recording through the PulseAudio sound server ...Check if the speaker works when you play an audio file
aplay sample.wav
import sounddevice as sd
Detecting the device
Make sure --device /dev/snd is added while executing docker run
sd.query_devices()
fs=44100
duration = 5 # seconds
myrecording = sd.rec(duration * fs, samplerate=fs, channels=2,dtype='float64') #pass device=.. to select device
sd.wait()
myrecording
sd.play(myrecording, fs) #pass device=.. to select device
sd.wait()
myrecording.shape #fs*5 = 220500