2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
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.
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.
19 #include <sys/types.h>
20 #include <sys/queue.h>
40 struct mad_stream mad_stream;
41 struct mad_frame mad_frame;
42 struct mad_synth mad_synth;
52 input(void *d, struct mad_stream *stream)
54 struct buffer *buffer = d;
56 if (buffer->length == 0)
59 mad_stream_buffer(stream, buffer->start, buffer->length);
61 buffer->sample_rate = 0;
63 return MAD_FLOW_CONTINUE;
67 output(void *data, const struct mad_header *header, struct mad_pcm *pcm)
69 static uint32_t buf[BUFSIZ];
71 struct buffer *buffer = data;
74 const mad_fixed_t *leftch, *rightch;
76 if (player_shouldstop())
79 nsamples = pcm->length;
80 leftch = pcm->samples[0];
81 rightch = pcm->samples[1];
83 if (buffer->sample_rate != pcm->samplerate ||
84 buffer->channels != pcm->channels) {
85 buffer->sample_rate = pcm->samplerate;
86 buffer->channels = pcm->channels;
87 if (player_setup(32, pcm->samplerate, pcm->channels) == -1)
88 err(1, "player_setrate");
91 for (i = 0, len = 0; i < nsamples; ++i) {
92 if (len+2 >= sizeof(buf)) {
93 sio_write(hdl, buf, len * sizeof(sample));
100 if (pcm->channels == 2) {
107 sio_write(hdl, buf, len * sizeof(sample));
109 return MAD_FLOW_CONTINUE;
113 error(void *d, struct mad_stream *stream, struct mad_frame *frame)
115 struct buffer *buffer = d;
118 * most of the decoding errors are actually ID3 tags. Since
119 * they're common, this has a lower priority to avoid spamming
122 log_debug("decoding error 0x%04x (%s) at byte offset %zu",
123 stream->error, mad_stream_errorstr(stream),
124 stream->this_frame - (const unsigned char *)buffer->start);
126 return MAD_FLOW_CONTINUE;
130 decode(void *m, size_t len)
132 struct buffer buffer;
133 struct mad_decoder decoder;
136 /* initialize our private message structure; */
140 /* configure input, output and error functions */
141 mad_decoder_init(&decoder, &buffer, input, 0 /* header */,
142 0 /* filter */, output, error, 0 /* message */);
145 result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
147 /* release the decoder */
148 mad_decoder_finish(&decoder);
159 if (fstat(fd, &stat) == -1) {
164 m = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
165 if (m == MAP_FAILED) {
166 log_warn("map failed");
170 decode(m, stat.st_size);
171 munmap(m, stat.st_size);