alsa调节音量代码

这是自己写的测试程序,根据声卡调节扬声器和麦克风音量。希望可以帮到有需要的人。

#include <stdio.h>
#include <alsa/asoundlib.h>

int main()
{
    setCapVal();
    return 0;
}

void update_playback_volume_single(snd_mixer_elem_t *elem, snd_mixer_t *handle, float percentage) {
    long volMin, volMax, leftVal, rightVal;
    volMin = 0;  //声音范围
    volMax = 0;
    leftVal = 0;  //左右声道音量
    rightVal = 0;

    snd_mixer_selem_get_playback_volume_range(elem, &volMin, &volMax);
    printf("扬声器音量范围: %ld -- %ld\n", volMin, volMax);
    if (volMax == 0) {
	return;
    }
    snd_mixer_handle_events(handle);
    snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, &leftVal);
    snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT, &rightVal);
    printf("扬声器当前音量: leftVal = %ld, rightVal = %ld\n", leftVal, rightVal);
    int volume = (int)volMax*percentage;
    snd_mixer_selem_set_playback_volume_all(elem, volume);

    //判断是不是单声道
    if( snd_mixer_selem_is_playback_mono(elem))
    {
	//左音量snd_mixer_selem_set_playback_volume(elem,SND_MIXER_SCHN_FRONT_LEFT, volume);
	printf("单声道: %d\n", volume);
    }
    else
    {
	//左音量snd_mixer_selem_set_playback_volume(elem,SND_MIXER_SCHN_FRONT_LEFT, volume);
	//右音量snd_mixer_selem_set_playback_volume(elem,SND_MIXER_SCHN_FRONT_RIGHT, volume);
	printf("双声道: %d,  %d\n", volume, volume);
    }
}

void update_captrue_volume_single(snd_mixer_elem_t *elem, snd_mixer_t *handle, float percentage) {
    long volMin, volMax, leftVal, rightVal;
    volMin = 0;  //声音范围
    volMax = 0;
    leftVal = 0;  //左右声道音量
    rightVal = 0;
	
    snd_mixer_selem_get_capture_volume_range(elem, &volMin, &volMax);
    printf("麦克风音量范围: %ld -- %ld\n", volMin, volMax);
    if (volMax == 0) {
 	return; 
    }
    snd_mixer_handle_events(handle);
    snd_mixer_selem_get_capture_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, &leftVal);
    snd_mixer_selem_get_capture_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT, &rightVal);
    printf("麦克风当前音量: leftVal = %ld, rightVal = %ld\n", leftVal, rightVal);
    int volume = (int)volMax*percentage;
    snd_mixer_selem_set_capture_volume_all(elem, volume);
}

//更新扬声器音量
int update_playback_volume(float percentage)
{
    int err;
    static snd_mixer_t *handle = NULL;
    snd_mixer_elem_t *elem;
    snd_mixer_selem_id_t *sid;
    snd_mixer_selem_id_alloca(&sid);
	
    //打开混音器设备
    if ((err = snd_mixer_open(&handle, 0)) < 0) {
	printf("snd_mixer_open Err\n");
	return err;
    }
    
    snd_mixer_attach(handle, "default"/*"hw:2"*/);
    snd_mixer_selem_register(handle, NULL, NULL);
    snd_mixer_load(handle);
    //循环找到element
    elem = snd_mixer_first_elem(handle);
    err = 1;
    printf("-----update_playback_volume-----\n");
    while (elem)
    {
	err = 0;
	update_playback_volume_single(elem, handle, percentage);
	elem = snd_mixer_elem_next(elem);
    }
    if (err) {
	printf("snd_mixer_find_selem Err\n");
	snd_mixer_close(handle);
	handle = NULL;
	return -ENOENT;
    }

    //关闭混音器设备
    snd_mixer_close(handle);
    handle = NULL;
}

//更新麦克风音量
int update_captrue_volume(float percentage)
{
    int err;
    static snd_mixer_t *handle = NULL;
    snd_mixer_elem_t *elem;
    snd_mixer_selem_id_t *sid;
    snd_mixer_selem_id_alloca(&sid);
	
    //打开混音器设备
    if ((err = snd_mixer_open(&handle, 0)) < 0) {
	printf("snd_mixer_open Err\n");
	return err;
    }
	
    snd_mixer_attach(handle, "default"/*"hw:2"*/);
    snd_mixer_selem_register(handle, NULL, NULL);
    snd_mixer_load(handle);
    //循环找到element
    elem = snd_mixer_first_elem(handle);
    err = 1;
    printf("-----update_captrue_volume-----\n");
    while (elem)
    {
	err = 0;
	update_captrue_volume_single(elem, handle, percentage);
	elem = snd_mixer_elem_next(elem);
    }
	
    if (err) {
	printf("snd_mixer_find_selem Err\n");
	snd_mixer_close(handle);
	handle = NULL;
	return -ENOENT;
    }

    //关闭混音器设备
    snd_mixer_close(handle);
    handle = NULL;
}

//一个简单使用的例子
int setCapVal()
{
    update_playback_volume(0.6);//更新扬声器音量为60%
    update_captrue_volume(0.6);//更新麦克风音量为60%
}
# alsa 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×