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 f36fd90a 2022-07-09 op #include "config.h"
18 3baa2617 2022-02-16 op
19 3baa2617 2022-02-16 op #include <fcntl.h>
20 3baa2617 2022-02-16 op #include <math.h>
21 3baa2617 2022-02-16 op #include <inttypes.h>
22 bb3f279f 2022-02-16 op #include <limits.h>
23 3baa2617 2022-02-16 op #include <stdio.h>
24 3baa2617 2022-02-16 op #include <stdint.h>
25 3baa2617 2022-02-16 op #include <unistd.h>
26 3baa2617 2022-02-16 op
27 3baa2617 2022-02-16 op #include <opusfile.h>
28 3baa2617 2022-02-16 op
29 3baa2617 2022-02-16 op #include "amused.h"
30 cec9ff75 2022-02-18 op #include "log.h"
31 3baa2617 2022-02-16 op
32 3baa2617 2022-02-16 op #ifndef nitems
33 3baa2617 2022-02-16 op #define nitems(x) (sizeof(x)/sizeof(x[0]))
34 3baa2617 2022-02-16 op #endif
35 3baa2617 2022-02-16 op
36 0da0ad46 2022-03-09 op int
37 17ef54d6 2022-06-22 op play_opus(int fd, const char **errstr)
38 3baa2617 2022-02-16 op {
39 33e8ddf3 2022-07-09 op static int16_t pcm[BUFSIZ];
40 3baa2617 2022-02-16 op static uint8_t out[BUFSIZ * 2];
41 3baa2617 2022-02-16 op OggOpusFile *of;
42 3baa2617 2022-02-16 op void *f;
43 791d3db3 2022-07-09 op int64_t seek = -1;
44 0da0ad46 2022-03-09 op int r, ret = 0;
45 3baa2617 2022-02-16 op OpusFileCallbacks cb = {NULL, NULL, NULL, NULL};
46 ff06024f 2022-07-08 op int i, li, prev_li = -1, duration_set = 0;
47 3baa2617 2022-02-16 op
48 986b215c 2022-07-09 op if ((f = op_fdopen(&cb, fd, "r")) == NULL) {
49 986b215c 2022-07-09 op *errstr = "fdopen failed";
50 986b215c 2022-07-09 op close(fd);
51 986b215c 2022-07-09 op return -1;
52 986b215c 2022-07-09 op }
53 3baa2617 2022-02-16 op
54 0da0ad46 2022-03-09 op of = op_open_callbacks(f, &cb, NULL, 0, &r);
55 cec9ff75 2022-02-18 op if (of == NULL) {
56 a130f027 2022-06-10 op fclose(f);
57 0da0ad46 2022-03-09 op return -1;
58 cec9ff75 2022-02-18 op }
59 3baa2617 2022-02-16 op
60 3baa2617 2022-02-16 op for (;;) {
61 791d3db3 2022-07-09 op if (seek != -1) {
62 791d3db3 2022-07-09 op r = op_pcm_seek(of, seek);
63 791d3db3 2022-07-09 op if (r != 0)
64 791d3db3 2022-07-09 op break;
65 791d3db3 2022-07-09 op player_setpos(seek);
66 791d3db3 2022-07-09 op }
67 791d3db3 2022-07-09 op
68 a58776af 2022-02-18 op /* NB: will downmix multichannels files into two channels */
69 0da0ad46 2022-03-09 op r = op_read_stereo(of, pcm, nitems(pcm));
70 0da0ad46 2022-03-09 op if (r == OP_HOLE) /* corrupt file segment? */
71 3baa2617 2022-02-16 op continue;
72 0da0ad46 2022-03-09 op if (r < 0) {
73 17ef54d6 2022-06-22 op *errstr = "opus decoding error";
74 0da0ad46 2022-03-09 op ret = -1;
75 cec9ff75 2022-02-18 op break;
76 cec9ff75 2022-02-18 op }
77 0da0ad46 2022-03-09 op if (r == 0)
78 3baa2617 2022-02-16 op break; /* eof */
79 3baa2617 2022-02-16 op
80 3baa2617 2022-02-16 op li = op_current_link(of);
81 3baa2617 2022-02-16 op if (li != prev_li) {
82 3baa2617 2022-02-16 op const OpusHead *head;
83 3baa2617 2022-02-16 op
84 3baa2617 2022-02-16 op prev_li = li;
85 3baa2617 2022-02-16 op head = op_head(of, li);
86 efe0c883 2022-02-18 op if (head->input_sample_rate &&
87 e24324f1 2022-02-21 op player_setup(16, head->input_sample_rate, 2) == -1)
88 1fb06c31 2022-06-10 op err(1, "player_setup");
89 ff06024f 2022-07-08 op
90 ff06024f 2022-07-08 op if (!duration_set) {
91 ff06024f 2022-07-08 op duration_set = 1;
92 ff06024f 2022-07-08 op player_setduration(op_pcm_total(of, -1));
93 ff06024f 2022-07-08 op }
94 3baa2617 2022-02-16 op }
95 3baa2617 2022-02-16 op
96 74a7482c 2022-03-23 op for (i = 0; i < 2*r; ++i) {
97 3baa2617 2022-02-16 op out[2*i+0] = pcm[i] & 0xFF;
98 3baa2617 2022-02-16 op out[2*i+1] = (pcm[i] >> 8) & 0xFF;
99 3baa2617 2022-02-16 op }
100 2139c525 2022-03-09 op
101 791d3db3 2022-07-09 op if (!play(out, 4*r, &seek)) {
102 0da0ad46 2022-03-09 op ret = 1;
103 2139c525 2022-03-09 op break;
104 0da0ad46 2022-03-09 op }
105 3baa2617 2022-02-16 op }
106 3baa2617 2022-02-16 op
107 3baa2617 2022-02-16 op op_free(of);
108 0da0ad46 2022-03-09 op return ret;
109 3baa2617 2022-02-16 op }