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 *buffer, void *data)
38 {
39 static uint8_t buf[BUFSIZ];
40 int i;
41 size_t len;
43 for (i = 0, len = 0; i < frame->header.blocksize; ++i) {
44 if (len+4 >= sizeof(buf)) {
45 if (!play(buf, len))
46 goto quit;
47 }
49 buf[len++] = buffer[0][i] & 0xff;
50 buf[len++] = (buffer[0][i] >> 8) & 0xff;
52 buf[len++] = buffer[1][i] & 0xff;
53 buf[len++] = (buffer[1][i] >> 8) & 0xff;
54 }
56 if (len != 0 && !play(buf, len))
57 goto quit;
59 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
60 quit:
61 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
62 }
64 static void
65 metacb(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *meta,
66 void *d)
67 {
68 uint32_t sample_rate;
69 int channels;
71 if (meta->type == FLAC__METADATA_TYPE_STREAMINFO) {
72 sample_rate = meta->data.stream_info.sample_rate;
73 channels = meta->data.stream_info.channels;
75 if (player_setup(16, sample_rate, channels) == -1)
76 err(1, "player_setrate");
77 }
78 }
80 static void
81 errcb(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status,
82 void *data)
83 {
84 log_warnx("flac error: %s", FLAC__StreamDecoderErrorStatusString[status]);
85 }
87 int
88 play_flac(int fd)
89 {
90 FILE *f;
91 int s, ok = 1;
92 const char *state;
93 FLAC__StreamDecoder *decoder = NULL;
94 FLAC__StreamDecoderInitStatus init_status;
96 if ((f = fdopen(fd, "r")) == NULL)
97 err(1, "fdopen");
99 decoder = FLAC__stream_decoder_new();
100 if (decoder == NULL)
101 err(1, "flac stream decoder");
103 FLAC__stream_decoder_set_md5_checking(decoder, 1);
105 init_status = FLAC__stream_decoder_init_FILE(decoder, f, writecb,
106 metacb, errcb, NULL);
107 if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
108 errx(1, "flac decoder: %s",
109 FLAC__StreamDecoderInitStatusString[init_status]);
111 ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
112 s = FLAC__stream_decoder_get_state(decoder);
114 FLAC__stream_decoder_delete(decoder);
115 fclose(f);
117 if (!ok && s != FLAC__STREAM_DECODER_ABORTED) {
118 state = FLAC__StreamDecoderStateString[s];
119 log_warnx("decoding failed; state: %s", state);
120 return 1;
122 if (!ok)
123 return -1;
124 return 0;