ALSA and TinyAlsa

来源:互联网 发布:经典的单机游戏 知乎 编辑:程序博客网 时间:2024/05/14 15:46

Alsa

Introduction

The Advanced Linux Sound Architecture(ALSA) is a Linux kernel component intended to replace the original OpenSound System (OSSv3) for providing device drivers for sound cards. Besides thesound device drivers,ALSA also bundles a user space library forapplication developers who want to use driver features with a higher level APIthan direct interaction with the kernel drivers. In a word, the Advanced LinuxSound Architecture (ALSA) comes with a kernel API and a library API.

ALSA Features

ALSA was designed to use some featureswhich were not, at the time of its conception, supported by theOpen SoundSystem (OSS):

  • Hardware-based MIDI synthesis.
  • Hardware mixing of multiple channels.
  • Full-duplex operation.
  • Multiprocessor-friendly, thread-safe device drivers.

ALSA has a larger and more complex API than OSS, so it can be harder to develop anapplication that uses ALSA as its sound technology. While ALSA may beconfigured to provide an OSSemulation layer, such functionality is no longer available in many Linuxdistributions.

Besides the sound device drivers, ALSAbundles a user spacelibrary for application developers whowant to use driver features through an interface that is higher level than the interface provided fordirect interaction with the kernel drivers; unlike the kernel API, which triesto reflect the capabilities of the hardware directly, ALSA's user space librarypresents an abstraction which remains as standardized as possible acrossdisparate underlying hardware. This goal is achieved in part by using softwareplugins. For example, many modern soundcards or built-in sound chips don't havea "master volume" control; for these devices, the user space libraryinstead provides a software volume control using the "softvol"plugin, and ordinary application software need not care whether such a controlis implemented by actual underlying hardware or software emulation of suchunderlying hardware.

ALSA Concepts

This section provides an overview of basicconcepts pertaining to ALSA.

Typically, ALSA supports up to eight cards,numbered 0 through 7; each card is a physical or logical kernel device capableof input, output, or control of sound, and card number 0 is used by defaultwhen no particular card is specified. Furthermore, each card may also beaddressed by its id, which is an explanatory string such as "Headset"or "ICH9".

A card has devices, numberedstarting at 0; a device may be of playback type, meaning it outputssound from the computer, or some other type such ascapture,control,timer, orsequencer; device number 0 is used by default when noparticular device is specified.

A device may have subdevices,numbered starting at 0; a subdevice represents some relevant sound endpoint forthe device, such as a speaker pair. If the subdevice is not specified, or ifsubdevice number -1 is specified, then any available subdevice is used.

A card's interface is a descriptionof an ALSA protocol for accessing the card; possible interfaces include:hw,plughw,default, andplug:dmix. Thehw interfaceprovides direct access to the kernel device, but no software mixing or streamadaptation support. Theplughw anddefault enable sound outputwhere thehw interface would produce an error.

An application typically describes soundoutput by combining all of the aforementioned specifications together in adevicestring, which has one of the following forms (which arecasesensitive):

  • interface:card,device,subdevice
  • interface:CARD=1,DEV=3,SUBDEV=2.

An ALSA stream is a data flowrepresenting sound; the most common stream format isPCM that must be produced in such a way asto match the characteristics or parameters of the hardware, including:

  • sampling rate: 44.1 kHz on home stereos, and 48 kHz on home theaters
  • sample width: measured in some number of bits per sample (such as 8, 16, 24, or 32 bits/sample)
  • sample encoding
  • number of channels: 1 for mono, 2 for stereo, or 6 for AC-3/IEC958

Also, ALSA needs a buffer parameter thatdetermines how often CPU attention is required to supply sound data.

ALSA Kernel Modules

  • snd_hda_codec_hdmi
  • snd_hda_codec_via
  • snd_hda_intel
  • snd_hda_codec         
  • snd_hwdep
  • snd_pcm
  • snd_timer
  • snd
  • snd_page_alloc

ALSA Interfaces

