source: roaraudio/roarclients/roarphone.c @ 2322:c1ae140b6ddc

Last change on this file since 2322:c1ae140b6ddc was 2322:c1ae140b6ddc, checked in by phi, 15 years ago

delay cuting data off

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