mixer.c revision 13435
16448Sache/*
26448Sache *	This is an example of a mixer program for Linux
36448Sache *
46448Sache *	updated 1/1/93 to add stereo, level query, broken
58857Srgrimes *      	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
308857Srgrimes	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;
368857Srgrimes		}
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
568857Srgrimesint
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])) {
10213435Smpp			if (argc != 3) {
10313435Smpp				usage();
10413435Smpp				/* NOTREACHED */
10513435Smpp			}
1066448Sache			for (dev = 0; dev < SOUND_MIXER_NRDEVICES && strcmp(names[dev], argv[2]); dev++);
1076448Sache			if (dev >= SOUND_MIXER_NRDEVICES)
1086448Sache				usage();
1096448Sache
1106448Sache			if (!((1 << dev) & recmask)) {
1116448Sache				fprintf(stderr, "Invalid recording source %s\n", argv[2]);
1126448Sache				exit(-1);
1136448Sache			}
1146448Sache			if (argv[1][0] == '+')
1156448Sache				recsrc |= (1 << dev);
1166448Sache			else
1176448Sache				recsrc &= ~(1 << dev);
1186448Sache
1196448Sache			if (ioctl(baz, SOUND_MIXER_WRITE_RECSRC, &recsrc) == -1) {
1206448Sache				perror("SOUND_MIXER_WRITE_RECSRC");
1216448Sache				exit(-1);
1226448Sache			}
1236448Sache			print_recsrc();
1246448Sache
1256448Sache		} else
1266448Sache			usage();
1276448Sache	} else {
1286448Sache		if (bar) {
1296448Sache			if (strchr(argv[2], ':') == NULL) {
1306448Sache				sscanf(argv[2], "%d", &bar);
1316448Sache				dev = bar;
1328857Srgrimes			} else
1336448Sache				sscanf(argv[2], "%d:%d", &bar, &dev);
1348857Srgrimes
1356448Sache			if (bar < 0)
1366448Sache				bar = 0;
1376448Sache			if (dev < 0)
1386448Sache				dev = 0;
1396448Sache			if (bar > 100)
1406448Sache				bar = 100;
1416448Sache			if (dev > 100)
1426448Sache				dev = 100;
1436448Sache
1446448Sache			printf("Setting the mixer %s to %d:%d.\n", names[foo], bar, dev);
1456448Sache
1466448Sache                        bar |= dev << 8;
1476448Sache			if (ioctl(baz, MIXER_WRITE(foo), &bar) == -1)
1486448Sache				perror("WRITE_MIXER");
1496448Sache	return (0);
1506448Sache		} else {
1516448Sache			if (ioctl(baz, MIXER_READ(foo),&bar)== -1)
1526448Sache			   perror("MIXER_READ");
1536448Sache			printf("The mixer %s is currently set to %d:%d.\n", names[foo], bar & 0x7f, (bar >> 8) & 0x7f);
1546448Sache		}
1556448Sache	}
1566448Sache
1576448Sache	close(baz);
1586448Sache}
159