source: roaraudio/roarclients/roarcat.c @ 5950:7fe8f5df7a83

Last change on this file since 5950:7fe8f5df7a83 was 5950:7fe8f5df7a83, checked in by phi, 10 years ago

code cleanup and again a patch for checking commandlion parameters

File size: 8.4 KB
Line 
1//roarcat.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2013
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 -E  CODEC     - Set the codec\n"
40        "  --aiprofile PROFILE   - Set audio profile\n"
41        "  --wave                - Use Wave Audio (PCM) as input\n"
42        "  --midi                - Use MIDI Audio as input\n"
43        "  --light               - Use light control input\n"
44        "  --raw                 - Use raw input\n"
45        "  --complex             - Use complex input\n"
46        "  --rdtcs               - Use Radio Data and Transmitter Control System input\n"
47        "  --rel-id ID           - Set ID of relative stream\n"
48        "  --role ROLE           - Set stream role\n"
49        "  --help                - Show this help\n"
50       );
51
52}
53
54int main (int argc, char * argv[]) {
55 struct roar_audio_info info = {
56  .rate = ROAR_AUDIO_INFO_INVALID,
57  .bits = ROAR_AUDIO_INFO_INVALID,
58  .channels = ROAR_AUDIO_INFO_INVALID,
59  .codec = ROAR_AUDIO_INFO_INVALID};
60 int    dir      = ROAR_DIR_PLAY;
61 int    rel_id   = -1;
62 int    role     = ROAR_ROLE_UNKNOWN;
63 const char * server   = NULL;
64 const char * k;
65 int    i;
66 const char * name = "roarcat";
67 struct roar_connection    con;
68 struct roar_stream        s;
69 struct roar_vio_calls     file, * stream;
70 struct roar_vio_defaults  def;
71 int file_opened = 0;
72
73 // TODO: make use of roar_stdin.
74 if ( roar_vio_open_fh(&file, ROAR_STDIN) == -1 )
75  return 1;
76
77 if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_RDONLY, 0644) == -1 )
78  return 1;
79
80 for (i = 1; i < argc; i++) {
81  k = argv[i];
82
83  if ( !strcmp(k, "--server") || !strcmp(k, "-s") ) {
84   ROAR_CKHAVEARGS(1);
85   server = argv[++i];
86  } else if ( !strcmp(k, "-n") ) {
87   ROAR_CKHAVEARGS(1);
88   name = argv[++i];
89  } else if ( !strcmp(k, "--rate") || !strcmp(k, "-r") || !strcmp(k, "-R") ) {
90   ROAR_CKHAVEARGS(1);
91   info.rate = roar_str2rate(argv[++i]);
92  } else if ( !strcmp(k, "--bits") || !strcmp(k, "-B") ) {
93   ROAR_CKHAVEARGS(1);
94   info.bits = roar_str2bits(argv[++i]);
95  } else if ( !strcmp(k, "-b") ) {
96   info.bits = 8;
97  } else if ( !strcmp(k, "--channels") || !strcmp(k, "--chans") || !strcmp(k, "-C") ) {
98   ROAR_CKHAVEARGS(1);
99   info.channels = roar_str2channels(argv[++i]);
100  } else if ( !strcmp(k, "-m") ) {
101   info.channels = 1;
102  } else if ( !strcmp(k, "--codec") || !strcmp(k, "-E") ) {
103   ROAR_CKHAVEARGS(1);
104   if ( (info.codec = roar_str2codec(argv[++i])) == ROAR_AUDIO_INFO_INVALID ) {
105    fprintf(stderr, "Error: Unknown codec: %s\n", argv[i]);
106    return 1;
107   }
108  } else if ( !strcmp(k, "--aiprofile") ) {
109   ROAR_CKHAVEARGS(1);
110   if ( roar_profile2info(&info, argv[++i]) == -1 ) {
111    fprintf(stderr, "Error: Can not load audio profile: %s: %s\n", argv[i], roar_error2str(roar_error));
112    return 1;
113   }
114
115  } else if ( !strcmp(k, "--wave") ) {
116   dir   = ROAR_DIR_PLAY;
117  } else if ( !strcmp(k, "--midi") ) {
118   dir   = ROAR_DIR_MIDI_IN;
119  } else if ( !strcmp(k, "--light") ) {
120   dir   = ROAR_DIR_LIGHT_IN;
121  } else if ( !strcmp(k, "--raw") ) {
122   dir   = ROAR_DIR_RAW_IN;
123  } else if ( !strcmp(k, "--complex") ) {
124   dir   = ROAR_DIR_COMPLEX_IN;
125  } else if ( !strcmp(k, "--rdtcs") ) {
126   dir   = ROAR_DIR_RDTCS_IN;
127
128  } else if ( !strcmp(k, "--rel-id") ) {
129   ROAR_CKHAVEARGS(1);
130   rel_id = atoi(argv[++i]);
131  } else if ( !strcmp(k, "--role") ) {
132   ROAR_CKHAVEARGS(1);
133   role = roar_str2role(argv[++i]);
134
135  } else if ( !strcmp(k, "--help") || !strcmp(k, "-h") ) {
136   usage();
137   return 0;
138  } else if ( !file_opened ) {
139   file_opened = 1;
140   if ( roar_vio_open_dstr(&file, k, &def, 1) == -1 ) {
141    fprintf(stderr, "Error: can not open file: %s: %s\n", k, roar_error2str(roar_error));
142    return 1;
143   }
144  } else {
145   fprintf(stderr, "Error: unknown argument: %s\n", k);
146   usage();
147   return 1;
148  }
149 }
150
151 switch (dir) {
152  case ROAR_DIR_PLAY:
153    if ( info.rate     == ROAR_AUDIO_INFO_INVALID ) info.rate     = ROAR_RATE_DEFAULT;
154    if ( info.bits     == ROAR_AUDIO_INFO_INVALID ) info.bits     = ROAR_BITS_DEFAULT;
155    if ( info.channels == ROAR_AUDIO_INFO_INVALID ) info.channels = ROAR_CHANNELS_DEFAULT;
156    if ( info.codec    == ROAR_AUDIO_INFO_INVALID ) info.codec    = ROAR_CODEC_DEFAULT;
157   break;
158  case ROAR_DIR_MIDI_IN:
159    if ( info.rate     == ROAR_AUDIO_INFO_INVALID ) info.rate     = 0;
160    if ( info.bits     == ROAR_AUDIO_INFO_INVALID ) info.bits     = ROAR_MIDI_BITS;
161    if ( info.channels == ROAR_AUDIO_INFO_INVALID ) info.channels = ROAR_MIDI_CHANNELS_DEFAULT;
162    if ( info.codec    == ROAR_AUDIO_INFO_INVALID ) info.codec    = ROAR_CODEC_MIDI;
163   break;
164  case ROAR_DIR_LIGHT_IN:
165    if ( info.rate     == ROAR_AUDIO_INFO_INVALID ) info.rate     = 0;
166    if ( info.bits     == ROAR_AUDIO_INFO_INVALID ) info.bits     = ROAR_LIGHT_BITS;
167    if ( info.channels == ROAR_AUDIO_INFO_INVALID ) info.channels = 0;
168    if ( info.codec    == ROAR_AUDIO_INFO_INVALID ) info.codec    = ROAR_CODEC_DMX512;
169   break;
170  case ROAR_DIR_COMPLEX_IN:
171    if ( info.rate     == ROAR_AUDIO_INFO_INVALID ) info.rate     = ROAR_COMPLEX_RATE;
172    if ( info.bits     == ROAR_AUDIO_INFO_INVALID ) info.bits     = ROAR_COMPLEX_BITS;
173    if ( info.channels == ROAR_AUDIO_INFO_INVALID ) info.channels = ROAR_COMPLEX_CHANNELS;
174    if ( info.codec    == ROAR_AUDIO_INFO_INVALID ) info.codec    = ROAR_COMPLEX_CODEC;
175   break;
176  case ROAR_DIR_RDTCS_IN:
177    if ( info.rate     == ROAR_AUDIO_INFO_INVALID ) info.rate     = ROAR_RDTCS_RATE;
178    if ( info.bits     == ROAR_AUDIO_INFO_INVALID ) info.bits     = ROAR_RDTCS_BITS;
179    if ( info.channels == ROAR_AUDIO_INFO_INVALID ) info.channels = ROAR_RDTCS_CHANNELS;
180    if ( info.codec    == ROAR_AUDIO_INFO_INVALID ) info.codec    = ROAR_RDTCS_CODEC;
181   break;
182  case ROAR_DIR_RAW_IN:
183  default:
184    if ( info.rate     == ROAR_AUDIO_INFO_INVALID ) info.rate     = 0;
185    if ( info.bits     == ROAR_AUDIO_INFO_INVALID ) info.bits     = 0;
186    if ( info.channels == ROAR_AUDIO_INFO_INVALID ) info.channels = 0;
187    if ( info.codec    == ROAR_AUDIO_INFO_INVALID ) info.codec    = ROAR_CODEC_DEFAULT;
188   break;
189 }
190
191 if ( roar_simple_connect(&con, server, name) == -1 ) {
192  fprintf(stderr, "Error: can not connect to server: %s\n", roar_error2str(roar_error));
193  return 10;
194 }
195
196 if ( roar_stream_new_by_info(&s, &info) == -1 ) {
197  fprintf(stderr, "Error: can not create stream\n");
198  roar_disconnect(&con);
199  return 20;
200 }
201
202 if ( rel_id != -1 ) {
203  if ( roar_stream_set_rel_id(&s, rel_id) ) {
204   fprintf(stderr, "Error: can not set id or realative stream\n");
205   roar_disconnect(&con);
206   return 21;
207  }
208 }
209
210 if ( roar_stream_connect(&con, &s, dir, -1) == -1 ) {
211  fprintf(stderr, "Error: can not connect stream to server\n");
212  roar_disconnect(&con);
213  return 11;
214 }
215
216 if ( role != ROAR_ROLE_UNKNOWN ) {
217  if ( roar_stream_set_role(&con, &s, role) == -1 ) {
218   fprintf(stderr, "Warning: can not set stream role\n");
219  }
220 }
221
222 if ( roar_stream_exec(&con, &s) == -1 ) {
223  fprintf(stderr, "Error: can not exec stream\n");
224  roar_disconnect(&con);
225  return 12;
226 }
227
228 if ( (stream = roar_get_connection_vio2(&con)) == NULL ) {
229  fprintf(stderr, "Error: can not get stream vio\n");
230  roar_disconnect(&con);
231  return 13;
232 }
233
234 if ( roar_vio_copy_data(stream, &file) == -1 ) {
235  fprintf(stderr, "Error: can not copy data from source to server.\n");
236  roar_vio_close(stream);
237  roar_vio_close(&file);
238  return 14;
239 }
240
241 roar_vio_close(stream);
242 roar_vio_close(&file);
243
244 return 0;
245}
246
247//ll
Note: See TracBrowser for help on using the repository browser.