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 <vorbis/codec.h>
34 3baa2617 2022-02-16 op #include <vorbis/vorbisfile.h>
35 3baa2617 2022-02-16 op
36 3baa2617 2022-02-16 op #include "amused.h"
37 ead41118 2022-02-18 op #include "log.h"
38 3baa2617 2022-02-16 op
39 3baa2617 2022-02-16 op #ifndef nitems
40 3baa2617 2022-02-16 op #define nitems(x) (sizeof(x)/sizeof(x[0]))
41 3baa2617 2022-02-16 op #endif
42 3baa2617 2022-02-16 op
43 3baa2617 2022-02-16 op void
44 3baa2617 2022-02-16 op play_oggvorbis(int fd)
45 3baa2617 2022-02-16 op {
46 3baa2617 2022-02-16 op static uint8_t pcmout[4096];
47 3baa2617 2022-02-16 op FILE *f;
48 3baa2617 2022-02-16 op OggVorbis_File vf;
49 3baa2617 2022-02-16 op int current_section, eof = 0;
50 3baa2617 2022-02-16 op
51 3baa2617 2022-02-16 op if ((f = fdopen(fd, "r")) == NULL)
52 3baa2617 2022-02-16 op err(1, "fdopen");
53 3baa2617 2022-02-16 op
54 ead41118 2022-02-18 op if (ov_open_callbacks(f, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
55 ead41118 2022-02-18 op log_warnx("input is not an Ogg bitstream");
56 ead41118 2022-02-18 op goto end;
57 ead41118 2022-02-18 op }
58 3baa2617 2022-02-16 op
59 3baa2617 2022-02-16 op {
60 3baa2617 2022-02-16 op char **ptr;
61 3baa2617 2022-02-16 op vorbis_info *vi;
62 3baa2617 2022-02-16 op
63 3baa2617 2022-02-16 op vi = ov_info(&vf, -1);
64 3baa2617 2022-02-16 op for (ptr = ov_comment(&vf, -1)->user_comments; *ptr; ++ptr)
65 3baa2617 2022-02-16 op printf("%s\n", *ptr);
66 3baa2617 2022-02-16 op
67 3baa2617 2022-02-16 op printf("bitstream is %d channel, %ldHz\n", vi->channels, vi->rate);
68 3baa2617 2022-02-16 op
69 7fc831ea 2022-02-18 op if (player_setup(vi->rate, vi->channels) == -1)
70 3baa2617 2022-02-16 op err(1, "player_setrate");
71 3baa2617 2022-02-16 op }
72 3baa2617 2022-02-16 op
73 3baa2617 2022-02-16 op while (!eof) {
74 3baa2617 2022-02-16 op long ret;
75 3baa2617 2022-02-16 op
76 3baa2617 2022-02-16 op if (player_shouldstop())
77 3baa2617 2022-02-16 op break;
78 3baa2617 2022-02-16 op
79 3baa2617 2022-02-16 op ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1,
80 3baa2617 2022-02-16 op &current_section);
81 3baa2617 2022-02-16 op if (ret == 0)
82 3baa2617 2022-02-16 op eof = 1;
83 3baa2617 2022-02-16 op else if (ret < 0)
84 ead41118 2022-02-18 op log_warnx("non-fatal error in the stream %ld", ret);
85 3baa2617 2022-02-16 op else {
86 3baa2617 2022-02-16 op /* TODO: deal with sample rate changes */
87 3baa2617 2022-02-16 op sio_write(hdl, pcmout, ret);
88 3baa2617 2022-02-16 op }
89 3baa2617 2022-02-16 op }
90 3baa2617 2022-02-16 op
91 3baa2617 2022-02-16 op ov_clear(&vf);
92 ead41118 2022-02-18 op
93 ead41118 2022-02-18 op end:
94 9491bb71 2022-02-18 op fclose(f);
95 3baa2617 2022-02-16 op }