Debian Codegraph @

utils/aplay/alsa-mixer.c

raw
1 /*
2 * BlueALSA - alsa-mixer.c
3 * Copyright (c) 2016-2021 Arkadiusz Bokowy
4 *
5 * This file is a part of bluez-alsa.
6 *
7 * This project is licensed under the terms of the MIT license.
8 *
9 */
10
11 #include "alsa-mixer.h"
12
13 #include <stdio.h>
14 #include <string.h>
15
16 int alsa_mixer_open(snd_mixer_t **mixer, snd_mixer_elem_t **elem,
17 const char *dev_name, const char *elem_name, unsigned int elem_idx,
18 char **msg) {
19
20 snd_mixer_t *_mixer = NULL;
21 snd_mixer_elem_t *_elem;
22 char buf[256];
23 int err;
24
25 snd_mixer_selem_id_t *id;
26 snd_mixer_selem_id_alloca(&id);
27 snd_mixer_selem_id_set_name(id, elem_name);
28 snd_mixer_selem_id_set_index(id, elem_idx);
29
30 if ((err = snd_mixer_open(&_mixer, 0)) != 0) {
31 snprintf(buf, sizeof(buf), "Open mixer: %s", snd_strerror(err));
32 goto fail;
33 }
34 if ((err = snd_mixer_attach(_mixer, dev_name)) != 0) {
35 snprintf(buf, sizeof(buf), "Attach mixer: %s", snd_strerror(err));
36 goto fail;
37 }
38 if ((err = snd_mixer_selem_register(_mixer, NULL, NULL)) != 0) {
39 snprintf(buf, sizeof(buf), "Register mixer class: %s", snd_strerror(err));
40 goto fail;
41 }
42 if ((err = snd_mixer_load(_mixer)) != 0) {
43 snprintf(buf, sizeof(buf), "Load mixer elements: %s", snd_strerror(err));
44 goto fail;
45 }
46 if ((_elem = snd_mixer_find_selem(_mixer, id)) == NULL) {
47 snprintf(buf, sizeof(buf), "Mixer element not found");
48 err = -1;
49 goto fail;
50 }
51
52 *mixer = _mixer;
53 *elem = _elem;
54 return 0;
55
56 fail:
57 if (_mixer != NULL)
58 snd_mixer_close(_mixer);
59 if (msg != NULL)
60 *msg = strdup(buf);
61 return err;
62 }