ALSA 简介

  ALSA 是 Advanced Linux Sound Architecture,高级 Linux 声音架构的简称,它在 Linux 操作系统上提供了音频和 MIDI(Musical Instrument Digital Interface,音乐设备数字化接口)的支持。在2.6系列内核中,ALSA 已经成为默认的声音子系统,用来替换2.4系列内核中的 OSS(Open Sound System,开放声音系统)。
  ALSA 的主要特性包括:高效地支持从消费类入门级声卡到专业级音频设备所有类型的音频接口,完全模块化的设计, 支持对称多处理(SMP)和线程安全,对 OSS 的向后兼容,以及提供了用户空间的 alsa-lib 库来简化应用程序的开发。
  想了解更多的关于 ALSA 的这一开源项目的信息和知识,请查看:​​​http://www.alsa-project.org/​​。


ALSA 音频工具编译安装

1、官网 ​​http://www.alsa-project.org​​​ 下载 alsa-lib 和 alsa-utils。
例如:alsa-lib-1.0.27.2.tar.bz2 和 alsa-utils-1.0.27.2.tar.bz2

2、创建 alsa 安装目录(如 /home/rudy/alsa 目录),并在该目录下创建 install 目录,接着把压缩包拷到 alsa 目录下。

3、编译 alsa-lib 用户空间库。

# tar -xvf alsa-lib-1.0.27.2.tar.bz2   
# cd alsa-lib-1.0.27.2
# CC=arm-none-linux-gnueabi-gcc ./configure --host=arm-linux --prefix=/home/rudy/alsa/install/
# make
# make install

4、编译 alsa-utils 工具包。
alsa-utils 包含的工具有:alsactl, aconnect, alsamixer, amidi, amixer, aplay, aplaymidi, arecord, arecordmidi, aseqnet, iecset, speaker-test

# tar -xvf alsa-utils-1.0.27.2.tar.bz2  
# cd alsa-utils-1.0.27.2
# CC=arm-none-linux-gnueabi-gcc ./configure --prefix=/home/rudy/alsa/install/ --host=arm-linux --with-alsa-inc-prefix=/home/rudy/alsa/install/include --with-alsa-prefix=/home/rudy/alsa/install/lib --disable-alsamixer --disable-xmlto --disable-nls
# make

5、简单说明
–prefix 指定了 alsa-lib 编译后的安装目录,包含库和头文件,编译 alsa-utils 时候要指明它的库 lib 和头文件 include 的目录。
alsamixer 是图形化的 amixer 工具,需要 ncurses 的支持,在 secure CRT 中乱码,怕麻烦加 –disable-alsamixer 禁用。

音频录制——arecord

# arecord -h

Usage: arecord [OPTION]... [FILE]...

