source: roaraudio/roarclients/roarvorbis.c @ 118:d3c4bca914cd

Last change on this file since 118:d3c4bca914cd was 118:d3c4bca914cd, checked in by phi, 16 years ago

some cleanup

File size: 3.1 KB
Line 
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
18void 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
29int main (int argc, char * argv[]) {
30 int    bits     = 16;
31 int    codec    = ROAR_CODEC_DEFAULT;
32 char * server   = NULL;
33 char * file     = NULL;
34 char * k;
35 int    i;
36 FILE * in;
37 int    out;
38 struct roar_connection con;
39 struct roar_stream     s;
40 OggVorbis_File vf;
41 int eof=0;
42 int current_section;
43 char pcmout[4096];
44
45
46 for (i = 1; i < argc; i++) {
47  k = argv[i];
48
49  if ( strcmp(k, "--server") == 0 ) {
50   server = argv[++i];
51  } else if ( strcmp(k, "--help") == 0 ) {
52   usage();
53   return 0;
54  } else if ( file == NULL ) {
55   file = k;
56  } else {
57   fprintf(stderr, "Error: unknown argument: %s\n", k);
58   usage();
59   return 1;
60  }
61 }
62
63 if ( roar_simple_connect(&con, server, "roarvorbis") == -1 ) {
64  ROAR_DBG("roar_simple_play(*): roar_simple_connect() faild!");
65  return -1;
66 }
67
68 if ( (in = fopen(file, "rb")) == NULL ) {
69  roar_disconnect(&con);
70  return -1;
71 }
72
73#ifdef _WIN32
74  _setmode(_fileno(in), _O_BINARY);
75#endif
76
77 if(ov_open(in, &vf, NULL, 0) < 0) {
78  fprintf(stderr,"Input does not appear to be an Ogg bitstream.\n");
79  roar_disconnect(&con);
80  return -1;
81 }
82
83 {
84  vorbis_info *vi = ov_info(&vf, -1);
85
86  fprintf(stderr, "Audio: %i channel, %liHz\n\n", vi->channels, vi->rate);
87
88  if ( roar_stream_new(&s, vi->rate, vi->channels, bits, codec) == -1 ) {
89   roar_disconnect(&con);
90   return -1;
91  }
92
93  if ( roar_stream_connect(&con, &s, ROAR_DIR_PLAY) == -1 ) {
94   roar_disconnect(&con);
95   return -1;
96  }
97 }
98
99 {
100  char **ptr = ov_comment(&vf, -1)->user_comments;
101  char key[80], value[80];
102  int j, h = 0;
103  struct roar_meta   meta;
104
105  meta.value = value;
106  meta.key[0] = 0;
107
108  meta.type = ROAR_META_TYPE_FILENAME;
109  strncpy(value, file, 79);
110  roar_stream_meta_set(&con, &s, ROAR_META_MODE_SET, &meta);
111
112
113
114  while(*ptr){
115    for (j = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++)
116     key[j] = (*ptr)[j];
117     key[j] = 0;
118
119    for (j++, h = 0; (*ptr)[j] != 0 && (*ptr)[j] != '='; j++)
120     value[h++] = (*ptr)[j];
121     value[h]   = 0;
122
123    meta.type = roar_meta_inttype(key);
124    if ( meta.type != -1 )
125     roar_stream_meta_set(&con, &s, ROAR_META_MODE_SET, &meta);
126
127    fprintf(stderr, "Meta %-16s: %s\n", key, value);
128    ++ptr;
129  }
130
131 }
132
133 if ( roar_stream_exec(&con, &s) == -1 ) {
134  roar_disconnect(&con);
135  return -1;
136 }
137 
138 while (!eof) {
139  long ret = ov_read(&vf, pcmout, sizeof(pcmout), 0, 2, 1, &current_section);
140  if (ret == 0) {
141   /* EOF */
142   eof=1;
143  } else if (ret < 0) {
144   /* error in the stream.  Not a problem, just reporting it in
145      case we (the app) cares.  In this case, we don't. */
146  } else {
147     /* we don't bother dealing with sample rate changes, etc, but
148        you'll have to */
149    write(con.fh, pcmout, ret);
150  }
151 }
152
153  ov_clear(&vf);
154
155// fclose(in);
156 close(con.fh);
157
158 return 0;
159}
160
161//ll
Note: See TracBrowser for help on using the repository browser.