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 <fcntl.h>
24 3baa2617 2022-02-16 op #include <math.h>
25 3baa2617 2022-02-16 op #include <inttypes.h>
26 bb3f279f 2022-02-16 op #include <limits.h>
27 3baa2617 2022-02-16 op #include <sndio.h>
28 3baa2617 2022-02-16 op #include <stdio.h>
29 3baa2617 2022-02-16 op #include <stdint.h>
30 3baa2617 2022-02-16 op #include <imsg.h>
31 3baa2617 2022-02-16 op #include <unistd.h>
32 3baa2617 2022-02-16 op
33 3baa2617 2022-02-16 op #include <opusfile.h>
34 3baa2617 2022-02-16 op
35 3baa2617 2022-02-16 op #include "amused.h"
36 cec9ff75 2022-02-18 op #include "log.h"
37 3baa2617 2022-02-16 op
38 3baa2617 2022-02-16 op #ifndef nitems
39 3baa2617 2022-02-16 op #define nitems(x) (sizeof(x)/sizeof(x[0]))
40 3baa2617 2022-02-16 op #endif
41 3baa2617 2022-02-16 op
42 3baa2617 2022-02-16 op void
43 3baa2617 2022-02-16 op play_opus(int fd)
44 3baa2617 2022-02-16 op {
45 3baa2617 2022-02-16 op static uint16_t pcm[BUFSIZ];
46 3baa2617 2022-02-16 op static uint8_t out[BUFSIZ * 2];
47 3baa2617 2022-02-16 op OggOpusFile *of;
48 3baa2617 2022-02-16 op void *f;
49 3baa2617 2022-02-16 op int ret;
50 3baa2617 2022-02-16 op OpusFileCallbacks cb = {NULL, NULL, NULL, NULL};
51 3baa2617 2022-02-16 op int i, li, prev_li = -1;
52 3baa2617 2022-02-16 op
53 3baa2617 2022-02-16 op if ((f = op_fdopen(&cb, fd, "r")) == NULL)
54 3baa2617 2022-02-16 op err(1, "fdopen");
55 3baa2617 2022-02-16 op
56 3baa2617 2022-02-16 op of = op_open_callbacks(f, &cb, NULL, 0, &ret);
57 cec9ff75 2022-02-18 op if (of == NULL) {
58 cec9ff75 2022-02-18 op close(fd);
59 cec9ff75 2022-02-18 op return;
60 cec9ff75 2022-02-18 op }
61 3baa2617 2022-02-16 op
62 3baa2617 2022-02-16 op for (;;) {
63 3baa2617 2022-02-16 op if (player_shouldstop())
64 3baa2617 2022-02-16 op break;
65 3baa2617 2022-02-16 op
66 a58776af 2022-02-18 op /* NB: will downmix multichannels files into two channels */
67 3baa2617 2022-02-16 op ret = op_read_stereo(of, pcm, nitems(pcm));
68 3baa2617 2022-02-16 op if (ret == OP_HOLE) /* corrupt file segment? */
69 3baa2617 2022-02-16 op continue;
70 cec9ff75 2022-02-18 op if (ret < 0) {
71 cec9ff75 2022-02-18 op log_warnx("error %d decoding file", ret);
72 cec9ff75 2022-02-18 op break;
73 cec9ff75 2022-02-18 op }
74 3baa2617 2022-02-16 op if (ret == 0)
75 3baa2617 2022-02-16 op break; /* eof */
76 3baa2617 2022-02-16 op
77 3baa2617 2022-02-16 op li = op_current_link(of);
78 3baa2617 2022-02-16 op if (li != prev_li) {
79 3baa2617 2022-02-16 op const OpusHead *head;
80 3baa2617 2022-02-16 op
81 3baa2617 2022-02-16 op prev_li = li;
82 3baa2617 2022-02-16 op
83 3baa2617 2022-02-16 op head = op_head(of, li);
84 efe0c883 2022-02-18 op if (head->input_sample_rate &&
85 efe0c883 2022-02-18 op player_setup(head->input_sample_rate, 2) == -1)
86 efe0c883 2022-02-18 op err(1, "player_setrate");
87 3baa2617 2022-02-16 op }
88 3baa2617 2022-02-16 op
89 3baa2617 2022-02-16 op for (i = 0; i < 2*ret; ++i) {
90 3baa2617 2022-02-16 op out[2*i+0] = pcm[i] & 0xFF;
91 3baa2617 2022-02-16 op out[2*i+1] = (pcm[i] >> 8) & 0xFF;
92 3baa2617 2022-02-16 op }
93 3baa2617 2022-02-16 op sio_write(hdl, out, 4*ret);
94 3baa2617 2022-02-16 op }
95 3baa2617 2022-02-16 op
96 3baa2617 2022-02-16 op op_free(of);
97 3baa2617 2022-02-16 op }