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) {
83 /* `name` is "/dev/mixer" so, we'll use the default unit. */
84 if (strncmp(name, BASEPATH, strlen(name)) == 0)
86 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 ioctl(m->fd, SNDCTL_CARDINFO, &m->ci) < 0 ||
106 ioctl(m->fd, SOUND_MIXER_READ_DEVMASK, &m->devmask) < 0 ||
107 ioctl(m->fd, SOUND_MIXER_READ_MUTE, &m->mutemask) < 0 ||
108 ioctl(m->fd, SOUND_MIXER_READ_RECMASK, &m->recmask) < 0 ||
109 ioctl(m->fd, SOUND_MIXER_READ_RECSRC, &m->recsrc) < 0)
112 TAILQ_INIT(&m->devs);
113 for (i = 0; i < SOUND_MIXER_NRDEVICES; i++) {
114 if (!MIX_ISDEV(m, i))
116 if ((dp = calloc(1, sizeof(struct mix_dev))) == NULL)
118 dp->parent_mixer = m;
121 if (_mixer_readvol(m, dp) < 0)
123 (void)strlcpy(dp->name, names[i], sizeof(dp->name));
124 TAILQ_INIT(&dp->ctls);
125 TAILQ_INSERT_TAIL(&m->devs, dp, devs);
129 /* The default device is always "vol". */
130 m->dev = TAILQ_FIRST(&m->devs);
135 (void)mixer_close(m);
141 * Free resources and close the mixer.
144 mixer_close(struct mixer *m)
150 while (!TAILQ_EMPTY(&m->devs)) {
151 dp = TAILQ_FIRST(&m->devs);
152 TAILQ_REMOVE(&m->devs, dp, devs);
153 while (!TAILQ_EMPTY(&dp->ctls))
154 (void)mixer_remove_ctl(TAILQ_FIRST(&dp->ctls));
163 * Select a mixer device. The mixer structure keeps a list of all the devices
164 * the mixer has, but only one can be manipulated at a time -- this is what
165 * the `dev` in the mixer structure field is for. Each time a device is to be
166 * manipulated, `dev` has to point to it first.
168 * The caller must manually assign the return value to `m->dev`.
171 mixer_get_dev(struct mixer *m, int dev)
175 if (dev < 0 || dev >= m->ndev) {
179 TAILQ_FOREACH(dp, &m->devs, devs) {
180 if (dp->devno == dev)
189 * Select a device by name.
191 * @param name device name (e.g vol, pcm, ...)
194 mixer_get_dev_byname(struct mixer *m, const char *name)
198 TAILQ_FOREACH(dp, &m->devs, devs) {
199 if (!strncmp(dp->name, name, sizeof(dp->name)))
208 * Add a mixer control to a device.
211 mixer_add_ctl(struct mix_dev *parent_dev, int id, const char *name,
212 int (*mod)(struct mix_dev *, void *),
213 int (*print)(struct mix_dev *, void *))
218 /* XXX: should we accept NULL name? */
219 if (parent_dev == NULL) {
223 if ((ctl = calloc(1, sizeof(mix_ctl_t))) == NULL)
225 ctl->parent_dev = parent_dev;
228 (void)strlcpy(ctl->name, name, sizeof(ctl->name));
231 dp = ctl->parent_dev;
232 /* Make sure the same ID or name doesn't exist already. */
233 TAILQ_FOREACH(cp, &dp->ctls, ctls) {
234 if (!strncmp(cp->name, name, sizeof(cp->name)) || cp->id == id) {
239 TAILQ_INSERT_TAIL(&dp->ctls, ctl, ctls);
246 * Same as `mixer_add_ctl`.
249 mixer_add_ctl_s(mix_ctl_t *ctl)
254 return (mixer_add_ctl(ctl->parent_dev, ctl->id, ctl->name,
255 ctl->mod, ctl->print));
259 * Remove a mixer control from a device.
262 mixer_remove_ctl(mix_ctl_t *ctl)
271 if (!TAILQ_EMPTY(&p->ctls)) {
272 TAILQ_REMOVE(&p->ctls, ctl, ctls);
280 * Get a mixer control by id.
283 mixer_get_ctl(struct mix_dev *d, int id)
287 TAILQ_FOREACH(cp, &d->ctls, ctls) {
297 * Get a mixer control by name.
300 mixer_get_ctl_byname(struct mix_dev *d, const char *name)
304 TAILQ_FOREACH(cp, &d->ctls, ctls) {
305 if (!strncmp(cp->name, name, sizeof(cp->name)))
314 * Change the mixer's left and right volume. The allowed volume values are
315 * between MIX_VOLMIN and MIX_VOLMAX. The `ioctl` for volume change requires
316 * an integer value between 0 and 100 stored as `lvol | rvol << 8` -- for
317 * that reason, we de-normalize the 32-bit float volume value, before
318 * we pass it to the `ioctl`.
320 * Volume clumping should be done by the caller.
323 mixer_set_vol(struct mixer *m, mix_volume_t vol)
327 if (vol.left < MIX_VOLMIN || vol.left > MIX_VOLMAX ||
328 vol.right < MIX_VOLMIN || vol.right > MIX_VOLMAX) {
332 v = MIX_VOLDENORM(vol.left) | MIX_VOLDENORM(vol.right) << 8;
333 if (ioctl(m->fd, MIXER_WRITE(m->dev->devno), &v) < 0)
335 if (_mixer_readvol(m, m->dev) < 0)
342 * Manipulate a device's mute.
344 * @param opt MIX_MUTE mute device
345 * MIX_UNMUTE unmute device
346 * MIX_TOGGLEMUTE toggle device's mute
349 mixer_set_mute(struct mixer *m, int opt)
353 m->mutemask |= (1 << m->dev->devno);
356 m->mutemask &= ~(1 << m->dev->devno);
359 m->mutemask ^= (1 << m->dev->devno);
365 if (ioctl(m->fd, SOUND_MIXER_WRITE_MUTE, &m->mutemask) < 0)
367 if (ioctl(m->fd, SOUND_MIXER_READ_MUTE, &m->mutemask) < 0)
374 * Modify a recording device. The selected device has to be a recording device,
375 * otherwise the function will fail.
377 * @param opt MIX_ADDRECSRC add device to recording sources
378 * MIX_REMOVERECSRC remove device from recording sources
379 * MIX_SETRECSRC set device as the only recording source
380 * MIX_TOGGLERECSRC toggle device from recording sources
383 mixer_mod_recsrc(struct mixer *m, int opt)
385 if (!m->recmask || !MIX_ISREC(m, m->dev->devno)) {
391 m->recsrc |= (1 << m->dev->devno);
393 case MIX_REMOVERECSRC:
394 m->recsrc &= ~(1 << m->dev->devno);
397 m->recsrc = (1 << m->dev->devno);
399 case MIX_TOGGLERECSRC:
400 m->recsrc ^= (1 << m->dev->devno);
406 if (ioctl(m->fd, SOUND_MIXER_WRITE_RECSRC, &m->recsrc) < 0)
408 if (ioctl(m->fd, SOUND_MIXER_READ_RECSRC, &m->recsrc) < 0)
415 * Get default audio card's number. This is used to open the default mixer
416 * and set the mixer structure's `f_default` flag.
419 mixer_get_dunit(void)
425 if (sysctlbyname("hw.snd.default_unit", &unit, &size, NULL, 0) < 0)
432 * Change the default audio card. This is normally _not_ a mixer feature, but
433 * it's useful to have, so the caller can avoid having to manually use
436 * @param unit the audio card number (e.g pcm0, pcm1, ...).
439 mixer_set_dunit(struct mixer *m, int unit)
444 if (sysctlbyname("hw.snd.default_unit", NULL, 0, &unit, size) < 0)
446 /* XXX: how will other mixers get updated? */
447 m->f_default = m->unit == unit;
453 * Get sound device mode (none, play, rec, play+rec). Userland programs can
454 * use the MIX_STATUS_* flags to determine the mode of the device.
457 mixer_get_mode(int unit)
463 (void)snprintf(buf, sizeof(buf), "dev.pcm.%d.mode", unit);
464 size = sizeof(unsigned int);
465 if (sysctlbyname(buf, &mode, &size, NULL, 0) < 0)
472 * Get the total number of mixers in the system.
475 mixer_get_nmixers(void)
481 * Open a dummy mixer because we need the `fd` field for the
484 if ((m = mixer_open(NULL)) == NULL)
486 if (ioctl(m->fd, OSS_SYSINFO, &si) < 0) {
487 (void)mixer_close(m);
490 (void)mixer_close(m);
492 return (si.nummixers);