alsa-lib如何解析asound.conf

alsa-lib之snd_pcm_open 目录 版本获取 前言 snd_pcm_open时序图 代码流程 版本获取 使用版本 alsa 1.2.5.1 release | Changes v1.2.5 v1.2.5.1 https://www.alsa-project.org/wiki/Main_Page https://github.com/alsa-project 前言 我们都知道alsa-lib包含开发人员编译 ALSA 应用程序的用户空间库, 提供接口给应用调用的。前面我们了解过ALSA框架 ,本节我们主要介绍一下alsa-lib中sn 阅读详情

http://blog.csdn.net/haohenghai/article/details/24255451

无论是在录音还是在放音,都要打开一个PCM流,具体对应的函数原型为:

[cpp]  view plain copy
  1. int snd_pcm_open(snd_pcm_t **pcmp, const char *name,  snd_pcm_stream_t stream, int mode);  

 本文以录音为例介绍一下它的流程。录音时传入name==AndroidCapture
 先解一下各个参数的函义:

  • pcmp: 是带回的pcm流
  • name是要找开的流即放音还是录音,放音对应AndroidPlayback_Speaker_normal,录音对应AndroidCapture。放音的字符串会因为当前使用设备和设备状况不一样而有所不同,不同之处在下划线后面那部分。录音则因为大多数板子成型后,都只有一个录音设备或者接口,因此录音串对应的就是AndroidCapture
  • stream对应流的类型,是放音(0)还是录音(1).
  • mode是指设备打开模式,阻塞或非阻塞。

2. 打开流程(snd_pcm_open_conf)

       首先会根据传入的name,到配置树中查找对应的结点。 snd_config_search_definition(root, "pcm", name, &pcm_conf); 此函数将在配置树中查找对应的结点,然后将查找到的结点复制一份到
pcm_conf中。 之后,系统会调用snd_pcm_open_conf(pcmp, name, root, pcm_conf, stream, mode);进一步分析,pcm_conf即为刚才根据传入name到配置树查找到的结点。

 

