2 * Copyright (c) 2023 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28 static struct sio_hdl *hdl;
29 static struct sio_par par;
30 static int stopped = 1;
33 audio_open(void (*onmove_cb)(void *, int))
35 if ((hdl = sio_open(SIO_DEVANY, SIO_PLAY, 1)) == NULL)
38 sio_onmove(hdl, onmove_cb, NULL);
43 audio_setup(unsigned int bits, unsigned int rate, unsigned int channels,
48 fpct = (rate * 5) / 100;
50 /* don't stop if the parameters are the same */
51 if (bits == par.bits && channels == par.pchan &&
52 par.rate - fpct <= rate && rate <= par.rate + fpct) {
68 if (!sio_setpar(hdl, &par)) {
69 if (errno == EAGAIN) {
70 nfds = sio_pollfd(hdl, pfds, POLLOUT);
71 if (poll(pfds, nfds, INFTIM) == -1)
75 log_warnx("invalid params (bits=%u, rate=%u, channels=%u)",
76 bits, rate, channels);
79 if (!sio_getpar(hdl, &par)) {
80 log_warnx("can't get params");
84 if (par.bits != bits || par.pchan != channels) {
85 log_warnx("failed to set params");
89 /* TODO: check sample rate? */
92 if (!sio_start(hdl)) {
93 log_warn("sio_start");
103 return sio_nfds(hdl);
107 audio_pollfd(struct pollfd *pfds, int events)
109 return sio_pollfd(hdl, pfds, events);
113 audio_revents(struct pollfd *pfds)
115 return sio_revents(hdl, pfds);
119 audio_write(const void *buf, size_t len)
121 return sio_write(hdl, buf, len);
128 return sio_flush(hdl);
135 return sio_stop(hdl);