Blob


1 /*
2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include "config.h"
19 #include <fcntl.h>
20 #include <math.h>
21 #include <inttypes.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <unistd.h>
27 #include <vorbis/codec.h>
28 #include <vorbis/vorbisfile.h>
30 #include "amused.h"
32 #ifndef nitems
33 #define nitems(x) (sizeof(x)/sizeof(x[0]))
34 #endif
36 int
37 play_oggvorbis(int fd, const char **errstr)
38 {
39 static char pcmout[4096];
40 FILE *f;
41 OggVorbis_File vf;
42 vorbis_info *vi;
43 int64_t seek = -1;
44 int current_section, ret = 0;
46 if ((f = fdopen(fd, "r")) == NULL) {
47 *errstr = "fdopen failed";
48 close(fd);
49 return -1;
50 }
52 if (ov_open_callbacks(f, &vf, NULL, 0, OV_CALLBACKS_NOCLOSE) < 0) {
53 *errstr = "input is not an Ogg bitstream";
54 fclose(f);
55 return -1;
56 }
58 /*
59 * we could extract some tags by looping over the NULL
60 * terminated array returned by ov_comment(&vf, -1), see
61 * previous revision of this file.
62 */
63 vi = ov_info(&vf, -1);
64 if (player_setup(16, vi->rate, vi->channels) == -1)
65 err(1, "player_setup");
67 player_setduration(ov_time_total(&vf, -1) * vi->rate);
69 for (;;) {
70 long r;
72 if (seek != -1) {
73 r = ov_pcm_seek(&vf, seek);
74 if (r != 0)
75 break;
76 player_setpos(seek);
77 }
79 r = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1,
80 &current_section);
81 if (r == 0)
82 break;
83 else if (r > 0) {
84 /* TODO: deal with sample rate changes */
85 if (!play(pcmout, r, &seek)) {
86 ret = 1;
87 break;
88 }
89 }
90 }
92 ov_clear(&vf);
93 fclose(f);
94 return ret;
95 }