xref: /freebsd/lib/libmixer/mixer.h (revision ec965063070e5753c166cf592c9336444b74720a)
1 /*-
2  * Copyright (c) 2021 Christos Margiolis <christos@FreeBSD.org>
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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
20  * THE SOFTWARE.
21  *
22  * $FreeBSD$
23  */
24 
25 #ifndef _MIXER_H_
26 #define _MIXER_H_
27 
28 #include <sys/cdefs.h>
29 #include <sys/queue.h>
30 #include <sys/soundcard.h>
31 
32 #include <limits.h>
33 
34 #define MIX_ISSET(n,f)		(((1U << (n)) & (f)) ? 1 : 0)
35 #define MIX_ISDEV(m,n)		MIX_ISSET(n, (m)->devmask)
36 #define MIX_ISMUTE(m,n)		MIX_ISSET(n, (m)->mutemask)
37 #define MIX_ISREC(m,n)		MIX_ISSET(n, (m)->recmask)
38 #define MIX_ISRECSRC(m,n)	MIX_ISSET(n, (m)->recsrc)
39 
40 /* Forward declarations */
41 struct mixer;
42 struct mix_dev;
43 
44 typedef struct mix_ctl mix_ctl_t;
45 typedef struct mix_volume mix_volume_t;
46 
47 /* User-defined controls */
48 struct mix_ctl {
49 	struct mix_dev *parent_dev;		/* parent device */
50 	int id;					/* control id */
51 	char name[NAME_MAX];			/* control name */
52 	int (*mod)(struct mix_dev *, void *);	/* modify control values */
53 	int (*print)(struct mix_dev *, void *);	/* print control */
54 	TAILQ_ENTRY(mix_ctl) ctls;
55 };
56 
57 struct mix_dev {
58 	struct mixer *parent_mixer;		/* parent mixer */
59 	char name[NAME_MAX];			/* device name (e.g "vol") */
60 	int devno;				/* device number */
61 	struct mix_volume {
62 #define MIX_VOLMIN		0.0f
63 #define MIX_VOLMAX		1.0f
64 #define MIX_VOLNORM(v)		((v) / 100.0f)
65 #define MIX_VOLDENORM(v)	((int)((v) * 100.0f + 0.5f))
66 		float left;			/* left volume */
67 		float right;			/* right volume */
68 	} vol;
69 	int nctl;				/* number of controls */
70 	TAILQ_HEAD(mix_ctlhead, mix_ctl) ctls;	/* control list */
71 	TAILQ_ENTRY(mix_dev) devs;
72 };
73 
74 struct mixer {
75 	TAILQ_HEAD(mix_devhead, mix_dev) devs;	/* device list */
76 	struct mix_dev *dev;			/* selected device */
77 	oss_mixerinfo mi;			/* mixer info */
78 	oss_card_info ci;			/* audio card info */
79 	char name[NAME_MAX];			/* mixer name (e.g /dev/mixer0) */
80 	int fd;					/* file descriptor */
81 	int unit;				/* audio card unit */
82 	int ndev;				/* number of devices */
83 	int devmask;				/* supported devices */
84 #define MIX_MUTE		0x01
85 #define MIX_UNMUTE		0x02
86 #define MIX_TOGGLEMUTE		0x04
87 	int mutemask;				/* muted devices */
88 	int recmask;				/* recording devices */
89 #define MIX_ADDRECSRC		0x01
90 #define MIX_REMOVERECSRC	0x02
91 #define MIX_SETRECSRC		0x04
92 #define MIX_TOGGLERECSRC	0x08
93 	int recsrc;				/* recording sources */
94 #define MIX_MODE_MIXER		0x01
95 #define MIX_MODE_PLAY		0x02
96 #define MIX_MODE_REC		0x04
97 	int mode;				/* dev.pcm.X.mode sysctl */
98 	int f_default;				/* default mixer flag */
99 };
100 
101 __BEGIN_DECLS
102 
103 struct mixer *mixer_open(const char *);
104 int mixer_close(struct mixer *);
105 struct mix_dev *mixer_get_dev(struct mixer *, int);
106 struct mix_dev *mixer_get_dev_byname(struct mixer *, const char *);
107 int mixer_add_ctl(struct mix_dev *, int, const char *,
108     int (*)(struct mix_dev *, void *), int (*)(struct mix_dev *, void *));
109 int mixer_add_ctl_s(mix_ctl_t *);
110 int mixer_remove_ctl(mix_ctl_t *);
111 mix_ctl_t *mixer_get_ctl(struct mix_dev *, int);
112 mix_ctl_t *mixer_get_ctl_byname(struct mix_dev *, const char *);
113 int mixer_set_vol(struct mixer *, mix_volume_t);
114 int mixer_set_mute(struct mixer *, int);
115 int mixer_mod_recsrc(struct mixer *, int);
116 int mixer_get_dunit(void);
117 int mixer_set_dunit(struct mixer *, int);
118 int mixer_get_mode(int);
119 int mixer_get_nmixers(void);
120 
121 __END_DECLS
122 
123 #endif /* _MIXER_H_ */
124