1 | //roarvorbis.c: |
---|
2 | |
---|
3 | #include <roaraudio.h> |
---|
4 | |
---|
5 | #include <stdio.h> |
---|
6 | #include <stdlib.h> |
---|
7 | #include <math.h> |
---|
8 | #include <vorbis/codec.h> |
---|
9 | #include <vorbis/vorbisfile.h> |
---|
10 | |
---|
11 | #ifdef _WIN32 |
---|
12 | #include <io.h> |
---|
13 | #include <fcntl.h> |
---|
14 | #endif |
---|
15 | |
---|
16 | #define BUFSIZE 1024 |
---|
17 | |
---|
18 | void usage (void) { |
---|
19 | printf("roarvorbis [OPTIONS]... FILE\n"); |
---|
20 | |
---|
21 | printf("\nOptions:\n\n"); |
---|
22 | |
---|
23 | printf(" --server SERVER - Set server hostname\n" |
---|
24 | " --help - Show this help\n" |
---|
25 | ); |
---|
26 | |
---|
27 | } |
---|
28 | |
---|
29 | int main (int argc, char * argv[]) { |
---|
30 | int rate = 44100; |
---|
31 | int bits = 16; |
---|
32 | int channels = 2; |
---|
33 | int codec = ROAR_CODEC_DEFAULT; |
---|
34 | char * server = NULL; |
---|
35 | char * file = NULL; |
---|
36 | char * k; |
---|
37 | int i; |
---|
38 | FILE * in; |
---|
39 | struct roar_connection con; |
---|
40 | struct roar_stream s; |
---|
41 | OggVorbis_File vf; |
---|
42 | int eof=0; |
---|
43 | int current_section; |
---|
44 | char pcmout[4096]; |
---|
45 | |
---|
46 | |
---|
47 | for (i = 1; i < argc; i++) { |
---|
48 | k = argv[i]; |
---|
49 | |
---|
50 | if ( strcmp(k, "--server") == 0 ) { |
---|
51 | server = argv[++i]; |
---|
52 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
53 | usage(); |
---|
54 | return 0; |
---|
55 | } else if ( file == NULL ) { |
---|
56 | file = k; |
---|
57 | } else { |
---|
58 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
59 | usage(); |
---|
60 | return 1; |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | if ( roar_simple_connect(&con, server, "roarcatad") == -1 ) { |
---|
65 | ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!"); |
---|
66 | return -1; |
---|
67 | } |
---|
68 | |
---|
69 | if ( roar_stream_new(&s, rate, channels, bits, codec) == -1 ) { |
---|
70 | roar_disconnect(&con); |
---|
71 | return -1; |
---|
72 | } |
---|
73 | |
---|
74 | if ( roar_stream_connect(&con, &s, ROAR_DIR_PLAY) == -1 ) { |
---|
75 | roar_disconnect(&con); |
---|
76 | return -1; |
---|
77 | } |
---|
78 | |
---|
79 | if ( (in = fopen(file, "rb")) == NULL ) { |
---|
80 | roar_disconnect(&con); |
---|
81 | return -1; |
---|
82 | } |
---|
83 | |
---|
84 | #ifdef _WIN32 |
---|
85 | _setmode(_fileno(in), _O_BINARY); |
---|
86 | #endif |
---|
87 | |
---|
88 | if(ov_open(in, &vf, NULL, 0) < 0) { |
---|
89 | fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n"); |
---|
90 | roar_disconnect(&con); |
---|
91 | return -1; |
---|
92 | } |
---|
93 | |
---|
94 | { |
---|
95 | char **ptr=ov_comment(&vf,-1)->user_comments; |
---|
96 | char key[80], value[80]; |
---|
97 | int j, h = 0; |
---|
98 | vorbis_info *vi=ov_info(&vf,-1); |
---|
99 | struct roar_meta meta; |
---|
100 | |
---|
101 | meta.value = value; |
---|
102 | meta.key[0] = 0; |
---|
103 | |
---|
104 | while(*ptr){ |
---|
105 | fprintf(stderr,"%s\n",*ptr); |
---|
106 | |
---|
107 | for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) |
---|
108 | key[j] = (*ptr)[j]; |
---|
109 | key[j] = 0; |
---|
110 | |
---|
111 | for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++) |
---|
112 | value[h++] = (*ptr)[j]; |
---|
113 | value[h] = 0; |
---|
114 | |
---|
115 | meta.type = roar_meta_inttype(key); |
---|
116 | if ( meta.type != -1 ) |
---|
117 | roar_stream_meta_set(&con, &s, ROAR_META_MODE_SET, &meta); |
---|
118 | |
---|
119 | ++ptr; |
---|
120 | } |
---|
121 | fprintf(stderr,"\nBitstream is %d channel, %ldHz\n",vi->channels,vi->rate); |
---|
122 | fprintf(stderr,"Encoded by: %s\n\n",ov_comment(&vf,-1)->vendor); |
---|
123 | } |
---|
124 | |
---|
125 | if ( roar_stream_exec(&con, &s) == -1 ) { |
---|
126 | roar_disconnect(&con); |
---|
127 | return -1; |
---|
128 | } |
---|
129 | |
---|
130 | while(!eof){ |
---|
131 | long ret=ov_read(&vf,pcmout,sizeof(pcmout),0,2,1,¤t_section); |
---|
132 | if (ret == 0) { |
---|
133 | /* EOF */ |
---|
134 | eof=1; |
---|
135 | } else if (ret < 0) { |
---|
136 | /* error in the stream. Not a problem, just reporting it in |
---|
137 | case we (the app) cares. In this case, we don't. */ |
---|
138 | } else { |
---|
139 | /* we don't bother dealing with sample rate changes, etc, but |
---|
140 | you'll have to */ |
---|
141 | write(con.fh, pcmout, ret); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | ov_clear(&vf); |
---|
146 | |
---|
147 | // fclose(in); |
---|
148 | close(con.fh); |
---|
149 | |
---|
150 | return 0; |
---|
151 | } |
---|
152 | |
---|
153 | //ll |
---|