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 <vorbis/codec.h>
28 3baa2617 2022-02-16 op #include <vorbis/vorbisfile.h>
29 3baa2617 2022-02-16 op
30 3baa2617 2022-02-16 op #include "amused.h"
31 ead41118 2022-02-18 op #include "log.h"
32 3baa2617 2022-02-16 op
33 3baa2617 2022-02-16 op #ifndef nitems
34 3baa2617 2022-02-16 op #define nitems(x) (sizeof(x)/sizeof(x[0]))
35 3baa2617 2022-02-16 op #endif
36 3baa2617 2022-02-16 op
37 0da0ad46 2022-03-09 op int
38 17ef54d6 2022-06-22 op play_oggvorbis(int fd, const char **errstr)
39 3baa2617 2022-02-16 op {
40 33e8ddf3 2022-07-09 op static char pcmout[4096];
41 3baa2617 2022-02-16 op FILE *f;
42 3baa2617 2022-02-16 op OggVorbis_File vf;
43 efe0c883 2022-02-18 op vorbis_info *vi;
44 791d3db3 2022-07-09 op int64_t seek = -1;
45 31b81bc4 2022-07-09 op int current_section, ret = 0;
46 3baa2617 2022-02-16 op
47 986b215c 2022-07-09 op if ((f = fdopen(fd, "r")) == NULL) {
48 986b215c 2022-07-09 op *errstr = "fdopen failed";
49 986b215c 2022-07-09 op close(fd);
50 986b215c 2022-07-09 op return -1;
51 986b215c 2022-07-09 op }
52 3baa2617 2022-02-16 op
53 ead41118 2022-02-18 op if (ov_open_callbacks(f, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
54 17ef54d6 2022-06-22 op *errstr = "input is not an Ogg bitstream";
55 34b3193b 2022-07-09 op fclose(f);
56 34b3193b 2022-07-09 op return -1;
57 ead41118 2022-02-18 op }
58 3baa2617 2022-02-16 op
59 efe0c883 2022-02-18 op /*
60 efe0c883 2022-02-18 op * we could extract some tags by looping over the NULL
61 efe0c883 2022-02-18 op * terminated array returned by ov_comment(&vf, -1), see
62 efe0c883 2022-02-18 op * previous revision of this file.
63 efe0c883 2022-02-18 op */
64 efe0c883 2022-02-18 op vi = ov_info(&vf, -1);
65 e24324f1 2022-02-21 op if (player_setup(16, vi->rate, vi->channels) == -1)
66 1fb06c31 2022-06-10 op err(1, "player_setup");
67 3baa2617 2022-02-16 op
68 ff06024f 2022-07-08 op player_setduration(ov_time_total(&vf, -1) * vi->rate);
69 ff06024f 2022-07-08 op
70 31b81bc4 2022-07-09 op for (;;) {
71 ae5d742e 2022-03-23 op long r;
72 3baa2617 2022-02-16 op
73 791d3db3 2022-07-09 op if (seek != -1) {
74 791d3db3 2022-07-09 op r = ov_pcm_seek(&vf, seek);
75 791d3db3 2022-07-09 op if (r != 0)
76 791d3db3 2022-07-09 op break;
77 791d3db3 2022-07-09 op player_setpos(seek);
78 791d3db3 2022-07-09 op }
79 791d3db3 2022-07-09 op
80 ae5d742e 2022-03-23 op r = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1,
81 3baa2617 2022-02-16 op &current_section);
82 ae5d742e 2022-03-23 op if (r == 0)
83 31b81bc4 2022-07-09 op break;
84 ae5d742e 2022-03-23 op else if (r > 0) {
85 3baa2617 2022-02-16 op /* TODO: deal with sample rate changes */
86 791d3db3 2022-07-09 op if (!play(pcmout, r, &seek)) {
87 0da0ad46 2022-03-09 op ret = 1;
88 2139c525 2022-03-09 op break;
89 0da0ad46 2022-03-09 op }
90 3baa2617 2022-02-16 op }
91 3baa2617 2022-02-16 op }
92 3baa2617 2022-02-16 op
93 3baa2617 2022-02-16 op ov_clear(&vf);
94 9491bb71 2022-02-18 op fclose(f);
95 0da0ad46 2022-03-09 op return ret;
96 3baa2617 2022-02-16 op }