Alsa 调试中篇:工具篇

1.前言

  笔者在上一篇的alsa调试上篇:编译篇,中介绍了如何将alsa官网下载的源码中交叉编译到自己的目标板上。在本章中,我将会为你介绍如何用alsa-utils提供的工具来快速熟悉和使用alsa.注意,笔者这里没在开发板上面运行命令来讲解,而是用我的ubuntu16.04主机来讲解。如果你也想在你自己的ubuntu上面运行相关命令,直接敲我接下来的命令就行。如果你想运行从alsa官网下载下来的包命令,很简单,在编译篇中介绍的,你只需运行./configure ,make,二条命令就行。

2.alsa播放工具-aplay

       aplay是alsa官网结合alsa-lib给出的api接口,给出的相关应用实例。当然你也可以根据alsa-lib给出的api接口写自己的应用程序,在之后的博客中,笔者将会介绍我项目中的一个具体应用,得益于这个应用的成功,笔者的整个框架异常方便扩张很多东西,比如对多路音频的切换(无论是做mux还是mix,都能很方便),对MRM(多房间)的应用等等。回归主题,在你的ubuntu主机上面运行如下命令:

ccion@ubuntu:~$ aplay -h
Usage: aplay [OPTION]... [FILE]...

-h, --help              help
    --version           print current version
-l, --list-devices      list all soundcards and digital audio devices
-L, --list-pcms         list device names
-D, --device=NAME       select PCM by name
-q, --quiet             quiet mode
-t, --file-type TYPE    file type (voc, wav, raw or au)
-c, --channels=#        channels
-f, --format=FORMAT     sample format (case insensitive)
-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 setup (accumulative)
-V, --vumeter=TYPE      enable VU meter (TYPE: mono or stereo)
-I, --separate-channels one file for each channel
-i, --interactive       allow interactive operation from stdin
-m, --chmap=ch1,ch2,..  Give the channel map to override or follow
    --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 coefficient 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 here
    --use-strftime      apply the strftime facility to the output file name
    --dump-hw-params    dump hw_params of the device
    --fatal-errors      treat all errors as fatal
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 U18_3BE G723_24 G723_24_1B G723_40 G723_40_1B DSD_U8 DSD_U16_LE DSD_U32_LE DSD_U16_BE DSD_U32_BE
Some of these may not be available on selected hardware
The available 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)

用法:

aplay [OPTION]... [FILE]...

以下,我将会为你介绍几个常用的参数。

  • aplay -l

        列出你当前系统有多少个声卡。比如笔者的ubuntu系统:

ccion@ubuntu:~$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC3202 Analog [ALC3202 Analog]
  Subdevices: 0/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

  笔者的系统中有4个声卡,其中主声卡是card0,device 0,也就是你看到的ALC3202 Analog.为什么是card 0,如果你有这个疑惑,可以去看看笔者的《Linux ALSA音频系统:platform,machine,codec》,在这里我详解介绍了card的生成。也请注意ALC3202 Analog这个名字,也是我那篇博客中,驱动codec的名字。

  • -D 表示设备,即aplay -l 列出的设备名。
  • -f      表示音频文件的格式,常用的有S16_LE ,S24_LE ,S32_LE。
  • -r      表示音频文件的采样率,常用44.1khz,48khz.
  • -c     表示音频文件的通道数,常用2
  • -t      表示音频文件的类型,目前alpay支持的音频类型,(1).voc(2).wav  (3).raw (4) .au

 note:这里的-f,-r,-c参数都与你的声卡驱动有关,在笔者的博客《Linux ALSA音频系统:platform,machine,codec》一文有详细介绍。

音频格式:S是有符号,U是无符号,BE是大端,LE是小端。

这都是PCM的一种表示范围的方法,所以表示方法中最小值等价,最大值等价。

S8: sighend 8 bits,有符号字符= char,    表示范围-128~127

U8:unsigned 8 bits,无符号字符=unsigned char, 表示范围0~255

S16_LE:little endian signed 16 bits,小端有符号字=short , 表示范围-32768~32767

S16_BE:big endian signed 16 bits,大端有符号字=short倒序(PPC),表示范围-32768~32767

U16_LE:little endian unsigned 16 bits,小端无符号字=unsigned short,表示范围0~65535

