當前位置:首頁 > IT技術(shù) > Windows編程 > 正文

alsa-amixer-api
2021-09-24 09:09:50

?

https://www.alsa-project.org/alsa-doc/alsa-lib/group___simple_mixer.html

?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <stdarg.h>
#include <ctype.h>
#include <math.h>
#include <errno.h>
#include <assert.h>
#include <alsa/asoundlib.h>
#include <sys/poll.h>
#include <stdint.h>
#include "amixer.h"
//使用 amixer scontrols,中的選項
char * sctrlstr = "Digital Input";
int main()
{
    setCapVal();
    return 0;
}
 
 
//一個簡單使用的例子
int setCapVal()
{
    long volMin, volMax, leftVal, rightVal;
    volMin = 0;  //聲音范圍
    volMax = 0;
    leftVal = 0;  //左右聲道音量
    rightVal = 0;
    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);
    
    
    //打開混音器設(shè)備
    if ((err = snd_mixer_open(&handle, 0)) < 0) {
        printf("snd_mixer_open Err
");
        return err;
    }
    
    snd_mixer_attach(handle, "default");
    snd_mixer_selem_register(handle, NULL, NULL);
    snd_mixer_load(handle);
    //循環(huán)找到自己想要的element
    elem = snd_mixer_first_elem(handle);
    while(elem)
    {
        //比較element名字是否是我們想要設(shè)置的選項
        if ( strcmp( sctrlstr, snd_mixer_selem_get_name (elem)) == 0)
        {
            printf( "elem name : %s
", snd_mixer_selem_get_name (elem) );
            break;
        }
        //如果不是就繼續(xù)尋找下一個
        elem = snd_mixer_elem_next(elem);
    }
    
    if (!elem) {
        printf("snd_mixer_find_selem Err
");
        snd_mixer_close(handle);
        handle = NULL;
        return -ENOENT;
    }
    
    //輸出elem的名字
    printf( "elem name : %s
", snd_mixer_selem_get_name (elem) );
    
    snd_mixer_selem_get_capture_volume_range(elem, &volMin, &volMax);
    printf("音量范圍: %ld -- %ld
", volMin, volMax);
    
    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
", leftVal, rightVal);
    
    
    //判斷是不是單聲道
    if( snd_mixer_selem_is_playback_mono( elem) )
    {
        //單聲道
        snd_mixer_selem_set_playback_volume(elem,SND_MIXER_SCHN_FRONT_LEFT, 45);
        printf("單聲道: 45
");
    }
    else
    {
        //左音量
        snd_mixer_selem_set_playback_volume(elem,SND_MIXER_SCHN_FRONT_LEFT, 45);
        //右音量
        snd_mixer_selem_set_playback_volume(elem,SND_MIXER_SCHN_FRONT_RIGHT, 67);
        printf("雙聲道: 45,  67
");
    }
    
    
    
    //關(guān)閉混音器設(shè)備
    snd_mixer_close(handle);
    handle = NULL;
}
 

?

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務(wù)立即開通 >