alsamixer 在音頻子系統的使用

alsamixer 在音頻子系統的使用

alsamixer是Linux 音頻架構ALSA工具的其中一個,用於配置音頻的各個參數。

alsamixer是基於文本下的圖形界面的,可以通過鍵盤的上下鍵,左右鍵等,很方便地設置需要的音量,開關某個switch(開關)等等操作,下圖是 在Ubuntu12.04系統上圖形配置界面,操作非常的簡單,這裏使用的是Ubuntu12.04的Line-in功能(實現立體聲功能):

聲音播放輸出通道設置:

聲音輸入設備通道設置:

筆者測試Ubuntu12.04的Line-in功能,所以選擇音頻輸入通道爲 Line。

amixer,是alsamixer的文本模式,即命令行模式,需要用amixer命令的形式去配置你的聲卡的各個選項,可以這麼說,你 也許會直接修改Linux內核音頻驅動源碼來滿足您的需求,比如選擇音頻輸入通道是Mic輸入,還是Line 輸入,需要修改WM9714的寄存器來決定,而amixer可以從應用層來修改音頻芯片的寄存器值,決定採用Mic輸入或者Line輸入。這樣就大大簡化 了代碼修改的難度,畢竟比直接修改Linux Kernel ALSA會簡單些。

對於amixer的使用,你首先需要搞懂你要設置的參數是哪些,然後纔可能去了解,如何去配置對應的值,整體來說,相對alsamixer來說,是有點繁瑣,下面簡要介紹其具體用法:

1.先看看amixer支持哪些命令,大概有哪些功能

amixer --help
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 control

2.再看看當前你的音頻系統(不同的音頻驅動對應不同的內容和操作接口)提供了那些供你使用的接口去操作

關於驅動裏面已經提供了多少接口可以去操作,可以用命令:

amixer contents

查看,比如:

[root@FORLINX6410]# ./amixer  controls
numid=49,iface=MIXER,name='Headphone Mixer Aux Playback Volume'
numid=43,iface=MIXER,name='Headphone Mixer Beep Playback Volume'
numid=32,iface=MIXER,name='Headphone Playback ZC Switch'
numid=4,iface=MIXER,name='Headphone Playback Switch'
numid=3,iface=MIXER,name='Headphone Playback Volume'
numid=6,iface=MIXER,name='PCM Playback Volume'
numid=5,iface=MIXER,name='Line In Volume'

××××××××××××××××××××××××××××××××××××××××××××××××

而對於所有的配置的值,可以通過 amixer contents打印:

[root@FORLINX6410]# ./amixer  contents
numid=49,iface=MIXER,name='Headphone Mixer Aux Playback Volume'
  ; type=INTEGER,access=rw---R--,values=1,min=0,max=7,step=0
  : values=5
  | dBscale-min=-15.00dB,step=3.00dB,mute=0
numid=43,iface=MIXER,name='Headphone Mixer Beep Playback Volume'
  ; type=INTEGER,access=rw---R--,values=1,min=0,max=7,step=0
  : values=5
  | dBscale-min=-15.00dB,step=3.00dB,mute=0
numid=32,iface=MIXER,name='Headphone Playback ZC Switch'
  ; type=BOOLEAN,access=rw------,values=2
  : values=off,off
。。。

3.搞懂如何去設置某個參數

總結起來就是,先要用get系列命令去看懂有哪些接口,然後再去用set系列的命令,去設置對應你所要設置的值。

想要針對某項設置,比如想要設置上面的Line-in輸入的音量,‘Line In Volume',即controls中顯示的:

numid=5,iface=MIXER,name='Line In Volume'
那麼,可以先看看當前的值:

[root@FORLINX6410]# ./amixer cget  numid=5,iface=MIXER,name='Line In Volume'
numid=5,iface=MIXER,name='Line In Volume'
  ; type=INTEGER,access=rw---R--,values=2,min=0,max=31,step=0
  : values=23,23
  | dBscale-min=-34.50dB,step=1.50dB,mute=0
