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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
30 #include <FLAC/stream_decoder.h>
35 static FLAC__StreamDecoderWriteStatus
36 writecb(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
37 const int32_t * const *src, void *data)
39 static uint8_t buf[BUFSIZ];
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)) {
53 for (c = 0; c < chans; ++c) {
56 buf[len++] = src[c][i] & 0xff;
59 buf[len++] = src[c][i] & 0xff;
60 buf[len++] = (src[c][i] >> 8) & 0xff;
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;
70 log_warnx("unsupported flac bps=%d", bps);
76 if (len != 0 && !play(buf, len))
79 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
81 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
85 metacb(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *meta,
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");
102 errcb(const FLAC__StreamDecoder *decoder,
103 FLAC__StreamDecoderErrorStatus status, void *data)
105 log_warnx("flac error: %s",
106 FLAC__StreamDecoderErrorStatusString[status]);
115 FLAC__StreamDecoder *decoder = NULL;
116 FLAC__StreamDecoderInitStatus init_status;
118 if ((f = fdopen(fd, "r")) == NULL)
121 decoder = FLAC__stream_decoder_new();
123 err(1, "flac stream decoder");
125 FLAC__stream_decoder_set_md5_checking(decoder, 1);
127 init_status = FLAC__stream_decoder_init_FILE(decoder, f, writecb,
128 metacb, errcb, NULL);
129 if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
130 errx(1, "flac decoder: %s",
131 FLAC__StreamDecoderInitStatusString[init_status]);
133 ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
134 s = FLAC__stream_decoder_get_state(decoder);
136 FLAC__stream_decoder_delete(decoder);
139 if (s == FLAC__STREAM_DECODER_ABORTED)
142 state = FLAC__StreamDecoderStateString[s];
143 log_warnx("decoding failed; state: %s", state);