Audio in Linux
ALSA
ALSA (Advanced Linux Sound Architecture) is the sound system built into the Linux kernel. The following is mostly useful for working on a Raspberry Pi with only ALSA (and no layer like PipeWire on top).
To list all your playback and capture devices you can use:
aplay -l
aplay -L
arecord -l
arecord -L
To get details about a particular playback or capture device (including supported number of channels, min/max sample rate and sample bits):
aplay -D hw:0 /dev/zero --dump-hw-params
arecord -D hw:0 --dump-hw-params
You can also get information via files under /proc/asound/
If a playback device supports hardware mixing, then ALSA allows multiple processes to open the device and send audio. If not, then normally only one process can open the device, but the “dmix” plugin of ALSA can be used to mix sound from multiple applications in software.
Your audio devices are available in these variants (as listed by aplay and arecord):
Prefix | Description |
---|---|
hw: | This should be used by default. |
hwplug: | Should generally not be used. ALSA will convert the sample rate and/or sample format if it is detected to be needed. |
dsnoop: | Only for capture devices. This allow letting multiple applications open the device, so that they can all receive the same audio. |
dmix: | Only for playback devices. This allows letting multiple applications open the device. Software mixing will be done by ALSA. |
The volume levels (and mute status) of your devices can be changed with this command-line tool. Use F6 to select the audio device.
alsamixer
To remember the selected volumes for every boot-up you need to store them with:
sudo alsactl store
ALSA loopback
To create ALSA loopback / virtual devices at boot-up, put this line in /etc/modules-load.d/snd-aloop.conf. These are useful in some situations.
snd-aloop
That will make devices starting with hw:Loopback available. There will be 8 subdevices that can be used independently. For each subdevice N there exists a pair:
- hw:Loopback,0,N where programs can send their audio to
- hw:Loopback,1,N where programs can read audio from
- ALSA sends the audio from hw:Loopback,0,N to hw:Loopback,1,N
Example of what is possible in parallel:
Sound server
This is mainly for desktop use. As mentioned, ALSA can mix sound in software with the dmix plugin. However it does not support sending audio over the network. (X is network transparent, but it does not have audio support built in.) Another consideration is that ALSA is only for Linux and not for other Unix systems, such as the BSD variants. A myriad of different “sound servers” has been used in the past to act as a layer in between ALSA (or something else) and desktop applications. Sound servers also support setting the volume per application.
Currently these are around:
- PulseAudio: Can do what was mentioned, including sending sound over the network. It was the default in many Linux distributions for years.
- JACK: For low-latency audio, comparable to ASIO for Windows. This is useful in a studio for music production.
- PipeWire: A newer multimedia framework designed to handle both audio and video streams. It is fast replacing both PulseAudio and JACK. Many distributions already made the switch.