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 <sndio.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"
34 static FLAC__StreamDecoderWriteStatus
35 writecb(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
36 const int32_t * const *buffer, void *data)
37 {
38 static uint8_t buf[BUFSIZ];
39 int i;
40 size_t len;
42 if (player_shouldstop())
43 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
45 for (i = 0, len = 0; i < frame->header.blocksize; ++i) {
46 if (len+4 >= sizeof(buf)) {
47 sio_write(hdl, buf, len);
48 len = 0;
49 }
51 buf[len++] = buffer[0][i] & 0xff;
52 buf[len++] = (buffer[0][i] >> 8) & 0xff;
54 buf[len++] = buffer[1][i] & 0xff;
55 buf[len++] = (buffer[1][i] >> 8) & 0xff;
56 }
58 if (len != 0)
59 sio_write(hdl, buf, len);
61 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
62 }
64 static void
65 metacb(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *meta,
66 void *d)
67 {
68 uint32_t sample_rate;
69 struct sio_par par;
71 if (meta->type == FLAC__METADATA_TYPE_STREAMINFO) {
72 sample_rate = meta->data.stream_info.sample_rate;
74 printf("sample rate: %d\n", sample_rate);
75 printf("channels: %d\n", meta->data.stream_info.channels);
76 printf("bps: %d\n", meta->data.stream_info.bits_per_sample);
77 printf("total samples: %"PRIu64"\n", meta->data.stream_info.total_samples);
79 if (player_setrate(sample_rate) == -1)
80 err(1, "player_setrate");
82 sio_stop(hdl);
84 sio_initpar(&par);
85 par.rate = sample_rate;
86 if (!sio_setpar(hdl, &par))
87 err(1, "sio_setpar");
88 if (!sio_getpar(hdl, &par))
89 err(1, "sio_getpar");
90 /* TODO: check that there is a sane sample rate? */
91 if (!sio_start(hdl))
92 err(1, "sio_start");
93 }
94 }
96 static void
97 errcb(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status,
98 void *data)
99 {
100 warnx("error: %s", FLAC__StreamDecoderErrorStatusString[status]);
103 void
104 play_flac(int fd)
106 FILE *f;
107 int ok = 1;
108 FLAC__StreamDecoder *decoder = NULL;
109 FLAC__StreamDecoderInitStatus init_status;
111 if ((f = fdopen(fd, "r")) == NULL)
112 err(1, "fdopen");
114 decoder = FLAC__stream_decoder_new();
115 if (decoder == NULL)
116 err(1, "flac stream decoder");
118 FLAC__stream_decoder_set_md5_checking(decoder, 1);
120 init_status = FLAC__stream_decoder_init_FILE(decoder, f, writecb,
121 metacb, errcb, NULL);
122 if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
123 errx(1, "flac decoder: %s",
124 FLAC__StreamDecoderInitStatusString[init_status]);
126 ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
127 warnx("decoding %s", ok ? "succeeded" : "failed");
128 warnx("state: %s",
129 FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(decoder)]);
131 FLAC__stream_decoder_delete(decoder);