source: roaraudio/roarclients/roarphone.c @ 2992:ff4cbda31297

Last change on this file since 2992:ff4cbda31297 was 2991:2006474e65c9, checked in by phi, 15 years ago

added support for downmix of channels an lowpass

File size: 15.5 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
49#define DTX_F        25
50
51#define CON_NONE     0x00
52#define CON_CON      0x01
53#define CON_STREAM   0x02
54
55struct {
56 int antiecho;
57 int samples;
58 int transcode;
59 int64_t dtx_threshold;
60 size_t jumbo_mtu;
61 struct {
62  struct {
63   int downmix;
64   float lowp_freq;
65  } in;
66 } filter;
67} g_conf;
68
69int dtx_counter = 0;
70
71struct {
72 int state;
73 struct roar_connection con;
74 struct roar_stream     stream;
75 struct roar_vio_calls * svio;
76} g_cons;
77
78struct roar_bixcoder transcoder[1];
79
80struct {
81 struct roardsp_filterchain input;
82 struct roardsp_filterchain output;
83} g_filterchains;
84
85struct {
86 struct {
87  char key[ROAR_META_MAX_NAMELEN];
88  char value[LIBROAR_BUFFER_MSGDATA];
89 } tmp;
90 char * rn;
91 char * nick;
92 char * org;
93 char * email;
94 char * hp;
95 char * loc;
96 char * thumbnail;
97} g_meta;
98
99void usage (void) {
100 printf("roarphone [OPTIONS]...\n");
101
102 printf("\nServer Options:\n\n");
103
104 printf("  --server   SERVER    - Set server hostname\n"
105        " --jumbo-mtu MTU       - Sets the MTU for Jumbo Packets\n"
106       );
107
108 printf("\nAudio Options:\n\n");
109 printf("  --rate     RATE      - Set sample rate\n"
110        "  --bits     BITS      - Set bits per sample\n"
111        "  --chans    CHANNELS  - Set number of channels\n"
112       );
113
114 printf("\nAudio Filter Options:\n\n");
115 printf("  --afi-downmix        - Enable input downmixing\n"
116        "  --afi-lowpass FREQ   - Enable input lowpass at FREQ (in Hz)\n"
117       );
118
119 printf("\nCodec Options:\n\n");
120 printf("  --codec    CODEC     - Set the codec\n"
121        "  --transcode          - Use local transcodeing\n"
122       );
123
124 printf("\nDriver Options:\n\n");
125 printf("  --driver   DRIVER    - Set the driver\n"
126        "  --device   DEVICE    - Set the device\n"
127       );
128
129 printf("\nGeneral Options:\n\n");
130 printf("  --antiecho AEMODE    - Set the anti echo mode\n"
131        "  --threshold DTXTHRES - Set the DTX threshold, disabled by default\n"
132        "  --help               - Show this help\n"
133       );
134
135 printf("\nMeta Data Options:\n\n");
136 printf("  --m-rn    REALNAME   - Sets the real name\n"
137        "  --m-nick  NICK       - Sets the nick name\n"
138        "  --m-email EMAIL      - Sets the email address\n"
139        "  --m-hp    HOMEPAGE   - Sets the homepage URL\n"
140        "  --m-thumbn THUMBNAIL - Sets a URL to a thumbnail\n"
141        "  --m-loc   LOCATION   - Sets the location (room number)\n"
142        "  --m-org ORGANIZATION - Sets the organization/company name\n"
143       );
144}
145
146int open_stream (struct roar_vio_calls * vio, char * server, struct roar_audio_info * info) {
147 int fh;
148
149 g_cons.svio = vio;
150
151 if ( !(g_cons.state & CON_CON) )
152  if ( roar_simple_connect(&(g_cons.con), server, "roarphone") == -1 )
153   return -1;
154
155 g_cons.state |= CON_CON;
156
157 // TODO: find out if this can be done more nicely by using VIOs
158
159 if ( (fh = roar_simple_new_stream_obj(&(g_cons.con), &(g_cons.stream),
160                                       info->rate, info->channels, info->bits, info->codec,
161                                       ROAR_DIR_BIDIR
162                                      )) == -1 )
163  return -1;
164
165 roar_vio_open_fh_socket(vio, fh);
166
167 g_cons.state |= CON_STREAM;
168
169 return 0;
170}
171
172#define _SET_META(ivar,itype) if ( (ivar) != NULL ) {  \
173                              meta.value = (ivar);     \
174                              meta.type  = (itype);    \
175                              roar_stream_meta_set(&(g_cons.con), &(g_cons.stream), ROAR_META_MODE_SET, &meta); \
176                              }
177int set_meta (void) {
178 struct roar_meta   meta;
179
180 meta.value  = g_meta.tmp.value;
181 meta.key[0] = 0;
182 meta.type   = ROAR_META_TYPE_NONE;
183
184 roar_stream_meta_set(&(g_cons.con), &(g_cons.stream), ROAR_META_MODE_CLEAR, &meta);
185
186 _SET_META(g_meta.thumbnail, ROAR_META_TYPE_THUMBNAIL);
187 _SET_META(g_meta.loc,       ROAR_META_TYPE_LOCATION);
188 _SET_META(g_meta.hp,        ROAR_META_TYPE_HOMEPAGE);
189 _SET_META(g_meta.org,       ROAR_META_TYPE_ORGANIZATION);
190
191 if ( g_meta.nick != NULL ) {
192  if ( g_meta.rn  != NULL ) {
193   snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "%s (%s)", g_meta.rn, g_meta.nick);
194   g_meta.tmp.value[LIBROAR_BUFFER_MSGDATA-1] = 0;
195   _SET_META(g_meta.tmp.value, ROAR_META_TYPE_AUTHOR);
196  } else {
197   _SET_META(g_meta.nick, ROAR_META_TYPE_AUTHOR);
198  }
199 } else {
200  if ( g_meta.rn  != NULL ) {
201   _SET_META(g_meta.rn, ROAR_META_TYPE_AUTHOR);
202  }
203 }
204
205 // TODO: make this more nice...
206 if ( g_meta.email != NULL ) {
207  if ( g_meta.nick != NULL ) {
208   if ( g_meta.rn != NULL ) {
209    snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "%s (%s) <%s>", g_meta.rn, g_meta.nick, g_meta.email);
210   } else {
211    snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "%s <%s>", g_meta.nick, g_meta.email);
212   }
213  } else {
214   if ( g_meta.rn != NULL ) {
215    snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "%s <%s>", g_meta.rn, g_meta.email);
216   } else {
217    snprintf(g_meta.tmp.value, LIBROAR_BUFFER_MSGDATA-1, "<%s>", g_meta.email);
218   }
219  }
220  g_meta.tmp.value[LIBROAR_BUFFER_MSGDATA-1] = 0;
221  _SET_META(g_meta.tmp.value, ROAR_META_TYPE_CONTACT);
222 }
223
224 return 0;
225}
226
227#ifdef _SPEEX_API_OLD
228int anti_echo_speex16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) {
229 static SpeexEchoState * state = NULL;
230 size_t samples = info->rate / TIMEDIV;
231 static int16_t * obuf = NULL;
232
233 if ( info->channels != 1 )
234  return -1;
235
236 if (len != samples)
237  return -1;
238
239 ROAR_DBG("anti_echo_speex16(*) = ?");
240
241 if ( state == NULL ) {
242  if ( (state = speex_echo_state_init(samples, 100*samples)) == NULL )
243   return -1;
244
245  // todo: set sample rate.
246 }
247
248 ROAR_DBG("anti_echo_speex16(*) = ?");
249
250 if ( obuf == NULL ) {
251  if ( (obuf = malloc(2*samples)) == NULL )
252   return -1;
253 }
254
255 ROAR_DBG("anti_echo_speex16(*) = ?");
256
257/*
258 speex_echo_cancellation(state, buf, aebuf, obuf);
259*/
260
261 speex_echo_cancel(state, buf, aebuf, obuf, NULL);
262
263 memcpy(buf, obuf, 2*samples);
264
265 ROAR_DBG("anti_echo_speex16(*) = 0");
266
267 return 0;
268}
269#endif
270
271int anti_echo16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) {
272 size_t i;
273
274 switch (g_conf.antiecho) {
275  case AE_NONE:
276    return 0;
277   break;
278  case AE_SIMPLE:
279    for (i = 0; i < len; i++)
280     buf[i] -= aebuf[i];
281    return 0;
282   break;
283#ifdef _SPEEX_API_OLD
284  case AE_SPEEX:
285    return anti_echo_speex16(buf, aebuf, len, info);
286   break;
287#endif
288  case AE_ROARD:
289    return 0;
290   break;
291  default:
292    return -1;
293   break;
294 }
295
296 return -1;
297}
298
299int zero_if_noise16 (int16_t * data, size_t samples) {
300 int64_t rms = roar_rms2_1_16(data, samples);
301
302 if ( rms < g_conf.dtx_threshold ) {
303  if ( dtx_counter ) {
304   dtx_counter--;
305  } else {
306   memset(data, 0, samples*2);
307  }
308 } else {
309  dtx_counter = DTX_F;
310 }
311
312 return 0;
313}
314
315int run_stream (struct roar_vio_calls * s0, struct roar_vio_calls * s1, struct roar_audio_info * info) {
316 size_t len;
317 void * outbuf, * micbuf;
318 ssize_t outlen, miclen;
319
320 ROAR_DBG("run_stream(*): g_conf.samples = %i, info->bits = %i", g_conf.samples, info->bits);
321 len = g_conf.samples * info->bits / 8;
322 ROAR_DBG("run_stream(*): len=%lu", (unsigned long) len);
323
324 if ( (outbuf = malloc(2*len)) == NULL )
325  return -1;
326
327 micbuf = outbuf + len;
328
329 while (1) {
330  if ( (miclen = roar_vio_read(s0, micbuf, len)) <= 0 )
331   break;
332
333  if ( roardsp_fchain_num(&(g_filterchains.input)) ) {
334   if ( roardsp_fchain_calc(&(g_filterchains.input), micbuf, len) == -1 )
335    break;
336  }
337
338  if ( g_conf.dtx_threshold > 0 )
339   if ( info->bits == 16 )
340    zero_if_noise16(micbuf, miclen/2);
341
342  if ( g_conf.transcode ) {
343   if ( roar_bixcoder_write_packet(transcoder, micbuf, miclen) == -1 )
344    break;
345  } else {
346   if ( roar_vio_write(s1, micbuf, miclen) != miclen )
347    break;
348  }
349
350  if ( g_conf.transcode ) {
351   ROAR_DBG("run_stream(*): outbuf=%p, len=%lu", outbuf, (unsigned long) len);
352   if ( roar_bixcoder_read_packet(transcoder, outbuf, len) == -1 )
353    break;
354
355   outlen = len;
356  } else {
357   if ( (outlen = roar_vio_read(s1, outbuf, len)) <= 0 )
358    break;
359  }
360
361  if ( g_conf.antiecho != AE_NONE && info->bits == 16 )
362   anti_echo16(outbuf, micbuf, ROAR_MIN(miclen, outlen)/2, info);
363
364  if ( roardsp_fchain_num(&(g_filterchains.output)) ) {
365   if ( roardsp_fchain_calc(&(g_filterchains.output), outbuf, outlen) == -1 )
366    break;
367  }
368
369  if ( roar_vio_write(s0, outbuf, outlen) != outlen )
370   break;
371 }
372
373 free(outbuf);
374
375 return 0;
376}
377
378int main (int argc, char * argv[]) {
379 struct roar_audio_info info = {.rate     = ROAR_RATE_DEFAULT,
380                                .bits     = ROAR_BITS_DEFAULT,
381                                .channels = ROAR_CHANNELS_DEFAULT,
382                                .codec    = ROAR_CODEC_DEFAULT
383                               };
384 struct roar_audio_info dinfo;
385 struct roar_vio_calls dvio, svio, svio_jumbo, svio_real;
386 struct roar_vio_calls * svio_p;
387 struct roardsp_filter * filter;
388 char * driver   = DRIVER;
389 char * device   = NULL;
390 char * server   = NULL;
391 char * k;
392 int    i;
393
394 memset(&g_conf, 0, sizeof(g_conf));
395
396 g_conf.antiecho      = AE_ROARD;
397 g_conf.dtx_threshold = -1;
398
399 memset(&g_cons, 0, sizeof(g_cons));
400 g_cons.state = CON_NONE;
401
402 memset(&g_meta, 0, sizeof(g_meta));
403
404 roardsp_fchain_init(&(g_filterchains.input));
405 roardsp_fchain_init(&(g_filterchains.output));
406
407 for (i = 1; i < argc; i++) {
408  k = argv[i];
409
410  if ( strcmp(k, "--server") == 0 ) {
411   server = argv[++i];
412  } else if ( strcmp(k, "--jumbo-mtu") == 0 ) {
413   g_conf.jumbo_mtu = atoi(argv[++i]);
414  } else if ( strcmp(k, "--rate") == 0 ) {
415   info.rate = atoi(argv[++i]);
416  } else if ( strcmp(k, "--bits") == 0 ) {
417   info.bits = atoi(argv[++i]);
418  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) {
419   info.channels = atoi(argv[++i]);
420
421/*
422 printf("  --afi-downmix        - Enable input downmixing\n"
423        "  --afi-lowpass FREQ   - Enable input lowpass at FREQ (in Hz)\n"
424*/
425  } else if ( strcmp(k, "--afi-downmix") == 0 ) {
426   g_conf.filter.in.downmix = 1;
427  } else if ( strcmp(k, "--afi-lowpass") == 0 ) {
428   g_conf.filter.in.lowp_freq = atof(argv[++i]);
429
430  } else if ( strcmp(k, "--codec") == 0 ) {
431   info.codec = roar_str2codec(argv[++i]);
432
433  } else if ( strcmp(k, "--driver") == 0 ) {
434   driver = argv[++i];
435  } else if ( strcmp(k, "--device") == 0 ) {
436   device = argv[++i];
437  } else if ( strcmp(k, "--antiecho") == 0 ) {
438   k = argv[++i];
439   if ( !strcmp(k, "none") ) {
440    g_conf.antiecho = AE_NONE;
441   } else if ( !strcmp(k, "simple") ) {
442    g_conf.antiecho = AE_SIMPLE;
443   } else if ( !strcmp(k, "speex") ) {
444    g_conf.antiecho = AE_SPEEX;
445   } else if ( !strcmp(k, "roard") ) {
446    g_conf.antiecho = AE_ROARD;
447   } else {
448    fprintf(stderr, "Error: unknown mode: %s\n", k);
449    return 1;
450   }
451  } else if ( strcmp(k, "--threshold") == 0 ) {
452   g_conf.dtx_threshold = atol(argv[++i]);
453
454   // use threshold^2 or threshold < 0 for not using DTX
455   if ( g_conf.dtx_threshold > 0 )
456    g_conf.dtx_threshold *= g_conf.dtx_threshold;
457  } else if ( strcmp(k, "--transcode") == 0 ) {
458   g_conf.transcode = 1;
459
460  // META DATA:
461  } else if ( strcmp(k, "--m-rn") == 0 ) {
462   g_meta.rn = argv[++i];
463  } else if ( strcmp(k, "--m-nick") == 0 ) {
464   g_meta.nick = argv[++i];
465  } else if ( strcmp(k, "--m-email") == 0 ) {
466   g_meta.email = argv[++i];
467  } else if ( strcmp(k, "--m-hp") == 0 ) {
468   g_meta.hp = argv[++i];
469  } else if ( strcmp(k, "--m-thumbn") == 0 ) {
470   g_meta.thumbnail = argv[++i];
471  } else if ( strcmp(k, "--m-loc") == 0 ) {
472   g_meta.loc = argv[++i];
473  } else if ( strcmp(k, "--m-org") == 0 ) {
474   g_meta.org = argv[++i];
475
476
477  } else if ( strcmp(k, "--help") == 0 ) {
478   usage();
479   return 0;
480  } else {
481   fprintf(stderr, "Error: unknown argument: %s\n", k);
482   usage();
483   return 1;
484  }
485 }
486
487 // ignore errors, maybe it will work even if this fails
488 // (btw. it will never fail without crashing the rest of the app ;)
489 roar_libroar_set_server(server);
490
491 if ( g_conf.antiecho == AE_SPEEX ) {
492  ROAR_WARN("Speex Antiecho is obsolete and may be removed in future versions. Use --antiecho roard");
493 }
494
495 g_conf.samples = info.channels * info.rate / TIMEDIV;
496
497 memcpy(&dinfo, &info, sizeof(dinfo));
498
499 if ( g_conf.transcode ) {
500  dinfo.bits  = 16;
501  dinfo.codec = ROAR_CODEC_DEFAULT;
502
503  switch (info.codec) {
504   case ROAR_CODEC_ALAW:
505   case ROAR_CODEC_MULAW:
506     info.bits = 8;
507    break;
508   case ROAR_CODEC_ROAR_CELT:
509     info.bits = 16;
510    break;
511   case ROAR_CODEC_ROAR_SPEEX:
512     info.bits = 16;
513    break;
514  }
515 }
516
517 if ( roar_cdriver_open(&dvio, driver, device, &dinfo, ROAR_DIR_BIDIR) == -1 ) {
518  ROAR_ERR("Can not open sound card.");
519  return 1;
520 }
521
522 ROAR_DBG("main(*): CALL open_stream(&svio, server, &info)");
523 if ( open_stream(&svio_real, server, &info) == -1 ) {
524  ROAR_ERR("Can not open connection to server.");
525  roar_vio_close(&dvio);
526  return 2;
527 }
528 ROAR_DBG("main(*): RET");
529
530 if ( roar_vio_open_re(&svio, &svio_real) == -1 ) {
531  ROAR_ERR("Can not open connection to server (RE VIO).");
532  roar_vio_close(&dvio);
533  return 2;
534 }
535
536 if ( g_conf.jumbo_mtu ) {
537  if ( roar_vio_open_jumbo(&svio_jumbo, &svio, g_conf.jumbo_mtu) == -1 ) {
538   roar_vio_close(&dvio);
539   roar_vio_close(&svio);
540   return 2;
541  }
542  svio_p = &svio_jumbo;
543 } else {
544  svio_p = &svio;
545 }
546
547 set_meta();
548
549#define _err(x) roar_vio_close(&dvio); roar_vio_close(&svio); return (x)
550
551 if ( g_conf.filter.in.downmix ) {
552  if ( roardsp_filter_new(&filter, &(g_cons.stream), ROARDSP_FILTER_DOWNMIX) == -1 ) {
553   _err(2);
554  }
555
556  if ( roardsp_fchain_add(&(g_filterchains.input), filter) == -1 ) {
557   _err(2);
558  }
559 }
560
561 if ( g_conf.filter.in.lowp_freq > 1 ) {
562  if ( roardsp_filter_new(&filter, &(g_cons.stream), ROARDSP_FILTER_LOWP) == -1 ) {
563   _err(2);
564  }
565
566  if ( roardsp_filter_ctl(filter, ROARDSP_FCTL_FREQ, &(g_conf.filter.in.lowp_freq)) == -1 ) {
567   _err(2);
568  }
569
570  if ( roardsp_fchain_add(&(g_filterchains.input), filter) == -1 ) {
571   _err(2);
572  }
573 }
574
575#undef _err
576
577 if ( g_conf.transcode ) {
578  dinfo.codec = info.codec;
579
580  if ( roar_bixcoder_init(transcoder, &dinfo, svio_p) == -1 ) {
581   roar_vio_close(&svio);
582   roar_vio_close(&dvio);
583   return 10;
584  }
585
586  // ignore errors as it may also work if this fails
587  roar_bixcoder_write_header(transcoder);
588  roar_bixcoder_read_header(transcoder);
589
590  g_conf.samples = 8 * roar_bixcoder_packet_size(transcoder, -1) / dinfo.bits;
591 }
592
593 ROAR_DBG("main(*): CALL run_stream(&dvio, &svio, &info);");
594 run_stream(&dvio, svio_p, &info);
595 ROAR_DBG("main(*): RET");
596
597 roar_bixcoder_close(transcoder);
598
599 roar_vio_close(svio_p);
600 roar_vio_close(&dvio);
601
602 roardsp_fchain_uninit(&(g_filterchains.input));
603 roardsp_fchain_uninit(&(g_filterchains.output));
604
605 roar_disconnect(&(g_cons.con));
606
607 return 0;
608}
609
610//ll
Note: See TracBrowser for help on using the repository browser.