2 e26da60a 2023-03-23 op * Copyright (c) 2023 Omar Polo <op@omarpolo.com>
4 e26da60a 2023-03-23 op * Permission to use, copy, modify, and distribute this software for any
5 e26da60a 2023-03-23 op * purpose with or without fee is hereby granted, provided that the above
6 e26da60a 2023-03-23 op * copyright notice and this permission notice appear in all copies.
8 e26da60a 2023-03-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 e26da60a 2023-03-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 e26da60a 2023-03-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 e26da60a 2023-03-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 e26da60a 2023-03-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 e26da60a 2023-03-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 e26da60a 2023-03-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 e26da60a 2023-03-23 op #include "config.h"
19 e26da60a 2023-03-23 op #include <alsa/asoundlib.h>
21 e26da60a 2023-03-23 op #include <limits.h>
23 e26da60a 2023-03-23 op #include "amused.h"
24 e26da60a 2023-03-23 op #include "log.h"
26 e26da60a 2023-03-23 op static snd_pcm_t *pcm;
27 bbd1402d 2023-03-23 op static size_t bpf;
30 e26da60a 2023-03-23 op audio_open(void (*onmove_cb)(void *, int))
32 e26da60a 2023-03-23 op const char *device = "default";
35 e26da60a 2023-03-23 op err = snd_pcm_open(&pcm, device, SND_PCM_STREAM_PLAYBACK,
36 e26da60a 2023-03-23 op SND_PCM_NONBLOCK);
37 e26da60a 2023-03-23 op if (err < 0) {
38 e26da60a 2023-03-23 op log_warnx("playback open error: %s", snd_strerror(err));
42 e26da60a 2023-03-23 op /* TODO: set up onmove callback? */
47 e26da60a 2023-03-23 op audio_setup(unsigned int bits, unsigned int rate, unsigned int channels,
48 e26da60a 2023-03-23 op struct pollfd *pfds)
51 e26da60a 2023-03-23 op snd_pcm_format_t fmt;
53 bbd1402d 2023-03-23 op if (bits == 8) {
54 e26da60a 2023-03-23 op fmt = SND_PCM_FORMAT_S8;
56 bbd1402d 2023-03-23 op } else if (bits == 16) {
57 e26da60a 2023-03-23 op fmt = SND_PCM_FORMAT_S16;
59 bbd1402d 2023-03-23 op } else if (bits == 24) {
60 e26da60a 2023-03-23 op fmt = SND_PCM_FORMAT_S24;
62 bbd1402d 2023-03-23 op } else if (bits == 32) {
63 e26da60a 2023-03-23 op fmt = SND_PCM_FORMAT_S32;
66 e26da60a 2023-03-23 op log_warnx("can't handle %d bits", bits);
70 bbd1402d 2023-03-23 op bpf *= channels;
72 e26da60a 2023-03-23 op err = snd_pcm_set_params(pcm, fmt, SND_PCM_ACCESS_RW_INTERLEAVED,
73 e26da60a 2023-03-23 op channels, rate, 1, 500000 /* 0.5s */);
74 e26da60a 2023-03-23 op if (err < 0) {
75 e26da60a 2023-03-23 op log_warnx("invalid params: %s", snd_strerror(err));
79 e26da60a 2023-03-23 op err = snd_pcm_prepare(pcm);
80 e26da60a 2023-03-23 op if (err < 0) {
81 e26da60a 2023-03-23 op log_warnx("snd_pcm_prepare failed: %s", snd_strerror(err));
89 e26da60a 2023-03-23 op audio_nfds(void)
91 e26da60a 2023-03-23 op return snd_pcm_poll_descriptors_count(pcm);
95 e26da60a 2023-03-23 op audio_pollfd(struct pollfd *pfds, int events)
97 e26da60a 2023-03-23 op return snd_pcm_poll_descriptors(pcm, pfds, audio_nfds());
101 e26da60a 2023-03-23 op audio_revents(struct pollfd *pfds)
104 e26da60a 2023-03-23 op unsigned short revents;
106 e26da60a 2023-03-23 op err = snd_pcm_poll_descriptors_revents(pcm, pfds, audio_nfds(),
108 e26da60a 2023-03-23 op if (err < 0) {
109 e26da60a 2023-03-23 op log_warnx("snd revents failure: %s", snd_strerror(err));
113 e26da60a 2023-03-23 op return revents;
117 e26da60a 2023-03-23 op audio_write(const void *buf, size_t len)
119 bbd1402d 2023-03-23 op snd_pcm_sframes_t avail, ret;
122 bbd1402d 2023-03-23 op * snd_pcm_writei works in terms of FRAMES, not BYTES!
126 bbd1402d 2023-03-23 op avail = snd_pcm_avail_update(pcm);
127 bbd1402d 2023-03-23 op if (avail < 0) {
128 bbd1402d 2023-03-23 op if (avail == -EPIPE) {
129 bbd1402d 2023-03-23 op log_warnx("alsa xrun occurred");
132 bbd1402d 2023-03-23 op log_warnx("snd_pcm_avail_update failure: %s",
133 bbd1402d 2023-03-23 op snd_strerror(avail));
137 bbd1402d 2023-03-23 op if (len > avail)
140 e26da60a 2023-03-23 op ret = snd_pcm_writei(pcm, buf, len);
141 e26da60a 2023-03-23 op if (ret < 0) {
142 e26da60a 2023-03-23 op log_warnx("snd_pcm_writei failed: %s", snd_strerror(ret));
145 bbd1402d 2023-03-23 op return ret * bpf;
149 e26da60a 2023-03-23 op audio_flush(void)
153 e26da60a 2023-03-23 op err = snd_pcm_drop(pcm);
154 e26da60a 2023-03-23 op if (err < 0) {
155 e26da60a 2023-03-23 op log_warnx("snd_pcm_drop: %s", snd_strerror(err));
163 e26da60a 2023-03-23 op audio_stop(void)
167 e26da60a 2023-03-23 op err = snd_pcm_drain(pcm);
168 e26da60a 2023-03-23 op if (err < 0) {
169 e26da60a 2023-03-23 op log_warnx("snd_pcm_drain: %s", snd_strerror(err));