Blame


1 e26da60a 2023-03-23 op /*
2 e26da60a 2023-03-23 op * Copyright (c) 2023 Omar Polo <op@omarpolo.com>
3 e26da60a 2023-03-23 op *
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.
7 e26da60a 2023-03-23 op *
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.
15 e26da60a 2023-03-23 op */
16 e26da60a 2023-03-23 op
17 e26da60a 2023-03-23 op #include "config.h"
18 e26da60a 2023-03-23 op
19 e26da60a 2023-03-23 op #include <alsa/asoundlib.h>
20 e26da60a 2023-03-23 op
21 e26da60a 2023-03-23 op #include <limits.h>
22 e26da60a 2023-03-23 op
23 e26da60a 2023-03-23 op #include "amused.h"
24 e26da60a 2023-03-23 op #include "log.h"
25 e26da60a 2023-03-23 op
26 e26da60a 2023-03-23 op static snd_pcm_t *pcm;
27 bbd1402d 2023-03-23 op static size_t bpf;
28 e26da60a 2023-03-23 op
29 e26da60a 2023-03-23 op int
30 e26da60a 2023-03-23 op audio_open(void (*onmove_cb)(void *, int))
31 e26da60a 2023-03-23 op {
32 e26da60a 2023-03-23 op const char *device = "default";
33 e26da60a 2023-03-23 op int err;
34 e26da60a 2023-03-23 op
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));
39 e26da60a 2023-03-23 op return -1;
40 e26da60a 2023-03-23 op }
41 e26da60a 2023-03-23 op
42 e26da60a 2023-03-23 op /* TODO: set up onmove callback? */
43 e26da60a 2023-03-23 op return 0;
44 e26da60a 2023-03-23 op }
45 e26da60a 2023-03-23 op
46 e26da60a 2023-03-23 op int
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)
49 e26da60a 2023-03-23 op {
50 e26da60a 2023-03-23 op int err;
51 e26da60a 2023-03-23 op snd_pcm_format_t fmt;
52 e26da60a 2023-03-23 op
53 bbd1402d 2023-03-23 op if (bits == 8) {
54 e26da60a 2023-03-23 op fmt = SND_PCM_FORMAT_S8;
55 bbd1402d 2023-03-23 op bpf = 1;
56 bbd1402d 2023-03-23 op } else if (bits == 16) {
57 e26da60a 2023-03-23 op fmt = SND_PCM_FORMAT_S16;
58 bbd1402d 2023-03-23 op bpf = 2;
59 bbd1402d 2023-03-23 op } else if (bits == 24) {
60 e26da60a 2023-03-23 op fmt = SND_PCM_FORMAT_S24;
61 bbd1402d 2023-03-23 op bpf = 4;
62 bbd1402d 2023-03-23 op } else if (bits == 32) {
63 e26da60a 2023-03-23 op fmt = SND_PCM_FORMAT_S32;
64 bbd1402d 2023-03-23 op bpf = 4;
65 bbd1402d 2023-03-23 op } else {
66 e26da60a 2023-03-23 op log_warnx("can't handle %d bits", bits);
67 e26da60a 2023-03-23 op return -1;
68 e26da60a 2023-03-23 op }
69 bbd1402d 2023-03-23 op
70 bbd1402d 2023-03-23 op bpf *= channels;
71 e26da60a 2023-03-23 op
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));
76 e26da60a 2023-03-23 op return -1;
77 e26da60a 2023-03-23 op }
78 e26da60a 2023-03-23 op
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));
82 e26da60a 2023-03-23 op return -1;
83 e26da60a 2023-03-23 op }
84 e26da60a 2023-03-23 op
85 e26da60a 2023-03-23 op return 0;
86 e26da60a 2023-03-23 op }
87 e26da60a 2023-03-23 op
88 e26da60a 2023-03-23 op int
89 e26da60a 2023-03-23 op audio_nfds(void)
90 e26da60a 2023-03-23 op {
91 e26da60a 2023-03-23 op return snd_pcm_poll_descriptors_count(pcm);
92 e26da60a 2023-03-23 op }
93 e26da60a 2023-03-23 op
94 e26da60a 2023-03-23 op int
95 e26da60a 2023-03-23 op audio_pollfd(struct pollfd *pfds, int events)
96 e26da60a 2023-03-23 op {
97 e26da60a 2023-03-23 op return snd_pcm_poll_descriptors(pcm, pfds, audio_nfds());
98 e26da60a 2023-03-23 op }
99 e26da60a 2023-03-23 op
100 e26da60a 2023-03-23 op int
101 e26da60a 2023-03-23 op audio_revents(struct pollfd *pfds)
102 e26da60a 2023-03-23 op {
103 e26da60a 2023-03-23 op int err;
104 e26da60a 2023-03-23 op unsigned short revents;
105 e26da60a 2023-03-23 op
106 e26da60a 2023-03-23 op err = snd_pcm_poll_descriptors_revents(pcm, pfds, audio_nfds(),
107 e26da60a 2023-03-23 op &revents);
108 e26da60a 2023-03-23 op if (err < 0) {
109 e26da60a 2023-03-23 op log_warnx("snd revents failure: %s", snd_strerror(err));
110 e26da60a 2023-03-23 op return 0;
111 e26da60a 2023-03-23 op }
112 e26da60a 2023-03-23 op
113 e26da60a 2023-03-23 op return revents;
114 e26da60a 2023-03-23 op }
115 e26da60a 2023-03-23 op
116 e26da60a 2023-03-23 op size_t
117 e26da60a 2023-03-23 op audio_write(const void *buf, size_t len)
118 e26da60a 2023-03-23 op {
119 bbd1402d 2023-03-23 op snd_pcm_sframes_t avail, ret;
120 e26da60a 2023-03-23 op
121 bbd1402d 2023-03-23 op /*
122 bbd1402d 2023-03-23 op * snd_pcm_writei works in terms of FRAMES, not BYTES!
123 bbd1402d 2023-03-23 op */
124 bbd1402d 2023-03-23 op len /= bpf;
125 bbd1402d 2023-03-23 op
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");
130 bbd1402d 2023-03-23 op return 0;
131 bbd1402d 2023-03-23 op }
132 bbd1402d 2023-03-23 op log_warnx("snd_pcm_avail_update failure: %s",
133 bbd1402d 2023-03-23 op snd_strerror(avail));
134 bbd1402d 2023-03-23 op return 0;
135 bbd1402d 2023-03-23 op }
136 bbd1402d 2023-03-23 op
137 bbd1402d 2023-03-23 op if (len > avail)
138 bbd1402d 2023-03-23 op len = avail;
139 bbd1402d 2023-03-23 op
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));
143 e26da60a 2023-03-23 op return 0;
144 e26da60a 2023-03-23 op }
145 bbd1402d 2023-03-23 op return ret * bpf;
146 e26da60a 2023-03-23 op }
147 e26da60a 2023-03-23 op
148 e26da60a 2023-03-23 op int
149 e26da60a 2023-03-23 op audio_flush(void)
150 e26da60a 2023-03-23 op {
151 e26da60a 2023-03-23 op int err;
152 e26da60a 2023-03-23 op
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));
156 e26da60a 2023-03-23 op return -1;
157 e26da60a 2023-03-23 op }
158 e26da60a 2023-03-23 op
159 e26da60a 2023-03-23 op return 0;
160 e26da60a 2023-03-23 op }
161 e26da60a 2023-03-23 op
162 e26da60a 2023-03-23 op int
163 e26da60a 2023-03-23 op audio_stop(void)
164 e26da60a 2023-03-23 op {
165 e26da60a 2023-03-23 op int err;
166 e26da60a 2023-03-23 op
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));
170 e26da60a 2023-03-23 op return -1;
171 e26da60a 2023-03-23 op }
172 e26da60a 2023-03-23 op
173 e26da60a 2023-03-23 op return 0;
174 e26da60a 2023-03-23 op }