aboutsummaryrefslogtreecommitdiff
path: root/lib/libmixer
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libmixer')
-rw-r--r--lib/libmixer/Makefile32
-rw-r--r--lib/libmixer/Makefile.depend16
-rw-r--r--lib/libmixer/Symbol.map25
-rw-r--r--lib/libmixer/mixer.3590
-rw-r--r--lib/libmixer/mixer.c520
-rw-r--r--lib/libmixer/mixer.h122
6 files changed, 1305 insertions, 0 deletions
diff --git a/lib/libmixer/Makefile b/lib/libmixer/Makefile
new file mode 100644
index 000000000000..a0bb37a85fb8
--- /dev/null
+++ b/lib/libmixer/Makefile
@@ -0,0 +1,32 @@
+LIB= mixer
+SRCS= ${LIB}.c
+INCS= ${LIB}.h
+MAN= ${LIB}.3
+VERSION_DEF= ${LIBCSRCDIR}/Versions.def
+SYMBOL_MAPS= ${.CURDIR}/Symbol.map
+
+MLINKS+= mixer.3 mixer_open.3
+MLINKS+= mixer.3 mixer_close.3
+MLINKS+= mixer.3 mixer_get_dev.3
+MLINKS+= mixer.3 mixer_get_dev_byname.3
+MLINKS+= mixer.3 mixer_add_ctl.3
+MLINKS+= mixer.3 mixer_add_ctl_s.3
+MLINKS+= mixer.3 mixer_remove_ctl.3
+MLINKS+= mixer.3 mixer_get_ctl.3
+MLINKS+= mixer.3 mixer_get_ctl_byname.3
+MLINKS+= mixer.3 mixer_set_vol.3
+MLINKS+= mixer.3 mixer_set_mute.3
+MLINKS+= mixer.3 mixer_mod_recsrc.3
+MLINKS+= mixer.3 mixer_get_dunit.3
+MLINKS+= mixer.3 mixer_set_dunit.3
+MLINKS+= mixer.3 mixer_get_mode.3
+MLINKS+= mixer.3 mixer_get_nmixers.3
+MLINKS+= mixer.3 mixer_get_path.3
+MLINKS+= mixer.3 MIX_ISDEV.3
+MLINKS+= mixer.3 MIX_ISMUTE.3
+MLINKS+= mixer.3 MIX_ISREC.3
+MLINKS+= mixer.3 MIX_ISRECSRC.3
+MLINKS+= mixer.3 MIX_VOLNORM.3
+MLINKS+= mixer.3 MIX_VOLDENORM.3
+
+.include <bsd.lib.mk>
diff --git a/lib/libmixer/Makefile.depend b/lib/libmixer/Makefile.depend
new file mode 100644
index 000000000000..40575a605e97
--- /dev/null
+++ b/lib/libmixer/Makefile.depend
@@ -0,0 +1,16 @@
+# Autogenerated - do NOT edit!
+
+DIRDEPS = \
+ include \
+ include/xlocale \
+ lib/${CSU_DIR} \
+ lib/libc \
+ lib/libcompiler_rt \
+ usr.bin/awk.host \
+
+
+.include <dirdeps.mk>
+
+.if ${DEP_RELDIR} == ${_DEP_RELDIR}
+# local dependencies - needed for -jN in clean tree
+.endif
diff --git a/lib/libmixer/Symbol.map b/lib/libmixer/Symbol.map
new file mode 100644
index 000000000000..2ce39fd058ab
--- /dev/null
+++ b/lib/libmixer/Symbol.map
@@ -0,0 +1,25 @@
+/*
+ */
+
+FBSD_1.7 {
+ mixer_open;
+ mixer_close;
+ mixer_get_dev;
+ mixer_get_dev_byname;
+ mixer_add_ctl;
+ mixer_add_ctl_s;
+ mixer_remove_ctl;
+ mixer_get_ctl;
+ mixer_get_ctl_byname;
+ mixer_set_vol;
+ mixer_set_mute;
+ mixer_mod_recsrc;
+ mixer_get_dunit;
+ mixer_set_dunit;
+ mixer_get_mode;
+ mixer_get_nmixers;
+};
+
+FBSD_1.8 {
+ mixer_get_path;
+};
diff --git a/lib/libmixer/mixer.3 b/lib/libmixer/mixer.3
new file mode 100644
index 000000000000..04aa3bd1dca7
--- /dev/null
+++ b/lib/libmixer/mixer.3
@@ -0,0 +1,590 @@
+.\"-
+.\" Copyright (c) 2021-2022 Christos Margiolis <christos@FreeBSD.org>
+.\"
+.\" 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 August 4, 2024
+.Dt MIXER 3
+.Os
+.Sh NAME
+.Nm mixer_open ,
+.Nm mixer_close ,
+.Nm mixer_get_dev ,
+.Nm mixer_get_dev_byname ,
+.Nm mixer_add_ctl ,
+.Nm mixer_add_ctl_s ,
+.Nm mixer_remove_ctl ,
+.Nm mixer_get_ctl ,
+.Nm mixer_get_ctl_byname ,
+.Nm mixer_set_vol ,
+.Nm mixer_set_mute ,
+.Nm mixer_mod_recsrc ,
+.Nm mixer_get_dunit ,
+.Nm mixer_set_dunit ,
+.Nm mixer_get_mode ,
+.Nm mixer_get_nmixers ,
+.Nm mixer_get_path ,
+.Nm MIX_ISDEV ,
+.Nm MIX_ISMUTE ,
+.Nm MIX_ISREC ,
+.Nm MIX_ISRECSRC ,
+.Nm MIX_VOLNORM ,
+.Nm MIX_VOLDENORM
+.Nd interface to OSS mixers
+.Sh LIBRARY
+Mixer library (libmixer, -lmixer)
+.Sh SYNOPSIS
+.In mixer.h
+.Ft struct mixer *
+.Fn mixer_open "const char *name"
+.Ft int
+.Fn mixer_close "struct mixer *m"
+.Ft struct mix_dev *
+.Fn mixer_get_dev "struct mixer *m" "int devno"
+.Ft struct mix_dev *
+.Fn mixer_get_dev_byname "struct mixer *m" "name"
+.Ft int
+.Fn mixer_add_ctl "struct mix_dev *parent" "int id" "const char *name" \
+ "int (*mod)(struct mix_dev *d, void *p)" \
+ "int (*print)(struct mix_dev *d, void *p)"
+.Ft int
+.Fn mixer_add_ctl_s "mix_ctl_t *ctl"
+.Ft int
+.Fn mixer_remove_ctl "mix_ctl_t *ctl"
+.Ft mix_ctl_t *
+.Fn mixer_get_ctl "struct mix_dev *d" "int id"
+.Ft mix_ctl_t *
+.Fn mixer_get_ctl_byname "struct mix_dev *d" "const char *name"
+.Ft int
+.Fn mixer_set_vol "struct mixer *m" "mix_volume_t vol"
+.Ft int
+.Fn mixer_set_mute "struct mixer *m" "int opt"
+.Ft int
+.Fn mixer_mod_recsrc "struct mixer *m" "int opt"
+.Ft int
+.Fn mixer_get_dunit "void"
+.Ft int
+.Fn mixer_set_dunit "struct mixer *m" "int unit"
+.Ft int
+.Fn mixer_get_mode "int unit"
+.Ft int
+.Fn mixer_get_nmixers "void"
+.Ft int
+.Fn mixer_get_path "char * buf" "size_t size" "int unit"
+.Ft int
+.Fn MIX_ISDEV "struct mixer *m" "int devno"
+.Ft int
+.Fn MIX_ISMUTE "struct mixer *m" "int devno"
+.Ft int
+.Fn MIX_ISREC "struct mixer *m" "int devno"
+.Ft int
+.Fn MIX_ISRECSRC "struct mixer *m" "int devno"
+.Ft float
+.Fn MIX_VOLNORM "int v"
+.Ft int
+.Fn MIX_VOLDENORM "float v"
+.Sh DESCRIPTION
+The
+.Nm mixer
+library allows userspace programs to access and manipulate OSS sound mixers in
+a simple way.
+.Ss Mixer
+A mixer is described by the following structure:
+.Bd -literal
+struct mixer {
+ TAILQ_HEAD(mix_devhead, mix_dev) devs; /* device list */
+ struct mix_dev *dev; /* selected device */
+ oss_mixerinfo mi; /* mixer info */
+ oss_card_info ci; /* audio card info */
+ char name[NAME_MAX]; /* mixer name (e.g /dev/mixer0) */
+ int fd; /* file descriptor */
+ int unit; /* audio card unit */
+ int ndev; /* number of devices */
+ int devmask; /* supported devices */
+#define MIX_MUTE 0x01
+#define MIX_UNMUTE 0x02
+#define MIX_TOGGLEMUTE 0x04
+ int mutemask; /* muted devices */
+ int recmask; /* recording devices */
+#define MIX_ADDRECSRC 0x01
+#define MIX_REMOVERECSRC 0x02
+#define MIX_SETRECSRC 0x04
+#define MIX_TOGGLERECSRC 0x08
+ int recsrc; /* recording sources */
+#define MIX_MODE_MIXER 0x01
+#define MIX_MODE_PLAY 0x02
+#define MIX_MODE_REC 0x04
+ int mode; /* dev.pcm.X.mode sysctl */
+ int f_default; /* default mixer flag */
+};
+.Ed
+.Pp
+The fields are follows:
+.Bl -tag -width "f_default"
+.It Fa devs
+A tail queue structure containing all supported mixer devices.
+.It Fa dev
+A pointer to the currently selected device.
+The device is one of the elements in
+.Ar devs .
+.It Fa mi
+OSS information about the mixer.
+Look at the definition of the
+.Ft oss_mixerinfo
+structure in
+.In sys/soundcard.h
+to see its fields.
+.It Fa ci
+OSS audio card information.
+This structure is also defined in
+.In sys/soundcard.h .
+.It Fa name
+Path to the mixer (e.g /dev/mixer0).
+.It Fa fd
+File descriptor returned when the mixer is opened in
+.Fn mixer_open .
+.It Fa unit
+Audio card unit.
+Since each mixer device maps to a pcmX device,
+.Ar unit
+is always equal to the number of that pcmX device.
+For example, if the audio device's number is 0 (i.e pcm0), then
+.Ar unit
+is 0 as well.
+This number is useful when checking if the mixer's audio card is the default
+one.
+.It Fa ndev
+Number of devices in
+.Ar devs .
+.It Fa devmask
+Bit mask containing all supported devices for the mixer.
+For example, if device 10 is supported, then the 10th bit in the mask will be
+set.
+By default,
+.Fn mixer_open
+stores only the supported devices in devs, so it is very unlikely this mask will
+be needed.
+.It Fa mutemask
+Bit mask containing all muted devices.
+The logic is the same as with
+.Ar devmask .
+.It Fa recmask
+Bit mask containing all recording devices.
+Again, same logic as with the other masks.
+.It Fa recsrc
+Bit mask containing all recording sources.
+Yes, same logic again.
+.It Fa mode
+Bit mask containing the supported modes for this audio device.
+It holds the value of the
+.Ar dev.pcm.X.mode
+sysctl.
+.It Fa f_default
+Flag which tells whether the mixer's audio card is the default one.
+.El
+.Ss Mixer device
+Each mixer device stored in a mixer is described as follows:
+.Bd -literal
+struct mix_dev {
+ struct mixer *parent_mixer; /* parent mixer */
+ char name[NAME_MAX]; /* device name (e.g "vol") */
+ int devno; /* device number */
+ struct mix_volume {
+#define MIX_VOLMIN 0.0f
+#define MIX_VOLMAX 1.0f
+#define MIX_VOLNORM(v) ((v) / 100.0f)
+#define MIX_VOLDENORM(v) ((int)((v) * 100.0f + 0.5f))
+ float left; /* left volume */
+ float right; /* right volume */
+ } vol;
+ int nctl; /* number of controls */
+ TAILQ_HEAD(mix_ctlhead, mix_ctl) ctls; /* control list */
+ TAILQ_ENTRY(mix_dev) devs;
+};
+.Ed
+.Pp
+The fields are follows:
+.Bl -tag -width "parent_mixer"
+.It Fa parent_mixer
+Pointer to the mixer the device is attached to.
+.It Fa name
+Device name given by the OSS API.
+Devices can have one of the following names:
+.Bd -ragged
+vol, bass, treble, synth, pcm, speaker, line, mic, cd, mix,
+pcm2, rec, igain, ogain, line1, line2, line3, dig1, dig2, dig3,
+phin, phout, video, radio, and monitor.
+.Ed
+.It Fa devno
+Device's index in the SOUND_MIXER_NRDEVICES macro defined in
+.In sys/soundcard.h .
+This number is used to check against the masks defined in the
+.Ar mixer
+structure.
+.It Fa left right
+Left and right-ear volumes.
+Although the OSS API stores volumes in integers from 0-100, \
+we normalize them to 32-bit floating point numbers.
+However, the volumes can be denormalized using the
+.Ar MIX_VOLDENORM
+macro if needed.
+.It Fa nctl
+Number of user-defined mixer controls associated with the device.
+.It Fa ctls
+A tail queue containing user-defined mixer controls.
+.El
+.Ss User-defined mixer controls
+Each mixer device can have user-defined controls.
+The control structure is defined as follows:
+.Bd -literal
+struct mix_ctl {
+ struct mix_dev *parent_dev; /* parent device */
+ int id; /* control id */
+ char name[NAME_MAX]; /* control name */
+ int (*mod)(struct mix_dev *, void *); /* modify control values */
+ int (*print)(struct mix_dev *, void *); /* print control */
+ TAILQ_ENTRY(mix_ctl) ctls;
+};
+.Ed
+.Pp
+The fields are follows:
+.Bl -tag -width "parent_dev"
+.It Fa parent_dev
+Pointer to the device the control is attached to.
+.It Fa id
+Control ID assigned by the caller.
+Even though the library will report it, care has to be taken to not give \
+a control the same ID in case the caller has to choose controls using their ID.
+.It Fa name
+Control name.
+As with
+.Ar id ,
+the caller has to make sure the same name is not used more than once.
+.It Fa mod
+Function pointer to a control modification function.
+As in
+.Xr mixer 8 ,
+each mixer control's values can be modified.
+For example, if we have a volume control, the
+.Ar mod
+function will be responsible for handling volume changes.
+.It Fa print
+Function pointer to a control print function.
+.El
+.Ss Opening and closing the mixer
+The application must first call the
+.Fn mixer_open
+function to obtain a handle to the device, which is used as an argument \
+in most other functions and macros.
+The parameter
+.Ar name
+specifies the path to the mixer.
+OSS mixers are stored under
+.Ar /dev/mixerN
+where
+.Ar N
+is the number of the mixer device.
+Each device maps to an actual
+.Ar pcm
+audio card, so
+.Ar /dev/mixer0
+is the mixer for
+.Ar pcm0 ,
+and so on.
+If
+.Ar name
+is
+.Ar NULL
+or
+.Ar /dev/mixer ,
+.Fn mixer_open
+opens the default mixer (hw.snd.default_unit).
+.Pp
+The
+.Fn mixer_close
+function frees resources and closes the mixer device.
+It is a good practice to always call it when the application is done using the
+mixer.
+.Ss Manipulating the mixer
+The
+.Fn mixer_get_dev
+and
+.Fn mixer_get_dev_byname
+functions select a mixer device, either by its number or by its name
+respectively.
+The mixer structure keeps a list of all the devices, but only one can be
+manipulated at a time.
+Each time a new device is to be manipulated, one of the two functions has to be
+called.
+.Pp
+The
+.Fn mixer_set_vol
+function changes the volume of the selected mixer device.
+The
+.Ar vol
+parameter is a structure that stores the left and right volumes of a given
+device.
+The allowed volume values are between MIX_VOLMIN (0.0) and MIX_VOLMAX (1.0).
+.Pp
+The
+.Fn mixer_set_mute
+function modifies the mute of a selected device.
+The
+.Ar opt
+parameter has to be one of the following options:
+.Bl -tag -width MIX_TOGGLEMUTE -offset indent
+.It Dv MIX_MUTE
+Mute the device.
+.It Dv MIX_UNMUTE
+Unmute the device.
+.It Dv MIX_TOGGLEMUTE
+Toggle the device's mute (e.g mute if unmuted and unmute if muted).
+.El
+.Pp
+The
+.Fn mixer_mod_recsrc
+function modifies a recording device.
+The selected device has to be a recording device, otherwise the function will
+fail.
+The
+.Ar opt
+parameter has to be one of the following options:
+.Bl -tag -width MIX_REMOVERECSRC -offset indent
+.It Dv MIX_ADDRECSRC
+Add device to the recording sources.
+.It Dv MIX_REMOVERECSRC
+Remove device from the recording sources.
+.It Dv MIX_SETRECSRC
+Set device as the only recording source.
+.It Dv MIX_TOGGLERECSRC
+Toggle device from the recording sources.
+.El
+.Pp
+The
+.Fn mixer_get_dunit
+and
+.Fn mixer_set_dunit
+functions get and set the default audio card in the system.
+Although this is not really a mixer feature, it is useful to have instead of \
+having to use the
+.Xr sysctl 3
+controls.
+.Pp
+The
+.Fn mixer_get_mode
+function returns the operating mode of the audio device the mixer belongs to.
+The following values can be OR'ed in case more than one mode is supported:
+.Bl -tag -width "MIX_MODE_MIXER" -offset indent
+.It Dv MIX_MODE_MIXER
+The audio device has a mixer.
+.It Dv MIX_MODE_PLAY
+The audio device supports playback.
+.It Dv MIX_MODE_REC
+The audio device supports recording.
+.El
+.Pp
+The
+.Fn mixer_get_nmixers
+function returns the maximum mixer unit number.
+Although this might sound as incorrect behavior, given that one would expect
+"nmixers" to refer to the total number of active mixers, it is more intuitive
+for applications that want to loop through all mixer devices (see the
+.Sx EXAMPLES
+section).
+.Pp
+The
+.Fn mixer_get_path
+function writes the path of the mixer device specified in the
+.Ar unit
+argument to the buffer specified in
+.Ar buf .
+.Ar unit
+can be either -1, in which case
+.Fn mixer_get_path
+will fetch the path of the default mixer, or between 0 and the maximum mixer
+unit.
+.Pp
+The
+.Fn MIX_ISDEV
+macro checks if a device is actually a valid device for a given mixer.
+It is very unlikely that this macro will ever be needed since the library \
+stores only valid devices by default.
+.Pp
+The
+.Fn MIX_ISMUTE
+macro checks if a device is muted.
+.Pp
+The
+.Fn MIX_ISREC
+macro checks if a device is a recording device.
+.Pp
+The
+.Fn MIX_ISRECSRC
+macro checks if a device is a recording source.
+.Pp
+The
+.Fn MIX_VOLNORM
+macro normalizes a value to 32-bit floating point number.
+It is used to normalize the volumes read from the OSS API.
+.Pp
+The
+.Fn MIX_VOLDENORM
+macro denormalizes the left and right volumes stores in the
+.Ft mix_dev
+structure.
+.Ss Defining and using mixer controls
+The
+.Fn mix_add_ctl
+function creates a control and attaches it to the device specified in the
+.Ar parent
+argument.
+.Pp
+The
+.Fn mix_add_ctl_s
+function does the same thing as with
+.Fn mix_add_ctl
+but the caller passes a
+.Ft mix_ctl_t *
+structure instead of each field as a separate argument.
+.Pp
+The
+.Fn mixer_remove_ctl
+functions removes a control from the device its attached to.
+.Pp
+The
+.Fn mixer_get_ctl
+function searches for a control in the device specified in the
+.Ar d
+argument and returns a pointer to it.
+The search is done using the control's ID.
+.Pp
+The
+.Fn mixer_get_ctl_byname
+function is the same as with
+.Fn mixer_get_ctl
+but the search is done using the control's name.
+.Sh RETURN VALUES
+The
+.Fn mixer_open
+function returns the newly created handle on success and NULL on failure.
+.Pp
+The
+.Fn mixer_close ,
+.Fn mixer_set_vol ,
+.Fn mixer_set_mute ,
+.Fn mixer_mod_recsrc ,
+.Fn mixer_get_dunut ,
+.Fn mixer_set_dunit ,
+.Fn mixer_get_nmixers ,
+and
+.Fn mixer_get_path
+functions return 0 or positive values on success and -1 on failure.
+.Pp
+The
+.Fn mixer_get_dev
+and
+.Fn mixer_get_dev_byname
+functions return the selected device on success and NULL on failure.
+.Pp
+All functions set the value of
+.Ar errno
+on failure.
+.Sh EXAMPLES
+.Ss Change the volume of a device
+.Bd -literal
+struct mixer *m;
+mix_volume_t vol;
+char *mix_name, *dev_name;
+
+mix_name = ...;
+if ((m = mixer_open(mix_name)) == NULL)
+ err(1, "mixer_open: %s", mix_name);
+
+dev_name = ...;
+if ((m->dev = mixer_get_dev_byname(m, dev_name)) < 0)
+ err(1, "unknown device: %s", dev_name);
+
+vol.left = ...;
+vol.right = ....;
+if (mixer_set_vol(m, vol) < 0)
+ warn("cannot change volume");
+
+(void)mixer_close(m);
+.Ed
+.Ss Mute all unmuted devices
+.Bd -literal
+struct mixer *m;
+struct mix_dev *dp;
+
+if ((m = mixer_open(NULL)) == NULL) /* Open the default mixer. */
+ err(1, "mixer_open");
+TAILQ_FOREACH(dp, &m->devs, devs) {
+ m->dev = dp; /* Select device. */
+ if (M_ISMUTE(m, dp->devno))
+ continue;
+ if (mixer_set_mute(m, MIX_MUTE) < 0)
+ warn("cannot mute device: %s", dp->name);
+}
+
+(void)mixer_close(m);
+.Ed
+.Ss Print all recording sources' names and volumes
+.Bd -literal
+struct mixer *m;
+struct mix_dev *dp;
+
+char *mix_name, *dev_name;
+
+mix_name = ...;
+if ((m = mixer_open(mix_name)) == NULL)
+ err(1, "mixer_open: %s", mix_name);
+
+TAILQ_FOREACH(dp, &m->devs, devs) {
+ if (M_ISRECSRC(m, dp->devno))
+ printf("%s\\t%.2f:%.2f\\n",
+ dp->name, dp->vol.left, dp->vol.right);
+}
+
+(void)mixer_close(m);
+.Ed
+.Ss Loop through all mixer devices in the system
+.Bd -literal
+struct mixer *m;
+char buf[NAME_MAX];
+int n;
+
+if ((n = mixer_get_nmixers()) < 0)
+ errx(1, "no mixers present in the system");
+for (i = 0; i < n; i++) {
+ (void)mixer_get_path(buf, sizeof(buf), i);
+ if ((m = mixer_open(buf)) == NULL)
+ continue;
+ ...
+ (void)mixer_close(m);
+}
+.Ed
+.Sh SEE ALSO
+.Xr queue 3 ,
+.Xr sysctl 3 ,
+.Xr sound 4 ,
+.Xr mixer 8
+and
+.Xr errno 2
+.Sh AUTHORS
+.An Christos Margiolis Aq Mt christos@FreeBSD.org
diff --git a/lib/libmixer/mixer.c b/lib/libmixer/mixer.c
new file mode 100644
index 000000000000..2be3a48eb755
--- /dev/null
+++ b/lib/libmixer/mixer.c
@@ -0,0 +1,520 @@
+/*-
+ * Copyright (c) 2021 Christos Margiolis <christos@FreeBSD.org>
+ *
+ * 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 <sys/types.h>
+#include <sys/ioctl.h>
+#include <sys/sysctl.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "mixer.h"
+
+#define BASEPATH "/dev/mixer"
+
+static int _mixer_readvol(struct mix_dev *);
+
+/*
+ * Fetch volume from the device.
+ */
+static int
+_mixer_readvol(struct mix_dev *dev)
+{
+ int v;
+
+ if (ioctl(dev->parent_mixer->fd, MIXER_READ(dev->devno), &v) < 0)
+ return (-1);
+ dev->vol.left = MIX_VOLNORM(v & 0x00ff);
+ dev->vol.right = MIX_VOLNORM((v >> 8) & 0x00ff);
+
+ return (0);
+}
+
+/*
+ * Open a mixer device in `/dev/mixerN`, where N is the number of the mixer.
+ * Each device maps to an actual pcm audio card, so `/dev/mixer0` is the
+ * mixer for pcm0, and so on.
+ *
+ * @param name path to mixer device. NULL or "/dev/mixer" for the
+ * the default mixer (i.e `hw.snd.default_unit`).
+ */
+struct mixer *
+mixer_open(const char *name)
+{
+ struct mixer *m = NULL;
+ struct mix_dev *dp;
+ const char *names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
+ int i;
+
+ if ((m = calloc(1, sizeof(struct mixer))) == NULL)
+ goto fail;
+
+ if (name != NULL) {
+ /* `name` does not start with "/dev/mixer". */
+ if (strncmp(name, BASEPATH, strlen(BASEPATH)) != 0) {
+ m->unit = -1;
+ } else {
+ /* `name` is "/dev/mixer" so, we'll use the default unit. */
+ if (strncmp(name, BASEPATH, strlen(name)) == 0)
+ goto dunit;
+ m->unit = strtol(name + strlen(BASEPATH), NULL, 10);
+ }
+ (void)strlcpy(m->name, name, sizeof(m->name));
+ } else {
+dunit:
+ if ((m->unit = mixer_get_dunit()) < 0)
+ goto fail;
+ (void)snprintf(m->name, sizeof(m->name), BASEPATH "%d", m->unit);
+ }
+
+ if ((m->fd = open(m->name, O_RDWR)) < 0)
+ goto fail;
+
+ m->devmask = m->recmask = m->recsrc = 0;
+ m->f_default = m->unit == mixer_get_dunit();
+ m->mode = mixer_get_mode(m->unit);
+ /* The unit number _must_ be set before the ioctl. */
+ m->mi.dev = m->unit;
+ m->ci.card = m->unit;
+ if (ioctl(m->fd, SNDCTL_MIXERINFO, &m->mi) < 0) {
+ memset(&m->mi, 0, sizeof(m->mi));
+ strlcpy(m->mi.name, m->name, sizeof(m->mi.name));
+ }
+ if (ioctl(m->fd, SNDCTL_CARDINFO, &m->ci) < 0)
+ memset(&m->ci, 0, sizeof(m->ci));
+ if (ioctl(m->fd, SOUND_MIXER_READ_DEVMASK, &m->devmask) < 0 ||
+ ioctl(m->fd, SOUND_MIXER_READ_MUTE, &m->mutemask) < 0 ||
+ ioctl(m->fd, SOUND_MIXER_READ_RECMASK, &m->recmask) < 0 ||
+ ioctl(m->fd, SOUND_MIXER_READ_RECSRC, &m->recsrc) < 0)
+ goto fail;
+
+ TAILQ_INIT(&m->devs);
+ for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
+ if (!MIX_ISDEV(m, i) && !MIX_ISREC(m, i))
+ continue;
+ if ((dp = calloc(1, sizeof(struct mix_dev))) == NULL)
+ goto fail;
+ dp->parent_mixer = m;
+ dp->devno = i;
+ dp->nctl = 0;
+ if (MIX_ISDEV(m, i) && _mixer_readvol(dp) < 0)
+ goto fail;
+ (void)strlcpy(dp->name, names[i], sizeof(dp->name));
+ TAILQ_INIT(&dp->ctls);
+ TAILQ_INSERT_TAIL(&m->devs, dp, devs);
+ m->ndev++;
+ }
+
+ /* The default device is always "vol". */
+ m->dev = TAILQ_FIRST(&m->devs);
+
+ return (m);
+fail:
+ if (m != NULL)
+ (void)mixer_close(m);
+
+ return (NULL);
+}
+
+/*
+ * Free resources and close the mixer.
+ */
+int
+mixer_close(struct mixer *m)
+{
+ struct mix_dev *dp;
+ int r;
+
+ r = close(m->fd);
+ while (!TAILQ_EMPTY(&m->devs)) {
+ dp = TAILQ_FIRST(&m->devs);
+ TAILQ_REMOVE(&m->devs, dp, devs);
+ while (!TAILQ_EMPTY(&dp->ctls))
+ (void)mixer_remove_ctl(TAILQ_FIRST(&dp->ctls));
+ free(dp);
+ }
+ free(m);
+
+ return (r);
+}
+
+/*
+ * Select a mixer device. The mixer structure keeps a list of all the devices
+ * the mixer has, but only one can be manipulated at a time -- this is what
+ * the `dev` in the mixer structure field is for. Each time a device is to be
+ * manipulated, `dev` has to point to it first.
+ *
+ * The caller must manually assign the return value to `m->dev`.
+ */
+struct mix_dev *
+mixer_get_dev(struct mixer *m, int dev)
+{
+ struct mix_dev *dp;
+
+ if (dev < 0 || dev >= m->ndev) {
+ errno = ERANGE;
+ return (NULL);
+ }
+ TAILQ_FOREACH(dp, &m->devs, devs) {
+ if (dp->devno == dev)
+ return (dp);
+ }
+ errno = EINVAL;
+
+ return (NULL);
+}
+
+/*
+ * Select a device by name.
+ *
+ * @param name device name (e.g vol, pcm, ...)
+ */
+struct mix_dev *
+mixer_get_dev_byname(struct mixer *m, const char *name)
+{
+ struct mix_dev *dp;
+
+ TAILQ_FOREACH(dp, &m->devs, devs) {
+ if (!strncmp(dp->name, name, sizeof(dp->name)))
+ return (dp);
+ }
+ errno = EINVAL;
+
+ return (NULL);
+}
+
+/*
+ * Add a mixer control to a device.
+ */
+int
+mixer_add_ctl(struct mix_dev *parent_dev, int id, const char *name,
+ int (*mod)(struct mix_dev *, void *),
+ int (*print)(struct mix_dev *, void *))
+{
+ struct mix_dev *dp;
+ mix_ctl_t *ctl, *cp;
+
+ /* XXX: should we accept NULL name? */
+ if (parent_dev == NULL) {
+ errno = EINVAL;
+ return (-1);
+ }
+ if ((ctl = calloc(1, sizeof(mix_ctl_t))) == NULL)
+ return (-1);
+ ctl->parent_dev = parent_dev;
+ ctl->id = id;
+ if (name != NULL)
+ (void)strlcpy(ctl->name, name, sizeof(ctl->name));
+ ctl->mod = mod;
+ ctl->print = print;
+ dp = ctl->parent_dev;
+ /* Make sure the same ID or name doesn't exist already. */
+ TAILQ_FOREACH(cp, &dp->ctls, ctls) {
+ if (!strncmp(cp->name, name, sizeof(cp->name)) || cp->id == id) {
+ errno = EINVAL;
+ return (-1);
+ }
+ }
+ TAILQ_INSERT_TAIL(&dp->ctls, ctl, ctls);
+ dp->nctl++;
+
+ return (0);
+}
+
+/*
+ * Same as `mixer_add_ctl`.
+ */
+int
+mixer_add_ctl_s(mix_ctl_t *ctl)
+{
+ if (ctl == NULL)
+ return (-1);
+
+ return (mixer_add_ctl(ctl->parent_dev, ctl->id, ctl->name,
+ ctl->mod, ctl->print));
+}
+
+/*
+ * Remove a mixer control from a device.
+ */
+int
+mixer_remove_ctl(mix_ctl_t *ctl)
+{
+ struct mix_dev *p;
+
+ if (ctl == NULL) {
+ errno = EINVAL;
+ return (-1);
+ }
+ p = ctl->parent_dev;
+ if (!TAILQ_EMPTY(&p->ctls)) {
+ TAILQ_REMOVE(&p->ctls, ctl, ctls);
+ free(ctl);
+ }
+
+ return (0);
+}
+
+/*
+ * Get a mixer control by id.
+ */
+mix_ctl_t *
+mixer_get_ctl(struct mix_dev *d, int id)
+{
+ mix_ctl_t *cp;
+
+ TAILQ_FOREACH(cp, &d->ctls, ctls) {
+ if (cp->id == id)
+ return (cp);
+ }
+ errno = EINVAL;
+
+ return (NULL);
+}
+
+/*
+ * Get a mixer control by name.
+ */
+mix_ctl_t *
+mixer_get_ctl_byname(struct mix_dev *d, const char *name)
+{
+ mix_ctl_t *cp;
+
+ TAILQ_FOREACH(cp, &d->ctls, ctls) {
+ if (!strncmp(cp->name, name, sizeof(cp->name)))
+ return (cp);
+ }
+ errno = EINVAL;
+
+ return (NULL);
+}
+
+/*
+ * Change the mixer's left and right volume. The allowed volume values are
+ * between MIX_VOLMIN and MIX_VOLMAX. The `ioctl` for volume change requires
+ * an integer value between 0 and 100 stored as `lvol | rvol << 8` -- for
+ * that reason, we de-normalize the 32-bit float volume value, before
+ * we pass it to the `ioctl`.
+ *
+ * Volume clumping should be done by the caller.
+ */
+int
+mixer_set_vol(struct mixer *m, mix_volume_t vol)
+{
+ int v;
+
+ if (vol.left < MIX_VOLMIN || vol.left > MIX_VOLMAX ||
+ vol.right < MIX_VOLMIN || vol.right > MIX_VOLMAX) {
+ errno = ERANGE;
+ return (-1);
+ }
+ v = MIX_VOLDENORM(vol.left) | MIX_VOLDENORM(vol.right) << 8;
+ if (ioctl(m->fd, MIXER_WRITE(m->dev->devno), &v) < 0)
+ return (-1);
+ if (_mixer_readvol(m->dev) < 0)
+ return (-1);
+
+ return (0);
+}
+
+/*
+ * Manipulate a device's mute.
+ *
+ * @param opt MIX_MUTE mute device
+ * MIX_UNMUTE unmute device
+ * MIX_TOGGLEMUTE toggle device's mute
+ */
+int
+mixer_set_mute(struct mixer *m, int opt)
+{
+ switch (opt) {
+ case MIX_MUTE:
+ m->mutemask |= (1 << m->dev->devno);
+ break;
+ case MIX_UNMUTE:
+ m->mutemask &= ~(1 << m->dev->devno);
+ break;
+ case MIX_TOGGLEMUTE:
+ m->mutemask ^= (1 << m->dev->devno);
+ break;
+ default:
+ errno = EINVAL;
+ return (-1);
+ }
+ if (ioctl(m->fd, SOUND_MIXER_WRITE_MUTE, &m->mutemask) < 0)
+ return (-1);
+ if (ioctl(m->fd, SOUND_MIXER_READ_MUTE, &m->mutemask) < 0)
+ return (-1);
+
+ return 0;
+}
+
+/*
+ * Modify a recording device. The selected device has to be a recording device,
+ * otherwise the function will fail.
+ *
+ * @param opt MIX_ADDRECSRC add device to recording sources
+ * MIX_REMOVERECSRC remove device from recording sources
+ * MIX_SETRECSRC set device as the only recording source
+ * MIX_TOGGLERECSRC toggle device from recording sources
+ */
+int
+mixer_mod_recsrc(struct mixer *m, int opt)
+{
+ if (!m->recmask || !MIX_ISREC(m, m->dev->devno)) {
+ errno = ENODEV;
+ return (-1);
+ }
+ switch (opt) {
+ case MIX_ADDRECSRC:
+ m->recsrc |= (1 << m->dev->devno);
+ break;
+ case MIX_REMOVERECSRC:
+ m->recsrc &= ~(1 << m->dev->devno);
+ break;
+ case MIX_SETRECSRC:
+ m->recsrc = (1 << m->dev->devno);
+ break;
+ case MIX_TOGGLERECSRC:
+ m->recsrc ^= (1 << m->dev->devno);
+ break;
+ default:
+ errno = EINVAL;
+ return (-1);
+ }
+ if (ioctl(m->fd, SOUND_MIXER_WRITE_RECSRC, &m->recsrc) < 0)
+ return (-1);
+ if (ioctl(m->fd, SOUND_MIXER_READ_RECSRC, &m->recsrc) < 0)
+ return (-1);
+
+ return (0);
+}
+
+/*
+ * Get default audio card's number. This is used to open the default mixer
+ * and set the mixer structure's `f_default` flag.
+ */
+int
+mixer_get_dunit(void)
+{
+ size_t size;
+ int unit;
+
+ size = sizeof(int);
+ if (sysctlbyname("hw.snd.default_unit", &unit, &size, NULL, 0) < 0)
+ return (-1);
+
+ return (unit);
+}
+
+/*
+ * Change the default audio card. This is normally _not_ a mixer feature, but
+ * it's useful to have, so the caller can avoid having to manually use
+ * the sysctl API.
+ *
+ * @param unit the audio card number (e.g pcm0, pcm1, ...).
+ */
+int
+mixer_set_dunit(struct mixer *m, int unit)
+{
+ size_t size;
+
+ size = sizeof(int);
+ if (sysctlbyname("hw.snd.default_unit", NULL, 0, &unit, size) < 0)
+ return (-1);
+ /* XXX: how will other mixers get updated? */
+ m->f_default = m->unit == unit;
+
+ return (0);
+}
+
+/*
+ * Get sound device mode (none, play, rec, play+rec). Userland programs can
+ * use the MIX_MODE_* flags to determine the mode of the device.
+ */
+int
+mixer_get_mode(int unit)
+{
+ char buf[64];
+ size_t size;
+ unsigned int mode;
+
+ (void)snprintf(buf, sizeof(buf), "dev.pcm.%d.mode", unit);
+ size = sizeof(unsigned int);
+ if (sysctlbyname(buf, &mode, &size, NULL, 0) < 0)
+ return (0);
+
+ return (mode);
+}
+
+/*
+ * Get the total number of mixers in the system.
+ */
+int
+mixer_get_nmixers(void)
+{
+ struct mixer *m;
+ oss_sysinfo si;
+
+ /*
+ * Open a dummy mixer because we need the `fd` field for the
+ * `ioctl` to work.
+ */
+ if ((m = mixer_open(NULL)) == NULL)
+ return (-1);
+ if (ioctl(m->fd, OSS_SYSINFO, &si) < 0) {
+ (void)mixer_close(m);
+ return (-1);
+ }
+ (void)mixer_close(m);
+
+ return (si.nummixers);
+}
+
+/*
+ * Get the full path to a mixer device.
+ */
+int
+mixer_get_path(char *buf, size_t size, int unit)
+{
+ size_t n;
+
+ if (!(unit == -1 || (unit >= 0 && unit < mixer_get_nmixers()))) {
+ errno = EINVAL;
+ return (-1);
+ }
+ if (unit == -1)
+ n = strlcpy(buf, BASEPATH, size);
+ else
+ n = snprintf(buf, size, BASEPATH "%d", unit);
+
+ if (n >= size) {
+ errno = ENOMEM;
+ return (-1);
+ }
+
+ return (0);
+}
diff --git a/lib/libmixer/mixer.h b/lib/libmixer/mixer.h
new file mode 100644
index 000000000000..80a4b5c91a82
--- /dev/null
+++ b/lib/libmixer/mixer.h
@@ -0,0 +1,122 @@
+/*-
+ * Copyright (c) 2021 Christos Margiolis <christos@FreeBSD.org>
+ *
+ * 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.
+ */
+
+#ifndef _MIXER_H_
+#define _MIXER_H_
+
+#include <sys/cdefs.h>
+#include <sys/queue.h>
+#include <sys/soundcard.h>
+
+#include <limits.h>
+
+#define MIX_ISSET(n,f) (((1U << (n)) & (f)) ? 1 : 0)
+#define MIX_ISDEV(m,n) MIX_ISSET(n, (m)->devmask)
+#define MIX_ISMUTE(m,n) MIX_ISSET(n, (m)->mutemask)
+#define MIX_ISREC(m,n) MIX_ISSET(n, (m)->recmask)
+#define MIX_ISRECSRC(m,n) MIX_ISSET(n, (m)->recsrc)
+
+/* Forward declarations */
+struct mixer;
+struct mix_dev;
+
+typedef struct mix_ctl mix_ctl_t;
+typedef struct mix_volume mix_volume_t;
+
+/* User-defined controls */
+struct mix_ctl {
+ struct mix_dev *parent_dev; /* parent device */
+ int id; /* control id */
+ char name[NAME_MAX]; /* control name */
+ int (*mod)(struct mix_dev *, void *); /* modify control values */
+ int (*print)(struct mix_dev *, void *); /* print control */
+ TAILQ_ENTRY(mix_ctl) ctls;
+};
+
+struct mix_dev {
+ struct mixer *parent_mixer; /* parent mixer */
+ char name[NAME_MAX]; /* device name (e.g "vol") */
+ int devno; /* device number */
+ struct mix_volume {
+#define MIX_VOLMIN 0.0f
+#define MIX_VOLMAX 1.0f
+#define MIX_VOLNORM(v) ((v) / 100.0f)
+#define MIX_VOLDENORM(v) ((int)((v) * 100.0f + 0.5f))
+ float left; /* left volume */
+ float right; /* right volume */
+ } vol;
+ int nctl; /* number of controls */
+ TAILQ_HEAD(mix_ctlhead, mix_ctl) ctls; /* control list */
+ TAILQ_ENTRY(mix_dev) devs;
+};
+
+struct mixer {
+ TAILQ_HEAD(mix_devhead, mix_dev) devs; /* device list */
+ struct mix_dev *dev; /* selected device */
+ oss_mixerinfo mi; /* mixer info */
+ oss_card_info ci; /* audio card info */
+ char name[NAME_MAX]; /* mixer name (e.g /dev/mixer0) */
+ int fd; /* file descriptor */
+ int unit; /* audio card unit */
+ int ndev; /* number of devices */
+ int devmask; /* supported devices */
+#define MIX_MUTE 0x01
+#define MIX_UNMUTE 0x02
+#define MIX_TOGGLEMUTE 0x04
+ int mutemask; /* muted devices */
+ int recmask; /* recording devices */
+#define MIX_ADDRECSRC 0x01
+#define MIX_REMOVERECSRC 0x02
+#define MIX_SETRECSRC 0x04
+#define MIX_TOGGLERECSRC 0x08
+ int recsrc; /* recording sources */
+#define MIX_MODE_MIXER 0x01
+#define MIX_MODE_PLAY 0x02
+#define MIX_MODE_REC 0x04
+ int mode; /* dev.pcm.X.mode sysctl */
+ int f_default; /* default mixer flag */
+};
+
+__BEGIN_DECLS
+
+struct mixer *mixer_open(const char *);
+int mixer_close(struct mixer *);
+struct mix_dev *mixer_get_dev(struct mixer *, int);
+struct mix_dev *mixer_get_dev_byname(struct mixer *, const char *);
+int mixer_add_ctl(struct mix_dev *, int, const char *,
+ int (*)(struct mix_dev *, void *), int (*)(struct mix_dev *, void *));
+int mixer_add_ctl_s(mix_ctl_t *);
+int mixer_remove_ctl(mix_ctl_t *);
+mix_ctl_t *mixer_get_ctl(struct mix_dev *, int);
+mix_ctl_t *mixer_get_ctl_byname(struct mix_dev *, const char *);
+int mixer_set_vol(struct mixer *, mix_volume_t);
+int mixer_set_mute(struct mixer *, int);
+int mixer_mod_recsrc(struct mixer *, int);
+int mixer_get_dunit(void);
+int mixer_set_dunit(struct mixer *, int);
+int mixer_get_mode(int);
+int mixer_get_nmixers(void);
+int mixer_get_path(char *, size_t, int);
+
+__END_DECLS
+
+#endif /* _MIXER_H_ */