U16_BE:big endian unsigned 16 bits,大端无符号字=unsigned short倒序(PPC),表示范围0~65535

BE是大端(低地址存高位),LE是小端(低地址存地位)

接下来,我们播放一个wav文件,这里作者的播放音频为Hello.wav。

aplay -Dhw:0,0 -f S16_LE -r 48000 -c 2 Hello.wav

  当然,对于播放来说,-f , -r,-c参数是可以不用加的,aplay的源码中,会自己检测wav文件的head就可以知道音频文件的相关参数,然后会自己配置。简单的命令:

aplay -Dhw:0,0 Hello.wav 

Note:后面加的音频文件一定要带上你文档放置的路劲。

3.alsa录音工具-arecord

    不多说,直接运行命令:

ccion@ubuntu:~$ arecord -h
Usage: arecord [OPTION]... [FILE]...

-h, --help              help
    --version           print current version
-l, --list-devices      list all soundcards and digital audio devices
-L, --list-pcms         list device names
-D, --device=NAME       select PCM by name
-q, --quiet             quiet mode
-t, --file-type TYPE    file type (voc, wav, raw or au)
-c, --channels=#        channels
-f, --format=FORMAT     sample format (case insensitive)
-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 setup (accumulative)
-V, --vumeter=TYPE      enable VU meter (TYPE: mono or stereo)
-I, --separate-channels one file for each channel
-i, --interactive       allow interactive operation from stdin
-m, --chmap=ch1,ch2,..  Give the channel map to override or follow
    --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 coefficient 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 here
    --use-strftime      apply the strftime facility to the output file name
    --dump-hw-params    dump hw_params of the device
    --fatal-errors      treat all errors as fatal
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 U18_3BE G723_24 G723_24_1B G723_40 G723_40_1B DSD_U8 DSD_U16_LE DSD_U32_LE DSD_U16_BE DSD_U32_BE
Some of these may not be available on selected hardware
The available 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 [OPTION]... [FILE]...

几个主要参数,-D,-f,-c,-r,-t同第二节讲解的参数含义同样。接下来看看笔者ubuntu的录音声卡,