[cpp]  view plain copy
  1. static int snd_pcm_open_conf(snd_pcm_t **pcmp, const char *name,  
  2.                  snd_config_t *pcm_root, snd_config_t *pcm_conf,  
  3.                  snd_pcm_stream_t stream, int mode)  
  4. {  
  5.     const char *str;  
  6.     char *buf = NULL, *buf1 = NULL;  
  7.     int err;  
  8.     snd_config_t *conf, *type_conf = NULL;  
  9.     snd_config_iterator_t i, next;  
  10.     const char *id;  
  11.     const char *lib = NULL, *open_name = NULL;  
  12.     int (*open_func)(snd_pcm_t **, const char *,   
  13.              snd_config_t *, snd_config_t *,   
  14.              snd_pcm_stream_t, int) = NULL;  
  15. #ifndef PIC  
  16.     extern void *snd_pcm_open_symbols(void);  
  17. #endif  
  18.     void *h = NULL;  
  19.     if (snd_config_get_type(pcm_conf) != SND_CONFIG_TYPE_COMPOUND) {  
  20.         char *val;  
  21.         id = NULL;  
  22.         snd_config_get_id(pcm_conf, &id);  
  23.         val = NULL;  
  24.         snd_config_get_ascii(pcm_conf, &val);  
  25.         SNDERR("Invalid type for PCM %s%sdefinition (id: %s, value: %s)", name ? name : "", name ? " " : "", id, val);  
  26.         free(val);  
  27.         return -EINVAL;  
  28.     }  
  29.     err = snd_config_search(pcm_conf, "type", &conf);  
  30.     if (err < 0) {  
  31.         SNDERR("type is not defined");  
  32.         return err;  
  33.     }  
  34.     err = snd_config_get_id(conf, &id);  
  35.     if (err < 0) {  
  36.         SNDERR("unable to get id");  
  37.         return err;  
  38.     }  
  39.     err = snd_config_get_string(conf, &str);  
  40.     if (err < 0) {  
  41.         SNDERR("Invalid type for %s", id);  
  42.         return err;  
  43.     }  
  44.     err = snd_config_search_definition(pcm_root, "pcm_type", str, &type_conf);  
  45.     if (err >= 0) {  
  46.         if (snd_config_get_type(type_conf) != SND_CONFIG_TYPE_COMPOUND) {  
  47.             SNDERR("Invalid type for PCM type %s definition", str);  
  48.             goto _err;  
  49.         }  
  50.         snd_config_for_each(i, next, type_conf) {  
  51.             snd_config_t *n = snd_config_iterator_entry(i);  
  52.             const char *id;  
  53.             if (snd_config_get_id(n, &id) < 0)  
  54.                 continue;  
  55.             if (strcmp(id, "comment") == 0)  
  56.                 continue;  
  57.             if (strcmp(id, "lib") == 0) {  
  58.                 err = snd_config_get_string(n, &lib);  
  59.                 if (err < 0) {  
  60.                     SNDERR("Invalid type for %s", id);  
  61.                     goto _err;  
  62.                 }  
  63.                 continue;  
  64.             }  
  65.             if (strcmp(id, "open") == 0) {  
  66.                 err = snd_config_get_string(n, &open_name);  
  67.                 if (err < 0) {  
  68.                     SNDERR("Invalid type for %s", id);  
  69.                     goto _err;  
  70.                 }  
  71.                 continue;  
  72.             }  
  73.             SNDERR("Unknown field %s", id);  
  74.             err = -EINVAL;  
  75.             goto _err;  
  76.         }  
  77.     }  
  78.     if (!open_name) {  
  79.         buf = malloc(strlen(str) + 32);  
  80.         if (buf == NULL) {  
  81.             err = -ENOMEM;  
  82.             goto _err;  
  83.         }  
  84.         open_name = buf;  
  85.         sprintf(buf, "_snd_pcm_%s_open", str); //生成真正的open函数  
  86.     }  
  87.     if (!lib) {  
  88.         const char *const *build_in = build_in_pcms;  
  89.         while (*build_in) {  
  90.             if (!strcmp(*build_in, str))  
  91.                 break;  
  92.             build_in++;  
  93.         }  
  94.         if (*build_in == NULL) {  
  95.             buf1 = malloc(strlen(str) + sizeof(ALSA_PLUGIN_DIR) + 32);  
  96.             if (buf1 == NULL) {  
  97.                 err = -ENOMEM;  
  98.                 goto _err;  
  99.             }  
  100.             lib = buf1;  
  101.             sprintf(buf1, "%s/libasound_module_pcm_%s.so", ALSA_PLUGIN_DIR, str);  
  102.         }  
  103.     }  
  104. #ifndef PIC  
  105.     snd_pcm_open_symbols(); /* this call is for static linking only */  
  106. #endif  
  107.     open_func = snd_dlobj_cache_lookup(open_name);  
  108.     if (open_func) {  
  109.         err = 0;  
  110.         goto _err;  
  111.     }  
  112.     h = snd_dlopen(lib, RTLD_NOW);  
  113.     if (h)  
  114.         open_func = snd_dlsym(h, open_name, SND_DLSYM_VERSION(SND_PCM_DLSYM_VERSION));  
  115.     err = 0;  
  116.     if (!h) {  
  117.         SNDERR("Cannot open shared library %s",  
  118.                lib ? lib : "[builtin]");  
  119.         err = -ENOENT;  
  120.     } else if (!open_func) {  
  121.         SNDERR("symbol %s is not defined inside %s", open_name,  
  122.                lib ? lib : "[builtin]");  
  123.         snd_dlclose(h);  
  124.         err = -ENXIO;  
  125.     }  
  126.        _err:  
  127.     if (err >= 0) {  
  128.         err = open_func(pcmp, name, pcm_root, pcm_conf, stream, mode);  
  129.         if (err >= 0) {  
  130.             if (h /*&& (mode & SND_PCM_KEEP_ALIVE)*/) {  
  131.                 snd_dlobj_cache_add(open_name, h, open_func);  
  132.                 h = NULL;  
  133.             }  
  134.             (*pcmp)->dl_handle = h;  
  135.             err = 0;  
  136.         } else {  
  137.             if (h)  
  138.                 snd_dlclose(h);  
  139.         }  
  140.     }  
  141.     if (type_conf)  
  142.         snd_config_delete(type_conf);  
  143.     free(buf);  
  144.     free(buf1);  
  145.     return err;  
  146. }  
  • snd_pcm_open_conf函数的主要功能:

        分析传递过来的配置树pcm_conf的type子结点对应的字符串:hooks,然后调用由type值(hooks)构成的字符串函数(_snd_pcm_hooks_open)的过程。在我们系统中后面还要调用此函数,而且在查找lib名时,都能在build_in_pcms中找到,所以对我们来说这个函数的主要功能就是查找pcm_conf子结点type对应的字符串<如plug>构成的函数。


       snd_pcm_open_conf会根据配置树的情况,获取子节点中名为type的结点(它是一个叶子结点),并获取它的值hooks,使用str变量存储。然后会在root对应配置树中查找名为pcm_type hooks的结点。查找成功后,会在查找到的结点中搜索所索要的lib,open_name字符串。(后面要打开)。 在本系统中没有找到pcm_type hooks的结点。那么lib和open_name均为空,下面就要对lib/open_name初始化,(如果找到pcm_type empyt结点则会用该结点的孩子结点初始化)。首先对lib初始化,首先查找str是否包含在:

