2 * Copyright (c) 2021 Christos Margiolis <christos@FreeBSD.org>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include <sys/types.h>
26 #include <sys/ioctl.h>
27 #include <sys/sysctl.h>
38 #define BASEPATH "/dev/mixer"
40 static int _mixer_readvol(struct mixer *, struct mix_dev *);
43 * Fetch volume from the device.
46 _mixer_readvol(struct mixer *m, struct mix_dev *dev)
50 if (ioctl(m->fd, MIXER_READ(dev->devno), &v) < 0)
52 dev->vol.left = MIX_VOLNORM(v & 0x00ff);
53 dev->vol.right = MIX_VOLNORM((v >> 8) & 0x00ff);
59 * Open a mixer device in `/dev/mixerN`, where N is the number of the mixer.
60 * Each device maps to an actual pcm audio card, so `/dev/mixer0` is the
61 * mixer for pcm0, and so on.
63 * @param name path to mixer device. NULL or "/dev/mixer" for the
64 * the default mixer (i.e `hw.snd.default_unit`).
67 mixer_open(const char *name)
69 struct mixer *m = NULL;
71 const char *names[SOUND_MIXER_NRDEVICES] = SOUND_DEVICE_NAMES;
74 if ((m = calloc(1, sizeof(struct mixer))) == NULL)
78 /* `name` does not start with "/dev/mixer". */
79 if (strncmp(name, BASEPATH, strlen(BASEPATH)) != 0) {
82 /* `name` is "/dev/mixer" so, we'll use the default unit. */
83 if (strncmp(name, BASEPATH, strlen(name)) == 0)
85 m->unit = strtol(name + strlen(BASEPATH), NULL, 10);
87 (void)strlcpy(m->name, name, sizeof(m->name));
90 if ((m->unit = mixer_get_dunit()) < 0)
92 (void)snprintf(m->name, sizeof(m->name), "/dev/mixer%d", m->unit);
95 if ((m->fd = open(m->name, O_RDWR)) < 0)
98 m->devmask = m->recmask = m->recsrc = 0;
99 m->f_default = m->unit == mixer_get_dunit();
100 m->mode = mixer_get_mode(m->unit);
101 /* The unit number _must_ be set before the ioctl. */
103 m->ci.card = m->unit;
104 if (ioctl(m->fd, SNDCTL_MIXERINFO, &m->mi) < 0) {
105 memset(&m->mi, 0, sizeof(m->mi));
106 strlcpy(m->mi.name, m->name, sizeof(m->mi.name));
108 if (ioctl(m->fd, SNDCTL_CARDINFO, &m->ci) < 0)
109 memset(&m->ci, 0, sizeof(m->ci));
110 if (ioctl(m->fd, SOUND_MIXER_READ_DEVMASK, &m->devmask) < 0 ||
111 ioctl(m->fd, SOUND_MIXER_READ_MUTE, &m->mutemask) < 0 ||
112 ioctl(m->fd, SOUND_MIXER_READ_RECMASK, &m->recmask) < 0 ||
113 ioctl(m->fd, SOUND_MIXER_READ_RECSRC, &m->recsrc) < 0)
116 TAILQ_INIT(&m->devs);
117 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
118 if (!MIX_ISDEV(m, i))
120 if ((dp = calloc(1, sizeof(struct mix_dev))) == NULL)
122 dp->parent_mixer = m;
125 if (_mixer_readvol(m, dp) < 0)
127 (void)strlcpy(dp->name, names[i], sizeof(dp->name));
128 TAILQ_INIT(&dp->ctls);
129 TAILQ_INSERT_TAIL(&m->devs, dp, devs);
133 /* The default device is always "vol". */
134 m->dev = TAILQ_FIRST(&m->devs);
139 (void)mixer_close(m);
145 * Free resources and close the mixer.
148 mixer_close(struct mixer *m)
154 while (!TAILQ_EMPTY(&m->devs)) {
155 dp = TAILQ_FIRST(&m->devs);
156 TAILQ_REMOVE(&m->devs, dp, devs);
157 while (!TAILQ_EMPTY(&dp->ctls))
158 (void)mixer_remove_ctl(TAILQ_FIRST(&dp->ctls));
167 * Select a mixer device. The mixer structure keeps a list of all the devices
168 * the mixer has, but only one can be manipulated at a time -- this is what
169 * the `dev` in the mixer structure field is for. Each time a device is to be
170 * manipulated, `dev` has to point to it first.
172 * The caller must manually assign the return value to `m->dev`.
175 mixer_get_dev(struct mixer *m, int dev)
179 if (dev < 0 || dev >= m->ndev) {
183 TAILQ_FOREACH(dp, &m->devs, devs) {
184 if (dp->devno == dev)
193 * Select a device by name.
195 * @param name device name (e.g vol, pcm, ...)
198 mixer_get_dev_byname(struct mixer *m, const char *name)
202 TAILQ_FOREACH(dp, &m->devs, devs) {
203 if (!strncmp(dp->name, name, sizeof(dp->name)))
212 * Add a mixer control to a device.
215 mixer_add_ctl(struct mix_dev *parent_dev, int id, const char *name,
216 int (*mod)(struct mix_dev *, void *),
217 int (*print)(struct mix_dev *, void *))
222 /* XXX: should we accept NULL name? */
223 if (parent_dev == NULL) {
227 if ((ctl = calloc(1, sizeof(mix_ctl_t))) == NULL)
229 ctl->parent_dev = parent_dev;
232 (void)strlcpy(ctl->name, name, sizeof(ctl->name));
235 dp = ctl->parent_dev;
236 /* Make sure the same ID or name doesn't exist already. */
237 TAILQ_FOREACH(cp, &dp->ctls, ctls) {
238 if (!strncmp(cp->name, name, sizeof(cp->name)) || cp->id == id) {
243 TAILQ_INSERT_TAIL(&dp->ctls, ctl, ctls);
250 * Same as `mixer_add_ctl`.
253 mixer_add_ctl_s(mix_ctl_t *ctl)
258 return (mixer_add_ctl(ctl->parent_dev, ctl->id, ctl->name,
259 ctl->mod, ctl->print));
263 * Remove a mixer control from a device.
266 mixer_remove_ctl(mix_ctl_t *ctl)
275 if (!TAILQ_EMPTY(&p->ctls)) {
276 TAILQ_REMOVE(&p->ctls, ctl, ctls);
284 * Get a mixer control by id.
287 mixer_get_ctl(struct mix_dev *d, int id)
291 TAILQ_FOREACH(cp, &d->ctls, ctls) {
301 * Get a mixer control by name.
304 mixer_get_ctl_byname(struct mix_dev *d, const char *name)
308 TAILQ_FOREACH(cp, &d->ctls, ctls) {
309 if (!strncmp(cp->name, name, sizeof(cp->name)))
318 * Change the mixer's left and right volume. The allowed volume values are
319 * between MIX_VOLMIN and MIX_VOLMAX. The `ioctl` for volume change requires
320 * an integer value between 0 and 100 stored as `lvol | rvol << 8` -- for
321 * that reason, we de-normalize the 32-bit float volume value, before
322 * we pass it to the `ioctl`.
324 * Volume clumping should be done by the caller.
327 mixer_set_vol(struct mixer *m, mix_volume_t vol)
331 if (vol.left < MIX_VOLMIN || vol.left > MIX_VOLMAX ||
332 vol.right < MIX_VOLMIN || vol.right > MIX_VOLMAX) {
336 v = MIX_VOLDENORM(vol.left) | MIX_VOLDENORM(vol.right) << 8;
337 if (ioctl(m->fd, MIXER_WRITE(m->dev->devno), &v) < 0)
339 if (_mixer_readvol(m, m->dev) < 0)
346 * Manipulate a device's mute.
348 * @param opt MIX_MUTE mute device
349 * MIX_UNMUTE unmute device
350 * MIX_TOGGLEMUTE toggle device's mute
353 mixer_set_mute(struct mixer *m, int opt)
357 m->mutemask |= (1 << m->dev->devno);
360 m->mutemask &= ~(1 << m->dev->devno);
363 m->mutemask ^= (1 << m->dev->devno);
369 if (ioctl(m->fd, SOUND_MIXER_WRITE_MUTE, &m->mutemask) < 0)
371 if (ioctl(m->fd, SOUND_MIXER_READ_MUTE, &m->mutemask) < 0)
378 * Modify a recording device. The selected device has to be a recording device,
379 * otherwise the function will fail.
381 * @param opt MIX_ADDRECSRC add device to recording sources
382 * MIX_REMOVERECSRC remove device from recording sources
383 * MIX_SETRECSRC set device as the only recording source
384 * MIX_TOGGLERECSRC toggle device from recording sources
387 mixer_mod_recsrc(struct mixer *m, int opt)
389 if (!m->recmask || !MIX_ISREC(m, m->dev->devno)) {
395 m->recsrc |= (1 << m->dev->devno);
397 case MIX_REMOVERECSRC:
398 m->recsrc &= ~(1 << m->dev->devno);
401 m->recsrc = (1 << m->dev->devno);
403 case MIX_TOGGLERECSRC:
404 m->recsrc ^= (1 << m->dev->devno);
410 if (ioctl(m->fd, SOUND_MIXER_WRITE_RECSRC, &m->recsrc) < 0)
412 if (ioctl(m->fd, SOUND_MIXER_READ_RECSRC, &m->recsrc) < 0)
419 * Get default audio card's number. This is used to open the default mixer
420 * and set the mixer structure's `f_default` flag.
423 mixer_get_dunit(void)
429 if (sysctlbyname("hw.snd.default_unit", &unit, &size, NULL, 0) < 0)
436 * Change the default audio card. This is normally _not_ a mixer feature, but
437 * it's useful to have, so the caller can avoid having to manually use
440 * @param unit the audio card number (e.g pcm0, pcm1, ...).
443 mixer_set_dunit(struct mixer *m, int unit)
448 if (sysctlbyname("hw.snd.default_unit", NULL, 0, &unit, size) < 0)
450 /* XXX: how will other mixers get updated? */
451 m->f_default = m->unit == unit;
457 * Get sound device mode (none, play, rec, play+rec). Userland programs can
458 * use the MIX_STATUS_* flags to determine the mode of the device.
461 mixer_get_mode(int unit)
467 (void)snprintf(buf, sizeof(buf), "dev.pcm.%d.mode", unit);
468 size = sizeof(unsigned int);
469 if (sysctlbyname(buf, &mode, &size, NULL, 0) < 0)
476 * Get the total number of mixers in the system.
479 mixer_get_nmixers(void)
485 * Open a dummy mixer because we need the `fd` field for the
488 if ((m = mixer_open(NULL)) == NULL)
490 if (ioctl(m->fd, OSS_SYSINFO, &si) < 0) {
491 (void)mixer_close(m);
494 (void)mixer_close(m);
496 return (si.nummixers);