1/* $OpenBSD: sndio.h,v 1.17 2026/01/22 09:31:22 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 16 /* max name length */
32#define SIOCTL_DISPLAYMAX 32 /* max display string length */
33
34/*
35 * private ``handle'' structure
36 */
37struct sio_hdl;
38struct mio_hdl;
39struct sioctl_hdl;
40
41/*
42 * parameters of a full-duplex stream
43 */
44struct sio_par {
45 unsigned int bits; /* bits per sample */
46 unsigned int bps; /* bytes per sample */
47 unsigned int sig; /* 1 = signed, 0 = unsigned */
48 unsigned int le; /* 1 = LE, 0 = BE byte order */
49 unsigned int msb; /* 1 = MSB, 0 = LSB aligned */
50 unsigned int rchan; /* number channels for recording direction */
51 unsigned int pchan; /* number channels for playback direction */
52 unsigned int rate; /* frames per second */
53 unsigned int bufsz; /* end-to-end buffer size */
54#define SIO_IGNORE 0 /* pause during xrun */
55#define SIO_SYNC 1 /* resync after xrun */
56#define SIO_ERROR 2 /* terminate on xrun */
57 unsigned int xrun; /* what to do on overruns/underruns */
58 unsigned int round; /* optimal bufsz divisor */
59 unsigned int appbufsz; /* minimum buffer size */
60 int __pad[3]; /* for future use */
61 unsigned int __magic; /* for internal/debug purposes only */
62};
63
64/*
65 * capabilities of a stream
66 */
67struct sio_cap {
68#define SIO_NENC 8
69#define SIO_NCHAN 8
70#define SIO_NRATE 16
71#define SIO_NCONF 4
72 struct sio_enc { /* allowed sample encodings */
73 unsigned int bits;
74 unsigned int bps;
75 unsigned int sig;
76 unsigned int le;
77 unsigned int msb;
78 } enc[SIO_NENC];
79 unsigned int rchan[SIO_NCHAN]; /* allowed values for rchan */
80 unsigned int pchan[SIO_NCHAN]; /* allowed values for pchan */
81 unsigned int rate[SIO_NRATE]; /* allowed rates */
82 int __pad[7]; /* for future use */
83 unsigned int nconf; /* number of elements in confs[] */
84 struct sio_conf {
85 unsigned int enc; /* mask of enc[] indexes */
86 unsigned int rchan; /* mask of chan[] indexes (rec) */
87 unsigned int pchan; /* mask of chan[] indexes (play) */
88 unsigned int rate; /* mask of rate[] indexes */
89 } confs[SIO_NCONF];
90};
91
92#define SIO_XSTRINGS { "ignore", "sync", "error" }
93
94/*
95 * controlled component of the device
96 */
97struct sioctl_node {
98 char name[SIOCTL_NAMEMAX]; /* ex. "spkr" */
99 int unit; /* optional number or -1 */
100};
101
102/*
103 * description of a control (index, value) pair
104 */
105struct sioctl_desc {
106 unsigned int addr; /* control address */
107#define SIOCTL_NONE 0 /* deleted */
108#define SIOCTL_NUM 2 /* integer in the 0..maxval range */
109#define SIOCTL_SW 3 /* on/off switch (0 or 1) */
110#define SIOCTL_VEC 4 /* number, element of vector */
111#define SIOCTL_LIST 5 /* switch, element of a list */
112#define SIOCTL_SEL 6 /* element of a selector */
113 unsigned int type; /* one of above */
114 unsigned int maxval; /* max value */
115 int __pad[3]; /* for future use */
116 char func[SIOCTL_NAMEMAX]; /* function name, ex. "level" */
117 char group[SIOCTL_NAMEMAX]; /* group this control belongs to */
118 struct sioctl_node node0; /* affected node */
119 struct sioctl_node node1; /* dito for SIOCTL_{VEC,LIST,SEL} */
120 char display[SIOCTL_DISPLAYMAX]; /* free-format hint */
121};
122
123/*
124 * mode bitmap
125 */
126#define SIO_PLAY 1
127#define SIO_REC 2
128#define MIO_OUT 4
129#define MIO_IN 8
130#define SIOCTL_READ 0x100
131#define SIOCTL_WRITE 0x200
132
133/*
134 * default bytes per sample for the given bits per sample
135 */
136#define SIO_BPS(bits) (((bits) <= 8) ? 1 : (((bits) <= 16) ? 2 : 4))
137
138/*
139 * default value of "sio_par->le" flag
140 */
141#if BYTE_ORDER == LITTLE_ENDIAN
142#define SIO_LE_NATIVE 1
143#else
144#define SIO_LE_NATIVE 0
145#endif
146
147/*
148 * maximum value of volume, eg. for sio_setvol()
149 */
150#define SIO_MAXVOL 127
151
152#ifdef __cplusplus
153extern "C" {
154#endif
155
156struct pollfd;
157
158void sio_initpar(struct sio_par *);
159struct sio_hdl *sio_open(const char *, unsigned int, int);
160void sio_close(struct sio_hdl *);
161int sio_setpar(struct sio_hdl *, struct sio_par *);
162int sio_getpar(struct sio_hdl *, struct sio_par *);
163int sio_getcap(struct sio_hdl *, struct sio_cap *);
164void sio_onmove(struct sio_hdl *, void (*)(void *, int), void *);
165void sio_onxrun(struct sio_hdl *, void (*)(void *), void *);
166size_t sio_write(struct sio_hdl *, const void *, size_t);
167size_t sio_read(struct sio_hdl *, void *, size_t);
168int sio_start(struct sio_hdl *);
169int sio_stop(struct sio_hdl *);
170int sio_flush(struct sio_hdl *);
171int sio_nfds(struct sio_hdl *);
172int sio_pollfd(struct sio_hdl *, struct pollfd *, int);
173int sio_revents(struct sio_hdl *, struct pollfd *);
174int sio_eof(struct sio_hdl *);
175int sio_setvol(struct sio_hdl *, unsigned int);
176int sio_onvol(struct sio_hdl *, void (*)(void *, unsigned int), void *);
177
178struct mio_hdl *mio_open(const char *, unsigned int, int);
179void mio_close(struct mio_hdl *);
180size_t mio_write(struct mio_hdl *, const void *, size_t);
181size_t mio_read(struct mio_hdl *, void *, size_t);
182int mio_nfds(struct mio_hdl *);
183int mio_pollfd(struct mio_hdl *, struct pollfd *, int);
184int mio_revents(struct mio_hdl *, struct pollfd *);
185int mio_eof(struct mio_hdl *);
186
187struct sioctl_hdl *sioctl_open(const char *, unsigned int, int);
188void sioctl_close(struct sioctl_hdl *);
189int sioctl_ondesc(struct sioctl_hdl *,
190 void (*)(void *, struct sioctl_desc *, int), void *);
191int sioctl_onval(struct sioctl_hdl *,
192 void (*)(void *, unsigned int, unsigned int), void *);
193int sioctl_setval(struct sioctl_hdl *, unsigned int, unsigned int);
194int sioctl_nfds(struct sioctl_hdl *);
195int sioctl_pollfd(struct sioctl_hdl *, struct pollfd *, int);
196int sioctl_revents(struct sioctl_hdl *, struct pollfd *);
197int sioctl_eof(struct sioctl_hdl *);
198
199int mio_rmidi_getfd(const char *, unsigned int, int);
200struct mio_hdl *mio_rmidi_fdopen(int, unsigned int, int);
201int sio_sun_getfd(const char *, unsigned int, int);
202struct sio_hdl *sio_sun_fdopen(int, unsigned int, int);
203int sioctl_sun_getfd(const char *, unsigned int, int);
204struct sioctl_hdl *sioctl_sun_fdopen(int, unsigned int, int);
205
206#ifdef __cplusplus
207}
208#endif
209
210#endif /* !defined(SNDIO_H) */