织梦CMS - 轻松建站从此开始!

罗索

change capturing volume by alsa mixer

jackyhwei 发布于 2011-07-08 15:49 点击:次 
在ALSA架构下进行音频采集时,对mixer采集上来的音频进行音量调整的代码实现。
TAG:

#include <alsa/asoundlib.h>

static char card[64] = "default";

void SetMicCaptureVolume(int uVolume)

 {

     int err;

     int orig_volume0 = 0;

     int orig_volume1 = 0;

     static snd_ctl_t *handle = NULL;

     snd_ctl_elem_info_t *info;

     snd_ctl_elem_id_t *id;

     snd_ctl_elem_value_t *control;

     unsigned int count;

     snd_ctl_elem_type_t type;

     snd_ctl_elem_info_alloca(&info);

     snd_ctl_elem_id_alloca(&id);

     snd_ctl_elem_value_alloca(&control);

     snd_ctl_elem_id_set_interface(id, SND_CTL_ELEM_IFACE_MIXER);/* default */

     snd_ctl_elem_id_set_name(id, "Capture Volume");

         //snd_ctl_elem_id_set_index(id, 0);  // "Mic Capture Volume" does not have index.

     if ((err = snd_ctl_open(&handle, card, 0)) < 0) {

         printf("Control %s open error: %s\n", card, snd_strerror(err));

         return;

     }

     snd_ctl_elem_info_set_id(info, id);

     if ((err = snd_ctl_elem_info(handle, info)) < 0)

     {

         printf("Cannot find the given element from control-1 %s\n", snd_strerror(err));

         snd_ctl_close(handle);

         handle = NULL;

         return;

     }

     type = snd_ctl_elem_info_get_type(info);

     count = snd_ctl_elem_info_get_count(info);

     printf("type = %d, count= %d\n", type, count);

    /* if(type != SND_CTL_ELEM_TYPE_INTEGER || 1 != count)

     {

         printf("Cannot find the given element from control-2 %s\n", snd_strerror(err));

         snd_ctl_close(handle);

         handle = NULL;

         return;

     }*/

     snd_ctl_elem_value_set_id(control, id);

     if (!snd_ctl_elem_read(handle, control))

     {

         orig_volume0 = snd_ctl_elem_value_get_integer(control, 0);

         orig_volume1 = snd_ctl_elem_value_get_integer(control, 1);

     }

     printf("min value = %ld, max value = %ld\n", snd_ctl_elem_info_get_min(info), snd_ctl_elem_info_get_max(info));

     if(uVolume != orig_volume0)

     {

         printf("uVolume != orig_volume ##################### new_value(%d) orgin_value(%d, %d)\n",uVolume,orig_volume0, orig_volume1);

         snd_ctl_elem_value_set_integer(control, 0, (long)(uVolume));

         snd_ctl_elem_value_set_integer(control, 1, (long)(uVolume));

         if ((err = snd_ctl_elem_write(handle, control)) < 0)

         {

             printf("Control %s element write error: %s\n", card, snd_strerror(err));

             snd_ctl_close(handle);

             handle = NULL;

             return;

         }

     }

     snd_ctl_close(handle);

     handle = NULL;

     return;

 }

(carlsonlee_freec)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201107/14691.html]
本文出处:hi.baidu.com/carlsonlee_freec 作者:carlsonlee_freec
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容