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 <sndio.h>
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <imsg.h>
30 #include <unistd.h>
32 #include <mad.h>
34 #include "amused.h"
36 struct mad_stream mad_stream;
37 struct mad_frame mad_frame;
38 struct mad_synth mad_synth;
40 struct buffer {
41 const void *start;
42 size_t length;
43 int sample_rate;
44 };
46 static enum mad_flow
47 input(void *d, struct mad_stream *stream)
48 {
49 struct buffer *buffer = d;
51 if (buffer->length == 0)
52 return MAD_FLOW_STOP;
54 printf("decode time! start=%p, len=%zu\n", buffer->start, buffer->length);
55 mad_stream_buffer(stream, buffer->start, buffer->length);
56 buffer->length = 0;
57 buffer->sample_rate = 0;
58 return MAD_FLOW_CONTINUE;
59 }
61 /* scale a mad sample to 16 bits */
62 static inline int
63 scale(mad_fixed_t sample)
64 {
65 /* round */
66 sample += (1L << (MAD_F_FRACBITS - 16));
68 /* clip */
69 if (sample >= MAD_F_ONE)
70 sample = MAD_F_ONE - 1;
71 else if (sample < -MAD_F_ONE)
72 sample -= MAD_F_ONE;
74 /* quantize */
75 return sample >> (MAD_F_FRACBITS + 1 - 16);
76 }
78 static enum mad_flow
79 output(void *data, const struct mad_header *header, struct mad_pcm *pcm)
80 {
81 static uint8_t buf[BUFSIZ];
82 size_t len;
83 struct buffer *buffer = data;
84 int nsamples, i;
85 uint16_t sample;
86 const mad_fixed_t *leftch, *rightch;
88 if (player_shouldstop())
89 return MAD_FLOW_STOP;
91 nsamples = pcm->length;
92 leftch = pcm->samples[0];
93 rightch = pcm->samples[1];
95 if (buffer->sample_rate != pcm->samplerate) {
96 buffer->sample_rate = pcm->samplerate;
97 if (player_setrate(pcm->samplerate) == -1)
98 err(1, "player_setrate");
99 }
101 if (pcm->channels != 2) {
102 printf("mono not supported!\n");
103 return MAD_FLOW_STOP;
106 for (i = 0, len = 0; i < nsamples; ++i) {
107 if (len+4 >= sizeof(buf)) {
108 sio_write(hdl, buf, len);
109 /* fwrite(buf, 1, len, stdout); */
110 len = 0;
113 sample = scale(*leftch++);
114 buf[len++] = sample & 0xff;
115 buf[len++] = (sample >> 8) & 0xff;
117 sample = scale(*rightch++);
118 buf[len++] = sample & 0xff;
119 buf[len++] = (sample >> 8) & 0xff;
122 if (len != 0)
123 /* fwrite(buf, 1, len, stdout); */
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 warnx("decoding error 0x%04x (%s) at byte offset %zu",
135 stream->error, mad_stream_errorstr(stream),
136 stream->this_frame - (const unsigned char *)buffer->start);
138 return MAD_FLOW_CONTINUE;
141 static int
142 decode(void *m, size_t len)
144 struct buffer buffer;
145 struct mad_decoder decoder;
146 int result;
148 /* initialize our private message structure; */
149 buffer.start = m;
150 buffer.length = len;
152 /* configure input, output and error functions */
153 mad_decoder_init(&decoder, &buffer, input, 0 /* header */,
154 0 /* filter */, output, error, 0 /* message */);
156 /* start decoding */
157 result = mad_decoder_run(&decoder, MAD_DECODER_MODE_SYNC);
159 /* release the decoder */
160 mad_decoder_finish(&decoder);
162 return result;
165 void
166 play_mp3(int fd)
168 struct stat stat;
169 void *m;
171 if (fstat(fd, &stat) == -1)
172 err(1, "fstat");
173 warnx("file size %lld", stat.st_size);
175 m = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
176 if (m == MAP_FAILED)
177 err(1, "mmap");
179 decode(m, stat.st_size);
180 munmap(m, stat.st_size);