The currently designed interfaces arelisted below:

  1. Information Interface (/proc/asound)
  2. Control Interface (/dev/snd/controlCX)
  3. Mixer Interface (/dev/snd/mixerCXDX)
  4. PCM Interface (/dev/snd/pcmCXDX)
  5. Raw MIDI Interface (/dev/snd/midiCXDX)
  6. Sequencer Interface (/dev/snd/seq)
  7. Timer Interface (/dev/snd/timer)

For the VIA EITX-3002, you might also wantto check the directory /dev/snd for the right device files:

  1. controlC0
  2. hwC0D0
  3. pcmC0D0p
  4. pcmC0D0c
  5. timer

ALSA API usage

The ALSA library API is the interface tothe ALSA drivers. Developers need to use the functions in this API to achievenative ALSA support for their applications, instead of kernel API. How itinterfaces with the kernel API is focused by the library API. Applicationprogrammers should use the library API rather than the kernel API. The libraryoffers 100% of the functionality of the kernel API, but adds major improvementsin usability, making the application code simpler and better looking. Inaddition, future fixes or compatibility code may be placed in the library codeinstead of the kernel driver.

ALSA API links

  • Page Control interface explains the primitive controls API.
  • Page High level control interface explains the high-level primitive controls API.
  • Page Mixer interface explains the mixer controls API.
  • Page PCM (digital audio) interface explains the design of the PCM (digital audio) API.
  • Page PCM (digital audio) plugins explains the design of PCM (digital audio) plugins.
  • Page PCM External Plugin SDK explains the external PCM plugin SDK.
  • Page External Control Plugin SDK explains the external control plugin SDK.
  • Page RawMidi interface explains the design of the RawMidi API.
  • Page Timer interface explains the design of the Timer API.
  • Page Sequencer interface explains the design of the Sequencer API.

Tinyalsa

Introduction

Tinyalsa is a small library to interface withALSA in the Linux kernel.

Tinyalsa basic files list:

-         pcm.c                           for control interface

-         mixer.c                         for pcminterface

, and provide three tools:

-         tinyplay.c                    for play

-         tinycap.c                     for capture

-         tinymixer.c                 for mixer

Tinyalsa Aims

Tinyalsa is a small ALSA library, in orderto substituting for original ALSA library in Android platform. The aims are:

  • Provide a basic pcm and mixer API
  • If it's not absolutely needed, don't add it to the API
  • Avoid supporting complex and unnecessary operations that could be dealt with at a higher level

Tinyalsa Functions

Provide a basic pcm and mixer API toimplement the Audio HAL (Hardware Abstract Level) for Android platform.

Tinyalsa vs. ALSA-lib

 

Tinyalsa

ALSA-lib

Control Interface

Partly support

Completely support

Mixer Interface

----

Completely support

PCM Interface

Partly support                            

Completely support                                 

Raw MIDI Interface

----

Completely support

Sequencer Interface                        

----

Completely support

Timer Interface

----

Completely support

Why use Tinyalsa

We can use ALSA-lib substitute for Tinyalsacompletely, but we can’t use Tinyalsa substitute for ALSA-lib completely. But,in some times, Tinyalsa can substitute for ALSA-lib to work for some basicfunction.

  • The only reason is that it is smaller and simpler than ALSA-lib.
  • Google Android supplies the Tinyalsa.
  • Audio HAL interface forms changed in Android 4.0.

Tinyalsa API Limitations

Notice: The following function Interfacesupplied by tinyalsa is not implemented.

/* Set and getconfig */

int pcm_get_config(structpcm *pcm, struct pcm_config *config);

intpcm_set_config(struct pcm *pcm, struct pcm_config *config);

/* Returns thepcm latency in ms */

unsigned intpcm_get_latency(struct pcm *pcm);

Reference:

http://www.alsa-project.org/main/index.php/Main_Page

http://en.wikipedia.org/wiki/Advanced_Linux_Sound_Architecture

https://wiki.archlinux.org/index.php/Advanced_Linux_Sound_Architecture