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和筆者項目實際的一個運用實例。

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