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 <sndio.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <imsg.h>
31 #include <FLAC/stream_decoder.h>
33 #include "amused.h"
34 #include "log.h"
36 static FLAC__StreamDecoderWriteStatus
37 writecb(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
38 const int32_t * const *buffer, void *data)
39 {
40 static uint8_t buf[BUFSIZ];
41 int i;
42 size_t len;
44 if (player_shouldstop())
45 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
47 for (i = 0, len = 0; i < frame->header.blocksize; ++i) {
48 if (len+4 >= sizeof(buf)) {
49 sio_write(hdl, buf, len);
50 len = 0;
51 }
53 buf[len++] = buffer[0][i] & 0xff;
54 buf[len++] = (buffer[0][i] >> 8) & 0xff;
56 buf[len++] = buffer[1][i] & 0xff;
57 buf[len++] = (buffer[1][i] >> 8) & 0xff;
58 }
60 if (len != 0)
61 sio_write(hdl, buf, len);
63 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
64 }
66 static void
67 metacb(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *meta,
68 void *d)
69 {
70 uint32_t sample_rate;
71 int channels;
73 if (meta->type == FLAC__METADATA_TYPE_STREAMINFO) {
74 sample_rate = meta->data.stream_info.sample_rate;
75 channels = meta->data.stream_info.channels;
77 if (player_setup(16, sample_rate, channels) == -1)
78 err(1, "player_setrate");
79 }
80 }
82 static void
83 errcb(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status,
84 void *data)
85 {
86 log_warnx("flac error: %s", FLAC__StreamDecoderErrorStatusString[status]);
87 }
89 void
90 play_flac(int fd)
91 {
92 FILE *f;
93 int s, ok = 1;
94 const char *state;
95 FLAC__StreamDecoder *decoder = NULL;
96 FLAC__StreamDecoderInitStatus init_status;
98 if ((f = fdopen(fd, "r")) == NULL)
99 err(1, "fdopen");
101 decoder = FLAC__stream_decoder_new();
102 if (decoder == NULL)
103 err(1, "flac stream decoder");
105 FLAC__stream_decoder_set_md5_checking(decoder, 1);
107 init_status = FLAC__stream_decoder_init_FILE(decoder, f, writecb,
108 metacb, errcb, NULL);
109 if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
110 errx(1, "flac decoder: %s",
111 FLAC__StreamDecoderInitStatusString[init_status]);
113 ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
114 s = FLAC__stream_decoder_get_state(decoder);
115 if (!ok && s != FLAC__STREAM_DECODER_ABORTED) {
116 state = FLAC__StreamDecoderStateString[s];
117 log_warnx("decoding failed; state: %s", state);
120 FLAC__stream_decoder_delete(decoder);
122 fclose(f);