其实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
[c-sharp] view plain copy
- 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.
我们可以定义一个名为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:
[c-sharp] view plain copy
- 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实现的。