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"
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 if (player_shouldstop())
44 return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
46 for (i = 0, len = 0; i < frame->header.blocksize; ++i) {
47 if (len+4 >= sizeof(buf)) {
48 sio_write(hdl, buf, len);
49 len = 0;
50 }
52 buf[len++] = buffer[0][i] & 0xff;
53 buf[len++] = (buffer[0][i] >> 8) & 0xff;
55 buf[len++] = buffer[1][i] & 0xff;
56 buf[len++] = (buffer[1][i] >> 8) & 0xff;
57 }
59 if (len != 0)
60 sio_write(hdl, buf, len);
62 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
63 }
65 static void
66 metacb(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *meta,
67 void *d)
68 {
69 uint32_t sample_rate;
70 int channels;
72 if (meta->type == FLAC__METADATA_TYPE_STREAMINFO) {
73 sample_rate = meta->data.stream_info.sample_rate;
74 channels = meta->data.stream_info.channels;
76 printf("sample rate: %d\n", sample_rate);
77 printf("channels: %d\n", meta->data.stream_info.channels);
78 printf("bps: %d\n", meta->data.stream_info.bits_per_sample);
79 printf("total samples: %"PRIu64"\n", meta->data.stream_info.total_samples);
81 if (player_setup(sample_rate, channels) == -1)
82 err(1, "player_setrate");
83 }
84 }
86 static void
87 errcb(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status,
88 void *data)
89 {
90 warnx("error: %s", FLAC__StreamDecoderErrorStatusString[status]);
91 }
93 void
94 play_flac(int fd)
95 {
96 FILE *f;
97 int ok = 1;
98 FLAC__StreamDecoder *decoder = NULL;
99 FLAC__StreamDecoderInitStatus init_status;
101 if ((f = fdopen(fd, "r")) == NULL)
102 err(1, "fdopen");
104 decoder = FLAC__stream_decoder_new();
105 if (decoder == NULL)
106 err(1, "flac stream decoder");
108 FLAC__stream_decoder_set_md5_checking(decoder, 1);
110 init_status = FLAC__stream_decoder_init_FILE(decoder, f, writecb,
111 metacb, errcb, NULL);
112 if (init_status != FLAC__STREAM_DECODER_INIT_STATUS_OK)
113 errx(1, "flac decoder: %s",
114 FLAC__StreamDecoderInitStatusString[init_status]);
116 ok = FLAC__stream_decoder_process_until_end_of_stream(decoder);
117 warnx("decoding %s", ok ? "succeeded" : "failed");
118 warnx("state: %s",
119 FLAC__StreamDecoderStateString[FLAC__stream_decoder_get_state(decoder)]);
121 FLAC__stream_decoder_delete(decoder);