[cpp]  view plain copy
  1. static const char *const build_in_pcms[] = {  
  2.     "adpcm", "alaw", "copy", "dmix", "file", "hooks", "hw", "ladspa", "lfloat",  
  3.     "linear", "meter", "mulaw", "multi", "null", "empty", "plug", "rate", "route", "share",  
  4.     "shm", "dsnoop", "dshare", "asym", "iec958", "softvol", "mmap_emul",  
  5.     NULL  
  6. };  

     这个数组中,如果在,就将lib设为null.如果不在,设置lib为默认库
 sprintf(lib, "%s/libasound_module_pcm_%s.so", ALSA_PLUGIN_DIR, str); 即/usr/lib/alsa-lib/libasound_module_pcm_hooks.so;如果查找发现hooks在数组中,所以lib为null。下面对open_name初始化sprintf(open_name, "_snd_pcm_%s_open", open_name);即_snd_pcm_hooks_open函数。


为了搞清楚配置选项在代码中的表现形式,可用如下码查看,此函数在conf.c中定义:

[cpp]  view plain copy
  1. int  show_snd_config(snd_config_t *config)  
  2. {   
  3.     assert(config);  
  4.     switch (config->type) {  
  5.     case SND_CONFIG_TYPE_COMPOUND:  
  6.         {  
  7.             int err;  
  8.             struct list_head *i;  
  9.             i = config->u.compound.fields.next;  
  10.             SNDERR("Start Compound---> node id =%s\n",config->id?config->id:"null");  
  11.               
  12.             while (i != &config->u.compound.fields) {  
  13.                 struct list_head *nexti = i->next;  
  14.                 snd_config_t *leaf = snd_config_iterator_entry(i);  
  15.                   
  16.                 err = show_snd_config(leaf);  
  17.                 if (err < 0)  
  18.                     return err;  
  19.                   
  20.                 i = nexti;  
  21.             }  
  22.             SNDERR("End Compound---> ---> node id =%s\n",config->id?config->id:"null");  
  23.             break;  
  24.         }  
  25.     case SND_CONFIG_TYPE_STRING:  
  26.         SNDERR("SND_CONFIG_TYPE_STRING---> leaf id=%s,str=%s\n",config->id?config->id:"null",config->u.string?config->u.string:"null");  
  27.         break;  
  28.       
  29.     case SND_CONFIG_TYPE_INTEGER:  
  30.         SNDERR("SND_CONFIG_TYPE_ INTGER --->leaf id=%s,num=%ld\n",config->id?config->id:"null",config->u.integer);   
  31.         break;  
  32.       
  33.     case SND_CONFIG_TYPE_REAL:  
  34.         SNDERR("SND_CONFIG_TYPE_ REAL--->leaf id=%s,num=%f\n",config->id?config->id:"null",config->u.real);    
  35.         break;  
  36.       
  37.     case   SND_CONFIG_TYPE_INTEGER64:  
  38.         SNDERR("SND_CONFIG_TYPE_INTEGER64--->leaf id=%s,num=%ld\n",config->id?config->id:"null",config->u.integer64);   
  39.         break;  
  40.       
  41.     case SND_CONFIG_TYPE_POINTER:  
  42.         SNDERR("SND_CONFIG_TYPE_POINTER--->leaf id=%s,num=%p\n",config->id?config->id:"null",config->u.ptr);   
  43.         break;  
  44.       
  45.     default:  
  46.         break;  
  47.     }  
  48.     return 0;  
  49. }  

    对于音频播放,其在asound.conf的配置项如下:

[cpp]  view plain copy
  1. pcm.AndroidPlayback_Speaker_normal {  
  2.     type plug  
  3.     slave.pcm {  
  4.         type hw  
  5.         card 0  
  6.         device 0  
  7.     }  
  8. }  

对于此节点,使用show_snd_config结果如下:

