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/types.h>
18 #include <sys/queue.h>
19 #include <sys/uio.h>
21 #include <err.h>
22 #include <event.h>
23 #include <inttypes.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdint.h>
28 #include <imsg.h>
30 #include <FLAC/stream_decoder.h>
32 #include "amused.h"
33 #include "log.h"
35 static FLAC__StreamDecoderWriteStatus
36 writecb(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
37 const int32_t * const *src, void *data)
38 {
39 static uint8_t buf[BUFSIZ];
40 int c, i, bps, chans;
41 size_t len;
43 bps = frame->header.bits_per_sample;
44 chans = frame->header.channels;
46 for (i = 0, len = 0; i < frame->header.blocksize; ++i) {
47 if (len + 4*chans >= sizeof(buf)) {
48 if (!play(buf, len))
49 goto quit;
50 len = 0;
51 }
53 for (c = 0; c < chans; ++c) {
54 switch (bps) {
55 case 8:
56 buf[len++] = src[c][i] & 0xff;
57 break;
58 case 16:
59 buf[len++] = src[c][i] & 0xff;
60 buf[len++] = (src[c][i] >> 8) & 0xff;
61 break;
62 case 24:
63 case 32:
64 buf[len++] = src[c][i] & 0xff;
65 buf[len++] = (src[c][i] >> 8) & 0xff;
66 buf[len++] = (src[c][i] >> 16) & 0xff;
67 buf[len++] = (src[c][i] >> 24) & 0xff;
68 break;
69 default:
70 log_warnx("unsupported flac bps=%d", bps);
71 goto quit;
72 }
73 }
74 }
76 if (len != 0 && !play(buf, len))
77 goto quit;
79 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
80 quit:
81 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
82 }
84 static void
85 metacb(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *meta,
86 void *d)
87 {
88 uint32_t sample_rate;
89 int channels, bits;
91 if (meta->type == FLAC__METADATA_TYPE_STREAMINFO) {
92 bits = meta->data.stream_info.bits_per_sample;
93 sample_rate = meta->data.stream_info.sample_rate;
94 channels = meta->data.stream_info.channels;
96 if (player_setup(bits, sample_rate, channels) == -1)
97 err(1, "player_setrate");
98 }
99 }
101 static void
102 errcb(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status,
103 void *data)
105 log_warnx("flac error: %s", FLAC__StreamDecoderErrorStatusString[status]);
108 int
109 play_flac(int fd)
111 FILE *f;
112 int s, ok = 1;
113 const char *state;
114 FLAC__StreamDecoder *decoder = NULL;
115 FLAC__StreamDecoderInitStatus init_status;
117 if ((f = fdopen(fd, "r")) == NULL)
118 err(1, "fdopen");
120 decoder = FLAC__stream_decoder_new();
121 if (decoder == NULL)
122 err(1, "flac stream decoder");
124 FLAC__stream_decoder_set_md5_checking(decoder, 1);
126 init_status = FLAC__stream_decoder_init_FILE(decoder, f, writecb,
127 metacb, errcb, NULL);
128 if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
129 errx(1, "flac decoder: %s",
130 FLAC__StreamDecoderInitStatusString[init_status]);
132 ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
133 s = FLAC__stream_decoder_get_state(decoder);
135 FLAC__stream_decoder_delete(decoder);
136 fclose(f);
138 if (s == FLAC__STREAM_DECODER_ABORTED)
139 return 1;
140 else if (!ok) {
141 state = FLAC__StreamDecoderStateString[s];
142 log_warnx("decoding failed; state: %s", state);
143 return -1;
144 } else
145 return 0;