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