2 * Copyright (c) 2022 Omar Polo <op@openbsd.org>
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.
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.
33 #define nitems(x) (sizeof(x)/sizeof(x[0]))
37 play_opus(int fd, const char **errstr)
39 static int16_t pcm[BUFSIZ];
40 static uint8_t out[BUFSIZ * 2];
45 OpusFileCallbacks cb = {NULL, NULL, NULL, NULL};
46 int i, li, prev_li = -1, duration_set = 0;
48 if ((f = op_fdopen(&cb, fd, "r")) == NULL) {
49 *errstr = "fdopen failed";
54 of = op_open_callbacks(f, &cb, NULL, 0, &r);
62 r = op_pcm_seek(of, seek);
68 /* NB: will downmix multichannels files into two channels */
69 r = op_read_stereo(of, pcm, nitems(pcm));
70 if (r == OP_HOLE) /* corrupt file segment? */
73 *errstr = "opus decoding error";
80 li = op_current_link(of);
85 head = op_head(of, li);
86 if (head->input_sample_rate &&
87 player_setup(16, head->input_sample_rate, 2) == -1)
88 err(1, "player_setup");
92 player_setduration(op_pcm_total(of, -1));
96 for (i = 0; i < 2*r; ++i) {
97 out[2*i+0] = pcm[i] & 0xFF;
98 out[2*i+1] = (pcm[i] >> 8) & 0xFF;
101 if (!play(out, 4*r, &seek)) {