ALSA lib--简单的control使用例子

代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <alsa/asoundlib.h>
#include <unistd.h>

int ctl_val(char *card, char *elem_name, int set_val, int elem_value)
{
    snd_ctl_t * handle;
    snd_ctl_elem_info_t *info;
    snd_ctl_elem_id_t *id;
    snd_ctl_elem_value_t *value;
    snd_ctl_elem_list_t *list;
    snd_ctl_elem_type_t type;
    unsigned int count, used, idx, info_count;
    int err;
    long tmp;
    long long tmp64;

    err = snd_ctl_open(&handle, card, 0);
    if (err < 0) {
        printf("%s(), L%d  err: %s\n", __FUNCTION__, __LINE__, snd_strerror(err));
        return err;
    }
    
    snd_ctl_elem_info_alloca(&info);
    snd_ctl_elem_id_alloca(&id);
    snd_ctl_elem_value_alloca(&value);
    snd_ctl_elem_list_alloca(&list);

    err = snd_ctl_elem_list(handle, list);
    if (err < 0) {
        printf("%s(), L%d  err: %s\n", __FUNCTION__, __LINE__, snd_strerror(err));
        return err;
    }

    count = snd_ctl_elem_list_get_count(list);
    used = snd_ctl_elem_list_get_used(list);
    while(count != used) {
        err = snd_ctl_elem_list_alloc_space(list, count);
        if (err < 0) {
            printf("%s(), L%d  err: %s\n", __FUNCTION__, __LINE__, snd_strerror(err));
            return err;
        }
        err = snd_ctl_elem_list(handle, list);
        if (err < 0) {
            printf("%s(), L%d  err: %s\n", __FUNCTION__, __LINE__, snd_strerror(err));
            return err;
        }
        used = snd_ctl_elem_list_get_used(list);
    }

    for (idx = 0; idx < used; idx++) {
        if (!strcmp(snd_ctl_elem_list_get_name(list, idx), elem_name))
            break;
    }
    
    if (idx >= used) {
        printf("%s(), L%d  idx: %d, used:%d\n", __FUNCTION__, __LINE__, idx, used);
        return -1;
    }

    snd_ctl_elem_list_get_id(list, idx, id);
    snd_ctl_elem_info_set_id(info, id);

    err = snd_ctl_elem_info(handle, info);
    if (err < 0) {
        printf("%s(), L%d  err: %s\n", __FUNCTION__, __LINE__, snd_strerror(err));
        return err;
    }
    
    snd_ctl_elem_info_get_id(info, id);
    snd_ctl_elem_value_set_id(value, id);

    err = snd_ctl_elem_read(handle, value);
    if (err < 0) {
        printf("%s(), L%d  err: %s\n", __FUNCTION__, __LINE__, snd_strerror(err));
        return err;
    }

    type = snd_ctl_elem_info_get_type(info);
    info_count = snd_ctl_elem_info_get_count(info);
    for (idx = 0; idx < info_count && idx < 128; idx++) {
        switch(type) {
            case SND_CTL_ELEM_TYPE_BOOLEAN:
                tmp = snd_ctl_elem_value_get_boolean(value, idx);
                printf("%s(), L%d  %s idx:%d, tmp:%ld\n", __FUNCTION__, __LINE__, elem_name, idx, tmp);
                if (set_val)
                    snd_ctl_elem_value_set_boolean(value, idx, elem_value);
                break;
            case SND_CTL_ELEM_TYPE_INTEGER:
                tmp = snd_ctl_elem_value_get_integer(value, idx);
                printf("%s(), L%d  %s idx:%d, tmp:%ld\n", __FUNCTION__, __LINE__, elem_name, idx, tmp);
                if (set_val)
                    snd_ctl_elem_value_set_integer(value, idx, elem_value);
                break;
            case SND_CTL_ELEM_TYPE_INTEGER64:
                tmp64 = snd_ctl_elem_value_get_integer64(value, idx);
                printf("%s(), L%d  %s idx:%d, tmp64:%lld\n", __FUNCTION__, __LINE__, elem_name, idx, tmp64);
                if (set_val)
                    snd_ctl_elem_value_set_integer64(value, idx, elem_value);
                break;
            case SND_CTL_ELEM_TYPE_ENUMERATED:
                tmp = snd_ctl_elem_value_get_enumerated(value, idx);
                printf("%s(), L%d  %s idx:%d, tmp:%ld\n", __FUNCTION__, __LINE__, elem_name, idx, tmp);
                if (set_val)
                    snd_ctl_elem_value_set_enumerated(value, idx, elem_value);
                break;
            case SND_CTL_ELEM_TYPE_BYTES:
                tmp = snd_ctl_elem_value_get_byte(value, idx);
                //tmp = snd_ctl_elem_value_get_bytes(value);
                printf("%s(), L%d  %s idx:%d, tmp:%ld\n", __FUNCTION__, __LINE__, elem_name, idx, tmp);
                if (set_val)
                    snd_ctl_elem_set_bytes(value, &elem_value, sizeof(elem_value));
                break;
            default:
                break;
        }
    }
    
    if (set_val) {
        err = snd_ctl_elem_write(handle, value);
        if (err < 0) {
            printf("%s(), L%d  err: %s\n", __FUNCTION__, __LINE__, snd_strerror(err));
            return err;
        }
    }
snd_ctl_elem_list_free_space(list);
snd_ctl_close(handle);
return 0; } int main(int argc, char **argv) { int err = 0; char *card = NULL; char *elem_name = NULL; int elem_value = 0; int set_val = 0; if (argc < 3) return -1; card = argv[1]; elem_name = argv[2]; if (argc >= 4) { elem_value = atoi(argv[3]); set_val = 1; } return ctl_val(card, elem_name, set_val, elem_value); }

 

Makefile:

ALSA_INC_PATH=/home/fellow/alsa-lib-1.2.2/output/usr/include
ALSA_LIB_PATH=/usr/lib/i386-linux-gnu

export LD_LIBRARY_PATH=${ALSA_LIB_PATH}:$LD_LIBRARY_PATH
export CC=gcc
export CFLAGS=-I${ALSA_INC_PATH}
export LDFLAGS=-L{ALSA_LIB_PATH} -lasound
SOURCE=ctl_test.c
TARGET=ctl_test
all:
    ${CC} ${SOURCE} ${CFLAGS} ${LDFLAGS} -o ${TARGET}

./ctl_test hw:0 "Master Playback Volume" 50

posted @ 2020-03-03 21:11  fellow_jing  阅读(1123)  评论(0编辑  收藏  举报