source: roaraudio/roarclients/roarphone.c @ 2312:98d61c023ae8

Last change on this file since 2312:98d61c023ae8 was 2312:98d61c023ae8, checked in by phi, 15 years ago

added a lot debug lions

File size: 8.0 KB
Line 
1//roarphone.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include <roaraudio.h>
26#include <libroardsp/libroardsp.h>
27#include <libroareio/libroareio.h>
28
29#if defined(ROAR_HAVE_LIBSPEEX) && !defined(ROAR_HAVE_LIBSPEEXDSP)
30#define _SPEEX_API_OLD
31#elif defined(ROAR_HAVE_LIBSPEEX) && defined(ROAR_HAVE_LIBSPEEXDSP)
32#define _SPEEX_API_NEW
33#endif
34
35#ifdef _SPEEX_API_OLD
36#include <speex/speex_echo.h>
37#endif
38
39#define TIMEDIV  100
40
41#define DRIVER  "oss"
42
43// anti echo:
44#define AE_NONE      0
45#define AE_SIMPLE    1
46#define AE_SPEEX     2
47#define AE_ROARD     3
48
49struct {
50 int antiecho;
51 int samples;
52 int transcode;
53} g_conf;
54
55struct roar_bixcoder transcoder[1];
56
57void usage (void) {
58 printf("roarphone [OPTIONS]...\n");
59
60 printf("\nOptions:\n\n");
61
62 printf("  --server   SERVER    - Set server hostname\n"
63        "  --rate     RATE      - Set sample rate\n"
64        "  --bits     BITS      - Set bits per sample\n"
65        "  --chans    CHANNELS  - Set number of channels\n"
66        "  --codec    CODEC     - Set the codec\n"
67        "  --driver   DRIVER    - Set the driver\n"
68        "  --device   DEVICE    - Set the device\n"
69        "  --antiecho AEMODE    - Set the anti echo mode\n"
70        "  --transcode          - Use local transcodeing\n"
71        "  --help               - Show this help\n"
72       );
73
74}
75
76int open_stream (struct roar_vio_calls * vio, char * server, struct roar_audio_info * info) {
77 return roar_vio_simple_stream(vio,
78                               info->rate, info->channels, info->bits, info->codec,
79                               server,
80                               ROAR_DIR_BIDIR,
81                               "roarphone");
82}
83
84#ifdef _SPEEX_API_OLD
85int anti_echo_speex16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) {
86 static SpeexEchoState * state = NULL;
87 size_t samples = info->rate / TIMEDIV;
88 static int16_t * obuf = NULL;
89
90 if ( info->channels != 1 )
91  return -1;
92
93 if (len != samples)
94  return -1;
95
96 ROAR_DBG("anti_echo_speex16(*) = ?");
97
98 if ( state == NULL ) {
99  if ( (state = speex_echo_state_init(samples, 100*samples)) == NULL )
100   return -1;
101
102  // todo: set sample rate.
103 }
104
105 ROAR_DBG("anti_echo_speex16(*) = ?");
106
107 if ( obuf == NULL ) {
108  if ( (obuf = malloc(2*samples)) == NULL )
109   return -1;
110 }
111
112 ROAR_DBG("anti_echo_speex16(*) = ?");
113
114/*
115 speex_echo_cancellation(state, buf, aebuf, obuf);
116*/
117
118 speex_echo_cancel(state, buf, aebuf, obuf, NULL);
119
120 memcpy(buf, obuf, 2*samples);
121
122 ROAR_DBG("anti_echo_speex16(*) = 0");
123
124 return 0;
125}
126#endif
127
128int anti_echo16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) {
129 size_t i;
130
131 switch (g_conf.antiecho) {
132  case AE_NONE:
133    return 0;
134   break;
135  case AE_SIMPLE:
136    for (i = 0; i < len; i++)
137     buf[i] -= aebuf[i];
138    return 0;
139   break;
140#ifdef _SPEEX_API_OLD
141  case AE_SPEEX:
142    return anti_echo_speex16(buf, aebuf, len, info);
143   break;
144#endif
145  case AE_ROARD:
146    return 0;
147   break;
148  default:
149    return -1;
150   break;
151 }
152
153 return -1;
154}
155
156int run_stream (struct roar_vio_calls * s0, struct roar_vio_calls * s1, struct roar_audio_info * info) {
157 size_t len;
158 void * outbuf, * micbuf;
159 ssize_t outlen, miclen;
160
161 ROAR_DBG("run_stream(*): g_conf.samples = %i, info->bits = %i", g_conf.samples, info->bits);
162 len = g_conf.samples * info->bits / 8;
163 ROAR_DBG("run_stream(*): len=%lu", (unsigned long) len);
164
165 if ( (outbuf = malloc(2*len)) == NULL )
166  return -1;
167
168 micbuf = outbuf + len;
169
170 while (1) {
171  if ( (miclen = roar_vio_read(s0, micbuf, len)) <= 0 )
172   break;
173  if ( g_conf.transcode ) {
174   if ( roar_bixcoder_write_packet(transcoder, micbuf, miclen) == -1 )
175    break;
176  } else {
177   if ( roar_vio_write(s1, micbuf, miclen) != miclen )
178    break;
179  }
180
181  if ( g_conf.transcode ) {
182   ROAR_DBG("run_stream(*): outbuf=%p, len=%lu", outbuf, (unsigned long) len);
183   if ( roar_bixcoder_read_packet(transcoder, outbuf, len) == -1 )
184    break;
185
186   outlen = len;
187  } else {
188   if ( (outlen = roar_vio_read(s1, outbuf, len)) <= 0 )
189    break;
190  }
191
192  if ( g_conf.antiecho != AE_NONE && info->bits == 16 )
193   anti_echo16(outbuf, micbuf, ROAR_MIN(miclen, outlen)/2, info);
194
195  if ( roar_vio_write(s0, outbuf, outlen) != outlen )
196   break;
197 }
198
199 free(outbuf);
200
201 return 0;
202}
203
204int main (int argc, char * argv[]) {
205 struct roar_audio_info info = {.rate     = ROAR_RATE_DEFAULT,
206                                .bits     = ROAR_BITS_DEFAULT,
207                                .channels = ROAR_CHANNELS_DEFAULT,
208                                .codec    = ROAR_CODEC_DEFAULT
209                               };
210 struct roar_audio_info dinfo;
211 struct roar_vio_calls dvio, svio;
212 char * driver   = DRIVER;
213 char * device   = NULL;
214 char * server   = NULL;
215 char * k;
216 int    i;
217
218 memset(&g_conf, 0, sizeof(g_conf));
219
220 g_conf.antiecho = AE_ROARD;
221
222 for (i = 1; i < argc; i++) {
223  k = argv[i];
224
225  if ( strcmp(k, "--server") == 0 ) {
226   server = argv[++i];
227  } else if ( strcmp(k, "--rate") == 0 ) {
228   info.rate = atoi(argv[++i]);
229  } else if ( strcmp(k, "--bits") == 0 ) {
230   info.bits = atoi(argv[++i]);
231  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) {
232   info.channels = atoi(argv[++i]);
233  } else if ( strcmp(k, "--codec") == 0 ) {
234   info.codec = roar_str2codec(argv[++i]);
235  } else if ( strcmp(k, "--driver") == 0 ) {
236   driver = argv[++i];
237  } else if ( strcmp(k, "--device") == 0 ) {
238   device = argv[++i];
239  } else if ( strcmp(k, "--antiecho") == 0 ) {
240   k = argv[++i];
241   if ( !strcmp(k, "none") ) {
242    g_conf.antiecho = AE_NONE;
243   } else if ( !strcmp(k, "simple") ) {
244    g_conf.antiecho = AE_SIMPLE;
245   } else if ( !strcmp(k, "speex") ) {
246    g_conf.antiecho = AE_SPEEX;
247   } else if ( !strcmp(k, "roard") ) {
248    g_conf.antiecho = AE_ROARD;
249   } else {
250    fprintf(stderr, "Error: unknown mode: %s\n", k);
251    return 1;
252   }
253  } else if ( strcmp(k, "--transcode") == 0 ) {
254   g_conf.transcode = 1;
255  } else if ( strcmp(k, "--help") == 0 ) {
256   usage();
257   return 0;
258  } else {
259   fprintf(stderr, "Error: unknown argument: %s\n", k);
260   usage();
261   return 1;
262  }
263 }
264
265 if ( g_conf.antiecho == AE_SPEEX ) {
266  ROAR_WARN("Speex Antiecho is obsolete and may be removed in future versions. Use --antiecho roard");
267 }
268
269 g_conf.samples = info.channels * info.rate / TIMEDIV;
270
271 memcpy(&dinfo, &info, sizeof(dinfo));
272
273 if ( g_conf.transcode ) {
274  dinfo.bits  = 16;
275  dinfo.codec = ROAR_CODEC_DEFAULT;
276
277  switch (info.codec) {
278   case ROAR_CODEC_ALAW:
279   case ROAR_CODEC_MULAW:
280     info.bits = 8;
281    break;
282   case ROAR_CODEC_ROAR_CELT:
283     info.bits = 16;
284    break;
285   case ROAR_CODEC_ROAR_SPEEX:
286     info.bits = 16;
287    break;
288  }
289 }
290
291 if ( roar_cdriver_open(&dvio, driver, device, &dinfo, ROAR_DIR_BIDIR) == -1 ) {
292  return 1;
293 }
294
295 ROAR_DBG("main(*): CALL open_stream(&svio, server, &info)");
296 if ( open_stream(&svio, server, &info) == -1 ) {
297  roar_vio_close(&dvio);
298  return 2;
299 }
300 ROAR_DBG("main(*): RET");
301
302 if ( g_conf.transcode ) {
303  dinfo.codec = info.codec;
304
305  if ( roar_bixcoder_init(transcoder, &dinfo, &svio) == -1 ) {
306   roar_vio_close(&svio);
307   roar_vio_close(&dvio);
308   return 10;
309  }
310
311  g_conf.samples = 8 * roar_bixcoder_packet_size(transcoder, -1) / dinfo.bits;
312 }
313
314 ROAR_DBG("main(*): CALL run_stream(&dvio, &svio, &info);");
315 run_stream(&dvio, &svio, &info);
316 ROAR_DBG("main(*): RET");
317
318 roar_bixcoder_close(transcoder);
319
320 roar_vio_close(&svio);
321 roar_vio_close(&dvio);
322
323 return 0;
324}
325
326//ll
Note: See TracBrowser for help on using the repository browser.