-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixer.c
164 lines (142 loc) · 3.58 KB
/
mixer.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* This is an example of a mixer program for Linux
*
* updated 1/1/93 to add stereo, level query, broken
* devmask kludge - cmetz@thor.tjhsst.edu
*
* slightly modified by in3otd@qsl.net
*
* (C) Craig Metz and Hannu Savolainen 1993.
*
* You may do anything you wish with this program.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>
void usage(void);
void print_recsrc(void);
char *names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
int devmask = 0, recmask = 0, recsrc = 0;
void usage(void)
{
int i, n = 0;
printf("Usage: mixer { ");
for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
if ((1 << i) & devmask) {
if (n)
putchar('|');
printf(names[i]);
n = 1;
}
printf(" } <value>\n or mixer { +rec|-rec } <devicename>\n");
exit(1);
}
void print_recsrc(void)
{
int i, n = 0;
fprintf(stderr, "Recording source: ");
for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
if ((1 << i) & recsrc) {
if (n)
fprintf(stderr, ", ");
fprintf(stderr, names[i]);
n = 1;
}
fprintf(stderr, "\n");
}
int main(int argc, char *argv[])
{
int foo, bar, baz, dev;
char name[30] = "/dev/mixer";
if (!strcmp(argv[0], "mixer0"))
strcpy(name, "/dev/mixer");
else if (!strcmp(argv[0], "mixer1"))
strcpy(name, "/dev/mixer1");
else if (!strcmp(argv[0], "mixer2"))
strcpy(name, "/dev/mixer2");
else if (!strcmp(argv[0], "mixer3"))
strcpy(name, "/dev/mixer3");
else if (!strcmp(argv[0], "mixer4"))
strcpy(name, "/dev/mixer4");
else if (!strcmp(argv[0], "mixer5"))
strcpy(name, "/dev/mixer5");
if ((baz = open(name, O_RDWR)) < 0) {
perror(name);
exit(1);
}
if (ioctl(baz, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
perror("SOUND_MIXER_READ_DEVMASK");
exit(-1);
}
if (ioctl(baz, SOUND_MIXER_READ_RECMASK, &recmask) == -1) {
perror("SOUND_MIXER_READ_RECMASK");
exit(-1);
}
if (ioctl(baz, SOUND_MIXER_READ_RECSRC, &recsrc) == -1) {
perror("SOUND_MIXER_READ_RECSRC");
exit(-1);
}
switch (argc) {
case 3:
bar = 1;
break;
case 2:
bar = 0;
break;
default:
usage();
}
for (foo = 0; foo < SOUND_MIXER_NRDEVICES && strcmp(names[foo], argv[1]); foo++);
if (foo >= SOUND_MIXER_NRDEVICES) {
if (!strcmp("+rec", argv[1]) || !strcmp("-rec", argv[1])) {
for (dev = 0; dev < SOUND_MIXER_NRDEVICES && strcmp(names[dev], argv[2]); dev++);
if (dev >= SOUND_MIXER_NRDEVICES)
usage();
if (!((1 << dev) & recmask)) {
fprintf(stderr, "Invalid recording source %s\n", argv[2]);
exit(-1);
}
if (argv[1][0] == '+')
recsrc |= (1 << dev);
else
recsrc &= ~(1 << dev);
if (ioctl(baz, SOUND_MIXER_WRITE_RECSRC, &recsrc) == -1) {
perror("SOUND_MIXER_WRITE_RECSRC");
exit(-1);
}
print_recsrc();
} else
usage();
} else {
if (bar) {
if (strchr(argv[2], ':') == NULL) {
sscanf(argv[2], "%d", &bar);
dev = bar;
} else
sscanf(argv[2], "%d:%d", &bar, &dev);
if (bar < 0)
bar = 0;
if (dev < 0)
dev = 0;
if (bar > 100)
bar = 100;
if (dev > 100)
dev = 100;
printf("Setting the mixer %s to %d:%d.\n", names[foo], bar, dev);
bar |= dev << 8;
if (ioctl(baz, MIXER_WRITE(foo), &bar) == -1)
perror("WRITE_MIXER");
return (0);
} else {
if (ioctl(baz, MIXER_READ(foo), &bar) == -1)
perror("MIXER_READ");
printf("The mixer %s is currently set to %d:%d.\n", names[foo], bar & 0x7f, (bar >> 8) & 0x7f);
}
}
close(baz);
return (0);
}