Blame


1 3baa2617 2022-02-16 op /*
2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 3baa2617 2022-02-16 op *
4 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
5 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
6 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
7 3baa2617 2022-02-16 op *
8 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 3baa2617 2022-02-16 op */
16 3baa2617 2022-02-16 op
17 3baa2617 2022-02-16 op #include <sys/mman.h>
18 3baa2617 2022-02-16 op #include <sys/stat.h>
19 3baa2617 2022-02-16 op #include <sys/types.h>
20 3baa2617 2022-02-16 op #include <sys/queue.h>
21 3baa2617 2022-02-16 op #include <sys/uio.h>
22 3baa2617 2022-02-16 op
23 3baa2617 2022-02-16 op #include <err.h>
24 3baa2617 2022-02-16 op #include <event.h>
25 3baa2617 2022-02-16 op #include <fcntl.h>
26 bb3f279f 2022-02-16 op #include <limits.h>
27 3baa2617 2022-02-16 op #include <sndio.h>
28 3baa2617 2022-02-16 op #include <stdio.h>
29 3baa2617 2022-02-16 op #include <stdint.h>
30 3baa2617 2022-02-16 op #include <imsg.h>
31 3baa2617 2022-02-16 op #include <unistd.h>
32 3baa2617 2022-02-16 op
33 821e4c10 2022-02-21 op #include <string.h>
34 821e4c10 2022-02-21 op
35 3baa2617 2022-02-16 op #include <mad.h>
36 3baa2617 2022-02-16 op
37 3baa2617 2022-02-16 op #include "amused.h"
38 795d0831 2022-02-18 op #include "log.h"
39 3baa2617 2022-02-16 op
40 3baa2617 2022-02-16 op struct mad_stream mad_stream;
41 3baa2617 2022-02-16 op struct mad_frame mad_frame;
42 3baa2617 2022-02-16 op struct mad_synth mad_synth;
43 3baa2617 2022-02-16 op
44 3baa2617 2022-02-16 op struct buffer {
45 3baa2617 2022-02-16 op const void *start;
46 3baa2617 2022-02-16 op size_t length;
47 3baa2617 2022-02-16 op int sample_rate;
48 18596703 2022-02-18 op int channels;
49 3baa2617 2022-02-16 op };
50 3baa2617 2022-02-16 op
51 3baa2617 2022-02-16 op static enum mad_flow
52 3baa2617 2022-02-16 op input(void *d, struct mad_stream *stream)
53 3baa2617 2022-02-16 op {
54 3baa2617 2022-02-16 op struct buffer *buffer = d;
55 3baa2617 2022-02-16 op
56 3baa2617 2022-02-16 op if (buffer->length == 0)
57 3baa2617 2022-02-16 op return MAD_FLOW_STOP;
58 3baa2617 2022-02-16 op
59 3baa2617 2022-02-16 op mad_stream_buffer(stream, buffer->start, buffer->length);
60 3baa2617 2022-02-16 op buffer->length = 0;
61 3baa2617 2022-02-16 op buffer->sample_rate = 0;
62 18596703 2022-02-18 op buffer->channels = 0;
63 3baa2617 2022-02-16 op return MAD_FLOW_CONTINUE;
64 3baa2617 2022-02-16 op }
65 3baa2617 2022-02-16 op
66 3baa2617 2022-02-16 op static enum mad_flow
67 3baa2617 2022-02-16 op output(void *data, const struct mad_header *header, struct mad_pcm *pcm)
68 3baa2617 2022-02-16 op {
69 821e4c10 2022-02-21 op static uint32_t buf[BUFSIZ];
70 3baa2617 2022-02-16 op size_t len;
71 3baa2617 2022-02-16 op struct buffer *buffer = data;
72 3baa2617 2022-02-16 op int nsamples, i;
73 821e4c10 2022-02-21 op uint32_t sample;
74 3baa2617 2022-02-16 op const mad_fixed_t *leftch, *rightch;
75 3baa2617 2022-02-16 op
76 3baa2617 2022-02-16 op if (player_shouldstop())
77 3baa2617 2022-02-16 op return MAD_FLOW_STOP;
78 3baa2617 2022-02-16 op
79 3baa2617 2022-02-16 op nsamples = pcm->length;
80 3baa2617 2022-02-16 op leftch = pcm->samples[0];
81 3baa2617 2022-02-16 op rightch = pcm->samples[1];
82 3baa2617 2022-02-16 op
83 18596703 2022-02-18 op if (buffer->sample_rate != pcm->samplerate ||
84 18596703 2022-02-18 op buffer->channels != pcm->channels) {
85 3baa2617 2022-02-16 op buffer->sample_rate = pcm->samplerate;
86 18596703 2022-02-18 op buffer->channels = pcm->channels;
87 821e4c10 2022-02-21 op if (player_setup(32, pcm->samplerate, pcm->channels) == -1)
88 3baa2617 2022-02-16 op err(1, "player_setrate");
89 3baa2617 2022-02-16 op }
90 3baa2617 2022-02-16 op
91 3baa2617 2022-02-16 op for (i = 0, len = 0; i < nsamples; ++i) {
92 821e4c10 2022-02-21 op if (len+2 >= sizeof(buf)) {
93 821e4c10 2022-02-21 op sio_write(hdl, buf, len * sizeof(sample));
94 3baa2617 2022-02-16 op len = 0;
95 3baa2617 2022-02-16 op }
96 3baa2617 2022-02-16 op
97 821e4c10 2022-02-21 op sample = *leftch++;
98 821e4c10 2022-02-21 op buf[len++] = sample;
99 3baa2617 2022-02-16 op
100 18596703 2022-02-18 op if (pcm->channels == 2) {
101 821e4c10 2022-02-21 op sample = *rightch++;
102 821e4c10 2022-02-21 op buf[len++] = sample;
103 18596703 2022-02-18 op }
104 3baa2617 2022-02-16 op }
105 3baa2617 2022-02-16 op
106 3baa2617 2022-02-16 op if (len != 0)
107 821e4c10 2022-02-21 op sio_write(hdl, buf, len * sizeof(sample));
108 3baa2617 2022-02-16 op
109 3baa2617 2022-02-16 op return MAD_FLOW_CONTINUE;
110 3baa2617 2022-02-16 op }
111 3baa2617 2022-02-16 op
112 3baa2617 2022-02-16 op static enum mad_flow
113 3baa2617 2022-02-16 op error(void *d, struct mad_stream *stream, struct mad_frame *frame)
114 3baa2617 2022-02-16 op {
115 3baa2617 2022-02-16 op struct buffer *buffer = d;
116 3baa2617 2022-02-16 op
117 795d0831 2022-02-18 op /*
118 795d0831 2022-02-18 op * most of the decoding errors are actually ID3 tags. Since
119 795d0831 2022-02-18 op * they're common, this has a lower priority to avoid spamming
120 795d0831 2022-02-18 op * syslog.
121 795d0831 2022-02-18 op */
122 795d0831 2022-02-18 op log_debug("decoding error 0x%04x (%s) at byte offset %zu",
123 3baa2617 2022-02-16 op stream->error, mad_stream_errorstr(stream),
124 3baa2617 2022-02-16 op stream->this_frame - (const unsigned char *)buffer->start);
125 3baa2617 2022-02-16 op
126 3baa2617 2022-02-16 op return MAD_FLOW_CONTINUE;
127 3baa2617 2022-02-16 op }
128 3baa2617 2022-02-16 op
129 3baa2617 2022-02-16 op static int
130 3baa2617 2022-02-16 op decode(void *m, size_t len)
131 3baa2617 2022-02-16 op {
132 3baa2617 2022-02-16 op struct buffer buffer;
133 3baa2617 2022-02-16 op struct mad_decoder decoder;
134 3baa2617 2022-02-16 op int result;
135 3baa2617 2022-02-16 op
136 3baa2617 2022-02-16 op /* initialize our private message structure; */
137 3baa2617 2022-02-16 op buffer.start = m;
138 3baa2617 2022-02-16 op buffer.length = len;
139 3baa2617 2022-02-16 op
140 3baa2617 2022-02-16 op /* configure input, output and error functions */
141 3baa2617 2022-02-16 op mad_decoder_init(&decoder, &buffer, input, 0 /* header */,
142 3baa2617 2022-02-16 op 0 /* filter */, output, error, 0 /* message */);
143 3baa2617 2022-02-16 op
144 3baa2617 2022-02-16 op /* start decoding */
145 3baa2617 2022-02-16 op result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
146 3baa2617 2022-02-16 op
147 3baa2617 2022-02-16 op /* release the decoder */
148 3baa2617 2022-02-16 op mad_decoder_finish(&decoder);
149 3baa2617 2022-02-16 op
150 3baa2617 2022-02-16 op return result;
151 3baa2617 2022-02-16 op }
152 3baa2617 2022-02-16 op
153 3baa2617 2022-02-16 op void
154 3baa2617 2022-02-16 op play_mp3(int fd)
155 3baa2617 2022-02-16 op {
156 3baa2617 2022-02-16 op struct stat stat;
157 3baa2617 2022-02-16 op void *m;
158 3baa2617 2022-02-16 op
159 bf0e053b 2022-02-18 op if (fstat(fd, &stat) == -1) {
160 bf0e053b 2022-02-18 op log_warn("fstat");
161 9491bb71 2022-02-18 op goto end;
162 bf0e053b 2022-02-18 op }
163 3baa2617 2022-02-16 op
164 3baa2617 2022-02-16 op m = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
165 bf0e053b 2022-02-18 op if (m == MAP_FAILED) {
166 bf0e053b 2022-02-18 op log_warn("map failed");
167 9491bb71 2022-02-18 op goto end;
168 bf0e053b 2022-02-18 op }
169 3baa2617 2022-02-16 op
170 3baa2617 2022-02-16 op decode(m, stat.st_size);
171 3baa2617 2022-02-16 op munmap(m, stat.st_size);
172 9491bb71 2022-02-18 op
173 9491bb71 2022-02-18 op end:
174 9491bb71 2022-02-18 op close(fd);
175 3baa2617 2022-02-16 op }