mixer.c revision 6448
16448Sache/*
26448Sache *	This is an example of a mixer program for Linux
36448Sache *
46448Sache *	updated 1/1/93 to add stereo, level query, broken
56448Sache *      	devmask kludge - cmetz@thor.tjhsst.edu
66448Sache *
76448Sache * (C) Craig Metz and Hannu Savolainen 1993.
86448Sache *
96448Sache * You may do anything you wish with this program.
106448Sache */
116448Sache
126448Sache#include <stdio.h>
136448Sache#include <fcntl.h>
146448Sache#include <string.h>
156448Sache#ifdef __FreeBSD__
166448Sache#include <machine/soundcard.h>
176448Sache#else
186448Sache#include <sys/soundcard.h>
196448Sache#endif
206448Sache
216448Sachechar *names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
226448Sache
236448Sacheint devmask = 0, recmask = 0, recsrc = 0;
246448Sache
256448Sachevoid usage(void)
266448Sache{
276448Sache	int i, n = 0;
286448Sache	printf("Usage: mixer { ");
296448Sache
306448Sache	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
316448Sache		if ((1 << i) & devmask)  {
326448Sache			if (n)
336448Sache				putchar('|');
346448Sache			printf(names[i]);
356448Sache			n = 1;
366448Sache		}
376448Sache	printf(" } <value>\n  or   mixer { +rec|-rec } <devicename>\n");
386448Sache	exit(1);
396448Sache}
406448Sache
416448Sachevoid print_recsrc(void)
426448Sache{
436448Sache	int i, n = 0;
446448Sache	fprintf(stderr, "Recording source: ");
456448Sache
466448Sache	for (i = 0; i < SOUND_MIXER_NRDEVICES; i++)
476448Sache		if ((1 << i) & recsrc) {
486448Sache			if (n)
496448Sache				fprintf(stderr, ", ");
506448Sache			fprintf(stderr, names[i]);
516448Sache			n = 1;
526448Sache		}
536448Sache	fprintf(stderr, "\n");
546448Sache}
556448Sache
566448Sacheint
576448Sachemain(int argc, char *argv[])
586448Sache{
596448Sache	int foo, bar, baz, dev;
606448Sache
616448Sache	char name[30] = "/dev/mixer";
626448Sache
636448Sache	if (!strcmp(argv[0], "mixer2"))
646448Sache	   strcpy(name, "/dev/mixer1");
656448Sache	else
666448Sache	  if (!strcmp(argv[0], "mixer3"))
676448Sache	     strcpy(name, "/dev/mixer2");
686448Sache
696448Sache	if ((baz = open(name, O_RDWR)) < 0) {
706448Sache		perror(name);
716448Sache		exit(1);
726448Sache	}
736448Sache	if (ioctl(baz, SOUND_MIXER_READ_DEVMASK, &devmask) == -1) {
746448Sache		perror("SOUND_MIXER_READ_DEVMASK");
756448Sache		exit(-1);
766448Sache	}
776448Sache	if (ioctl(baz, SOUND_MIXER_READ_RECMASK, &recmask) == -1) {
786448Sache		perror("SOUND_MIXER_READ_RECMASK");
796448Sache		exit(-1);
806448Sache	}
816448Sache	if (ioctl(baz, SOUND_MIXER_READ_RECSRC, &recsrc) == -1) {
826448Sache		perror("SOUND_MIXER_READ_RECSRC");
836448Sache		exit(-1);
846448Sache	}
856448Sache
866448Sache	switch (argc) {
876448Sache		case 3:
886448Sache			bar = 1;
896448Sache			break;
906448Sache		case 2:
916448Sache			bar = 0;
926448Sache			break;
936448Sache		default:
946448Sache			usage();
956448Sache	}
966448Sache
976448Sache	for (foo = 0; foo < SOUND_MIXER_NRDEVICES && strcmp(names[foo], argv[1]); foo++);
986448Sache
996448Sache	if (foo >= SOUND_MIXER_NRDEVICES) {
1006448Sache
1016448Sache		if (!strcmp("+rec", argv[1]) || !strcmp("-rec", argv[1])) {
1026448Sache			for (dev = 0; dev < SOUND_MIXER_NRDEVICES && strcmp(names[dev], argv[2]); dev++);
1036448Sache			if (dev >= SOUND_MIXER_NRDEVICES)
1046448Sache				usage();
1056448Sache
1066448Sache			if (!((1 << dev) & recmask)) {
1076448Sache				fprintf(stderr, "Invalid recording source %s\n", argv[2]);
1086448Sache				exit(-1);
1096448Sache			}
1106448Sache			if (argv[1][0] == '+')
1116448Sache				recsrc |= (1 << dev);
1126448Sache			else
1136448Sache				recsrc &= ~(1 << dev);
1146448Sache
1156448Sache			if (ioctl(baz, SOUND_MIXER_WRITE_RECSRC, &recsrc) == -1) {
1166448Sache				perror("SOUND_MIXER_WRITE_RECSRC");
1176448Sache				exit(-1);
1186448Sache			}
1196448Sache			print_recsrc();
1206448Sache
1216448Sache		} else
1226448Sache			usage();
1236448Sache	} else {
1246448Sache		if (bar) {
1256448Sache			if (strchr(argv[2], ':') == NULL) {
1266448Sache				sscanf(argv[2], "%d", &bar);
1276448Sache				dev = bar;
1286448Sache			} else
1296448Sache				sscanf(argv[2], "%d:%d", &bar, &dev);
1306448Sache
1316448Sache			if (bar < 0)
1326448Sache				bar = 0;
1336448Sache			if (dev < 0)
1346448Sache				dev = 0;
1356448Sache			if (bar > 100)
1366448Sache				bar = 100;
1376448Sache			if (dev > 100)
1386448Sache				dev = 100;
1396448Sache
1406448Sache			printf("Setting the mixer %s to %d:%d.\n", names[foo], bar, dev);
1416448Sache
1426448Sache                        bar |= dev << 8;
1436448Sache			if (ioctl(baz, MIXER_WRITE(foo), &bar) == -1)
1446448Sache				perror("WRITE_MIXER");
1456448Sache	return (0);
1466448Sache		} else {
1476448Sache			if (ioctl(baz, MIXER_READ(foo),&bar)== -1)
1486448Sache			   perror("MIXER_READ");
1496448Sache			printf("The mixer %s is currently set to %d:%d.\n", names[foo], bar & 0x7f, (bar >> 8) & 0x7f);
1506448Sache		}
1516448Sache	}
1526448Sache
1536448Sache	close(baz);
1546448Sache}
155