金泰妍专辑台版:Use ALSA lib apis to set the Mic Capture Volu...

来源:百度文库 编辑:偶看新闻 时间:2024/05/08 20:41:42

Use ALSA lib apis to set the Mic Capture Volume

分类: Linux 技术相关 2009-07-23 20:43 591人阅读 评论(0) 收藏 举报

 下面这个函数的作用相当与 调用ALSA utility amixer 设置 Mic Capture Volume。

 #amixer cset name="Mic Capture Volume" 10

 

[cpp] view plaincopy?

  1. #include    
  2. static char card[64] = "default";  
  3.   
  4. BOOL   
  5. CXXXSoundDevice::SetMicCaptureVolume(UINT32 uVolume)  
  6. {  
  7.     int err;  
  8.     int orig_volume = 0;  
  9.     static snd_ctl_t *handle = NULL;  
  10.     snd_ctl_elem_info_t *info;  
  11.     snd_ctl_elem_id_t *id;  
  12.     snd_ctl_elem_value_t *control;  
  13.     unsigned int count;  
  14.     snd_ctl_elem_type_t type;  
  15.     snd_ctl_elem_info_alloca(&info);  
  16.     snd_ctl_elem_id_alloca(&id);  
  17.     snd_ctl_elem_value_alloca(&control);      
  18.       
  19.     snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);/* default */  
  20.     snd_ctl_elem_id_set_name(id, "Mic Capture Volume");  
  21. //  snd_ctl_elem_id_set_index(id, 1);  // "Mic Capture Volume" does not have index.   
  22.           
  23.     if ((err = snd_ctl_open(&handle, card, 0)) < 0) {  
  24.         BTLOGERROR(LCBTLib,"Control %s open error: %s/n", card, snd_strerror(err));  
  25.         return FALSE;  
  26.     }   
  27.              
  28.     snd_ctl_elem_info_set_id(info, id);  
  29.     if ((err = snd_ctl_elem_info(handle, info)) < 0) {  
  30.         BTLOGERROR(LCBTLib,"Cannot find the given element from control %s/n", card);  
  31.         snd_ctl_close(handle);  
  32.         handle = NULL;  
  33.         return FALSE;  
  34.     }  
  35.     type = snd_ctl_elem_info_get_type(info);  
  36.     count = snd_ctl_elem_info_get_count(info);  
  37.       
  38.     if(type != SND_CTL_ELEM_TYPE_INTEGER || 1 != count) {  
  39.         BTLOGERROR(LCBTLib,"Cannot find the given element from control %s/n", card);  
  40.         snd_ctl_close(handle);  
  41.         handle = NULL;  
  42.         return FALSE;  
  43.     }  
  44.       
  45.     snd_ctl_elem_value_set_id(control, id);  
  46.       
  47.     if (!snd_ctl_elem_read(handle, control)) {  
  48.         orig_volume = snd_ctl_elem_value_get_integer(control, 0);  
  49.     }  
  50.       
  51.     if(uVolume != orig_volume) {  
  52.         BTLOGERROR(LCBTLib,"uVolume != orig_volume ##################### new_value(%d) orgin_value(%d)/n",uVolume,orig_volume);  
  53.         snd_ctl_elem_value_set_integer(control, 0, static_cast(uVolume));  
  54.         if ((err = snd_ctl_elem_write(handle, control)) < 0) {  
  55.             BTLOGERROR(LCBTLib,"Control %s element write error: %s/n", card, snd_strerror(err));  
  56.             snd_ctl_close(handle);  
  57.             handle = NULL;  
  58.             return FALSE;  
  59.         }  
  60.     }   
  61.     snd_ctl_close(handle);  
  62.     handle = NULL;  
  63.     return TRUE;  
  64. }