ALSA

Advanced Linux Sound Architecture

The Advanced Linux Sound Architecture (ALSA) provides kernel driven sound card drivers.
Besides the sound device drivers, ALSA also bundles a user space driven library for application developers. This enables direct (kernel) interaction with sound devices through ALSA libraries.

Understanding ALSA device, subdevice and cards

ALSA arranges hardware audio devices and their components into a hierarchy of:
  • cards
  • ALSA calls each audio controller a 'card'.
    ALSA cards correspond one-to-one to hardware sound cards.
  • devices
  • Each card has 'devices'; a device is a unit which's capable of handling an audio stream: processing (for 'playback) or sending an audio stream ('capture).
    Most of ALSA's hardware access happens at the device level.
    Different devices can be opened and used independently of each other. Typically, specifying a sound card and device will be sufficient to determine on which connector or set of connectors your audio signal will come out, or from which it is read.
  • subdevices
  • There are also subdevices which are a part of an input or output device.
    • In context of output devices, a device having multiple subdevices means the hardware can do mixing, i.e. it can take multiple streams of PCM and mix them to produce a single output.
    • For input subdevice it means from which port will the input be taken from (like line in, mic, front mic etc...)
ALSA numbers these cards and devices starting from 0.

Installation

ALSA is a set of built-in Linux kernel modules.
udev will automatically detect your hardware and select needed drivers at boot time.

ALSA Utilities

  • alsa-utils package
    • amixer is a shell command to change audio setting
    • amixer supports multiple soundcards. amixer with no arguments will display the current mixer settings for the default soundcard and device. This is a good way to see a list of the simple mixer controls you can use.
      
              amixer [-option] [cmd]
              
      option:
      • -D device
      • The device name has syntax 'hw:N' where N is specified card number.
      • -c card
      • Select the card number to control
      cmd:
      • scontrols
      • Shows a complete list of simple mixer controls.
      • scontents
      • Shows a complete list of simple mixer controls with their contents.
      • sget <SCONTROL>
      • Shows the simple mixer control contents.
      • sset <SCONTROL>l; <PARAMETER> ...
      • Sets the simple mixer control contents.
    • alsamixer provides a more intuitive ncurses based interface for audio device configuration
    • alsactl
    • alsactl saves the driver state to the configuration file which, by default, is /var/lib/alsa/asound.state.
      Once done, this file can be included in the build system and shipped with the root filesystem. Usual distributions already include a script that will invoke alsactl at boot time to restore the settings. If it is not the case, then it is simply a matter of calling alsactl restore.
    • Systemd unit configuration files
    • The alsa-utils package comes with Systemd unit configuration files which are automatically installed and activated during installation (via package provided symlink to sound.target).
      Both services are mutually exclusive and you can decide which one to be used:
      • alsa-restore.service
      • Reads /var/lib/alsa/asound.state on boot and writes updated values on shutdown, granted it is already present by having ran alsactl store at some point
      • alsa-state.service
      • (Re-)Starts alsactl in daemon mode to continouously keep track of, and persist, volume changes, again under the condition that the user has consciously started alsactl daemon at least once
      You can check their status using systemctl.

Unmuting the channels

By default ALSA has all channels muted. Those have to be unmuted manually.

$ amixer sset Master unmute
$ amixer sset Speaker unmute
$ amixer sset Headphone unmute

Configuration

Configuration files serve to define ALSA devices.

The "built-in" configuration file alsa.conf contains definitions of devices. If you have special requirements, or if you experience trouble, you might want to add definitions of your own.

  • The built-in configuration file is /usr/share/alsa
  • The system-wide configuration file is /etc/asound.conf
  • the per-user configuration file is ~/.asoundrc
All configuration files are parsed every time an ALSA device is opened. So changes take effect immediately, and it is not necessary to restart anything.

Syntax summary

ALSA configuration files contain hierarchically structured parameter-value pairs.


# Configuration file syntax
# Include a new configuration file
<filename>
# Simple assignment
name [=] value [,|;]
# Compound assignment (first style)
name [=] {
        name1 [=] value [,|;]
        ...
}
# Compound assignment (second style)
name.name1 [=] value [,|;]
# Array assignment (first style)
name [
        value0 [,|;]
        value1 [,|;]
        ...
]
# Array assignment (second style)
name.0 [=] value0 [,|;]
name.1 [=] value1 [,|;]

Advanced Linux Sound Architecture/Troubleshooting

Microphone

To use Microphone,
  1. Find the card number you want to use
  2. 
    $ arecord --list-devices 
    	
  3. Determine the name of the capture device for the card
  4. 
    $ amixer -c 2
    	
  5. Operations
    • Get the current settings
    • 
      $ amixer -c 2 sget Mic
      		
    • Unmute the microphone and set the volume to 100%
    • 
      $ amixer -c 2 sset Mic 100% cap unmute
      		
    • Mute the microphone and set the volume to 0%
    • 
      $ amixer -c 2 sset Mic  0% cap mute
      		

Use microphone from USB webcam


$ lsusb | grep -i cam 
Bus 001 Device 008: ID 046d:0825 Logitech, Inc. Webcam C270
$ arecord --list-devices 
**** List of CAPTURE Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC887-VD Analog [ALC887-VD Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 2: ALC887-VD Alt Analog [ALC887-VD Alt Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: U0x46d0x825 [USB Device 0x46d:0x825], device 0: USB Audio [USB Audio]
  Subdevices: 0/1
  Subdevice #0: subdevice #0

$ amixer -D hw:2
Simple mixer control 'Mic',0
  Capabilities: cvolume cvolume-joined cswitch cswitch-joined
  Capture channels: Mono
  Limits: Capture 0 - 16
  Mono: Capture 12 [75%] [24.00dB] [on]
$ pacmd list-cards
3 card(s) available.
...
    index: 3
	name: <alsa_card.usb-046d_0825_5564B250-02>
	driver: <module-alsa-card.c>
	owner module: 29
	properties:
		alsa.card = "2"
		alsa.card_name = "USB Device 0x46d:0x825"
		alsa.long_card_name = "USB Device 0x46d:0x825 at usb-0000:00:14.0-8, high speed"
		alsa.driver_name = "snd_usb_audio"
		device.bus_path = "pci-0000:00:14.0-usb-0:8:1.2"
		sysfs.path = "/devices/pci0000:00/0000:00:14.0/usb1/1-8/1-8:1.2/sound/card2"
		udev.id = "usb-046d_0825_5564B250-02"
		device.bus = "usb"
		device.vendor.id = "046d"
		device.vendor.name = "Logitech, Inc."
		device.product.id = "0825"
		device.product.name = "Webcam C270"
		device.serial = "046d_0825_5564B250"
		device.form_factor = "webcam"
		device.string = "2"
		device.description = "Webcam C270"
		module-udev-detect.discovered = "1"
		device.icon_name = "camera-web-usb"
	profiles:
		input:mono-fallback: Mono Input (priority 1, available: unknown)
		input:multichannel-input: Multichannel Input (priority 1, available: unknown)
		off: Off (priority 0, available: unknown)
	active profile: <input:mono-fallback>
	sources:
		alsa_input.usb-046d_0825_5564B250-02.mono-fallback/#3: Webcam C270 Mono
	ports:
		analog-input-mic: Microphone (priority 8700, latency offset 0 usec, available: unknown)
			properties:
				device.icon_name = "audio-input-microphone"
		multichannel-input: Multichannel Input (priority 0, latency offset 0 usec, available: unknown)
			properties:
Test:
  • record
  • 
    $ arecord -f S24_LE -c 2 -r 192000 -d 20 test.wav
    Recording WAVE 'test.wav' : Signed 24 bit Little Endian, Rate 192000 Hz, Stereo    
        
  • play
  • 
    $ aplay test.wav
    Playing WAVE 'test.wav' : Signed 24 bit Little Endian, Rate 192000 Hz, Stereo    
        
You might also want to check the directory /dev/snd/ for the right device files:

$ sudo ls -l /dev/snd
total 0
drwxr-xr-x    2 root     root            60 Aug 18 00:33 by-path
crw-rw----    1 root     audio     116,   0 Aug 18 00:33 controlC0
crw-rw----    1 root     audio     116,  24 Aug 18 00:33 pcmC0D0c
crw-rw----    1 root     audio     116,  16 Aug 18 00:33 pcmC0D0p
crw-rw----    1 root     audio     116,  25 Aug 18 00:33 pcmC0D1c
crw-rw----    1 root     audio     116,  17 Aug 18 00:33 pcmC0D1p
crw-rw----    1 root     audio     116,  33 Aug 18 00:33 timer
If you have at least the devices controlC0 and pcmC0D0p or similar, then your sound modules have been detected and loaded properly.

List all soundcards and digital audio devices:


$ sudo aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC256 Analog [ALC256 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 3: HDMI 0 [HDMI 0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 7: HDMI 1 [HDMI 1]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 8: HDMI 2 [HDMI 2]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 9: HDMI 3 [HDMI 3]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: PCH [HDA Intel PCH], device 10: HDMI 4 [HDMI 4]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: DSP [Plantronics .Audio 400 DSP], device 0: USB Audio [USB Audio]
  Subdevices: 0/1
  Subdevice #0: subdevice #0
  
The variable ALSA_CARD can be set to the name which comes after the colon and before the bracket. For ex. PCH or DSP.

/proc/asound

ALSA has its own proc tree, /proc/asound.


/proc/asound
|-- DSP -> card1
|-- PCH -> card0
|-- card0
|   |-- codec#0
|   |-- codec#2
|   |-- eld#2.0
|   |-- eld#2.1
|   |-- eld#2.2
|   |-- eld#2.3
|   |-- eld#2.4
|   |-- eld#2.5
|   |-- eld#2.6
|   |-- eld#2.7
|   |-- eld#2.8
|   |-- id
|   |-- pcm0c
|   |   |-- info
|   |   `-- sub0
|   |       |-- hw_params
|   |       |-- info
|   |       |-- prealloc
|   |       |-- prealloc_max
|   |       |-- status
|   |       `-- sw_params
|   |-- pcm0p
|   |   |-- info
|   |   `-- sub0
|   |       |-- hw_params
|   |       |-- info
|   |       |-- prealloc
|   |       |-- prealloc_max
|   |       |-- status
|   |       `-- sw_params
|   |-- pcm10p
|   |   |-- info
|   |   `-- sub0
|   |       |-- hw_params
|   |       |-- info
|   |       |-- prealloc
|   |       |-- prealloc_max
|   |       |-- status
|   |       `-- sw_params
|   |-- pcm3p
|   |   |-- info
|   |   `-- sub0
|   |       |-- hw_params
|   |       |-- info
|   |       |-- prealloc
|   |       |-- prealloc_max
|   |       |-- status
|   |       `-- sw_params
|   |-- pcm7p
|   |   |-- info
|   |   `-- sub0
|   |       |-- hw_params
|   |       |-- info
|   |       |-- prealloc
|   |       |-- prealloc_max
|   |       |-- status
|   |       `-- sw_params
|   |-- pcm8p
|   |   |-- info
|   |   `-- sub0
|   |       |-- hw_params
|   |       |-- info
|   |       |-- prealloc
|   |       |-- prealloc_max
|   |       |-- status
|   |       `-- sw_params
|   `-- pcm9p
|       |-- info
|       `-- sub0
|           |-- hw_params
|           |-- info
|           |-- prealloc
|           |-- prealloc_max
|           |-- status
|           `-- sw_params
|-- card1
|   |-- id
|   |-- pcm0c
|   |   |-- info
|   |   `-- sub0
|   |       |-- hw_params
|   |       |-- info
|   |       |-- status
|   |       `-- sw_params
|   |-- pcm0p
|   |   |-- info
|   |   `-- sub0
|   |       |-- hw_params
|   |       |-- info
|   |       |-- status
|   |       `-- sw_params
|   |-- stream0
|   |-- usbbus
|   |-- usbid
|   `-- usbmixer
|-- cards
|-- devices
|-- hwdep
|-- modules
|-- oss
|   |-- devices
|   `-- sndstat
|-- pcm
|-- seq
|   |-- clients
|   |-- drivers
|   |-- queues
|   `-- timer
|-- timers
`-- version

To get the loaded sound modules and their order

$ sudo cat /proc/asound/modules
 0 snd_soc_simple_card

Configuration Files

The default global configuration: /usr/share/alsa/alsa.conf

Some ALSA variables are used  in the default global configuration:

# pre-load the configuration files

@hooks [
        {
                func load
                files [
                        "/etc/alsa/conf.d"
                        "/etc/asound.conf"
                        "~/.asoundrc"
                ]
                errors false
        }
]
...
#
# defaults
#

# show all name hints also for definitions without hint {} section
defaults.namehint.showall on
# show just basic name hints
defaults.namehint.basic on
# show extended name hints
defaults.namehint.extended on
#
defaults.ctl.card 0
defaults.pcm.card 0
...
#
#  PCM interface
#

# redirect to load-on-demand extended pcm definitions
pcm.cards cards.pcm

pcm.default cards.pcm.default
...
pcm.hw {
...
}

pcm.plughw {
...
}
...
#
#  Control interface
#
...
#
#  RawMidi interface
#
...
  • ALSA_CARD
    • pcm.default
    • pcm.hw
    • pcm.plughw
    • ctl.sysdefault
    • ctl.hw
    • rawmidi.default
    • rawmidi.hw
    • hwdep.hw
  • ALSA_CTL_CARD
    • ctl.sysdefault
    • ctl.hw
  • ALSA_HWDEP_CARD
    • hwdep.default
    • hwdep.hw
  • ALSA_HWDEP_DEVICE
    • hwdep.default
    • hwdep.hw
  • ALSA_PCM_CARD
    • pcm.default
    • pcm.hw
    • pcm.plughw
  • ALSA_PCM_DEVICE
    • pcm.hw
    • pcm.plughw
  • ALSA_RAWMIDI_CARD
    • rawmidi.default
    • rawmidi.hw
  • ALSA_RAWMIDI_DEVICE
    • rawmidi.default
    • rawmidi.hw
You can override the default behavior in your own configuration file /etc/asound.conf.
The 'pcm' options affect which card and device will be used for audio playback while the 'ctl' option affects which card is used by control utilities like alsamixer.

ALSA SoC Layer

ALSA SoC Layer Overview

ASoC Design

The ASoC layer provides the following features :
  • Codec independence.
  • Allows reuse of codec drivers on other platforms and machines.
  • Easy I2S/PCM audio interface setup between codec and SoC.
  • Each SoC interface and codec registers its audio interface capabilities with the core and are subsequently matched and configured when the application hardware parameters are known.
  • Dynamic Audio Power Management (DAPM).
  • DAPM automatically sets the codec to its minimum power state at all times. This includes powering up/down internal power blocks depending on the internal codec audio routing and any active streams.
  • Pop and click reduction.
  • Pops and clicks can be reduced by powering the codec up/down in the correct sequence (including using digital mute). ASoC signals the codec when to change power states.
  • Machine specific controls:
  • Allow machines to add controls to the sound card (e.g. volume control for speaker amplifier).
ASoC basically splits an embedded audio system into multiple re-usable component drivers:
  • Codec class drivers:
  • The codec class driver is platform independent and contains audio controls, audio interface capabilities, codec DAPM definition and codec IO functions. This class extends to BT, FM and MODEM ICs if required. Codec class drivers should be generic code that can run on any architecture and machine.
  • Platform class drivers:
  • The platform class driver includes the audio DMA engine driver, digital audio interface (DAI) drivers (e.g. I2S, AC97, PCM) and any audio DSP drivers for that platform.
  • Machine class driver:
  • The machine driver class acts as the glue that describes and binds the other component drivers together to form an ALSA "sound card device". It handles any machine specific controls and machine level audio events (e.g. turning on an amp at start of playback).

ALSA Audio Servers

ALSA contains the actual device drivers (in the kernel source), and a library to access those drivers. You use sound perfectly fine with just ALSA alone.

"Audio server" isn't a particular well-defined concept.
ALSA is a library, PulseAudio and JACK both run a server process.
You can have other "audio servers" on top of that, depending on your definition.

A sound server mixes different data streams and sends out a single unified audio to an output device.

PulseAudio

PulseAudio is the standard audio server in desktop installations.

Install

  • Install ALSA
  • 
    $ sudo apt-get install libasound2 libasound2-plugins alsa-utils alsa-oss
      
  • Install PulseAudio
  • 
    $ sudo apt-get install pulseaudio pulseaudio-utils
      
  • Set Group Memberships for PA
  • 
    $ sudo usermod -aG pulse,pulse-access username
      

SOF Driver Architecture

Overview

Audio Driver Architecture

The Sound Open Firmware (SOF)-based audio driver stack consists of:
  • an architecture-independent SOF driver core
  • an SOF DSP driver for Intel High Definition Audio (HD-Audio) platforms
  • an ALSA System-on-Chip (ASoC)-compliant audio codec driver
  • and a hardware-specific machine driver
Sound Open Firmware (SOF) is an open source audio DSP firmware infrastructure and SDK.
A generic SOF subsystem is implemented in Linux as a subsystem of ALSA SoC(ASoC).
ASoC provides a modular architecture to share audio codec drivers across different SoC implementations, unify the controls provided to applications, and provide a common infrastructure to manage SoC audio component power and clocks.

The upstream Linux kernel has a few drivers that are related to the SOF-based audio driver stack:

  • the Intel AZX HD Audio driver (linux/sound/pci/hda)
  • The AZX driver is intended to be used with Intel HD Audio PCI hardware when the Audio DSP is disabled (e.g. BIOS configuration).
  • the Intel SST Audio driver (linux/sound/soc/intel)
  • The SST Audio implements an ASoC-compliant driver for Intel HD Audio hardware which utilizes the Intel SST firmware.
    The SST driver and firmware should be used when the DSP is enabled and SOF firmware is not available for the platform.

Driver Probe

SOF Platform Driver

IPC Processing

PCM Driver

Power Management

Intel Drivers

Support for High Definition Audio (HD-Audio)

Kernel Configuration/Kconfig

Debug Options

留言

熱門文章