ccion@ubuntu:~$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC3202 Analog [ALC3202 Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

  只有一个录音声卡,codec的名字也是ALC3202 Analog看来这个codec的能力很强。详细的请参考笔者的《Linux ALSA音频系统:platform,machine,codec》一文。

  录音命令,一定要加上-f,-r,-c等参数,不然录出来的音频文件不一定是你想要的:

ccion@ubuntu:~$ arecord -Dhw:0,0 -f S16_LE -r 48000 -c 2 /tmp/arecord.wav

笔者这里录制了采样率为48khz,位深16bit,2通道数的wav文件。我们用Audition来查看音频文件(笔者这里强力推荐这款软件),以下是我录制的音频文件在audition上面的显示。

红色圈圈部分可以查看你录制的音频文件类型。软件窗口部分绿色就是表示录制的通道数,红色部分为对应的频率谱。

4.alsa控制工具-amixer

命令:

ccion@ubuntu:~$ 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
  -R,--raw-volume Use the raw value (default)
  -M,--mapped-volume Use the mapped volume

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 control

用法:

Usage: amixer <options> [command]

几个重要命令介绍:

  • scontrols :  显示所有的简单控件
  • scontents:  显示所有的简单控件的描述
  • sset : 控制简单控件
  • sget:显示简单控件的内容
  • controsl: 显示所有的控件
  • cset:控制控件
  • cget:显示控件

显示你所有能用的控件,笔者的:

ccion@ubuntu:~$ amixer controls
numid=24,iface=CARD,name='Dock Headphone Jack'
numid=20,iface=CARD,name='Dock Mic Jack'
numid=29,iface=CARD,name='HDMI/DP,pcm=3 Jack'
numid=35,iface=CARD,name='HDMI/DP,pcm=7 Jack'
numid=41,iface=CARD,name='HDMI/DP,pcm=8 Jack'
numid=23,iface=CARD,name='Headphone Jack'
numid=21,iface=CARD,name='Internal Mic Phantom Jack'
numid=19,iface=CARD,name='Mic Jack'
numid=22,iface=CARD,name='Speaker Phantom Jack'
numid=18,iface=MIXER,name='Master Playback Switch'
numid=17,iface=MIXER,name='Master Playback Volume'
numid=4,iface=MIXER,name='Headphone Playback Switch'
numid=5,iface=MIXER,name='Headphone Playback Switch',index=1
numid=3,iface=MIXER,name='Headphone Playback Volume'
numid=50,iface=MIXER,name='PCM Playback Volume'
numid=14,iface=MIXER,name='Mic Boost Volume'
numid=8,iface=MIXER,name='Mic Playback Switch'
numid=7,iface=MIXER,name='Mic Playback Volume'
numid=13,iface=MIXER,name='Capture Switch'
numid=12,iface=MIXER,name='Capture Volume'
numid=6,iface=MIXER,name='Loopback Mixing'
numid=30,iface=MIXER,name='IEC958 Playback Con Mask'
numid=36,iface=MIXER,name='IEC958 Playback Con Mask',index=1
numid=42,iface=MIXER,name='IEC958 Playback Con Mask',index=2
numid=31,iface=MIXER,name='IEC958 Playback Pro Mask'
numid=37,iface=MIXER,name='IEC958 Playback Pro Mask',index=1
numid=43,iface=MIXER,name='IEC958 Playback Pro Mask',index=2
numid=32,iface=MIXER,name='IEC958 Playback Default'
numid=38,iface=MIXER,name='IEC958 Playback Default',index=1
numid=44,iface=MIXER,name='IEC958 Playback Default',index=2
numid=33,iface=MIXER,name='IEC958 Playback Switch'
numid=39,iface=MIXER,name='IEC958 Playback Switch',index=1
numid=45,iface=MIXER,name='IEC958 Playback Switch',index=2
numid=11,iface=MIXER,name='Auto-Mute Mode'
numid=26,iface=MIXER,name='Beep Playback Switch'
numid=25,iface=MIXER,name='Beep Playback Volume'
numid=15,iface=MIXER,name='Dock Mic Boost Volume'
numid=10,iface=MIXER,name='Dock Mic Playback Switch'
numid=9,iface=MIXER,name='Dock Mic Playback Volume'
numid=16,iface=MIXER,name='Internal Mic Boost Volume'
numid=2,iface=MIXER,name='Speaker Playback Switch'
numid=1,iface=MIXER,name='Speaker Playback Volume'
numid=28,iface=PCM,name='Capture Channel Map'
numid=27,iface=PCM,name='Playback Channel Map'
numid=34,iface=PCM,name='ELD',device=3
numid=47,iface=PCM,name='Playback Channel Map',device=3
numid=40,iface=PCM,name='ELD',device=7
numid=48,iface=PCM,name='Playback Channel Map',device=7
numid=46,iface=PCM,name='ELD',device=8
numid=49,iface=PCM,name='Playback Channel Map',device=8

amixer有那些应用,比如能控制codec的音量,cpu的内部录音回环等等。

命令:

ccion@ubuntu:~$ amixer cget numid=17 
numid=17,iface=MIXER,name='Master Playback Volume'
  ; type=INTEGER,access=rw---R--,values=1,min=0,max=87,step=0
  : values=25
  | dBscale-min=-65.25dB,step=0.75dB,mute=0

这个命令得到当前系统的放音音量,可以看到音量的范围为0~87,当前的数值为25.数值越大,音量越大。

ccion@ubuntu:~$ amixer cset numid=17 50
numid=17,iface=MIXER,name='Master Playback Volume'
  ; type=INTEGER,access=rw---R--,values=1,min=0,max=87,step=0
  : values=50
  | dBscale-min=-65.25dB,step=0.75dB,mute=0

  这个命令是设置系统的放音音量,可以看到,我把音量设置到了50,你可以用aplay播放一个音频文件,然后通过amixer来控制音量试试。相应的录音也是如此,就不重复讲解。

  在这节我为你介绍了aplay,arecord.amixer三个工具的用法,如果你想自己写,你可以参考alsa-utils-1.1.5软件中的amixer,aplay中的amixer.c,aplay.c里面有详细的代码供你参考,笔者这里就不介绍。在下篇应用篇中,我将会为你介绍相关api和笔者项目实际的一个运用实例。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章