轉自:http://blog.csdn.net/sepnic/article/details/6324901
前些日子寫了一篇snd_kcontrol探究,該文主要從內核源碼出發簡單講述一下kcontrol接口的始末。這幾天因爲要在Android裏面添加一些音頻控制接口,配合alsa_amixer scontents分析,對此有了更深的體會,記錄於此。因爲這方面的資料實在太少,很多東西都是自我理解的,如有錯誤請見諒並指出。
name字段是名稱標識,這個字段非常重要,因爲kcontrol的作用由名稱來區分,對於名稱相同的kcontrol,則使用index區分。name定義的標準是“SOURCE DIRECTION FUNCTION”即“源 方向 功能”,SOURCE定義了kcontrol的源,如“Master”、“PCM”等;DIRECTION 則爲“Playback”、“Capture”等,如果DIRECTION忽略,意味着Playback和capture雙向;FUNCTION則可以是“Switch”、“Volume”和“Route”等。
以上的說法比較簡略,下面會較詳細補充一下。
先內核源碼中的文檔ControlNames.txt:
This document describes standard names of mixer controls.
Syntax: SOURCE [DIRECTION] FUNCTION
DIRECTION:
<nothing> (both directions)
Playback
Capture
Bypass Playback
Bypass Capture
FUNCTION:
Switch (on/off switch)
Volume
Route (route control, hardware specific)
SOURCE:
Master
Master Mono
Hardware Master
Headphone
PC Speaker
Phone
Phone Input
Phone Output
Synth
FM
Mic
Line
CD
Video
Zoom Video
Aux
PCM
PCM Front
PCM Rear
PCM Pan
Loopback
Analog Loopback (D/A -> A/D loopback)
Digital Loopback (playback -> capture loopback - without analog path)
Mono
Mono Output
Multi
ADC
Wave
Music
I2S
IEC958
Exceptions:
[Digital] Capture Source
[Digital] Capture Switch (aka input gain switch)
[Digital] Capture Volume (aka input gain volume)
[Digital] Playback Switch (aka output gain switch)
[Digital] Playback Volume (aka output gain volume)
Tone Control - Switch
Tone Control - Bass
Tone Control - Treble
3D Control - Switch
3D Control - Center
3D Control - Depth
3D Control - Wide
3D Control - Space
3D Control - Level
Mic Boost [(?dB)]
PCM interface:
Sample Clock Source { "Word", "Internal", "AutoSync" }
Clock Sync Status { "Lock", "Sync", "No Lock" }
External Rate /* external capture rate */
Capture Rate /* capture rate taken from external source */
IEC958 (S/PDIF) interface:
IEC958 [...] [Playback|Capture] Switch /* turn on/off the IEC958 interface */
IEC958 [...] [Playback|Capture] Volume /* digital volume control */
IEC958 [...] [Playback|Capture] Default /* default or global value - read/write */
IEC958 [...] [Playback|Capture] Mask /* consumer and professional mask */
IEC958 [...] [Playback|Capture] Con Mask /* consumer mask */
IEC958 [...] [Playback|Capture] Pro Mask /* professional mask */
IEC958 [...] [Playback|Capture] PCM Stream /* the settings assigned to a PCM stream */
IEC958 Q-subcode [Playback|Capture] Default /* Q-subcode bits */
IEC958 Preamble [Playback|Capture] Default /* burst preamble words (4*16bits) */
可以看到表示方向DIRECTION有五種,其中DIRECTION爲空時則表示Playback、Capture雙向。
一個標準的kcontrol如:SOC_DOUBLE("PCM Playback Volume", AC97_PHONE, 8, 0, 31, 1),其中“PCM”爲源SOURCE、“Playback”爲方向DIRECTION、“Volume”爲功能FUNCTION--音量調節,這個kcontrol是調節放音PCM的Volume的。
又如:SOC_SINGLE("Mic Switch", AC97_CD, 15, 1, 1),其中“Mic”爲源SOURCE、方向DIRECTION爲空表示Playback和Capture雙向、“Switch”爲功能FUNCTION--開關切換,這個kcontrol是用於切換Mic開關的。
實際應用中,並不是所有的kcontrol命名都符合ControlNames.txt規則。如:SOC_SINGLE("ALC Hold Time", AC97_CODEC_CLASS_REV, 8, 15, 0),這個kcontrol的name只有名爲“ALC Hold Time”的SOURCE字段,DIRECTION和FUNCTION都欠缺。
alsa_amixer命令可以輸出這些kcontrol的名稱、numid和當前值等信息。而選項sconctols/controls、scontents/contents、sset/cset、sget/cget有所不同,前者是mixer simple control信息,後者是control信息;前者的handle通過snd_mixer_open取得,後者的handle通過snd_ctl_open取得。
兩個kcontrol:1、SOC_SINGLE("Capture Switch", AC97_CD, 15, 1, 1), 2、SOC_DOUBLE("Capture Volume", AC97_CD, 8, 0, 31, 0):
在alsa_amixer scontrols來看,顯示一個名稱爲“Capture”的mixer simple control,根據我的結果,一般這個mixer simple control對應的是後者。【猜測:之所以有這樣的結果,是因爲兩個kcontrol的SOURCE名稱相同,而mixer simple control是匹配kcontrol的SOURCE字段的;因此SOURCE字段區別體現在mixer simple control上,而name區別體現在control上,待驗證。】
在alsa_amixer controls來看,列表會顯示所有的kcontrol,因此會顯示“Capture Switch”和“Capture Volume”這兩個kcontrol。這不難理解,在snd_kcontrol探究中就提到,上層是根據name來找到底層的kcontrol的,這個name包含了SOURCE、DIRECTION和FUNCTION這三個字段。
【建議:編寫snd_kcontrol時,儘量使用標準的ControlName,留意SOURCE字段是否有重複。在上層應用中,很多都是使用snd_mixer_open得到的mixer simple control,一個典型的例子就是Android2.2的AlsaMixer.cpp。之前我在爲Android添加mic mute接口時,發現無論如何都找不到“Capture Switch”的kcontrol,因爲它與“Capture Volume”的kcontrol的SOURCE字段重名了。將其改名爲“Mic Switch”就行了。】
4/16 2011
找到一個alsa基本架構圖