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