DAPM之三:audio paths與asound.conf


轉自:http://blog.csdn.net/sepnic/article/details/6384249

其實asound.conf真跟dapm沒多大關係,之所以把它也納入dapm系列之一,是爲了考慮到知識的連貫性。在<DAPM之二:AUDIO PATHS與dapm kcontrol>提到:通過配置好asound.conf,上層則可打開asound.conf中定義的虛擬設備,而自動選擇相應的音頻通道。這是asound.conf很重要的一個作用,從這方面來說,並不是跟dapm完全沒關係。

 

一、認識asound.conf

 

做alsa的基本都能體會到alsa-lib的複雜與強大,而alsa-lib的強大正是從asound.conf與.asoundrc等配置文件體現出來。alsa驅動開發只是一個方面,而真正想隨心所欲的配置音頻設備,asound.conf與.asoundrc的掌握是必不可少的。所幸,這方面的資料還是比較豐富,所需瞭解的知識點基本都能從官網上找到文檔甚至example。

http://www.alsa-project.org/alsa-doc/alsa-lib/pcm_plugins.html

http://alsa.opensrc.org/.asoundrc

 

二、配置audio path

 

首先我們先看看plugin中hooks:http://www.alsa-project.org/alsa-doc/alsa-lib/pcm_plugins.html#pcm_plugins_hooks

This plugin is used to call some 'hook' function when this plugin is opened, modified or closed. Typically, it is used to change control values for a certain state specially for the PCM (see the example below).

# Hook arguments definition
hook_args.NAME {
        ...                     # Arbitrary arguments
}

# PCM hook type
pcm_hook_type.NAME {
        [lib STR]               # Library file (default libasound.so)
        [install STR]           # Install function (default _snd_pcm_hook_NAME_install)
}

# PCM hook definition
pcm_hook.NAME {
        type STR                # PCM Hook type (see pcm_hook_type)
        [args STR]              # Arguments for install function (see hook_args)
        # or
        [args { }]              # Arguments for install function
}

# PCM hook plugin
pcm.NAME {
        type hooks              # PCM with hooks
        slave STR               # Slave name
        # or
        slave {                 # Slave definition
                pcm STR         # Slave PCM name
                # or
                pcm { }         # Slave PCM definition
        }
        hooks {
                ID STR          # Hook name (see pcm_hook)
                # or
                ID { }          # Hook definition (see pcm_hook)
        }
}

Example:


        hooks.0 {
                type ctl_elems
                hook_args [
                        {
                                name "Wave Surround Playback Volume"
                                preserve true
                                lock true
                                optional true
                                value [ 0 0 ]
                        }
                        {
                                name "EMU10K1 PCM Send Volume"
                                index { @func private_pcm_subdevice }
                                lock true
                                value [ 0 0 0 0 0 0 255 0 0 0 0 255 ]
                        }
                ]
        }

Here, the controls "Wave Surround Playback Volume" and "EMU10K1 PCM Send Volume" are set to the given values when this pcm is accessed. Since these controls take multi-dimensional values, the value field is written as an array. When preserve is true, the old values are saved and restored when the pcm is closed. The lock means that the control is locked during this pcm is opened, and cannot be changed by others. When optional is set, no error is returned but ignored even if the specified control doesn't exist.

Here, the controls "Wave Surround Playback Volume" and "EMU10K1 PCM Send Volume" are set to the given values when this pcm is accessed. Since these controls take multi-dimensional values, the value field is written as an array. When preserve is true, the old values are saved and restored when the pcm is closed. The lock means that the control is locked during this pcm is opened, and cannot be changed by others. When optional is set, no error is returned but ignored even if the specified control doesn't exist.


我們可以定義一個名爲NAME的hook plugin,在這個plugin中,我們可以操作之前提到的dapm kcontrol,達到音頻通道切換的目的。另外注意:

When preserve is true, the old values are saved and restored when the pcm is closed.  當preserve設置爲true時,則該pcm關閉時,kcontrol會恢復到之前的值.
The lock means that the control is locked during this pcm is opened, and cannot be changed by others.  當lock設置爲true時,則在該pcm打開期間,kcontrol的值不會被其他的pcm改變.
When optional is set, no error is returned but ignored even if the specified control doesn't exist. 當optional設置爲true時,則指定的kcontrol不存在時不會返回錯誤.

 

以<DAPM之二:AUDIO PATHS與dapm kcontrol>的紅色線路爲例,在Android平臺上寫一個linein錄音直送到SPK輸出的hooks plugin:

pcm.AndroidPlayback_Speaker_normal {
	type hooks 
	slave.pcm { 
		type hw 
		card 0 
		device 0 
	} 
	hooks.0 { 
		type ctl_elems 
		hook_args [
			{ 
				name 'Left Input PGA Switch' 
				value true 
			}
			{ 
				name 'Left Input PGA LINPUT1 Switch' 
				preserve true
				lock true
				value true 
			}
			{ 
				name 'Left Input Mixer Input PGA Switch'
				preserve true
				lock true 
				value true 
			}
			{ 
				name 'Left Output Mixer Left Input Mixer Switch'
				preserve true
				lock true 
				value true 
			}
			{ 
				name 'LINEOUT1 Switch' 
				value true 
			}
		] 
	}
}

把這個asound.conf放到/etc目錄下,再啓動Android可以做這個測試,應該可以聽到Linein輸入的錄音信號直接在SPK上輸出。

Android的三種模式:normal、ringtone和incall,這些模式的音頻通道切換也是可以通過這樣配置asound.conf實現的。



發佈了12 篇原創文章 · 獲贊 11 · 訪問量 28萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章