[cpp]  view plain copy
  1. pcm/pcm.c:2280:(snd_pcm_open) ***2280:start:name:AndroidPlayback_Speaker_normal  
  2. pcm/pcm.c:2235:(snd_pcm_open_noupdate) ***Begin:2235:name=AndroidPlayback_Speaker_normal, stream type:0  
  3. pcm/pcm.c:2249:(snd_pcm_open_noupdate) 2222222 pcm_conf  
  4. conf.c:2504:(show_snd_config) Start Compound---> node id =AndroidPlayback_Speaker_normal  
  5. conf.c:2520:(show_snd_config) SND_CONFIG_TYPE_STRING---> leaf id=type,str=plug  
  6. conf.c:2504:(show_snd_config) Start Compound---> node id =slave  
  7. conf.c:2504:(show_snd_config) Start Compound---> node id =pcm            
  8. conf.c:2520:(show_snd_config) SND_CONFIG_TYPE_STRING---> leaf id=type,str=hw  
  9. conf.c:2524:(show_snd_config) SND_CONFIG_TYPE_ INTGER --->leaf id=card,num=0  
  10. conf.c:2524:(show_snd_config) SND_CONFIG_TYPE_ INTGER --->leaf id=device,num=0  
  11. conf.c:2516:(show_snd_config) End Compound---> ---> node id =pcm        
  12. conf.c:2516:(show_snd_config) End Compound---> ---> node id =slave       
  13. conf.c:2516:(show_snd_config) End Compound---> ---> node id =AndroidPlayback_Speaker_normal  
  14. pcm/pcm.c:2260:(snd_pcm_open_noupdate) ***2260:name=AndroidPlayback_Speaker_normal,hop=0  
  15. pcm/pcm.c:2079:(snd_pcm_open_conf) ***Begin: 2079                  
  16. pcm/pcm.c:2100:(snd_pcm_open_conf) ***2100:id=type  
  17. pcm/pcm.c:2107:(snd_pcm_open_conf) ***2107:str=plug  
  18. pcm/pcm.c:2152:(snd_pcm_open_conf) ***2152:buf=_snd_pcm_plug_open  
  19. pcm/pcm.c:2156:(snd_pcm_open_conf) ***2156:str=plug                     
  20. pcm/pcm.c:2177:(snd_pcm_open_conf) ***2177:open_name=_snd_pcm_plug_open  
  21. pcm/pcm.c:2199:(snd_pcm_open_conf) ***2199:cal open_func<_snd_pcm_plug_open>  
  22. pcm/pcm_plug.c:1227:(_snd_pcm_plug_open) ****Begin:1227  
  23. pcm/pcm.c:2079:(snd_pcm_open_conf) ***Begin: 2079  
  24. pcm/pcm.c:2100:(snd_pcm_open_conf) ***2100:id=type  
  25. pcm/pcm.c:2107:(snd_pcm_open_conf) ***2107:str=hw  
  26. pcm/pcm.c:2152:(snd_pcm_open_conf) ***2152:buf=_snd_pcm_hw_open  
  27. pcm/pcm.c:2156:(snd_pcm_open_conf) ***2156:str=hw                     
  28. pcm/pcm.c:2177:(snd_pcm_open_conf) ***2177:open_name=_snd_pcm_hw_open  
  29. pcm/pcm.c:2199:(snd_pcm_open_conf) ***2199:call open_func<_snd_pcm_hw_open>  
  30. pcm/pcm_hw.c:1397:(_snd_pcm_hw_open) ***Begin:1397                     
  31. pcm/pcm_hw.c:1272:(snd_pcm_hw_open) ***Begin:1272:err=0                  
  32. pcm/pcm_hw.c:1273:(snd_pcm_hw_open) ***1273:name=(null),card=0,device=0,subdevice=-1  
  33. control/control_hw.c:375:(snd_ctl_hw_open) ***375:card:0  
  34. pcm/pcm_hw.c:1129:(snd1_pcm_hw_open_fd) ***Begin:1129:ret=0              
  35. pcm/pcm_hw.c:1237:(snd1_pcm_hw_open_fd) ***End:1237:ret=0                
  36. pcm/pcm_hw.c:1327:(snd_pcm_hw_open) ***End:1327:ret=0  
  37. pcm/pcm_hw.c:1519:(_snd_pcm_hw_open) ***Begin:1519:err=0  
  38. pcm/pcm.c:2218:(snd_pcm_open_conf) ***END: 2218:err=0  
  39. pcm/pcm_plug.c:1328:(_snd_pcm_plug_open) ****End: 1328: err=0  
  40. pcm/pcm.c:2218:(snd_pcm_open_conf) ***END: 2218:err=0  
  41. pcm/pcm.c:2264:(snd_pcm_open_noupdate) ***End:2264:err=0  

     所以与此节点对应的open函数为_snd_pcm_plug_open,通过函数sprintf(buf, "_snd_pcm_%s_open", str);生成。

 3. _snd_pcm_hooks_open <如_snd_pcm_plug_open>

 

