-
Notifications
You must be signed in to change notification settings - Fork 0
/
anal-alsa-setValues.c
106 lines (88 loc) · 2.45 KB
/
anal-alsa-setValues.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <ctype.h>
#include <alsa/asoundlib.h>
char *_card = "hw:0";
void write_alsa(int nth, char *str)
{
snd_mixer_t* handle;
if ((snd_mixer_open(&handle, 0)) < 0) {
return;
}
if ((snd_mixer_attach(handle, _card)) < 0) {
goto cleanup;
}
if ((snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
goto cleanup;
}
int ret = snd_mixer_load(handle);
if (ret < 0) {
goto cleanup;
}
snd_mixer_elem_t *elem = snd_mixer_first_elem(handle);
for (int i=0; i<nth; i++) {
if (!elem) {
break;
}
elem = snd_mixer_elem_next(elem);
}
if (!elem) {
goto cleanup;
}
char *p;
if (p = strstr(str, "playbackVolume:")) {
p += 15;
double volume = strtod(p, NULL);
if ((volume >= 0.0) && (volume <= 1.0)) {
long minv, maxv;
snd_mixer_selem_get_playback_volume_range (elem, &minv, &maxv);
long val = minv + (double)(maxv-minv)*volume;
snd_mixer_selem_set_playback_volume_all(elem, val);
}
} else if (p = strstr(str, "playbackSwitch:")) {
p += 15;
int val = strtol(p, NULL, 10);
snd_mixer_selem_set_playback_switch_all(elem, val);
} else if (p = strstr(str, "enum:")) {
p += 5;
int val = strtol(p, NULL, 10);
snd_mixer_selem_set_enum_item(elem, 0, val);
} else if (p = strstr(str, "captureVolume:")) {
p += 14;
double volume = strtod(p, NULL);
if ((volume >= 0.0) && (volume <= 1.0)) {
long minv, maxv;
snd_mixer_selem_get_capture_volume_range (elem, &minv, &maxv);
long val = minv + (double)(maxv-minv)*volume;
snd_mixer_selem_set_capture_volume_all(elem, val);
}
} else if (p = strstr(str, "captureSwitch:")) {
p += 14;
int val = strtol(p, NULL, 10);
snd_mixer_selem_set_capture_switch_all(elem, val);
}
cleanup:
snd_mixer_close(handle);
}
/*
Usage: anal-alsa-setVolume [card name]
Reads from stdin
'card name' is 'hw:0' by default
*/
int main(int argc, char **argv)
{
if (argc >= 2) {
_card = argv[1];
}
for(;;) {
char buf[256];
if (!fgets(buf, 256, stdin)) {
break;
}
char *p = strstr(buf, "nth:");
if (p) {
p += 4;
int nth = strtol(p, NULL, 10);
write_alsa(nth, buf);
}
}
exit(0);
}