source: roaraudio/roarclients/roarphone.c @ 2321:9df21a38f3af

Last change on this file since 2321:9df21a38f3af was 2321:9df21a38f3af, checked in by phi, 15 years ago

added basic threshold based /DTX/

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