ALSA and TinyAlsa

本文详细介绍了ALSA(高级Linux声音架构)及其核心功能,包括硬件MIDI合成、混合多个通道等,并对比了TinyALSA,一种用于Android平台的小型ALSA库。
部署运行你感兴趣的模型镜像

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):

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 thehwinterface 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

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 get config */

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

int pcm_set_config(struct pcm *pcm, struct pcm_config *config);

/* Returns the pcm latency in ms */

unsigned int pcm_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



您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

### ALSATinyALSA 的功能区别与应用场景对比 ALSA(Advanced Linux Sound Architecture)是一种为Linux系统设计的音频架构,提供完整的音频驱动和用户空间API支持,广泛用于桌面、服务器以及部分嵌入式系统。而TinyALSA是一个轻量级的ALSA实现,专为嵌入式系统设计,提供了基础的音频功能,适用于资源有限的嵌入式设备[^1]。 #### 功能区别 ALSA 提供了完整的音频功能,包括混音、多声道音频处理、硬件抽象层、OSS兼容层、设备管理、插件支持等。它通过alsa-lib为应用程序提供统一的高级接口,支持多种音频格式和硬件抽象[^1]。ALSA 的工具链如 `aplay`、`arecord`、`amixer` 等功能丰富,适合调试和管理复杂的音频系统。 TinyALSA 则是一个精简版的ALSA实现,去除了alsa-lib中复杂的插件和抽象层,仅保留最核心的音频播放、录音和控制功能。它提供了与ALSA类似的API,但更轻量,适合资源受限的系统[^1]。其配套工具如 `tinyplay` 和 `tinycap` 也更为简单,适合基本的音频测试和调试。 #### 应用场景 ALSA 适用于桌面系统、服务器以及对音频功能要求较高的嵌入式平台。例如,Ubuntu、Fedora 等主流Linux发行版默认使用ALSA作为音频系统,提供完整的音频支持。ALSA 的完整性和灵活性使其成为需要复杂音频处理能力的首选方案[^1]。 TinyALSA 主要应用于Android系统及资源受限的嵌入式设备,如智能音箱、IoT设备、车载系统等。由于Android系统在音频架构上采用了自定义的AudioFlinger服务,底层仍需与硬件交互,因此使用TinyALSA替代标准alsa-lib,以减少内存占用和提升性能[^2]。TinyALSA 的轻量化设计使其在启动速度和资源消耗方面具有优势。 #### 代码示例对比 ALSA播放音频的代码示例(使用alsa-lib): ```c #include <alsa/asoundlib.h> int main() { snd_pcm_t *pcm_handle; snd_pcm_open(&pcm_handle, "default", SND_PCM_STREAM_PLAYBACK, 0); snd_pcm_set_params(pcm_handle, SND_PCM_FORMAT_U8, SND_PCM_ACCESS_RW_INTERLEAVED, 2, 44100, 1, 500000); // 播放音频数据 char *buffer = "dummy audio data"; snd_pcm_writei(pcm_handle, buffer, sizeof(buffer)); snd_pcm_close(pcm_handle); return 0; } ``` TinyALSA播放音频的代码示例: ```c #include <tinyalsa/tinyalsa.h> int main() { struct pcm *pcm = pcm_open(0, 0, PCM_OUT, NULL); pcm_set_config(pcm, 44100, PCM_FORMAT_S16_LE, 2, 1024, 0); // 播放音频数据 char *buffer = "dummy audio data"; pcm_write(pcm, buffer, sizeof(buffer)); pcm_close(pcm); return 0; } ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值