Blame


1 3baa2617 2022-02-16 op /*
2 e3317c86 2023-05-02 op * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
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 f36fd90a 2022-07-09 op #include "config.h"
18 3baa2617 2022-02-16 op
19 3baa2617 2022-02-16 op #include <inttypes.h>
20 bb3f279f 2022-02-16 op #include <limits.h>
21 3baa2617 2022-02-16 op #include <stdio.h>
22 3baa2617 2022-02-16 op #include <stdlib.h>
23 3baa2617 2022-02-16 op #include <stdint.h>
24 791d3db3 2022-07-09 op #include <string.h>
25 0e896216 2022-07-12 op #include <unistd.h>
26 3baa2617 2022-02-16 op
27 3baa2617 2022-02-16 op #include <FLAC/stream_decoder.h>
28 3baa2617 2022-02-16 op
29 3baa2617 2022-02-16 op #include "amused.h"
30 bee9cc8d 2022-02-18 op #include "log.h"
31 3baa2617 2022-02-16 op
32 791d3db3 2022-07-09 op struct write_args {
33 791d3db3 2022-07-09 op FLAC__StreamDecoder *decoder;
34 791d3db3 2022-07-09 op int seek_failed;
35 791d3db3 2022-07-09 op };
36 791d3db3 2022-07-09 op
37 791d3db3 2022-07-09 op static int
38 791d3db3 2022-07-09 op sample_seek(struct write_args *wa, int64_t seek)
39 791d3db3 2022-07-09 op {
40 791d3db3 2022-07-09 op int ok;
41 791d3db3 2022-07-09 op
42 791d3db3 2022-07-09 op ok = FLAC__stream_decoder_seek_absolute(wa->decoder, seek);
43 791d3db3 2022-07-09 op if (ok)
44 791d3db3 2022-07-09 op player_setpos(seek);
45 791d3db3 2022-07-09 op else
46 791d3db3 2022-07-09 op wa->seek_failed = 1;
47 791d3db3 2022-07-09 op return ok;
48 791d3db3 2022-07-09 op }
49 791d3db3 2022-07-09 op
50 3baa2617 2022-02-16 op static FLAC__StreamDecoderWriteStatus
51 3baa2617 2022-02-16 op writecb(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
52 44cf9512 2022-03-25 op const int32_t * const *src, void *data)
53 3baa2617 2022-02-16 op {
54 791d3db3 2022-07-09 op struct write_args *wa = data;
55 3baa2617 2022-02-16 op static uint8_t buf[BUFSIZ];
56 791d3db3 2022-07-09 op int64_t seek;
57 44cf9512 2022-03-25 op int c, i, bps, chans;
58 3baa2617 2022-02-16 op size_t len;
59 3baa2617 2022-02-16 op
60 44cf9512 2022-03-25 op bps = frame->header.bits_per_sample;
61 44cf9512 2022-03-25 op chans = frame->header.channels;
62 44cf9512 2022-03-25 op
63 3baa2617 2022-02-16 op for (i = 0, len = 0; i < frame->header.blocksize; ++i) {
64 d9cc6713 2022-03-25 op if (len + 4*chans >= sizeof(buf)) {
65 791d3db3 2022-07-09 op if (!play(buf, len, &seek))
66 2139c525 2022-03-09 op goto quit;
67 791d3db3 2022-07-09 op if (seek != -1) {
68 791d3db3 2022-07-09 op if (sample_seek(wa, seek))
69 791d3db3 2022-07-09 op break;
70 791d3db3 2022-07-09 op else
71 791d3db3 2022-07-09 op goto quit;
72 791d3db3 2022-07-09 op }
73 ac02581f 2022-03-13 op len = 0;
74 3baa2617 2022-02-16 op }
75 3baa2617 2022-02-16 op
76 44cf9512 2022-03-25 op for (c = 0; c < chans; ++c) {
77 44cf9512 2022-03-25 op switch (bps) {
78 44cf9512 2022-03-25 op case 8:
79 44cf9512 2022-03-25 op buf[len++] = src[c][i] & 0xff;
80 44cf9512 2022-03-25 op break;
81 44cf9512 2022-03-25 op case 16:
82 44cf9512 2022-03-25 op buf[len++] = src[c][i] & 0xff;
83 44cf9512 2022-03-25 op buf[len++] = (src[c][i] >> 8) & 0xff;
84 44cf9512 2022-03-25 op break;
85 44cf9512 2022-03-25 op case 24:
86 44cf9512 2022-03-25 op case 32:
87 44cf9512 2022-03-25 op buf[len++] = src[c][i] & 0xff;
88 44cf9512 2022-03-25 op buf[len++] = (src[c][i] >> 8) & 0xff;
89 44cf9512 2022-03-25 op buf[len++] = (src[c][i] >> 16) & 0xff;
90 44cf9512 2022-03-25 op buf[len++] = (src[c][i] >> 24) & 0xff;
91 44cf9512 2022-03-25 op break;
92 44cf9512 2022-03-25 op default:
93 44cf9512 2022-03-25 op log_warnx("unsupported flac bps=%d", bps);
94 44cf9512 2022-03-25 op goto quit;
95 44cf9512 2022-03-25 op }
96 44cf9512 2022-03-25 op }
97 3baa2617 2022-02-16 op }
98 3baa2617 2022-02-16 op
99 791d3db3 2022-07-09 op if (len != 0 && !play(buf, len, &seek))
100 2139c525 2022-03-09 op goto quit;
101 3baa2617 2022-02-16 op
102 791d3db3 2022-07-09 op if (seek != -1 && !sample_seek(wa, seek))
103 791d3db3 2022-07-09 op goto quit;
104 791d3db3 2022-07-09 op
105 3baa2617 2022-02-16 op return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
106 2139c525 2022-03-09 op quit:
107 2139c525 2022-03-09 op return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
108 3baa2617 2022-02-16 op }
109 3baa2617 2022-02-16 op
110 3baa2617 2022-02-16 op static void
111 3baa2617 2022-02-16 op metacb(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *meta,
112 3baa2617 2022-02-16 op void *d)
113 3baa2617 2022-02-16 op {
114 3baa2617 2022-02-16 op uint32_t sample_rate;
115 44cf9512 2022-03-25 op int channels, bits;
116 3baa2617 2022-02-16 op
117 3baa2617 2022-02-16 op if (meta->type == FLAC__METADATA_TYPE_STREAMINFO) {
118 44cf9512 2022-03-25 op bits = meta->data.stream_info.bits_per_sample;
119 3baa2617 2022-02-16 op sample_rate = meta->data.stream_info.sample_rate;
120 7fc831ea 2022-02-18 op channels = meta->data.stream_info.channels;
121 3baa2617 2022-02-16 op
122 44cf9512 2022-03-25 op if (player_setup(bits, sample_rate, channels) == -1)
123 1fb06c31 2022-06-10 op err(1, "player_setup");
124 ff06024f 2022-07-08 op
125 ff06024f 2022-07-08 op player_setduration(meta->data.stream_info.total_samples);
126 3baa2617 2022-02-16 op }
127 3baa2617 2022-02-16 op }
128 3baa2617 2022-02-16 op
129 3baa2617 2022-02-16 op static void
130 1771f738 2022-06-09 op errcb(const FLAC__StreamDecoder *decoder,
131 1771f738 2022-06-09 op FLAC__StreamDecoderErrorStatus status, void *data)
132 3baa2617 2022-02-16 op {
133 1771f738 2022-06-09 op log_warnx("flac error: %s",
134 1771f738 2022-06-09 op FLAC__StreamDecoderErrorStatusString[status]);
135 3baa2617 2022-02-16 op }
136 3baa2617 2022-02-16 op
137 0da0ad46 2022-03-09 op int
138 17ef54d6 2022-06-22 op play_flac(int fd, const char **errstr)
139 3baa2617 2022-02-16 op {
140 3baa2617 2022-02-16 op FILE *f;
141 791d3db3 2022-07-09 op struct write_args wa;
142 bee9cc8d 2022-02-18 op int s, ok = 1;
143 3baa2617 2022-02-16 op FLAC__StreamDecoder *decoder = NULL;
144 3baa2617 2022-02-16 op FLAC__StreamDecoderInitStatus init_status;
145 3baa2617 2022-02-16 op
146 986b215c 2022-07-09 op if ((f = fdopen(fd, "r")) == NULL) {
147 986b215c 2022-07-09 op *errstr = "fdopen failed";
148 986b215c 2022-07-09 op close(fd);
149 986b215c 2022-07-09 op return -1;
150 986b215c 2022-07-09 op }
151 3baa2617 2022-02-16 op
152 3baa2617 2022-02-16 op decoder = FLAC__stream_decoder_new();
153 986b215c 2022-07-09 op if (decoder == NULL) {
154 986b215c 2022-07-09 op *errstr = "FLAC__stream_decoder_new() failed";
155 986b215c 2022-07-09 op fclose(f);
156 986b215c 2022-07-09 op return -1;
157 986b215c 2022-07-09 op }
158 3baa2617 2022-02-16 op
159 3baa2617 2022-02-16 op FLAC__stream_decoder_set_md5_checking(decoder, 1);
160 3baa2617 2022-02-16 op
161 791d3db3 2022-07-09 op memset(&wa, 0, sizeof(wa));
162 791d3db3 2022-07-09 op wa.decoder = decoder;
163 791d3db3 2022-07-09 op
164 3baa2617 2022-02-16 op init_status = FLAC__stream_decoder_init_FILE(decoder, f, writecb,
165 791d3db3 2022-07-09 op metacb, errcb, &wa);
166 3baa2617 2022-02-16 op if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
167 3baa2617 2022-02-16 op errx(1, "flac decoder: %s",
168 3baa2617 2022-02-16 op FLAC__StreamDecoderInitStatusString[init_status]);
169 3baa2617 2022-02-16 op
170 3baa2617 2022-02-16 op ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
171 0da0ad46 2022-03-09 op
172 791d3db3 2022-07-09 op s = FLAC__stream_decoder_get_state(decoder);
173 0da0ad46 2022-03-09 op FLAC__stream_decoder_delete(decoder);
174 0da0ad46 2022-03-09 op fclose(f);
175 0da0ad46 2022-03-09 op
176 791d3db3 2022-07-09 op if (s == FLAC__STREAM_DECODER_ABORTED && !wa.seek_failed)
177 0923bedc 2022-03-13 op return 1;
178 791d3db3 2022-07-09 op else if (!ok && !wa.seek_failed) {
179 17ef54d6 2022-06-22 op *errstr = "flac decoding error";
180 0da0ad46 2022-03-09 op return -1;
181 0923bedc 2022-03-13 op } else
182 0923bedc 2022-03-13 op return 0;
183 3baa2617 2022-02-16 op }