1 /* mix.c - A very basic mixer.
2  *
3  * Copyright 2014 Brad Conroy, dedicated to the Public Domain.
4  *
5 
6 USE_MIX(NEWTOY(mix, "c:d:l#r#", TOYFLAG_USR|TOYFLAG_BIN))
7 
8 config MIX
9   bool "mix"
10   default y
11   help
12    usage: mix [-d DEV] [-c CHANNEL] [-l VOL] [-r RIGHT]
13 
14    List OSS sound channels (module snd-mixer-oss), or set volume(s).
15 
16    -c CHANNEL	Set/show volume of CHANNEL (default first channel found)
17    -d DEV		Device node (default /dev/mixer)
18    -l VOL		Volume level
19    -r RIGHT	Volume of right stereo channel (with -r, -l sets left volume)
20 */
21 
22 #define FOR_mix
23 #include "toys.h"
24 #include <linux/soundcard.h>
25 
GLOBALS(long right;long level;char * dev;char * chan;)26 GLOBALS(
27    long right;
28    long level;
29    char *dev;
30    char *chan;
31 )
32 
33 void mix_main(void)
34 {
35   const char *channels[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
36   int mask, channel = -1, level, fd;
37 
38   if (!TT.dev) TT.dev = "/dev/mixer";
39   fd = xopen(TT.dev, O_RDWR|O_NONBLOCK);
40   xioctl(fd, SOUND_MIXER_READ_DEVMASK, &mask);
41 
42   for (channel = 0; channel < SOUND_MIXER_NRDEVICES; channel++) {
43     if ((1<<channel) & mask) {
44       if (TT.chan) {
45         if (!strcmp(channels[channel], TT.chan)) break;
46       } else if (toys.optflags & FLAG_l) break;
47       else printf("%s\n", channels[channel]);
48     }
49   }
50 
51   if (!(toys.optflags & (FLAG_c|FLAG_l))) return;
52   else if (channel == SOUND_MIXER_NRDEVICES) error_exit("bad -c '%s'", TT.chan);
53 
54   if (!(toys.optflags & FLAG_l)) {
55     xioctl(fd, MIXER_READ(channel), &level);
56     if (level > 0xFF)
57       xprintf("%s:%s = left:%d\t right:%d\n",
58               TT.dev, channels[channel], level>>8, level & 0xFF);
59     else xprintf("%s:%s = %d\n", TT.dev, channels[channel], level);
60   } else {
61     level = TT.level;
62     if (!(toys.optflags & FLAG_r)) level = TT.right | (level<<8);
63 
64     xioctl(fd, MIXER_WRITE(channel), &level);
65   }
66 
67   if (CFG_TOYBOX_FREE) close(fd);
68 }
69