[cpp]  view plain copy
  1. /** 
  2.  * \brief Creates a new Plug PCM 
  3.  * \param pcmp Returns created PCM handle 
  4.  * \param name Name of PCM 
  5.  * \param root Root configuration node 
  6.  * \param conf Configuration node with Plug PCM description 
  7.  * \param stream Stream type 
  8.  * \param mode Stream mode 
  9.  * \retval zero on success otherwise a negative error code 
  10.  * \warning Using of this function might be dangerous in the sense 
  11.  *          of compatibility reasons. The prototype might be freely 
  12.  *          changed in future. 
  13.  */  
  14. int _snd_pcm_plug_open(snd_pcm_t **pcmp, const char *name,  
  15.                snd_config_t *root, snd_config_t *conf,   
  16.                snd_pcm_stream_t stream, int mode)  
  17. {  
  18.     snd_config_iterator_t i, next;  
  19.     int err;  
  20.     snd_pcm_t *spcm;  
  21.     snd_config_t *slave = NULL, *sconf;  
  22.     snd_config_t *tt = NULL;  
  23.     enum snd_pcm_plug_route_policy route_policy = PLUG_ROUTE_POLICY_DEFAULT;  
  24.     snd_pcm_route_ttable_entry_t *ttable = NULL;  
  25.     unsigned int csize, ssize;  
  26.     unsigned int cused, sused;  
  27.     snd_pcm_format_t sformat = SND_PCM_FORMAT_UNKNOWN;  
  28.     int schannels = -1, srate = -1;  
  29.     const snd_config_t *rate_converter = NULL;  
  30.   
  31.     SNDERR("****Begin:%d\n",__LINE__);  
  32.     snd_config_for_each(i, next, conf) {  
  33.         snd_config_t *n = snd_config_iterator_entry(i);  
  34.         const char *id;  
  35.         if (snd_config_get_id(n, &id) < 0)  
  36.             continue;  
  37.         if (snd_pcm_conf_generic_id(id))  
  38.             continue;  
  39.         if (strcmp(id, "slave") == 0) {  
  40.             slave = n;  
  41.             continue;  
  42.         }  
  43. #ifdef BUILD_PCM_PLUGIN_ROUTE  
  44.         if (strcmp(id, "ttable") == 0) {  
  45.             route_policy = PLUG_ROUTE_POLICY_NONE;  
  46.             if (snd_config_get_type(n) != SND_CONFIG_TYPE_COMPOUND) {  
  47.                 SNDERR("Invalid type for %s", id);  
  48.                 return -EINVAL;  
  49.             }  
  50.             tt = n;  
  51.             continue;  
  52.         }  
  53.         if (strcmp(id, "route_policy") == 0) {  
  54.             const char *str;  
  55.             if ((err = snd_config_get_string(n, &str)) < 0) {  
  56.                 SNDERR("Invalid type for %s", id);  
  57.                 return -EINVAL;  
  58.             }  
  59.             if (tt != NULL)  
  60.                 SNDERR("Table is defined, route policy is ignored");  
  61.             if (!strcmp(str, "default"))  
  62.                 route_policy = PLUG_ROUTE_POLICY_DEFAULT;  
  63.             else if (!strcmp(str, "average"))  
  64.                 route_policy = PLUG_ROUTE_POLICY_AVERAGE;  
  65.             else if (!strcmp(str, "copy"))  
  66.                 route_policy = PLUG_ROUTE_POLICY_COPY;  
  67.             else if (!strcmp(str, "duplicate"))  
  68.                 route_policy = PLUG_ROUTE_POLICY_DUP;  
  69.             continue;  
  70.         }  
  71. #endif  
  72. #ifdef BUILD_PCM_PLUGIN_RATE  
  73.         if (strcmp(id, "rate_converter") == 0) {  
  74.             rate_converter = n;  
  75.             continue;  
  76.         }  
  77. #endif  
  78.         SNDERR("Unknown field %s", id);  
  79.         return -EINVAL;  
  80.     }  
  81.     if (!slave) {  
  82.         SNDERR("slave is not defined");  
  83.         return -EINVAL;  
  84.     }  
  85.     err = snd_pcm_slave_conf(root, slave, &sconf, 3,  
  86.                  SND_PCM_HW_PARAM_FORMAT, SCONF_UNCHANGED, &sformat,  
  87.                  SND_PCM_HW_PARAM_CHANNELS, SCONF_UNCHANGED, &schannels,  
  88.                  SND_PCM_HW_PARAM_RATE, SCONF_UNCHANGED, &srate);  
  89.     if (err < 0){  
  90.         SNDERR("****End: %d: err=%d\n",__LINE__,err);  
  91.         return err;  
  92.     }  
  93. #ifdef BUILD_PCM_PLUGIN_ROUTE  
  94.     if (tt) {  
  95.         err = snd_pcm_route_determine_ttable(tt, &csize, &ssize);  
  96.         if (err < 0) {  
  97.             snd_config_delete(sconf);  
  98.             SNDERR("****End: %d: err=%d\n",__LINE__,err);  
  99.             return err;  
  100.         }  
  101.         ttable = malloc(csize * ssize * sizeof(*ttable));  
  102.         if (ttable == NULL) {  
  103.             snd_config_delete(sconf);  
  104.             SNDERR("****End: %d: err=%d\n",__LINE__,err);  
  105.             return err;  
  106.         }  
  107.         err = snd_pcm_route_load_ttable(tt, ttable, csize, ssize, &cused, &sused, -1);  
  108.         if (err < 0) {  
  109.             snd_config_delete(sconf);  
  110.             SNDERR("****End: %d: err=%d\n",__LINE__,err);  
  111.             return err;  
  112.         }  
  113.     }  
  114. #endif  
  115.       
  116. #ifdef BUILD_PCM_PLUGIN_RATE  
  117.     if (! rate_converter)  
  118.         rate_converter = snd_pcm_rate_get_default_converter(root);  
  119. #endif  
  120.   
  121.     err = snd_pcm_open_slave(&spcm, root, sconf, stream, mode, conf);  
  122.     snd_config_delete(sconf);  
  123.     if (err < 0){  
  124.         SNDERR("****End: %d: err=%d\n",__LINE__,err);  
  125.         return err;  
  126.     }  
  127.     err = snd_pcm_plug_open(pcmp, name, sformat, schannels, srate, rate_converter,  
  128.                 route_policy, ttable, ssize, cused, sused, spcm, 1);  
  129.     if (err < 0)  
  130.         snd_pcm_close(spcm);  
  131.       
  132.     SNDERR("****End: %d: err=%d\n",__LINE__,err);  
  133.     return err;  
  134. }  

