diff --git a/usr.sbin/mixer/mixer.8 b/usr.sbin/mixer/mixer.8 index db0ec5f23a0c..75c6a81e3a55 100644 --- a/usr.sbin/mixer/mixer.8 +++ b/usr.sbin/mixer/mixer.8 @@ -1,285 +1,288 @@ .\"- .\" Copyright (c) 2021 Christos Margiolis .\" .\" Permission is hereby granted, free of charge, to any person obtaining a copy .\" of this software and associated documentation files (the "Software"), to deal .\" in the Software without restriction, including without limitation the rights .\" to use, copy, modify, merge, publish, distribute, sublicense, and/or sell .\" copies of the Software, and to permit persons to whom the Software is .\" furnished to do so, subject to the following conditions: .\" .\" The above copyright notice and this permission notice shall be included in .\" all copies or substantial portions of the Software. .\" .\" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR .\" IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, .\" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE .\" AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER .\" LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, .\" OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN .\" THE SOFTWARE. .\" .Dd February 8, 2024 .Dt MIXER 8 .Os .Sh NAME .Nm mixer .Nd manipulate soundcard mixer controls .Sh SYNOPSIS .Nm .Op Fl f Ar device .Op Fl d Ar pcmN | N .Op Fl os .Op Ar dev Ns Op Cm \&. Ns Ar control Ns Op Cm \&= Ns Ar value .Ar ... .Nm .Op Fl os .Fl a .Nm .Fl h .Sh DESCRIPTION The .Nm utility is used to set and display soundcard mixer device controls. .Pp The options are as follows: .Bl -tag -width "-d pcmN | N" .It Fl a Print the values for all mixer devices available in the system .Pq see Sx FILES . .It Fl d Ar pcmN | N Change the default audio card to .Ar pcmN , where N is the unit number (e.g for pcm0, the unit number is 0). See .Sx EXAMPLES on how to list all available audio devices in the system. .It Fl f Ar device Open .Ar device as the mixer device .Pq see Sx FILES . .It Fl h Print a help message. .It Fl o Print mixer values in a format suitable for use inside scripts. The mixer's header (name, audio card name, ...) will not be printed. .It Fl s Print only the recording source(s) of the mixer device. .El .Pp The list of mixer devices that may be modified are: .Bd -ragged -offset indent .Cm vol , bass , treble , synth , pcm , speaker , line , mic , cd , mix , .Cm pcm2 , rec , .Cm igain , ogain , .Cm line1 , line2 , line3 , .Cm dig1 , dig2 , dig3 , .Cm phin , phout , video , radio , and .Cm monitor . .Ed .Pp Not all mixer devices are available. .Pp Without any arguments, .Nm displays all information for each one of the mixer's supported devices to .Ar stdout . If the .Ar dev argument is specified, .Nm displays only the values for .Ar dev . More than one device may be specified. .Pp Commands use the following format: .Bl -column xxxxxxxxxxxxxxxxxxxxxxxx -offset indent .It Sy Name Ta Sy Action .It Ar dev Ta Display all controls .It Ar dev Ns Cm \&. Ns Ar control Ta Display only the specified control .It Ar dev Ns Cm \&. Ns Ar control Ns Cm \&= Ns Ar value Ta Set control value .El .Pp The available controls are as follows (replace .Ar dev with one of the available devices): .Sm off .Bl -column xxxxxxxxxxxxxxxxxxxxxxxx -offset indent .It Sy Name Ta Sy Value .It Ar dev Cm .volume Ta Xo .Ar vol | .Oo Cm \&+ | Cm \&- Oc Ar lvol Oo % Oc .Oo Cm \&: Oo Cm \&+ | Cm \&- Oc Ar rvol Oo % Oc Oc .Xc .It Ar dev Cm .mute Ta Cm 0 | 1 | ^ +.It Ar dev Cm .mute Ta Cm off | on | toggle .It Ar dev Cm .recsrc Ta Cm ^ | + | - | = +.It Ar dev Cm .recsrc Ta Cm toggle | add | remove | set .El .Sm on .Pp The .Ar dev Ns Cm .volume control modifies a device's volume. The optional .Ar lvol and/or .Ar rvol values have to be specified. The values should typically be decimal numbers between 0 and 1 with at most 2 digits after the decimal point. A trailing percent sign indicates that the value should be treated as a percentage of 1.0, rather than an absolute value. Thus, 70% means the same as 0.7. If the left or right volume values are prefixed with .Cm + or .Cm - , the value following will be used as a relative adjustment, modifying the current settings by the amount specified. Note that relative percentages are still relative to 1.0, not to the current value. If the volume is currently 0.40 and an adjustment of +20% is specified, then thet final volume will be set to 0.60. .Pp Volume can also be set using the shorthand .Ar dev Ns Cm =value . This syntax does not apply to other controls. .Pp The .Ar dev Ns Cm .mute control (un)mutes a device. The following values are available: -.Bl -tag -width = -offset indent -.It Cm 0 +.Bl -tag -width "xxxxxxxxxx" -offset indent +.It Cm 0 | off unmutes .Ar dev -.It Cm 1 +.It Cm 1 | on mutes .Ar dev -.It Cm ^ +.It Cm ^ | toggle toggles the mute of .Ar dev .El .Pp The .Ar dev Ns Cm .recsrc control modifies the recording sources of a mixer. .Nm marks devices which can be used as a recording source with .Sy rec . Recording sources are marked with .Sy src . To modify the recording source you can use one of the following modifiers on a .Sy rec device: -.Bl -tag -width = -offset indent -.It Cm ^ +.Bl -tag -width "xxxxxxxxxx" -offset indent +.It Cm ^ | toggle toggles .Ar dev of possible recording devices -.It Cm + +.It Cm + | add adds .Ar dev to possible recording devices -.It Cm - +.It Cm - | remove removes .Ar dev from possible recording devices -.It Cm = -sets the recording device to +.It Cm = | set +makes .Ar dev +the only recording device. .El .Sh FILES .Bl -tag -width /dev/mixerN -compact .It Pa /dev/mixerN The mixer device, where .Ar N is the number of that device, for example .Ar /dev/mixer0 . PCM cards and mixers have a 1:1 relationship, which means that .Pa /dev/mixer0 is the mixer for .Pa /dev/pcm0 and so on. By default, .Nm prints both the audio card's number and the mixer associated with it in the form of .Ar pcmN:mixer . The .Pa /dev/mixer file, although it does not exist in the filesystem, points to the default mixer device and is the file .Nm opens when the .Fl f Ar device option has not been specified. .El .Sh EXAMPLES List all available audio devices in the system: .Bd -literal -offset indent $ mixer -a | grep ^pcm .Ed .Pp Increase the .Cm vol device's volume by 5%: .Bd -literal -offset indent $ mixer vol=+5% .Ed .Pp Change the volume for the .Cm vol device of the .Pa /dev/mixer0 mixer device to 0.65: .Bd -literal -offset indent $ mixer -f /dev/mixer0 vol.volume=0.65 .Ed .Pp Increase the .Cm mic device's left volume by 0.10 and decrease the right volume by 0.05: .Bd -literal -offset indent $ mixer mic.volume=+0.10:-0.05 .Ed .Pp Toggle the mute for .Cm vol : .Bd -literal -offset indent -$ mixer vol.mute=^ +$ mixer vol.mute=toggle .Ed .Pp -Set +Add .Cm mic -and toggle +and remove .Cm line -recording sources: +from the recording devices: .Bd -literal -offset indent -$ mixer mic.recsrc=+ line.recsrc=^ +$ mixer mic.recsrc=add line.recsrc=remove .Ed .Pp Dump .Pa /dev/mixer0 information to a file and retrieve back later: .Bd -literal -offset indent $ mixer -f /dev/mixer0 -o > info \&... $ mixer -f /dev/mixer0 `cat info` .Ed .Sh SEE ALSO .Xr mixer 3 , .Xr sound 4 , .Xr sysctl 8 .Sh HISTORY The .Nm utility first appeared in .Fx 2.0.5 and was rewritten completely in .Fx 14.0 . .Sh AUTHORS .An Christos Margiolis Aq Mt christos@FreeBSD.org diff --git a/usr.sbin/mixer/mixer.c b/usr.sbin/mixer/mixer.c index 0c0c37ccb2bc..83e97df19116 100644 --- a/usr.sbin/mixer/mixer.c +++ b/usr.sbin/mixer/mixer.c @@ -1,512 +1,508 @@ /*- * Copyright (c) 2021 Christos Margiolis * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include #include #include #include #include #include #include enum { C_VOL = 0, C_MUT, C_SRC, }; static void usage(void) __dead2; static void initctls(struct mixer *); static void printall(struct mixer *, int); static void printminfo(struct mixer *, int); static void printdev(struct mixer *, int); static void printrecsrc(struct mixer *, int); /* XXX: change name */ static int set_dunit(struct mixer *, int); /* Control handlers */ static int mod_volume(struct mix_dev *, void *); static int mod_mute(struct mix_dev *, void *); static int mod_recsrc(struct mix_dev *, void *); static int print_volume(struct mix_dev *, void *); static int print_mute(struct mix_dev *, void *); static int print_recsrc(struct mix_dev *, void *); int main(int argc, char *argv[]) { struct mixer *m; mix_ctl_t *cp; char *name = NULL, buf[NAME_MAX]; char *p, *q, *devstr, *ctlstr, *valstr = NULL; int dunit, i, n, pall = 1, shorthand; int aflag = 0, dflag = 0, oflag = 0, sflag = 0; int ch; while ((ch = getopt(argc, argv, "ad:f:hos")) != -1) { switch (ch) { case 'a': aflag = 1; break; case 'd': if (strncmp(optarg, "pcm", 3) == 0) optarg += 3; errno = 0; dunit = strtol(optarg, NULL, 10); if (errno == EINVAL || errno == ERANGE) err(1, "strtol(%s)", optarg); dflag = 1; break; case 'f': name = optarg; break; case 'o': oflag = 1; break; case 's': sflag = 1; break; case 'h': /* FALLTHROUGH */ case '?': default: usage(); } } argc -= optind; argv += optind; /* Print all mixers and exit. */ if (aflag) { if ((n = mixer_get_nmixers()) < 0) errx(1, "no mixers present in the system"); for (i = 0; i < n; i++) { (void)snprintf(buf, sizeof(buf), "/dev/mixer%d", i); if ((m = mixer_open(buf)) == NULL) errx(1, "%s: no such mixer", buf); initctls(m); if (sflag) printrecsrc(m, oflag); else { printall(m, oflag); if (oflag) printf("\n"); } (void)mixer_close(m); } return (0); } if ((m = mixer_open(name)) == NULL) errx(1, "%s: no such mixer", name); initctls(m); if (dflag && set_dunit(m, dunit) < 0) goto parse; if (sflag) { printrecsrc(m, oflag); (void)mixer_close(m); return (0); } parse: while (argc > 0) { if ((p = strdup(*argv)) == NULL) err(1, "strdup(%s)", *argv); /* Check if we're using the shorthand syntax for volume setting. */ shorthand = 0; for (q = p; *q != '\0'; q++) { if (*q == '=') { q++; shorthand = ((*q >= '0' && *q <= '9') || *q == '+' || *q == '-' || *q == '.'); break; } else if (*q == '.') break; } /* Split the string into device, control and value. */ devstr = strsep(&p, ".="); if ((m->dev = mixer_get_dev_byname(m, devstr)) == NULL) { warnx("%s: no such device", devstr); goto next; } /* Input: `dev`. */ if (p == NULL) { printdev(m, 1); pall = 0; goto next; } else if (shorthand) { /* * Input: `dev=N` -> shorthand for `dev.volume=N`. * * We don't care what the rest of the string contains as * long as we're sure the very beginning is right, * mod_volume() will take care of parsing it properly. */ cp = mixer_get_ctl(m->dev, C_VOL); cp->mod(cp->parent_dev, p); goto next; } ctlstr = strsep(&p, "="); if ((cp = mixer_get_ctl_byname(m->dev, ctlstr)) == NULL) { warnx("%s.%s: no such control", devstr, ctlstr); goto next; } /* Input: `dev.control`. */ if (p == NULL) { (void)cp->print(cp->parent_dev, cp->name); pall = 0; goto next; } valstr = p; /* Input: `dev.control=val`. */ cp->mod(cp->parent_dev, valstr); next: free(p); argc--; argv++; } if (pall) printall(m, oflag); (void)mixer_close(m); return (0); } static void __dead2 usage(void) { fprintf(stderr, "usage: %1$s [-f device] [-d pcmN | N] [-os] [dev[.control[=value]]] ...\n" " %1$s [-os] -a\n" " %1$s -h\n", getprogname()); exit(1); } static void initctls(struct mixer *m) { struct mix_dev *dp; int rc = 0; TAILQ_FOREACH(dp, &m->devs, devs) { rc += mixer_add_ctl(dp, C_VOL, "volume", mod_volume, print_volume); rc += mixer_add_ctl(dp, C_MUT, "mute", mod_mute, print_mute); rc += mixer_add_ctl(dp, C_SRC, "recsrc", mod_recsrc, print_recsrc); } if (rc) { (void)mixer_close(m); errx(1, "cannot make mixer controls"); } } static void printall(struct mixer *m, int oflag) { struct mix_dev *dp; printminfo(m, oflag); TAILQ_FOREACH(dp, &m->devs, devs) { m->dev = dp; printdev(m, oflag); } } static void printminfo(struct mixer *m, int oflag) { int playrec = MIX_MODE_PLAY | MIX_MODE_REC; if (oflag) return; printf("%s:", m->mi.name); if (*m->ci.longname != '\0') printf(" <%s>", m->ci.longname); if (*m->ci.hw_info != '\0') printf(" %s", m->ci.hw_info); if (m->mode != 0) printf(" ("); if (m->mode & MIX_MODE_PLAY) printf("play"); if ((m->mode & playrec) == playrec) printf("/"); if (m->mode & MIX_MODE_REC) printf("rec"); if (m->mode != 0) printf(")"); if (m->f_default) printf(" (default)"); printf("\n"); } static void printdev(struct mixer *m, int oflag) { struct mix_dev *d = m->dev; mix_ctl_t *cp; if (!oflag) { printf(" %-10s= %.2f:%.2f ", d->name, d->vol.left, d->vol.right); if (!MIX_ISREC(m, d->devno)) printf(" pbk"); if (MIX_ISREC(m, d->devno)) printf(" rec"); if (MIX_ISRECSRC(m, d->devno)) printf(" src"); if (MIX_ISMUTE(m, d->devno)) printf(" mute"); printf("\n"); } else { TAILQ_FOREACH(cp, &d->ctls, ctls) { (void)cp->print(cp->parent_dev, cp->name); } } } static void printrecsrc(struct mixer *m, int oflag) { struct mix_dev *dp; int n = 0; if (!m->recmask) return; if (!oflag) printf("%s: ", m->mi.name); TAILQ_FOREACH(dp, &m->devs, devs) { if (MIX_ISRECSRC(m, dp->devno)) { if (n++ && !oflag) printf(", "); printf("%s", dp->name); if (oflag) printf(".%s=+%s", mixer_get_ctl(dp, C_SRC)->name, n ? " " : ""); } } printf("\n"); } static int set_dunit(struct mixer *m, int dunit) { int n; if ((n = mixer_get_dunit()) < 0) { warn("cannot get default unit"); return (-1); } if (mixer_set_dunit(m, dunit) < 0) { warn("cannot set default unit to %d", dunit); return (-1); } printf("default_unit: %d -> %d\n", n, dunit); return (0); } static int mod_volume(struct mix_dev *d, void *p) { struct mixer *m; mix_ctl_t *cp; mix_volume_t v; const char *val; char *endp, lstr[8], rstr[8]; float lprev, rprev, lrel, rrel; int n; m = d->parent_mixer; cp = mixer_get_ctl(m->dev, C_VOL); val = p; n = sscanf(val, "%7[^:]:%7s", lstr, rstr); if (n == EOF) { warnx("invalid volume value: %s", val); return (-1); } lrel = rrel = 0; if (n > 0) { if (*lstr == '+' || *lstr == '-') lrel = 1; v.left = strtof(lstr, &endp); if (*endp != '\0' && (*endp != '%' || *(endp + 1) != '\0')) { warnx("invalid volume value: %s", lstr); return (-1); } if (*endp == '%') v.left /= 100.0f; } if (n > 1) { if (*rstr == '+' || *rstr == '-') rrel = 1; v.right = strtof(rstr, &endp); if (*endp != '\0' && (*endp != '%' || *(endp + 1) != '\0')) { warnx("invalid volume value: %s", rstr); return (-1); } if (*endp == '%') v.right /= 100.0f; } switch (n) { case 1: v.right = v.left; /* FALLTHROUGH */ rrel = lrel; case 2: if (lrel) v.left += m->dev->vol.left; if (rrel) v.right += m->dev->vol.right; if (v.left < MIX_VOLMIN) v.left = MIX_VOLMIN; else if (v.left > MIX_VOLMAX) v.left = MIX_VOLMAX; if (v.right < MIX_VOLMIN) v.right = MIX_VOLMIN; else if (v.right > MIX_VOLMAX) v.right = MIX_VOLMAX; lprev = m->dev->vol.left; rprev = m->dev->vol.right; if (mixer_set_vol(m, v) < 0) warn("%s.%s=%.2f:%.2f", m->dev->name, cp->name, v.left, v.right); else printf("%s.%s: %.2f:%.2f -> %.2f:%.2f\n", m->dev->name, cp->name, lprev, rprev, v.left, v.right); } return (0); } static int mod_mute(struct mix_dev *d, void *p) { struct mixer *m; mix_ctl_t *cp; const char *val; int n, opt = -1; m = d->parent_mixer; cp = mixer_get_ctl(m->dev, C_MUT); val = p; - switch (*val) { - case '0': + if (strncmp(val, "off", strlen(val)) == 0 || *val == '0') opt = MIX_UNMUTE; - break; - case '1': + else if (strncmp(val, "on", strlen(val)) == 0 || *val == '1') opt = MIX_MUTE; - break; - case '^': + else if (strncmp(val, "toggle", strlen(val)) == 0 || *val == '^') opt = MIX_TOGGLEMUTE; - break; - default: - warnx("%c: no such modifier", *val); + else { + warnx("%s: no such modifier", val); return (-1); } n = MIX_ISMUTE(m, m->dev->devno); if (mixer_set_mute(m, opt) < 0) - warn("%s.%s=%c", m->dev->name, cp->name, *val); + warn("%s.%s=%s", m->dev->name, cp->name, val); else - printf("%s.%s: %d -> %d\n", - m->dev->name, cp->name, n, MIX_ISMUTE(m, m->dev->devno)); + printf("%s.%s: %s -> %s\n", + m->dev->name, cp->name, + n ? "on" : "off", + MIX_ISMUTE(m, m->dev->devno) ? "on" : "off"); return (0); } static int mod_recsrc(struct mix_dev *d, void *p) { struct mixer *m; mix_ctl_t *cp; const char *val; int n, opt = -1; m = d->parent_mixer; cp = mixer_get_ctl(m->dev, C_SRC); val = p; - switch (*val) { - case '+': + if (strncmp(val, "add", strlen(val)) == 0 || *val == '+') opt = MIX_ADDRECSRC; - break; - case '-': + else if (strncmp(val, "remove", strlen(val)) == 0 || *val == '-') opt = MIX_REMOVERECSRC; - break; - case '=': + else if (strncmp(val, "set", strlen(val)) == 0 || *val == '=') opt = MIX_SETRECSRC; - break; - case '^': + else if (strncmp(val, "toggle", strlen(val)) == 0 || *val == '^') opt = MIX_TOGGLERECSRC; - break; - default: - warnx("%c: no such modifier", *val); + else { + warnx("%s: no such modifier", val); return (-1); } n = MIX_ISRECSRC(m, m->dev->devno); if (mixer_mod_recsrc(m, opt) < 0) - warn("%s.%s=%c", m->dev->name, cp->name, *val); + warn("%s.%s=%s", m->dev->name, cp->name, val); else - printf("%s.%s: %d -> %d\n", - m->dev->name, cp->name, n, MIX_ISRECSRC(m, m->dev->devno)); + printf("%s.%s: %s -> %s\n", + m->dev->name, cp->name, + n ? "add" : "remove", + MIX_ISRECSRC(m, m->dev->devno) ? "add" : "remove"); return (0); } static int print_volume(struct mix_dev *d, void *p) { struct mixer *m = d->parent_mixer; const char *ctl_name = p; printf("%s.%s=%.2f:%.2f\n", m->dev->name, ctl_name, m->dev->vol.left, m->dev->vol.right); return (0); } static int print_mute(struct mix_dev *d, void *p) { struct mixer *m = d->parent_mixer; const char *ctl_name = p; - printf("%s.%s=%d\n", m->dev->name, ctl_name, MIX_ISMUTE(m, m->dev->devno)); + printf("%s.%s=%s\n", m->dev->name, ctl_name, + MIX_ISMUTE(m, m->dev->devno) ? "on" : "off"); return (0); } static int print_recsrc(struct mix_dev *d, void *p) { struct mixer *m = d->parent_mixer; const char *ctl_name = p; if (!MIX_ISRECSRC(m, m->dev->devno)) return (-1); - printf("%s.%s=+\n", m->dev->name, ctl_name); + printf("%s.%s=add\n", m->dev->name, ctl_name); return (0); }