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)
13 * $FreeBSD: src/usr.sbin/mixer/mixer.c,v 1.11.2.6 2001/07/30 10:22:58 dd Exp $
14 * $DragonFly: src/usr.sbin/mixer/mixer.c,v 1.6 2004/04/15 12:58:12 joerg Exp $
23 #include <sys/soundcard.h>
25 #define LEFT(vol) (vol & 0x7f)
26 #define RIGHT(vol) ((vol >> 8) & 0x7f)
28 static const char *names
[SOUND_MIXER_NRDEVICES
] = SOUND_DEVICE_NAMES
;
29 static const char *defaultdev
= "/dev/mixer";
31 static void usage(int devmask
, int recmask
);
32 static int res_name(const char *name
, int mask
);
33 static void print_recsrc(int recsrc
);
34 static void print_recsrc_short(int recsrc
);
37 usage(int devmask
, int recmask
)
41 printf("usage: mixer [-f device] [-s] [dev [+|-][voll[:[+|-]volr]] ...\n"
42 " mixer [-f device] [-s] recsrc ...\n"
43 " mixer [-f device] [-s] {^|+|-|=}rec recdev ...\n"
46 for (i
= 0, n
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
47 if ((1 << i
) & devmask
) {
50 printf("%s", names
[i
]);
54 printf("\n rec devices: ");
55 for (i
= 0, n
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
56 if ((1 << i
) & recmask
) {
59 printf("%s", names
[i
]);
68 res_name(const char *name
, int mask
)
72 for (i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++)
73 if ((1 << i
) & mask
&& !strcmp(names
[i
], name
))
76 if (i
== SOUND_MIXER_NRDEVICES
)
83 print_recsrc(int recsrc
)
86 printf("Recording source: ");
88 for (i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
89 if ((1 << i
) & recsrc
) {
92 printf("%s", names
[i
]);
100 print_recsrc_short(int recsrc
)
106 for (i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
107 if ((1 << i
) & recsrc
) {
112 printf("%s ", names
[i
]);
118 main(int argc
, char **argv
)
120 int i
, mset
, fd
, dev
;
121 int devmask
= 0, recmask
= 0, recsrc
= 0, orecsrc
;
122 int dusage
= 0, drecsrc
= 0, shortflag
= 0;
123 int l
= 0, r
= 0, t
= 0;
124 int n
= 0, lrel
= 0, rrel
= 0;
125 char lstr
[8], rstr
[8];
128 const char *name
= defaultdev
;
130 while ((ch
= getopt(argc
, argv
, "f:sh")) != -1)
138 case 'h': /* Fall through */
145 if ((fd
= open(name
, O_RDWR
)) < 0)
147 if (ioctl(fd
, SOUND_MIXER_READ_DEVMASK
, &devmask
) == -1)
148 err(1, "SOUND_MIXER_READ_DEVMASK");
149 if (ioctl(fd
, SOUND_MIXER_READ_RECMASK
, &recmask
) == -1)
150 err(1, "SOUND_MIXER_READ_RECMASK");
151 if (ioctl(fd
, SOUND_MIXER_READ_RECSRC
, &recsrc
) == -1)
152 err(1, "SOUND_MIXER_READ_RECSRC");
157 usage(devmask
, recmask
); /* Does not return */
161 for (i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
162 if (!((1 << i
) & devmask
))
164 if (ioctl(fd
, MIXER_READ(i
),&mset
)== -1) {
169 printf("%s %d:%d ", names
[i
], LEFT(mset
),
172 printf("Mixer %-8s is currently set to %3d:%d\n",
173 names
[i
], LEFT(mset
), RIGHT(mset
));
175 if (ioctl(fd
, SOUND_MIXER_READ_RECSRC
, &recsrc
) == -1)
176 err(1, "SOUND_MIXER_READ_RECSRC");
178 print_recsrc_short(recsrc
);
179 if (isatty(STDOUT_FILENO
))
182 print_recsrc(recsrc
);
189 if (!strcmp("recsrc", *argv
)) {
193 } else if (argc
> 1 && !strcmp("rec", *argv
+ 1)) {
194 if (**argv
!= '+' && **argv
!= '-' &&
195 **argv
!= '=' && **argv
!= '^') {
196 warnx("unknown modifier: %c", **argv
);
200 if ((dev
= res_name(argv
[1], recmask
)) == -1) {
201 warnx("unknown recording device: %s", argv
[1]);
207 recsrc
|= (1 << dev
);
210 recsrc
&= ~(1 << dev
);
216 recsrc
^= (1 << dev
);
220 argc
-= 2; argv
+= 2;
224 if ((t
= sscanf(*argv
, "%d:%d", &l
, &r
)) > 0) {
227 else if((dev
= res_name(*argv
, devmask
)) == -1) {
228 warnx("unknown device: %s", *argv
);
233 #define issign(c) (((c) == '+') || ((c) == '-'))
236 n
= sscanf(argv
[1], "%7[^:]:%7s", lstr
, rstr
);
250 switch(argc
> 1 ? n
: t
) {
252 if (ioctl(fd
, MIXER_READ(dev
),&mset
)== -1) {
258 printf("%s %d:%d ", names
[dev
], LEFT(mset
),
261 printf("Mixer %-8s is currently set to %3d:%d\n",
262 names
[dev
], LEFT(mset
), RIGHT(mset
));
269 if (ioctl(fd
, MIXER_READ(dev
),&mset
)== -1) {
289 printf("Setting the mixer %s to %d:%d.\n", names
[dev
],
293 if (ioctl(fd
, MIXER_WRITE(dev
), &l
) == -1)
296 argc
-= 2; argv
+= 2;
301 if (orecsrc
!= recsrc
)
302 if (ioctl(fd
, SOUND_MIXER_WRITE_RECSRC
, &recsrc
) == -1)
303 err(1, "SOUND_MIXER_WRITE_RECSRC");
306 if (ioctl(fd
, SOUND_MIXER_READ_RECSRC
, &recsrc
) == -1)
307 err(1, "SOUND_MIXER_READ_RECSRC");
308 print_recsrc(recsrc
);