这个函数主要完成三件事:

    1)在传入的配置树中查找子孩子ID为slave的结点

    2)然后调用snd_pcm_open_slave和snd_pcm_hooks_open

    3)最后再分析配置树是否有hooks结点,有的话调用snd_pcm_hook_add_conf。

    为了方便大家理解,还是先看一下传入_snd_pcm_hooks_open配置树的情况, 先来看一下snd_pcm_open_slave(&spcm, root, sconf, stream, mode, conf);函数,在这里sconf即为slave结点中ID为pcm对应的配置树。root对应的是总的配置树(系统启动时会根据配置文件asound.conf/alsa-lib.conf加载构建)。

      snd_pcm_open_slave-->

         snd_pcm_open_named_slave-->

                 snd_pcm_open_noupdate

      由此可见snd_pcm_open_slave仍然会走到snd_pcm_open_noupdate这个函数处。然后会根据传入的参数name=default,在总的配置树中查找pcm default结点。snd_config_search_definition最终根据pcm default串查找对应的树。之后会调用 snd_pcm_open_conf(pcmp, name, root, pcm_conf, stream, mode)前面已经详细介过本函数了,本次调用只是传递过来的配置树不一样。

       snd_pcm_open_conf(pcmp, name, root, pcm_conf, stream, mode)根据当前传入树pcm_conf的情况会调用如下函数_snd_pcm_empty_open。这个函数会从当前传入树取下slave孩子结点的pcm结点对应的配置树做为根结点,并调用 snd_pcm_open_named_slave.
      snd_pcm_open_named_slave-->

          snd_pcm_open_conf

      可以看出又回到了snd_pcm_open_conf函数,只是传入的树不一样。snd_pcm_open_conf会调用_snd_pcm_plug_open.
     函数_snd_pcm_plug_open会调 用snd_pcm_open_slave及snd_pcm_plug_open。
     在传入树中有这样的子树,调用

      snd_pcm_open_slave-->

          nd_pcm_open_named_slave-->

               snd_pcm_open_conf-->

                  _snd_pcm_hw_open.

4. _snd_pcm_hw_open

     _snd_pcm_hw_open-->snd_pcm_hw_open,此函数中会根据树中的card号码,调用snd_ctl_hw_open打开/dev/snd/ControlC0,然后再打开/dev/snd/pcmC0D0c,完成后调用snd_ctl_close关闭/dev/snd/ControlC0.并调用snd_pcm_hw_open_fd,传入fd为打开的/dev/snd/pcmC0D0c.该函数完成PCM流的创建,首先创建pcm的私有数据hw (snd_pcm_hw_t结构),然后用参数(参数获取是通过ioctl命令发送到fd,然后内核将请求信息带回最后赋值给hw的成员中)初始化它,接着创建pcm(_snd_pcm结构),并将hw赋值给pcm->private_data成员。最后再调用snd_pcm_hw_mmap_status/snd_pcm_hw_mmap_control两个函数完成映射。需要注意在这里pcm有一个类型,本处创建的pcm类型为SND_PCM_TYPE_HW.
 对pcm的初始化代码如下:

