1/*	$OpenBSD: sndio.h,v 1.14 2022/04/29 08:30:48 ratchov Exp $	*/
2/*
3 * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17#ifndef SNDIO_H
18#define SNDIO_H
19
20#include <sys/types.h>
21
22/*
23 * default audio device and MIDI port
24 */
25#define SIO_DEVANY	"default"
26#define MIO_PORTANY	"default"
27
28/*
29 * limits
30 */
31#define SIOCTL_NAMEMAX		12	/* max name length */
32
33/*
34 * private ``handle'' structure
35 */
36struct sio_hdl;
37struct mio_hdl;
38struct sioctl_hdl;
39
40/*
41 * parameters of a full-duplex stream
42 */
43struct sio_par {
44	unsigned int bits;	/* bits per sample */
45	unsigned int bps;	/* bytes per sample */
46	unsigned int sig;	/* 1 = signed, 0 = unsigned */
47	unsigned int le;	/* 1 = LE, 0 = BE byte order */
48	unsigned int msb;	/* 1 = MSB, 0 = LSB aligned */
49	unsigned int rchan;	/* number channels for recording direction */
50	unsigned int pchan;	/* number channels for playback direction */
51	unsigned int rate;	/* frames per second */
52	unsigned int bufsz;	/* end-to-end buffer size */
53#define SIO_IGNORE	0	/* pause during xrun */
54#define SIO_SYNC	1	/* resync after xrun */
55#define SIO_ERROR	2	/* terminate on xrun */
56	unsigned int xrun;	/* what to do on overruns/underruns */
57	unsigned int round;	/* optimal bufsz divisor */
58	unsigned int appbufsz;	/* minimum buffer size */
59	int __pad[3];		/* for future use */
60	unsigned int __magic;	/* for internal/debug purposes only */
61};
62
63/*
64 * capabilities of a stream
65 */
66struct sio_cap {
67#define SIO_NENC	8
68#define SIO_NCHAN	8
69#define SIO_NRATE	16
70#define SIO_NCONF	4
71	struct sio_enc {			/* allowed sample encodings */
72		unsigned int bits;
73		unsigned int bps;
74		unsigned int sig;
75		unsigned int le;
76		unsigned int msb;
77	} enc[SIO_NENC];
78	unsigned int rchan[SIO_NCHAN];	/* allowed values for rchan */
79	unsigned int pchan[SIO_NCHAN];	/* allowed values for pchan */
80	unsigned int rate[SIO_NRATE];	/* allowed rates */
81	int __pad[7];			/* for future use */
82	unsigned int nconf;		/* number of elements in confs[] */
83	struct sio_conf {
84		unsigned int enc;	/* mask of enc[] indexes */
85		unsigned int rchan;	/* mask of chan[] indexes (rec) */
86		unsigned int pchan;	/* mask of chan[] indexes (play) */
87		unsigned int rate;	/* mask of rate[] indexes */
88	} confs[SIO_NCONF];
89};
90
91#define SIO_XSTRINGS { "ignore", "sync", "error" }
92
93/*
94 * controlled component of the device
95 */
96struct sioctl_node {
97	char name[SIOCTL_NAMEMAX];	/* ex. "spkr" */
98	int unit;			/* optional number or -1 */
99};
100
101/*
102 * description of a control (index, value) pair
103 */
104struct sioctl_desc {
105	unsigned int addr;		/* control address */
106#define SIOCTL_NONE		0	/* deleted */
107#define SIOCTL_NUM		2	/* integer in the 0..maxval range */
108#define SIOCTL_SW		3	/* on/off switch (0 or 1) */
109#define SIOCTL_VEC		4	/* number, element of vector */
110#define SIOCTL_LIST		5	/* switch, element of a list */
111#define SIOCTL_SEL		6	/* element of a selector */
112	unsigned int type;		/* one of above */
113	char func[SIOCTL_NAMEMAX];	/* function name, ex. "level" */
114	char group[SIOCTL_NAMEMAX];	/* group this control belongs to */
115	struct sioctl_node node0;	/* affected node */
116	struct sioctl_node node1;	/* dito for SIOCTL_{VEC,LIST,SEL} */
117	unsigned int maxval;		/* max value */
118	int __pad[3];
119};
120
121/*
122 * mode bitmap
123 */
124#define SIO_PLAY	1
125#define SIO_REC		2
126#define MIO_OUT		4
127#define MIO_IN		8
128#define SIOCTL_READ	0x100
129#define SIOCTL_WRITE	0x200
130
131/*
132 * default bytes per sample for the given bits per sample
133 */
134#define SIO_BPS(bits) (((bits) <= 8) ? 1 : (((bits) <= 16) ? 2 : 4))
135
136/*
137 * default value of "sio_par->le" flag
138 */
139#if BYTE_ORDER == LITTLE_ENDIAN
140#define SIO_LE_NATIVE 1
141#else
142#define SIO_LE_NATIVE 0
143#endif
144
145/*
146 * maximum value of volume, eg. for sio_setvol()
147 */
148#define SIO_MAXVOL 127
149
150#ifdef __cplusplus
151extern "C" {
152#endif
153
154struct pollfd;
155
156void sio_initpar(struct sio_par *);
157struct sio_hdl *sio_open(const char *, unsigned int, int);
158void sio_close(struct sio_hdl *);
159int sio_setpar(struct sio_hdl *, struct sio_par *);
160int sio_getpar(struct sio_hdl *, struct sio_par *);
161int sio_getcap(struct sio_hdl *, struct sio_cap *);
162void sio_onmove(struct sio_hdl *, void (*)(void *, int), void *);
163size_t sio_write(struct sio_hdl *, const void *, size_t);
164size_t sio_read(struct sio_hdl *, void *, size_t);
165int sio_start(struct sio_hdl *);
166int sio_stop(struct sio_hdl *);
167int sio_flush(struct sio_hdl *);
168int sio_nfds(struct sio_hdl *);
169int sio_pollfd(struct sio_hdl *, struct pollfd *, int);
170int sio_revents(struct sio_hdl *, struct pollfd *);
171int sio_eof(struct sio_hdl *);
172int sio_setvol(struct sio_hdl *, unsigned int);
173int sio_onvol(struct sio_hdl *, void (*)(void *, unsigned int), void *);
174
175struct mio_hdl *mio_open(const char *, unsigned int, int);
176void mio_close(struct mio_hdl *);
177size_t mio_write(struct mio_hdl *, const void *, size_t);
178size_t mio_read(struct mio_hdl *, void *, size_t);
179int mio_nfds(struct mio_hdl *);
180int mio_pollfd(struct mio_hdl *, struct pollfd *, int);
181int mio_revents(struct mio_hdl *, struct pollfd *);
182int mio_eof(struct mio_hdl *);
183
184struct sioctl_hdl *sioctl_open(const char *, unsigned int, int);
185void sioctl_close(struct sioctl_hdl *);
186int sioctl_ondesc(struct sioctl_hdl *,
187    void (*)(void *, struct sioctl_desc *, int), void *);
188int sioctl_onval(struct sioctl_hdl *,
189    void (*)(void *, unsigned int, unsigned int), void *);
190int sioctl_setval(struct sioctl_hdl *, unsigned int, unsigned int);
191int sioctl_nfds(struct sioctl_hdl *);
192int sioctl_pollfd(struct sioctl_hdl *, struct pollfd *, int);
193int sioctl_revents(struct sioctl_hdl *, struct pollfd *);
194int sioctl_eof(struct sioctl_hdl *);
195
196int mio_rmidi_getfd(const char *, unsigned int, int);
197struct mio_hdl *mio_rmidi_fdopen(int, unsigned int, int);
198int sio_sun_getfd(const char *, unsigned int, int);
199struct sio_hdl *sio_sun_fdopen(int, unsigned int, int);
200int sioctl_sun_getfd(const char *, unsigned int, int);
201struct sioctl_hdl *sioctl_sun_fdopen(int, unsigned int, int);
202
203#ifdef __cplusplus
204}
205#endif
206
207#endif /* !defined(SNDIO_H) */
208