Blame


1 3baa2617 2022-02-16 op /*
2 3baa2617 2022-02-16 op * Copyright (c) 2022 Omar Polo <op@openbsd.org>
3 3baa2617 2022-02-16 op *
4 3baa2617 2022-02-16 op * Permission to use, copy, modify, and distribute this software for any
5 3baa2617 2022-02-16 op * purpose with or without fee is hereby granted, provided that the above
6 3baa2617 2022-02-16 op * copyright notice and this permission notice appear in all copies.
7 3baa2617 2022-02-16 op *
8 3baa2617 2022-02-16 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3baa2617 2022-02-16 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3baa2617 2022-02-16 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3baa2617 2022-02-16 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3baa2617 2022-02-16 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3baa2617 2022-02-16 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3baa2617 2022-02-16 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 3baa2617 2022-02-16 op */
16 3baa2617 2022-02-16 op
17 3baa2617 2022-02-16 op #include <sys/types.h>
18 3baa2617 2022-02-16 op #include <sys/queue.h>
19 3baa2617 2022-02-16 op #include <sys/uio.h>
20 3baa2617 2022-02-16 op
21 3baa2617 2022-02-16 op #include <err.h>
22 3baa2617 2022-02-16 op #include <event.h>
23 3baa2617 2022-02-16 op #include <inttypes.h>
24 bb3f279f 2022-02-16 op #include <limits.h>
25 3baa2617 2022-02-16 op #include <stdio.h>
26 3baa2617 2022-02-16 op #include <stdlib.h>
27 3baa2617 2022-02-16 op #include <stdint.h>
28 3baa2617 2022-02-16 op #include <imsg.h>
29 3baa2617 2022-02-16 op
30 3baa2617 2022-02-16 op #include <FLAC/stream_decoder.h>
31 3baa2617 2022-02-16 op
32 3baa2617 2022-02-16 op #include "amused.h"
33 bee9cc8d 2022-02-18 op #include "log.h"
34 3baa2617 2022-02-16 op
35 3baa2617 2022-02-16 op static FLAC__StreamDecoderWriteStatus
36 3baa2617 2022-02-16 op writecb(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
37 44cf9512 2022-03-25 op const int32_t * const *src, void *data)
38 3baa2617 2022-02-16 op {
39 3baa2617 2022-02-16 op static uint8_t buf[BUFSIZ];
40 44cf9512 2022-03-25 op int c, i, bps, chans;
41 3baa2617 2022-02-16 op size_t len;
42 3baa2617 2022-02-16 op
43 44cf9512 2022-03-25 op bps = frame->header.bits_per_sample;
44 44cf9512 2022-03-25 op chans = frame->header.channels;
45 44cf9512 2022-03-25 op
46 3baa2617 2022-02-16 op for (i = 0, len = 0; i < frame->header.blocksize; ++i) {
47 d9cc6713 2022-03-25 op if (len + 4*chans >= sizeof(buf)) {
48 2139c525 2022-03-09 op if (!play(buf, len))
49 2139c525 2022-03-09 op goto quit;
50 ac02581f 2022-03-13 op len = 0;
51 3baa2617 2022-02-16 op }
52 3baa2617 2022-02-16 op
53 44cf9512 2022-03-25 op for (c = 0; c < chans; ++c) {
54 44cf9512 2022-03-25 op switch (bps) {
55 44cf9512 2022-03-25 op case 8:
56 44cf9512 2022-03-25 op buf[len++] = src[c][i] & 0xff;
57 44cf9512 2022-03-25 op break;
58 44cf9512 2022-03-25 op case 16:
59 44cf9512 2022-03-25 op buf[len++] = src[c][i] & 0xff;
60 44cf9512 2022-03-25 op buf[len++] = (src[c][i] >> 8) & 0xff;
61 44cf9512 2022-03-25 op break;
62 44cf9512 2022-03-25 op case 24:
63 44cf9512 2022-03-25 op case 32:
64 44cf9512 2022-03-25 op buf[len++] = src[c][i] & 0xff;
65 44cf9512 2022-03-25 op buf[len++] = (src[c][i] >> 8) & 0xff;
66 44cf9512 2022-03-25 op buf[len++] = (src[c][i] >> 16) & 0xff;
67 44cf9512 2022-03-25 op buf[len++] = (src[c][i] >> 24) & 0xff;
68 44cf9512 2022-03-25 op break;
69 44cf9512 2022-03-25 op default:
70 44cf9512 2022-03-25 op log_warnx("unsupported flac bps=%d", bps);
71 44cf9512 2022-03-25 op goto quit;
72 44cf9512 2022-03-25 op }
73 44cf9512 2022-03-25 op }
74 3baa2617 2022-02-16 op }
75 3baa2617 2022-02-16 op
76 2139c525 2022-03-09 op if (len != 0 && !play(buf, len))
77 2139c525 2022-03-09 op goto quit;
78 3baa2617 2022-02-16 op
79 3baa2617 2022-02-16 op return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
80 2139c525 2022-03-09 op quit:
81 2139c525 2022-03-09 op return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
82 3baa2617 2022-02-16 op }
83 3baa2617 2022-02-16 op
84 3baa2617 2022-02-16 op static void
85 3baa2617 2022-02-16 op metacb(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *meta,
86 3baa2617 2022-02-16 op void *d)
87 3baa2617 2022-02-16 op {
88 3baa2617 2022-02-16 op uint32_t sample_rate;
89 44cf9512 2022-03-25 op int channels, bits;
90 3baa2617 2022-02-16 op
91 3baa2617 2022-02-16 op if (meta->type == FLAC__METADATA_TYPE_STREAMINFO) {
92 44cf9512 2022-03-25 op bits = meta->data.stream_info.bits_per_sample;
93 3baa2617 2022-02-16 op sample_rate = meta->data.stream_info.sample_rate;
94 7fc831ea 2022-02-18 op channels = meta->data.stream_info.channels;
95 3baa2617 2022-02-16 op
96 44cf9512 2022-03-25 op if (player_setup(bits, sample_rate, channels) == -1)
97 1fb06c31 2022-06-10 op err(1, "player_setup");
98 3baa2617 2022-02-16 op }
99 3baa2617 2022-02-16 op }
100 3baa2617 2022-02-16 op
101 3baa2617 2022-02-16 op static void
102 1771f738 2022-06-09 op errcb(const FLAC__StreamDecoder *decoder,
103 1771f738 2022-06-09 op FLAC__StreamDecoderErrorStatus status, void *data)
104 3baa2617 2022-02-16 op {
105 1771f738 2022-06-09 op log_warnx("flac error: %s",
106 1771f738 2022-06-09 op FLAC__StreamDecoderErrorStatusString[status]);
107 3baa2617 2022-02-16 op }
108 3baa2617 2022-02-16 op
109 0da0ad46 2022-03-09 op int
110 3baa2617 2022-02-16 op play_flac(int fd)
111 3baa2617 2022-02-16 op {
112 3baa2617 2022-02-16 op FILE *f;
113 bee9cc8d 2022-02-18 op int s, ok = 1;
114 bee9cc8d 2022-02-18 op const char *state;
115 3baa2617 2022-02-16 op FLAC__StreamDecoder *decoder = NULL;
116 3baa2617 2022-02-16 op FLAC__StreamDecoderInitStatus init_status;
117 3baa2617 2022-02-16 op
118 3baa2617 2022-02-16 op if ((f = fdopen(fd, "r")) == NULL)
119 3baa2617 2022-02-16 op err(1, "fdopen");
120 3baa2617 2022-02-16 op
121 3baa2617 2022-02-16 op decoder = FLAC__stream_decoder_new();
122 3baa2617 2022-02-16 op if (decoder == NULL)
123 3baa2617 2022-02-16 op err(1, "flac stream decoder");
124 3baa2617 2022-02-16 op
125 3baa2617 2022-02-16 op FLAC__stream_decoder_set_md5_checking(decoder, 1);
126 3baa2617 2022-02-16 op
127 3baa2617 2022-02-16 op init_status = FLAC__stream_decoder_init_FILE(decoder, f, writecb,
128 3baa2617 2022-02-16 op metacb, errcb, NULL);
129 3baa2617 2022-02-16 op if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
130 3baa2617 2022-02-16 op errx(1, "flac decoder: %s",
131 3baa2617 2022-02-16 op FLAC__StreamDecoderInitStatusString[init_status]);
132 3baa2617 2022-02-16 op
133 3baa2617 2022-02-16 op ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
134 933a4d96 2022-02-18 op s = FLAC__stream_decoder_get_state(decoder);
135 0da0ad46 2022-03-09 op
136 0da0ad46 2022-03-09 op FLAC__stream_decoder_delete(decoder);
137 0da0ad46 2022-03-09 op fclose(f);
138 0da0ad46 2022-03-09 op
139 0923bedc 2022-03-13 op if (s == FLAC__STREAM_DECODER_ABORTED)
140 0923bedc 2022-03-13 op return 1;
141 0923bedc 2022-03-13 op else if (!ok) {
142 bee9cc8d 2022-02-18 op state = FLAC__StreamDecoderStateString[s];
143 bee9cc8d 2022-02-18 op log_warnx("decoding failed; state: %s", state);
144 0da0ad46 2022-03-09 op return -1;
145 0923bedc 2022-03-13 op } else
146 0923bedc 2022-03-13 op return 0;
147 3baa2617 2022-02-16 op }