-h, --help help
--version print current version
-l, --list-devices list all soundcards and-L, --list-pcms list device names
-D, --device=NAME select PCM-q, --quiet quiet mode
-t, --file-type TYPE file type (voc, wav, raw or-c, --channels=# channels -f, --format=FORMAT sample format (case-r, --rate=# sample rate -d, --duration=# interrupt after # seconds -M, --mmap mmap stream
-N, --nonblock nonblocking mode
-F, --period-time=# distance between interrupts is # microseconds -B, --buffer-time=# buffer duration is # microseconds --period-size=# distance between interrupts is # frames --buffer-size=# buffer duration is # frames -A, --avail-min=# min available space for wakeup is # microseconds -R, --start-delay=# delay for automatic PCM start is # microseconds (relative to buffer size if <= 0)
-T, --stop-delay=# delay for automatic PCM stop is # microseconds from xrun -v, --verbose show PCM structure and-V, --vumeter=TYPE enable VU meter (TYPE: mono or-I, --separate-channels one file for --disable-resample disable automatic rate resample
--disable-channels disable automatic channel conversions
--disable-format disable automatic format conversions
--disable-softvol disable software volume control (softvol)
--test-position test ring buffer position
--test-coef=# test coeficient for ring buffer position (default 8) expression for validation is: coef * (buffer_size / 2)
--test-nowait do not wait for ring buffer - eats whole CPU --max-file-time=# start another output file when the old file has recorded for this many seconds
--process-id-file write the process ID --use-strftime apply the strftime facility to the output file name
Recognized sample formats are: S8 U8 S16_LE S16_BE U16_LE U16_BE S24_LE S24_BE U24_LE U24_BE S32_LE S32_BE U32_LE U32_BE FLOAT_LE FLOAT_BE FLOAT64_LE FLOAT64_BE IEC958_SUBFRAME_LE IEC958_SUBFRAME_BE MU_LAW A_LAW IMA_ADPCM MPEG GSM SPECIAL S24_3LE S24_3BE U24_3LE U24_3BE S20_3LE S20_3BE U20_3LE U20_3BE S18_3LE S18_3BE U18_3LE
Some of these may not be available on selected hardware
The availabled format shortcuts are:
-f cd (16 bit little endian, 44100, stereo)
-f cdr (16 bit big endian, 44100, stereo)
-f dat (16 bit little endian, 48000, stereo)

  用 ​​# arecord -l​​ 命令可以列出声卡和数字音频设备,如:

card 0: SOUND0 [TI81XX SOUND0], device 0: TVP-PCM tvp5158-hifi-0 []  
Subdevices: 1/1
Subdevice #0: subdevice #0
card 0: SOUND0 [TI81XX SOUND0], device 1: AIC3X tlv320aic3x-hifi-1 []
Subdevices: 1/1
Subdevice #0: subdevice #0

-D 参数用于指定音频设备 PCM,以 hwx,x 开头(前 x 表示声卡号,后 x 表示设备号)。根据上面 ​​# arecord -l​​ 列出的设备,如果选择 tvp5158 来录制声音的话,那么 pcm 设备就为 hw0,0,如果选择 tlv320aic3x,则 pcm 设备为 hw0,1。

-r 指定采样频率:5512/8000/11025/16000/22050/32000/44100/48000/64000/88200/96000/176400/192000。

-f 指定采样格式:cd/cdr/dat。

用 arecord 录制音频的例子如下(录制完后在 /a 目录下可以找到 1.wmv 文件):

# arecord -Dhw:0,1 -r8000 -f cd /a/1.wmv  

音频播放——aplay

# aplay -h

Usage: aplay [OPTION]... [FILE]...

-h, --help help
--version print current version
-l, --list-devices list all soundcards and-L, --list-pcms list device names
-D, --device=NAME select PCM-q, --quiet quiet mode
-t, --file-type TYPE file type (voc, wav, raw or-c, --channels=# channels -f, --format=FORMAT sample format (case-r, --rate=# sample rate -d, --duration=# interrupt after # seconds -M, --mmap mmap stream
-N, --nonblock nonblocking mode
-F, --period-time=# distance between interrupts is # microseconds -B, --buffer-time=# buffer duration is # microseconds --period-size=# distance between interrupts is # frames --buffer-size=# buffer duration is # frames -A, --avail-min=# min available space for wakeup is # microseconds -R, --start-delay=# delay for automatic PCM start is # microseconds (relative to buffer size if <= 0)
-T, --stop-delay=# delay for automatic PCM stop is # microseconds from xrun -v, --verbose show PCM structure and-V, --vumeter=TYPE enable VU meter (TYPE: mono or-I, --separate-channels one file for --disable-resample disable automatic rate resample
--disable-channels disable automatic channel conversions
--disable-format disable automatic format conversions
--disable-softvol disable software volume control (softvol)
--test-position test ring buffer position
--test-coef=# test coeficient for ring buffer position (default 8) expression for validation is: coef * (buffer_size / 2)
--test-nowait do not wait for ring buffer - eats whole CPU --max-file-time=# start another output file when the old file has recorded for this many seconds
--process-id-file write the process ID --use-strftime apply the strftime facility to the output file name
Recognized sample formats are: S8 U8 S16_LE S16_BE U16_LE U16_BE S24_LE S24_BE U24_LE U24_BE S32_LE S32_BE U32_LE U32_BE FLOAT_LE FLOAT_BE FLOAT64_LE FLOAT64_BE IEC958_SUBFRAME_LE IEC958_SUBFRAME_BE MU_LAW A_LAW IMA_ADPCM MPEG GSM SPECIAL S24_3LE S24_3BE U24_3LE U24_3BE S20_3LE S20_3BE U20_3LE U20_3BE S18_3LE S18_3BE U18_3LE
Some of these may not be available on selected hardware
The availabled format shortcuts are:
-f cd (16 bit little endian, 44100, stereo)
-f cdr (16 bit big endian, 44100, stereo)
-f dat (16 bit little endian, 48000, stereo)

  与 arecord 命令类似,用 ​​# aplay -l​​​ 可以列出声卡和数字音频设备。
一个简单的播放例子如下:

# aplay -Dhw:0,1 -r8000 -f cd /a/1.wmv  

如果想要一边播放一边收听可以用下面利用管道的命令:

# arecord -Dhw:0,1 -r192000 -f cd|aplay -Dhw:0,1 -r192000  

音频配置——amixer

  alsamixer 是Linux 音频架构 ALSA 中的 Alsa 工具的其中一个,用于配置音频的各个参数。alsamixer 是基于文本下的图形界面的,可以通过键盘的上下键,左右键等,很方便地设置需要的音量,开关某个 switch(开关)等等操作。
  amixer,是 alsamixer 的文本模式,即命令行模式,需要用 amixer 命令的形式去配置你的声卡的各个选项。对于 amixer 的使用,你首先需要搞懂你要设置的参数是哪些,然后才可能去了解,如何去配置对应的值,相对 alsamixer 来说,amixer 是有点繁琐。

amixer 用法说明:

# amixer -h

Usage: amixer <options> [command]

Available options:
-h,--help this help
-c,--card N select the card
-D,--device N select the device, default 'default'
-d,--debug debug mode
-n,--nocheck do not perform range checking
-v,--version print version of this program
-q,--quiet be quiet
-i,--inactive show also inactive controls
-a,--abstract L select abstraction level (none or basic)
-s,--stdin Read and execute commands from stdin sequentially

Available commands:
scontrols show all mixer simple controls
scontents show contents of all mixer simple controls (default command)
sset sID P set contents for one mixer simple control
sget sID get contents for one mixer simple control
controls show all controls for given card
contents show contents of all controls for given card
cset cID P set control contents for one control
cget cID get control contents for one

主要用到的命令参数有:
controls:列出控制项
contents:列出控制上下文(也就是控制项和值)
cset cID P:设置控制项值
cget cID:获取设置项值

使用方法:
1、用 ​​​# amixer controls​​​ 列出所有控制项。
2、如果对控制项不熟悉或者想查看当前的所有配置可以使用 ​​​# amixer contents​​​ 命令。
3、查看获取某个设置项的值用 ​​​# amixer cget + 控制参数​​​ 命令。
  这里控制参数直接复制 ​​​# amixer controls​​ 打印出来的数据就行,如:

# amixer cget numid=94,iface=MIXER,name='Left PGA Mixer Line1R Switch'  
numid=94,iface=MIXER,name='Left PGA Mixer Line1R Switch'
; type=BOOLEAN,access=rw------,values=1

4、根据控制参数类型设置控制参数使用 ​​# amixer cset + 控制参数 + 设置参数​​ 命令。如:

# amixer cset numid=94,iface=MIXER,name='Left PGA Mixer Line1R Switch' on  
numid=94,iface=MIXER,name='Left PGA Mixer Line1R Switch'
; type=BOOLEAN,access=rw------,values=1
: values=on

5、关于参数
values 表示数值对于 volume 之类的是具体的数字,在上下文菜单中有显示数值的范围,对于 switch 之类的表示开关一般是 on 或 off。