Docker Container Audio

Introduction

In some applications, we would like to record and play audio in a Docker container. In this blog post, I would like to discuss how to setup such a Docker container and how to record and play audio inside the Docker container.

Docker Container Audio

Build Docker Image

The audio.Dockerfile is a minimum Docker file for building an environment that allows recording and playing audio.

audio.Dockerfile
1
2
3
4
5
6
7
8
9
10
11
FROM ubuntu:22.04

ENV DEBIAN_FRONTEND noninteractive

# Install package dependencies
RUN apt-get update -y && \
apt-get install -y --no-install-recommends \
alsa-base \
alsa-utils \
libsndfile1-dev && \
apt-get clean

To build the Docker image, please run the following command.

1
$ docker build -f docker/audio.Dockerfile --no-cache --tag audio:0.0.1 .

Run Docker Container

To run the Docker container, please run the following command. Notice that --device /dev/snd is necessary for Docker container to have access to the audio device on the host machine.

1
$ docker run -it --rm --device /dev/snd audio:0.0.1

List Audio Devices

In the Docker container, we could list the audio devices that are available using the following command.

1
2
3
4
5
6
7
8
9
10
11
$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC1220 Analog [ALC1220 Analog]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 2: ALC1220 Alt Analog [ALC1220 Alt Analog]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 2: C930e [Logitech Webcam C930e], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0

Here the ALC1220 device on card 0 is the audio device on my ROG Z390 motherboard which does not have a microphone and the C930e on card 2 is my Logitech webcam which has a microphone that we will use for recording audio.

Record Audio

To record a piece of audio using the webcam, please run the following command. Notice that --device="hw:2,0" specifies the recording to use the C930e webcam.

1
2
$ arecord -f S16_LE -c 2 -d 10 -r 48000 --device="hw:2,0" /tmp/test-mic.wav
Recording WAVE '/tmp/test-mic.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo

Play Audio

To play the audio we just recorded, please run the following command.

1
2
$ aplay /tmp/test-mic.wav
Playing WAVE '/tmp/test-mic.wav' : Signed 16 bit Little Endian, Rate 48000 Hz, Stereo
Author

Lei Mao

Posted on

06-21-2022

Updated on

06-21-2022

Licensed under


Comments