顯示最大音量爲31,假設想要設置爲25,那麼就用cset去設置:

amixer cset  numid=5,iface=MIXER,name='Line In Volume'  25
[root@FORLINX6410]# ../amixer cset  numid=5,iface=MIXER,name='Line In Volume'  25
numid=5,iface=MIXER,name='Line In Volume'
  ; type=INTEGER,access=rw---R--,values=2,min=0,max=31,step=0
  : values=25,25
  | dBscale-min=-34.50dB,step=1.50dB,mute=0

[提示]:

同上面介紹的的cget/cset系列命令:

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

類似的,還有另外一套sget/sset系列的命令,實現簡單的參數設置,一般情況下使用 scontrols ,scontents即可滿足您的需求。

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

也是同樣做法,比如:

[root@FORLINX6410]# ./amixer  scontrols
Simple mixer control 'Headphone',0
Simple mixer control 'Headphone Mixer Aux',0
Simple mixer control 'Headphone Mixer Beep',0
Simple mixer control 'Headphone Playback ZC',0
Simple mixer control 'Tone',0
Simple mixer control 'Tone Cut-off',0
Simple mixer control 'Bass',0
Simple mixer control 'Bass Control',0
Simple mixer control 'Bass Cut-off',0
Simple mixer control 'PCM',0
Simple mixer control 'Sidetone Mux',0
Simple mixer control 'Line In',0-----------(這裏是設置Line-in的音量的參數,同以上設置Line-in音量功能相同)
Simple mixer control 'Mic 1',0
Simple mixer control 'Mic 1 Preamp',0
Simple mixer control 'Mic 2',0
Simple mixer control 'Mic 2 Preamp',0
Simple mixer control 'Mic A Source',0
Simple mixer control 'Mic B Source',0

。。。

Simple mixer control 'Left Capture Source',0 (這項很重要,左聲道音頻源輸入選擇)

Simple mixer control 'Right Capture Source',0 (右聲道音頻源輸入選擇)
。。。

同理,amixer scontents,可以查看當前所有的值,具體就不在這列舉了。

另外,去查看或者配置用sget,比如:

[root@FORLINX6410]# ./amixer  sget  'Left Capture Source',0
Simple mixer control 'Left Capture Source',0
  Capabilities: enum
  Items: 'Mic 1' 'Mic 2' 'Line' 'Mono In' 'Headphone' 'Speaker' 'Mono Out' 'Zh'
  Item0: 'Mic 1'
如果想要修改對應設置,用amixer sset ,具體用法是:

amixer sset  sID(控制字符串)  P(支持的某個值)

其中sID,就是上面的Simple mixer control後面的那個字符串,比如 'Left Capture Source' 而對其設置就是,

[root@FORLINX6410]# ./amixer  sset 'Left Capture Source',0 Line
Simple mixer control 'Left Capture Source',0
  Capabilities: enum
  Items: 'Mic 1' 'Mic 2' 'Line' 'Mono In' 'Headphone' 'Speaker' 'Mono Out' 'Zh'
  Item0: 'Line'

然後再把右聲道輸入源設置爲Line-in:

[root@FORLINX6410]# ./amixer  sset 'Right Capture Source',0 Line
Simple mixer control 'Right  Capture Source',0
  Capabilities: enum
  Items: 'Mic 1' 'Mic 2' 'Line' 'Mono In' 'Headphone' 'Speaker' 'Mono Out' 'Zh'
  Item0: 'Line'

這樣您的設備就可以使用Line-in功能錄音了。

執行命令:

[root@FORLINX6410]#./arecord -f cd -c 1 -t wav my-file.wav
Recording WAVE 'my-file.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Mono

開始Line-in錄音 ,-c 1代表使用單聲道,2 代表使用立體聲.

播放剛纔錄製的聲音文件:

.[root@FORLINX6410]#./aplay my-file.wav
Playing WAVE 'my-file.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Mono

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