2 * This is an example of a mixer program for Linux
4 * updated 1/1/93 to add stereo, level query, broken
5 * devmask kludge - cmetz@thor.tjhsst.edu
7 * (C) Craig Metz and Hannu Savolainen 1993.
9 * You may do anything you wish with this program.
11 * ditto for my modifications (John-Mark Gurney, 1997)
14 #include <sys/cdefs.h>
15 __FBSDID("$FreeBSD$");
23 #include <sys/soundcard.h>
25 const char *names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
27 void usage(int devmask, int recmask);
28 int res_name(const char *name, int mask);
29 void print_recsrc(int recsrc, int shortflag);
32 usage(int devmask, int recmask)
36 printf("usage: mixer [-f device] [-s | -S] [dev [+|-][voll[:[+|-]volr]] ...\n"
37 " mixer [-f device] [-s | -S] recsrc ...\n"
38 " mixer [-f device] [-s | -S] {^|+|-|=}rec rdev ... \n");
40 for (i = 0, n = 0; i < SOUND_MIXER_NRDEVICES; i++)
41 if ((1 << i) & devmask) {
44 printf("%s", names[i]);
47 printf("\n rec devices: ");
48 for (i = 0, n = 0; i < SOUND_MIXER_NRDEVICES; i++)
49 if ((1 << i) & recmask) {
52 printf("%s", names[i]);
60 res_name(const char *name, int mask)
64 for (foo = 0; foo < SOUND_MIXER_NRDEVICES; foo++)
65 if ((1 << foo) & mask && !strcmp(names[foo], name))
68 return foo == SOUND_MIXER_NRDEVICES ? -1 : foo;
72 print_recsrc(int recsrc, int shortflag)
77 printf("Recording source: ");
79 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
80 if ((1 << i) & recsrc) {
88 printf("%s", names[i]);
96 main(int argc, char *argv[])
98 int foo, bar, baz, dev;
99 int devmask = 0, recmask = 0, recsrc = 0, orecsrc;
100 int dusage = 0, drecsrc = 0, shortflag = 0, Shortflag = 0;
101 int l = 0, r = 0, t = 0;
102 char lstr[5], rstr[5];
103 int n = 0, lrel = 0, rrel = 0;
110 if (!strcmp(argv[0], "mixer2"))
111 name = "/dev/mixer1";
112 else if (!strcmp(argv[0], "mixer3"))
113 name = "/dev/mixer2";
115 while ((ch = getopt(argc, argv, "f:sS")) != -1)
129 argc -= (optind - 1);
130 argv += (optind - 1);
132 if ((baz = open(name, O_RDWR)) < 0)
134 if (ioctl(baz, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
135 err(1, "SOUND_MIXER_READ_DEVMASK");
136 if (ioctl(baz, SOUND_MIXER_READ_RECMASK, &recmask) == -1)
137 err(1, "SOUND_MIXER_READ_RECMASK");
138 if (ioctl(baz, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
139 err(1, "SOUND_MIXER_READ_RECSRC");
142 if ((argc == 1) && (dusage == 0)) {
143 for (foo = 0; foo < SOUND_MIXER_NRDEVICES; foo++) {
144 if (!((1 << foo) & devmask))
146 if (ioctl(baz, MIXER_READ(foo),&bar)== -1) {
151 printf("%s:%d:%d ", names[foo], bar & 0x7f,
154 printf("%s %d:%d ", names[foo], bar & 0x7f,
157 printf("Mixer %-8s is currently set to %3d:%d\n",
158 names[foo], bar & 0x7f, (bar >> 8) & 0x7f);
160 if (ioctl(baz, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
161 err(1, "SOUND_MIXER_READ_RECSRC");
162 print_recsrc(recsrc, shortflag || Shortflag);
168 while ((argc > 0) && (dusage == 0)) {
169 if (!strcmp("recsrc", *argv)) {
173 } else if (argc > 1 && !strcmp("rec", *argv + 1)) {
174 if (**argv != '+' && **argv != '-' &&
175 **argv != '=' && **argv != '^') {
176 warnx("unknown modifier: %c", **argv);
180 if ((dev = res_name(argv[1], recmask)) == -1) {
181 warnx("unknown recording device: %s", argv[1]);
187 recsrc |= (1 << dev);
190 recsrc &= ~(1 << dev);
196 recsrc ^= (1 << dev);
200 argc -= 2; argv += 2;
204 if ((t = sscanf(*argv, "%d:%d", &l, &r)) > 0) {
207 else if((dev = res_name(*argv, devmask)) == -1) {
208 warnx("unknown device: %s", *argv);
213 #define issign(c) (((c) == '+') || ((c) == '-'))
216 n = sscanf(argv[1], "%7[^:]:%7s", lstr, rstr);
230 switch(argc > 1 ? n : t) {
232 if (ioctl(baz, MIXER_READ(dev),&bar)== -1) {
238 printf("%s:%d:%d ", names[dev], bar & 0x7f,
241 printf("%s %d:%d ", names[dev], bar & 0x7f,
244 printf("Mixer %-8s is currently set to %3d:%d\n",
245 names[dev], bar & 0x7f, (bar >> 8) & 0x7f);
252 if (ioctl(baz, MIXER_READ(dev),&bar)== -1) {
259 l = (bar & 0x7f) + l;
261 r = ((bar >> 8) & 0x7f) + r;
273 printf("Setting the mixer %s from %d:%d to %d:%d.\n",
274 names[dev], bar & 0x7f, (bar >> 8) & 0x7f, l, r);
277 if (ioctl(baz, MIXER_WRITE(dev), &l) == -1)
280 argc -= 2; argv += 2;
287 usage(devmask, recmask);
291 if (orecsrc != recsrc)
292 if (ioctl(baz, SOUND_MIXER_WRITE_RECSRC, &recsrc) == -1)
293 err(1, "SOUND_MIXER_WRITE_RECSRC");
296 if (ioctl(baz, SOUND_MIXER_READ_RECSRC, &recsrc) == -1)
297 err(1, "SOUND_MIXER_READ_RECSRC");
298 print_recsrc(recsrc, shortflag || Shortflag);