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 e26da60a 2023-03-23 op
28 e26da60a 2023-03-23 op int
29 e26da60a 2023-03-23 op audio_open(void (*onmove_cb)(void *, int))
30 e26da60a 2023-03-23 op {
31 e26da60a 2023-03-23 op const char *device = "default";
32 e26da60a 2023-03-23 op int err;
33 e26da60a 2023-03-23 op
34 e26da60a 2023-03-23 op err = snd_pcm_open(&pcm, device, SND_PCM_STREAM_PLAYBACK,
35 e26da60a 2023-03-23 op SND_PCM_NONBLOCK);
36 e26da60a 2023-03-23 op if (err < 0) {
37 e26da60a 2023-03-23 op log_warnx("playback open error: %s", snd_strerror(err));
38 e26da60a 2023-03-23 op return -1;
39 e26da60a 2023-03-23 op }
40 e26da60a 2023-03-23 op
41 e26da60a 2023-03-23 op /* TODO: set up onmove callback? */
42 e26da60a 2023-03-23 op return 0;
43 e26da60a 2023-03-23 op }
44 e26da60a 2023-03-23 op
45 e26da60a 2023-03-23 op int
46 e26da60a 2023-03-23 op audio_setup(unsigned int bits, unsigned int rate, unsigned int channels,
47 e26da60a 2023-03-23 op struct pollfd *pfds)
48 e26da60a 2023-03-23 op {
49 e26da60a 2023-03-23 op int err;
50 e26da60a 2023-03-23 op snd_pcm_format_t fmt;
51 e26da60a 2023-03-23 op
52 e26da60a 2023-03-23 op if (bits == 8)
53 e26da60a 2023-03-23 op fmt = SND_PCM_FORMAT_S8;
54 e26da60a 2023-03-23 op else if (bits == 16)
55 e26da60a 2023-03-23 op fmt = SND_PCM_FORMAT_S16;
56 e26da60a 2023-03-23 op else if (bits == 24)
57 e26da60a 2023-03-23 op fmt = SND_PCM_FORMAT_S24;
58 e26da60a 2023-03-23 op else if (bits == 32)
59 e26da60a 2023-03-23 op fmt = SND_PCM_FORMAT_S32;
60 e26da60a 2023-03-23 op else {
61 e26da60a 2023-03-23 op log_warnx("can't handle %d bits", bits);
62 e26da60a 2023-03-23 op return -1;
63 e26da60a 2023-03-23 op }
64 e26da60a 2023-03-23 op
65 e26da60a 2023-03-23 op err = snd_pcm_set_params(pcm, fmt, SND_PCM_ACCESS_RW_INTERLEAVED,
66 e26da60a 2023-03-23 op channels, rate, 1, 500000 /* 0.5s */);
67 e26da60a 2023-03-23 op if (err < 0) {
68 e26da60a 2023-03-23 op log_warnx("invalid params: %s", snd_strerror(err));
69 e26da60a 2023-03-23 op return -1;
70 e26da60a 2023-03-23 op }
71 e26da60a 2023-03-23 op
72 e26da60a 2023-03-23 op err = snd_pcm_prepare(pcm);
73 e26da60a 2023-03-23 op if (err < 0) {
74 e26da60a 2023-03-23 op log_warnx("snd_pcm_prepare failed: %s", snd_strerror(err));
75 e26da60a 2023-03-23 op return -1;
76 e26da60a 2023-03-23 op }
77 e26da60a 2023-03-23 op
78 e26da60a 2023-03-23 op return 0;
79 e26da60a 2023-03-23 op }
80 e26da60a 2023-03-23 op
81 e26da60a 2023-03-23 op int
82 e26da60a 2023-03-23 op audio_nfds(void)
83 e26da60a 2023-03-23 op {
84 e26da60a 2023-03-23 op return snd_pcm_poll_descriptors_count(pcm);
85 e26da60a 2023-03-23 op }
86 e26da60a 2023-03-23 op
87 e26da60a 2023-03-23 op int
88 e26da60a 2023-03-23 op audio_pollfd(struct pollfd *pfds, int events)
89 e26da60a 2023-03-23 op {
90 e26da60a 2023-03-23 op return snd_pcm_poll_descriptors(pcm, pfds, audio_nfds());
91 e26da60a 2023-03-23 op }
92 e26da60a 2023-03-23 op
93 e26da60a 2023-03-23 op int
94 e26da60a 2023-03-23 op audio_revents(struct pollfd *pfds)
95 e26da60a 2023-03-23 op {
96 e26da60a 2023-03-23 op int err;
97 e26da60a 2023-03-23 op unsigned short revents;
98 e26da60a 2023-03-23 op
99 e26da60a 2023-03-23 op err = snd_pcm_poll_descriptors_revents(pcm, pfds, audio_nfds(),
100 e26da60a 2023-03-23 op &revents);
101 e26da60a 2023-03-23 op if (err < 0) {
102 e26da60a 2023-03-23 op log_warnx("snd revents failure: %s", snd_strerror(err));
103 e26da60a 2023-03-23 op return 0;
104 e26da60a 2023-03-23 op }
105 e26da60a 2023-03-23 op
106 e26da60a 2023-03-23 op return revents;
107 e26da60a 2023-03-23 op }
108 e26da60a 2023-03-23 op
109 e26da60a 2023-03-23 op size_t
110 e26da60a 2023-03-23 op audio_write(const void *buf, size_t len)
111 e26da60a 2023-03-23 op {
112 e26da60a 2023-03-23 op snd_pcm_sframes_t ret;
113 e26da60a 2023-03-23 op
114 e26da60a 2023-03-23 op ret = snd_pcm_writei(pcm, buf, len);
115 e26da60a 2023-03-23 op if (ret < 0) {
116 e26da60a 2023-03-23 op log_warnx("snd_pcm_writei failed: %s", snd_strerror(ret));
117 e26da60a 2023-03-23 op return 0;
118 e26da60a 2023-03-23 op }
119 e26da60a 2023-03-23 op return ret;
120 e26da60a 2023-03-23 op }
121 e26da60a 2023-03-23 op
122 e26da60a 2023-03-23 op int
123 e26da60a 2023-03-23 op audio_flush(void)
124 e26da60a 2023-03-23 op {
125 e26da60a 2023-03-23 op int err;
126 e26da60a 2023-03-23 op
127 e26da60a 2023-03-23 op err = snd_pcm_drop(pcm);
128 e26da60a 2023-03-23 op if (err < 0) {
129 e26da60a 2023-03-23 op log_warnx("snd_pcm_drop: %s", snd_strerror(err));
130 e26da60a 2023-03-23 op return -1;
131 e26da60a 2023-03-23 op }
132 e26da60a 2023-03-23 op
133 e26da60a 2023-03-23 op return 0;
134 e26da60a 2023-03-23 op }
135 e26da60a 2023-03-23 op
136 e26da60a 2023-03-23 op int
137 e26da60a 2023-03-23 op audio_stop(void)
138 e26da60a 2023-03-23 op {
139 e26da60a 2023-03-23 op int err;
140 e26da60a 2023-03-23 op
141 e26da60a 2023-03-23 op err = snd_pcm_drain(pcm);
142 e26da60a 2023-03-23 op if (err < 0) {
143 e26da60a 2023-03-23 op log_warnx("snd_pcm_drain: %s", snd_strerror(err));
144 e26da60a 2023-03-23 op return -1;
145 e26da60a 2023-03-23 op }
146 e26da60a 2023-03-23 op
147 e26da60a 2023-03-23 op return 0;
148 e26da60a 2023-03-23 op }