Linux Audio
- Details
- Last Updated: Sunday, 10 September 2023 23:34
- Published: Sunday, 10 September 2023 19:58
- Hits: 832
Linux Audio
Audio and wifi are the 2 things you need to be working for any system to be usable. Audio is something that's giving me problems with Linux distro CentOS 7 (as of Sept 2023), so I thought to add this section
Background:
Basic info: https://linuxhint.com/guide_linux_audio/
Below I show the general Liux Audio Architecture. Though the contents are taken from QNX link below, it applies to all architectures.
Link for QNX (NOT alsa): https://www.qnx.com/developers/docs/6.4.1/neutrino/audio/architecture.html
In order for an application to produce sound, the system must have these 3 components:
- Hardware: This is in the form of a sound card or sound chip. The basic piece of hardware needed to produce or capture (i.e. record) sound is an audio chip or sound card, referred to simply as a card. Linux Arch can support more than one card at a time, and can even mount and unmount cards “on the fly”. All the sound devices are attached to a card, so in order to reach a device, you must first know what card it's attached to. Each sound card has multiple devices. /dev/snd/ shows all the devices. 3 most common sound devices present in each sound card are:
- Control device: There's one control device for each sound card in the system. This device is special because it doesn't directly control any real hardware. It's a concentration point for information about its card and the other devices attached to its card. The primary information kept by the control device includes the type and number of additional devices attached to the card.
- Mixer devices: These are responsible for combining or mixing the various analog signals on the sound card. A mixer may also provide a series of controls for selecting which signals are mixed and how they're mixed together, adjusting the gain or attenuation of signals, and/or the muting of signals.
- PCM (Pulse Code Modulation) devices: These are responsible for converting digital sound sequences to analog waveforms, or analog waveforms to digital sound sequences. Each device operates only in one mode or the other. If it converts digital to analog, it's a playback channel device; if it converts analog to digital, it's a capture channel device.
- Device driver: for the hardware
- API (Application Programming Interface): a well-defined way for the application to talk to the driver, in the form of an API.
Centos-7: ls /dev/snd/ => controlC0 (Control device) hwC0D0 pcmC0D3p seq timer
ALSA: Advanced Linux Sound Architecture
ALSA is a software framework included with Linux Kernel which provides an API for sound card device drivers. So, basically we don't have to call device driver functions,but instead call ALSA API. ALSA was introduced with Kernel 2.6 onwards (year 2002) and replaced the older OSS (open sound system), though it does provide support for OSS too.
Linux Audio Architecture: https://www.thewelltemperedcomputer.com/Linux/AudioArchitecture.htm
Official site: https://www.alsa-project.org/
ALSA provides fully modularized Sound drivers. It also provides User space library (alsa-lib) to simplify application programming and provide higher level functionality. ALSA native applications communicate with ALSA library (outside of kernel) via ALSA lib API, which then communicates with ALSA kernel API (inside of Kernel) which in turn talk to ALSA kernel drivers, which finally communicate with real sound hardware.
PulseAudio:
Our applications never interact with ALSA directly, but instead we use another application called PulseAudio that is the user facing interface in most linux distro. PulseAudio sound server provides further tools to better control our sound system. This is done with modules to define volume levels, audio card profiles, output sinks, or input sources, and more for easy access from most Ubuntu applications. Like ALSA, PulseAudio is also designed to run with as few user interactions as possible. Whenever we need to change volume, balance, or input/output devices, it is a PulseAudio module or application we or our application call. PulseAudio is the interface engine between applications and ALSA. However, its use is not mandatory and audio can still be played and mixed together without PulseAudio.
A professional alternative to PulseAudio is the JACK audio server, with more options to interact with our sound hardware and a better low latency support. Only few music production applications expect to have JACK running, which can be done in parallel to PulseAudio.
To see modules loaded by ALSA, we can use lsmod cmd:
lsmod | grep snd => this shows about a dozen snd alsa modules. Imp modules are: snd_hda_intel, snd_seq, snd_pcm, snd_timer,
snd_hda_codec_hdmi 52340 1
snd_hda_intel 40390 2
snd_hda_codec 136355 2 snd_hda_codec_hdmi,snd_hda_intel
snd_hda_core 91007 3 snd_hda_codec_hdmi,snd_hda_codec,snd_hda_intel
snd_hwdep 13608 1 snd_hda_codec
snd_seq 62774 0
snd_seq_device 14356 1 snd_seq
snd_pcm 101339 4 snd_hda_codec_hdmi,snd_hda_codec,snd_hda_intel,snd_hda_core
snd_timer 30014 2 snd_pcm,snd_seq
snd 83987 12 snd_hwdep,snd_timer,snd_hda_codec_hdmi,snd_pcm,snd_seq,snd_hda_codec,snd_hda_intel,snd_seq_device
soundcore 15047 1 snd