source: roaraudio/roarclients/roarcat.c @ 5457:d4b1d5287492

Last change on this file since 5457:d4b1d5287492 was 5457:d4b1d5287492, checked in by phi, 12 years ago

convert to use struct roar_audio_info

File size: 7.9 KB
Line 
1//roarcat.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012
5 *
6 *  This file is part of roarclients a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include <roaraudio.h>
27
28#define BUFSIZE 1024
29
30void usage (void) {
31 printf("roarcat [OPTIONS]... [FILE]\n");
32
33 printf("\nOptions:\n\n");
34
35 printf("  --server    SERVER    - Set server hostname\n"
36        "  --rate  -R  RATE      - Set sample rate\n"
37        "  --bits  -B  BITS      - Set bits per sample\n"
38        "  --chans -C  CHANNELS  - Set number of channels\n"
39        "  --codec     CODEC     - Set the codec\n"
40        "  --wave                - Use Wave Audio (PCM) as input\n"
41        "  --midi                - Use MIDI Audio as input\n"
42        "  --light               - Use light control input\n"
43        "  --raw                 - Use raw input\n"
44        "  --complex             - Use complex input\n"
45        "  --rdtcs               - Use Radio Data and Transmitter Control System input\n"
46        "  --rel-id ID           - Set ID of relative stream\n"
47        "  --role ROLE           - Set stream role\n"
48        "  --help                - Show this help\n"
49       );
50
51}
52
53int main (int argc, char * argv[]) {
54 struct roar_audio_info info = {
55  .rate = ROAR_AUDIO_INFO_INVALID,
56  .bits = ROAR_AUDIO_INFO_INVALID,
57  .channels = ROAR_AUDIO_INFO_INVALID,
58  .codec = ROAR_AUDIO_INFO_INVALID};
59 int    dir      = ROAR_DIR_PLAY;
60 int    rel_id   = -1;
61 int    role     = ROAR_ROLE_UNKNOWN;
62 char * server   = NULL;
63 char * k;
64 int    i;
65 char * name = "roarcat";
66 struct roar_connection    con;
67 struct roar_stream        s;
68 struct roar_vio_calls     file, * stream;
69 struct roar_vio_defaults  def;
70 int file_opened = 0;
71
72 if ( roar_vio_open_fh(&file, ROAR_STDIN) == -1 )
73  return 1;
74
75 if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_RDONLY, 0644) == -1 )
76  return 1;
77
78 for (i = 1; i < argc; i++) {
79  k = argv[i];
80
81  if ( !strcmp(k, "--server") || !strcmp(k, "-s") ) {
82   server = argv[++i];
83  } else if ( !strcmp(k, "-n") ) {
84   name = argv[++i];
85  } else if ( !strcmp(k, "--rate") || !strcmp(k, "-r") || !strcmp(k, "-R") ) {
86   info.rate = roar_str2rate(argv[++i]);
87  } else if ( !strcmp(k, "--bits") || !strcmp(k, "-B") ) {
88   info.bits = roar_str2bits(argv[++i]);
89  } else if ( !strcmp(k, "-b") ) {
90   info.bits = 8;
91  } else if ( !strcmp(k, "--channels") || !strcmp(k, "--chans") || !strcmp(k, "-C") ) {
92   info.channels = roar_str2channels(argv[++i]);
93  } else if ( !strcmp(k, "-m") ) {
94   info.channels = 1;
95  } else if ( !strcmp(k, "--codec") || !strcmp(k, "-E") ) {
96   if ( (info.codec = roar_str2codec(argv[++i])) == ROAR_AUDIO_INFO_INVALID ) {
97    fprintf(stderr, "Error: Unknown codec: %s\n", argv[i]);
98    return 1;
99   }
100
101  } else if ( !strcmp(k, "--wave") ) {
102   dir   = ROAR_DIR_PLAY;
103  } else if ( !strcmp(k, "--midi") ) {
104   dir   = ROAR_DIR_MIDI_IN;
105  } else if ( !strcmp(k, "--light") ) {
106   dir   = ROAR_DIR_LIGHT_IN;
107  } else if ( !strcmp(k, "--raw") ) {
108   dir   = ROAR_DIR_RAW_IN;
109  } else if ( !strcmp(k, "--complex") ) {
110   dir   = ROAR_DIR_COMPLEX_IN;
111  } else if ( !strcmp(k, "--rdtcs") ) {
112   dir   = ROAR_DIR_RDTCS_IN;
113
114  } else if ( !strcmp(k, "--rel-id") ) {
115   rel_id = atoi(argv[++i]);
116  } else if ( !strcmp(k, "--role") ) {
117   role = roar_str2role(argv[++i]);
118
119  } else if ( !strcmp(k, "--help") || !strcmp(k, "-h") ) {
120   usage();
121   return 0;
122  } else if ( !file_opened ) {
123   file_opened = 1;
124   if ( roar_vio_open_dstr(&file, k, &def, 1) == -1 ) {
125    fprintf(stderr, "Error: can not open file: %s: %s\n", k, roar_error2str(roar_error));
126    return 1;
127   }
128  } else {
129   fprintf(stderr, "Error: unknown argument: %s\n", k);
130   usage();
131   return 1;
132  }
133 }
134
135 switch (dir) {
136  case ROAR_DIR_PLAY:
137    if ( info.rate     == ROAR_AUDIO_INFO_INVALID ) info.rate     = ROAR_RATE_DEFAULT;
138    if ( info.bits     == ROAR_AUDIO_INFO_INVALID ) info.bits     = ROAR_BITS_DEFAULT;
139    if ( info.channels == ROAR_AUDIO_INFO_INVALID ) info.channels = ROAR_CHANNELS_DEFAULT;
140    if ( info.codec    == ROAR_AUDIO_INFO_INVALID ) info.codec    = ROAR_CODEC_DEFAULT;
141   break;
142  case ROAR_DIR_MIDI_IN:
143    if ( info.rate     == ROAR_AUDIO_INFO_INVALID ) info.rate     = 0;
144    if ( info.bits     == ROAR_AUDIO_INFO_INVALID ) info.bits     = ROAR_MIDI_BITS;
145    if ( info.channels == ROAR_AUDIO_INFO_INVALID ) info.channels = ROAR_MIDI_CHANNELS_DEFAULT;
146    if ( info.codec    == ROAR_AUDIO_INFO_INVALID ) info.codec    = ROAR_CODEC_MIDI;
147   break;
148  case ROAR_DIR_LIGHT_IN:
149    if ( info.rate     == ROAR_AUDIO_INFO_INVALID ) info.rate     = 0;
150    if ( info.bits     == ROAR_AUDIO_INFO_INVALID ) info.bits     = ROAR_LIGHT_BITS;
151    if ( info.channels == ROAR_AUDIO_INFO_INVALID ) info.channels = 0;
152    if ( info.codec    == ROAR_AUDIO_INFO_INVALID ) info.codec    = ROAR_CODEC_DMX512;
153   break;
154  case ROAR_DIR_COMPLEX_IN:
155    if ( info.rate     == ROAR_AUDIO_INFO_INVALID ) info.rate     = ROAR_COMPLEX_RATE;
156    if ( info.bits     == ROAR_AUDIO_INFO_INVALID ) info.bits     = ROAR_COMPLEX_BITS;
157    if ( info.channels == ROAR_AUDIO_INFO_INVALID ) info.channels = ROAR_COMPLEX_CHANNELS;
158    if ( info.codec    == ROAR_AUDIO_INFO_INVALID ) info.codec    = ROAR_COMPLEX_CODEC;
159   break;
160  case ROAR_DIR_RDTCS_IN:
161    if ( info.rate     == ROAR_AUDIO_INFO_INVALID ) info.rate     = ROAR_RDTCS_RATE;
162    if ( info.bits     == ROAR_AUDIO_INFO_INVALID ) info.bits     = ROAR_RDTCS_BITS;
163    if ( info.channels == ROAR_AUDIO_INFO_INVALID ) info.channels = ROAR_RDTCS_CHANNELS;
164    if ( info.codec    == ROAR_AUDIO_INFO_INVALID ) info.codec    = ROAR_RDTCS_CODEC;
165   break;
166  case ROAR_DIR_RAW_IN:
167  default:
168    if ( info.rate     == ROAR_AUDIO_INFO_INVALID ) info.rate     = 0;
169    if ( info.bits     == ROAR_AUDIO_INFO_INVALID ) info.bits     = 0;
170    if ( info.channels == ROAR_AUDIO_INFO_INVALID ) info.channels = 0;
171    if ( info.codec    == ROAR_AUDIO_INFO_INVALID ) info.codec    = ROAR_CODEC_DEFAULT;
172   break;
173 }
174
175 if ( roar_simple_connect(&con, server, name) == -1 ) {
176  fprintf(stderr, "Error: can not connect to server: %s\n", roar_error2str(roar_error));
177  return 10;
178 }
179
180 if ( roar_stream_new(&s, info.rate, info.channels, info.bits, info.codec) == -1 ) {
181  fprintf(stderr, "Error: can not create stream\n");
182  roar_disconnect(&con);
183  return 20;
184 }
185
186 if ( rel_id != -1 ) {
187  if ( roar_stream_set_rel_id(&s, rel_id) ) {
188   fprintf(stderr, "Error: can not set id or realative stream\n");
189   roar_disconnect(&con);
190   return 21;
191  }
192 }
193
194 if ( roar_stream_connect(&con, &s, dir, -1) == -1 ) {
195  fprintf(stderr, "Error: can not connect stream to server\n");
196  roar_disconnect(&con);
197  return 11;
198 }
199
200 if ( role != ROAR_ROLE_UNKNOWN ) {
201  if ( roar_stream_set_role(&con, &s, role) == -1 ) {
202   fprintf(stderr, "Warning: can not set stream role\n");
203  }
204 }
205
206 if ( roar_stream_exec(&con, &s) == -1 ) {
207  fprintf(stderr, "Error: can not exec stream\n");
208  roar_disconnect(&con);
209  return 12;
210 }
211
212 if ( (stream = roar_get_connection_vio2(&con)) == NULL ) {
213  fprintf(stderr, "Error: can not get stream vio\n");
214  roar_disconnect(&con);
215  return 13;
216 }
217
218 if ( roar_vio_copy_data(stream, &file) == -1 ) {
219  fprintf(stderr, "Error: can not copy data from source to server.\n");
220  roar_vio_close(stream);
221  roar_vio_close(&file);
222  return 14;
223 }
224
225 roar_vio_close(stream);
226 roar_vio_close(&file);
227
228 return 0;
229}
230
231//ll
Note: See TracBrowser for help on using the repository browser.