xref: /third_party/toybox/toys/other/mix.c (revision 0f66f451)
1/* mix.c - A very basic mixer.
2 *
3 * Copyright 2014 Brad Conroy, dedicated to the Public Domain.
4 *
5
6USE_MIX(NEWTOY(mix, "c:d:l#r#", TOYFLAG_USR|TOYFLAG_BIN))
7
8config 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
26GLOBALS(
27   long r, l;
28   char *d, *c;
29)
30
31void mix_main(void)
32{
33  const char *channels[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
34  int mask, channel = -1, level, fd;
35
36  if (!TT.d) TT.d = "/dev/mixer";
37  fd = xopen(TT.d, O_RDWR|O_NONBLOCK);
38  xioctl(fd, SOUND_MIXER_READ_DEVMASK, &mask);
39
40  for (channel = 0; channel < SOUND_MIXER_NRDEVICES; channel++) {
41    if ((1<<channel) & mask) {
42      if (TT.c) {
43        if (!strcmp(channels[channel], TT.c)) break;
44      } else if (toys.optflags & FLAG_l) break;
45      else printf("%s\n", channels[channel]);
46    }
47  }
48
49  if (!(toys.optflags & (FLAG_c|FLAG_l))) return;
50  else if (channel == SOUND_MIXER_NRDEVICES) error_exit("bad -c '%s'", TT.c);
51
52  if (!(toys.optflags & FLAG_l)) {
53    xioctl(fd, MIXER_READ(channel), &level);
54    if (level > 0xFF)
55      xprintf("%s:%s = left:%d\t right:%d\n",
56              TT.d, channels[channel], level>>8, level & 0xFF);
57    else xprintf("%s:%s = %d\n", TT.d, channels[channel], level);
58  } else {
59    level = TT.l;
60    if (!(toys.optflags & FLAG_r)) level = TT.r | (level<<8);
61
62    xioctl(fd, MIXER_WRITE(channel), &level);
63  }
64
65  if (CFG_TOYBOX_FREE) close(fd);
66}
67