Blob


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