[cpp]  view plain copy
  1. //创建类型为SND_PCM_TYPE_HW的pcm流,并完成一些基本初始化。  
  2.  ret = snd_pcm_new(&pcm, SND_PCM_TYPE_HW, name, info.stream, mode);  
  3.  if (ret < 0) {  
  4.   free(hw);  
  5.   close(fd);  
  6.   return ret;  
  7.  }  
  8.  //初始化ops函数集  
  9.  pcm->ops = &snd_pcm_hw_ops;  
  10.  //初始化fast_ops函数集  
  11.  pcm->fast_ops = &snd_pcm_hw_fast_ops;  
  12.  pcm->private_data = hw;//如上所述私有数据赋值  
  13.  pcm->poll_fd = fd;/dev/snd/pcmC0D0c  
  14.  pcm->poll_events = info.stream == SND_PCM_STREAM_PLAYBACK ? POLLOUT : POLLIN;  
  15.  pcm->monotonic = monotonic;  




【ALSA】 asound.conf 插件讲解 Alsa-lib层,为不同的驱动提供统一的接口alsa API,简化了开发人员对于驱动层的调用开发。接口定义地址: https://www.alsa-project.org/alsa-doc/alsa-lib/ 关于asound.conf的配置,可以参考官网解释: https://www.alsa-project.org/main/index.php/Asoundrc 详细的插件讲解... 阅读详情

相关推荐

asoundconf

alsa utilus 新版本中不再包含asoundconf 而好多相关解决方案中都用到了这个资源 使用方法,直接将asoundconf拷贝到/usr/bin 然后修改其权限 使之可运行即可(在Ubuntu13.04测试成功)

alsa 配置文件asound.conf

1.介绍      笔者在博客《Alsa调试下篇:应用篇》中一文中提到过asound.conf这个配置文件,在本文中,笔者将为你详细介绍这个文件。在《Alsa调试上篇:编译篇》中通过交叉编译我们可以得到alsa-lib的一些文件,在usr/local/shar/alsa/中有一个文件叫---alsa.conf alsa官网解释:https://www.alsa-project.org/main...

ccion的博客 1万+

腾讯会议录屏损坏修复:使用 recover_mp4 工具的完整解决方案

腾讯会议录屏损坏修复指南 当腾讯会议录屏因电脑蓝屏等意外导致MP4文件损坏时,可使用recover_mp4工具结合FFMPEG进行修复。操作步骤包括:1)准备相同参数正常视频作为参考;2)分析正常视频生成结构文件;3)分离损坏视频的音视频流;4)使用FFMPEG重新封装。该方法能有效修复因意外中断导致的视频索引损坏问题。修复前需将工具和文件放在同一目录下,并确保参考视频与损坏录屏的参数完全一致。

m0_62498496的博客 2638

rk3566-linux声卡asound.conf配置:两个声卡不同的I2S输出(喇叭和HDMI),同时输出声音的问题

/etc/asound.conf控制两个声卡同时输入声音,并控制其大小输出的问题

lxm1781801320的博客 3024

嵌入式设备低功耗设计误区:为什么你的RK3568总是被‘意外’唤醒?

本文深入分析RK3568嵌入式设备低功耗设计中的常见误区,重点探讨唤醒源管理不当导致的意外唤醒问题。通过解析硬件设计陷阱、软件配置错误及系统化审计方法,帮助开发者有效排查和解决GPIO配置、唤醒源设置等关键因素引起的功耗异常,提升设备续航能力。

php55的博客 463

PC网站实现微信扫码登录功能(二)

上篇我们介绍了怎么申请账号以及最后拿到appId和appSecret,如果有不清楚的可以看一下 PC网站实现微信扫码登录功能(一) 下面我就来说一下怎样在PC端怎么实现扫码登录功能。 一、进入官方文档网站 https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html 其实官网对过程描述的已经非常细致了。并且提供了两种扫码方式。一种是跳转二维码扫描页面,另一种是内嵌式二维码。大家可以

qq_35771266的博客 3万+

alsa分析:alsa的那些配置文件 ( 1 )

http://blog.csdn.net/doom66151/article/details/6573176 在根文件系统下,alsa相关的配置文件有: 在/system/usr/share/alsa目录下: ├── alsa.conf  ├── cards  │   └── aliases.conf  └── pcm      ├── center_lfe.conf

martin的专栏 1710

工业缺陷检测深度学习方法综述

异常:点异常、 上下文异常和集群异常。点异常:又称为离群值 (outliers)[9], 描述数值上偏离正 常样本的独立数据。与异常相近的概念还包括新颖点(novelty)和分布外数据。上下文异常:同样描述数据点,其数值属于正常范围,但不符合局部上下文规律。集群异常:描述一系列相关数据的集合,集合中的每一个实例的数值在单独考察时都处于正常值域,但集 合整体的相关性特征不服从正常模式。一般的图像异常检测往往仅需区分正常与异常样本,而工业缺陷检测更关注于检测 图像中的异常像素.

2301_80355452的博客 1850

alsa分析:alsa的那些配置文件1

在根文件系统下,alsa相关的配置文件有: 在/system/usr/share/alsa目录下: [c-sharp] view plaincopy ├── alsa.conf   ├── cards   │   └── aliases.conf   └── pcm       ├── center_lfe.conf

