DAPM之三:audio paths与asound.conf

来源:互联网 发布:程序员的数学pdf下载 编辑:程序博客网 时间:2024/05/07 21:11

  其实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 plaincopy
  1. 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).  
  2.  
  3. # Hook arguments definition  
  4. hook_args.NAME {  
  5.         ...                     # Arbitrary arguments  
  6. }  
  7.  
  8. # PCM hook type  
  9. pcm_hook_type.NAME {  
  10.         [lib STR]               # Library file (default libasound.so)  
  11.         [install STR]           # Install function (default _snd_pcm_hook_NAME_install)  
  12. }  
  13.  
  14. # PCM hook definition  
  15. pcm_hook.NAME {  
  16.         type STR                # PCM Hook type (see pcm_hook_type)  
  17.         [args STR]              # Arguments for install function (see hook_args)  
  18.         # or  
  19.         [args { }]              # Arguments for install function  
  20. }  
  21.  
  22. # PCM hook plugin  
  23. pcm.NAME {  
  24.         type hooks              # PCM with hooks  
  25.         slave STR               # Slave name  
  26.         # or  
  27.         slave {                 # Slave definition  
  28.                 pcm STR         # Slave PCM name  
  29.                 # or  
  30.                 pcm { }         # Slave PCM definition  
  31.         }  
  32.         hooks {  
  33.                 ID STR          # Hook name (see pcm_hook)  
  34.                 # or  
  35.                 ID { }          # Hook definition (see pcm_hook)  
  36.         }  
  37. }  
  38.   
  39. Example:  
  40.   
  41.   
  42.         hooks.0 {  
  43.                 type ctl_elems  
  44.                 hook_args [  
  45.                         {  
  46.                                 name "Wave Surround Playback Volume"  
  47.                                 preserve true  
  48.                                 lock true  
  49.                                 optional true  
  50.                                 value [ 0 0 ]  
  51.                         }  
  52.                         {  
  53.                                 name "EMU10K1 PCM Send Volume"  
  54.                                 index { @func private_pcm_subdevice }  
  55.                                 lock true  
  56.                                 value [ 0 0 0 0 0 0 255 0 0 0 0 255 ]  
  57.                         }  
  58.                 ]  
  59.         }  
  60.   
  61. 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 plaincopy
  1. pcm.AndroidPlayback_Speaker_normal {  
  2.     type hooks   
  3.     slave.pcm {   
  4.         type hw   
  5.         card 0   
  6.         device 0   
  7.     }   
  8.     hooks.0 {   
  9.         type ctl_elems   
  10.         hook_args [  
  11.             {   
  12.                 name 'Left Input PGA Switch'   
  13.                 value true   
  14.             }  
  15.             {   
  16.                 name 'Left Input PGA LINPUT1 Switch'   
  17.                 preserve true  
  18.                 lock true  
  19.                 value true   
  20.             }  
  21.             {   
  22.                 name 'Left Input Mixer Input PGA Switch'  
  23.                 preserve true  
  24.                 lock true   
  25.                 value true   
  26.             }  
  27.             {   
  28.                 name 'Left Output Mixer Left Input Mixer Switch'  
  29.                 preserve true  
  30.                 lock true   
  31.                 value true   
  32.             }  
  33.             {   
  34.                 name 'LINEOUT1 Switch'   
  35.                 value true   
  36.             }  
  37.         ]   
  38.     }  
  39. }  

把这个asound.conf放到/etc目录下,再启动Android可以做这个测试,应该可以听到Linein输入的录音信号直接在SPK上输出。

Android的三种模式:normal、ringtone和incall,这些模式的音频通道切换也是可以通过这样配置asound.conf实现的。

0 0