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: head/usr.sbin/mixer/mixer.c 230611 2012-01-27 09:15:55Z mav $
24 #include <sys/soundcard.h>
26 static const char *names
[SOUND_MIXER_NRDEVICES
] = SOUND_DEVICE_NAMES
;
28 static void usage(int devmask
, int recmask
);
29 static int res_name(const char *name
, int mask
);
30 static void print_recsrc(int recsrc
, int recmask
, int sflag
);
33 usage(int devmask
, int recmask
)
37 printf("usage: mixer [-f device] [-s | -S] [dev [+|-][voll[:[+|-]volr]] ...\n"
38 " mixer [-f device] [-s | -S] recsrc ...\n"
39 " mixer [-f device] [-s | -S] {^|+|-|=}rec rdev ...\n");
42 for (i
= 0, n
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
43 if ((1 << i
) & devmask
) {
46 printf("%s", names
[i
]);
52 printf("\n rec devices: ");
53 for (i
= 0, n
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
54 if ((1 << i
) & recmask
) {
57 printf("%s", names
[i
]);
67 res_name(const char *name
, int mask
)
71 for (i
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++)
72 if ((1 << i
) & mask
&& strcmp(names
[i
], name
) == 0)
75 if (i
== SOUND_MIXER_NRDEVICES
)
82 print_recsrc(int recsrc
, int recmask
, int sflag
)
90 printf("Recording source: ");
92 for (i
= 0, n
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
93 if ((1 << i
) & recsrc
) {
95 printf("%srec ", n
? " +" : "=");
98 printf("%s", names
[i
]);
107 main(int argc
, char *argv
[])
109 char mixer
[PATH_MAX
] = "/dev/mixer";
110 char lstr
[8], rstr
[8];
112 int devmask
= 0, recmask
= 0, recsrc
= 0, orecsrc
;
113 int dusage
= 0, drecsrc
= 0, sflag
= 0, Sflag
= 0;
114 int l
, r
, lrel
, rrel
;
115 int ch
, i
, bar
, baz
, dev
, m
, n
, t
;
117 if ((name
= strdup(basename(argv
[0]))) == NULL
)
119 if (strncmp(name
, "mixer", 5) == 0 && name
[5] != '\0') {
120 n
= strtol(name
+ 5, &eptr
, 10) - 1;
121 if (n
> 0 && *eptr
== '\0')
122 snprintf(mixer
, PATH_MAX
- 1, "/dev/mixer%d", n
);
129 if (n
>= argc
|| *argv
[n
] != '-')
131 if (strlen(argv
[n
]) != 2) {
132 if (strcmp(argv
[n
] + 1, "rec") != 0)
137 if (ch
== 'f' && n
< argc
- 1) {
140 } else if (ch
== 's') {
143 } else if (ch
== 'S') {
157 if ((baz
= open(name
, O_RDWR
)) < 0)
159 if (ioctl(baz
, SOUND_MIXER_READ_DEVMASK
, &devmask
) == -1)
160 err(1, "SOUND_MIXER_READ_DEVMASK");
161 if (ioctl(baz
, SOUND_MIXER_READ_RECMASK
, &recmask
) == -1)
162 err(1, "SOUND_MIXER_READ_RECMASK");
163 if (ioctl(baz
, SOUND_MIXER_READ_RECSRC
, &recsrc
) == -1)
164 err(1, "SOUND_MIXER_READ_RECSRC");
167 if (argc
== 1 && dusage
== 0) {
168 for (i
= 0, n
= 0; i
< SOUND_MIXER_NRDEVICES
; i
++) {
169 if (!((1 << i
) & devmask
))
171 if (ioctl(baz
, MIXER_READ(i
),&bar
) == -1) {
175 if (Sflag
|| sflag
) {
176 printf("%s%s%c%d:%d", n
? " " : "",
177 names
[i
], Sflag
? ':' : ' ',
178 bar
& 0x7f, (bar
>> 8) & 0x7f);
181 printf("Mixer %-8s is currently set to "
182 "%3d:%d\n", names
[i
], bar
& 0x7f,
187 print_recsrc(recsrc
, recmask
, Sflag
|| sflag
);
195 while (argc
> 0 && dusage
== 0) {
196 if (strcmp("recsrc", *argv
) == 0) {
201 } else if (strcmp("rec", *argv
+ 1) == 0) {
202 if (**argv
!= '+' && **argv
!= '-' &&
203 **argv
!= '=' && **argv
!= '^') {
204 warnx("unknown modifier: %c", **argv
);
209 warnx("no recording device specified");
213 if ((dev
= res_name(argv
[1], recmask
)) == -1) {
214 warnx("unknown recording device: %s", argv
[1]);
220 recsrc
|= (1 << dev
);
223 recsrc
&= ~(1 << dev
);
229 recsrc
^= (1 << dev
);
238 if ((t
= sscanf(*argv
, "%d:%d", &l
, &r
)) > 0)
240 else if ((dev
= res_name(*argv
, devmask
)) == -1) {
241 warnx("unknown device: %s", *argv
);
248 m
= sscanf(argv
[1], "%7[^:]:%7s", lstr
, rstr
);
250 if (*lstr
== '+' || *lstr
== '-')
252 l
= strtol(lstr
, NULL
, 10);
255 if (*rstr
== '+' || *rstr
== '-')
257 r
= strtol(rstr
, NULL
, 10);
261 switch (argc
> 1 ? m
: t
) {
263 if (ioctl(baz
, MIXER_READ(dev
), &bar
) == -1) {
269 if (Sflag
|| sflag
) {
270 printf("%s%s%c%d:%d", n
? " " : "",
271 names
[dev
], Sflag
? ':' : ' ',
272 bar
& 0x7f, (bar
>> 8) & 0x7f);
275 printf("Mixer %-8s is currently set to "
276 "%3d:%d\n", names
[dev
], bar
& 0x7f,
286 if (ioctl(baz
, MIXER_READ(dev
), &bar
) == -1) {
294 l
= (bar
& 0x7f) + l
;
296 r
= ((bar
>> 8) & 0x7f) + r
;
308 printf("Setting the mixer %s from %d:%d to "
309 "%d:%d.\n", names
[dev
], bar
& 0x7f,
310 (bar
>> 8) & 0x7f, l
, r
);
313 if (ioctl(baz
, MIXER_WRITE(dev
), &l
) == -1)
324 usage(devmask
, recmask
);
328 if (orecsrc
!= recsrc
) {
329 if (ioctl(baz
, SOUND_MIXER_WRITE_RECSRC
, &recsrc
) == -1)
330 err(1, "SOUND_MIXER_WRITE_RECSRC");
331 if (ioctl(baz
, SOUND_MIXER_READ_RECSRC
, &recsrc
) == -1)
332 err(1, "SOUND_MIXER_READ_RECSRC");
336 print_recsrc(recsrc
, recmask
, Sflag
|| sflag
);