RopenYuan的专栏 1144

从毫K级温控到分子级洁净:光刻机环境控制系统的技术演进与专利格局

摘要 光刻机环境控制系统是半导体制造中的"隐形精密工程",通过毫K级温度控制(±0.002℃)、超高洁净度(ISO Class 1)等核心技术为纳米级光刻提供环境保障。本文系统分析了温度、湿度、洁净度、流场和压力控制五大技术方向的发展趋势,揭示了从单参数控制向多参数协同智能控制的转型路径。研究显示:1)温度控制正从PID向神经网络智能控制演进;2)EUV光刻将湿度控制推至ppbv级;3)洁净度控制扩展到分子级污染物管理。头部厂商中,ASML侧重系统集成,Nikon专注浸没流场,Canon

huanghm88的专栏 328

将Keil中的变量数组导出

首先,写好导出数据的函数。

小柚子的博客 1956

alsa相关的配置文件

http://blog.csdn.net/doom66151/article/details/6573176 在根文件系统下,alsa相关的配置文件有: 在/system/usr/share/alsa目录下: ├── alsa.conf   ├── cards   │   └── aliases.conf   └── pcm       ├── center_lfe.conf

933

基于自抗扰控制的虚拟同步发电机二次调频控制研究

摘要:本文针对高比例可再生能源并网导致的电力系统频率稳定性问题,提出基于自抗扰控制(ADRC)的虚拟同步发电机(VSG)二次调频控制策略。通过分析VSG工作原理和传统调频方法的不足,设计了非线性ADRC和线性ADRC两种控制器,详细阐述了参数整定方法。仿真结果表明,该策略能有效消除频率稳态偏差,提升系统抗扰能力,为分布式电源参与电网调频提供了新思路。 关键词:虚拟同步发电机;自抗扰控制;二次调频;频率调节;抗扰能力

huanghm88的专栏 249

DAPM之三:audio paths与asound.conf

其实asound.conf真跟dapm没多大关系,之所以把它也纳入dapm系列之一,是为了考虑到知识的连贯性。在DAPM之二:AUDIO PATHS与dapm kcontrol>提到:通过配置好asound.conf,上层则可打开asound.conf中定义的虚拟设备,而自动选择相应的音频通道。这是asound.conf很重要的一个作用,从这方面来说,并不是跟dapm完全没关系。   一、认识

tronteng的专栏 512

Python爬虫实战:携程旅游平台数据爬取(酒店+机票+景点),破解加密与反爬(附完整代码)

携程的反爬机制再复杂,核心逻辑都是“识别非真实用户的请求”,而爬虫的核心就是“让请求看起来像真实用户的行为”。本文的方案从登录态处理、参数加密破解、动态数据抓取,到反爬应对,都是围绕“模拟真实”展开:用Playwright模拟真实浏览器登录,用住宅代理模拟真实用户IP,用随机延迟模拟真实用户的操作频率。掌握了这个核心思路,无论平台的反爬如何升级,都能找到应对方法。当然,爬虫技术是一把双刃剑,合理使用才能发挥其价值。如果你的爬虫项目中遇到了携程的反爬问题,不妨按照本文的步骤试试,大概率能解决问题。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 2613

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

ALSA分析

最近闲了,将去年分析的alsa库分析一下,和大家共享一下 ,请多多指点。 里面有一部分链接,图片等有需要可联系我 哈   硬 件: Arm   软件: Android 2.1  无论是在录音还是在放音,都要打开一个PCM流,具体对应的函数原型为:  int snd_pcm_open(snd_pcm_t **pcmp, const char *name,  snd_pcm_stream_

武溪嵌人 3985

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

alsa-lib应用层接口分析

ALSA lib接口调用简介 ALSA逻辑,在我当前看来,总共有两条线:1、录放音流控,2、amixer cset控件。   录放音流控(自定义名称),相当于操作OSS的/dev/dsp设备,可以设置三大参数等,并且启动录放音。这里,aplay,mplayer等播放器,调用ALSA lib中的snd_pcm_***等,标准接口函数,再通过该函数,操作/dev/snd/pcmC0D0c pcm...

yuhuqiao的博客 7473

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

asound.conf 插件讲解

asound.conf插件讲解

qq_38532590的博客 1891

alsa-lib、alsa-utils编译

alsa-lib、alsa-utils编译

yangguoyu8023的博客 3405

Linux ALSA声卡驱动之一:ALSA架构简介

ALSA架构简介

houxiaoni01的博客 8660
上一篇: 关于音频播放的那点儿事儿(一) 初探iOS音频 CoreAudio Essentials
下一篇: 全面解析四大主流音频技术
sunjing_
博客等级 码龄11年 196粉丝 3原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值