source: roaraudio/roarclients/roarphone.c @ 2320:990b50b4d2c0

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

added a lot debug lions

File size: 8.0 KB
RevLine 
[2292]1//roarphone.c:
[2124]2
3/*
[2282]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
[2124]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>
[2185]26#include <libroardsp/libroardsp.h>
[2282]27#include <libroareio/libroareio.h>
[2124]28
[2215]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
[2141]36#include <speex/speex_echo.h>
37#endif
38
39#define TIMEDIV  100
[2124]40
[2125]41#define DRIVER  "oss"
42
[2140]43// anti echo:
44#define AE_NONE      0
45#define AE_SIMPLE    1
46#define AE_SPEEX     2
[2141]47#define AE_ROARD     3
[2140]48
49struct {
50 int antiecho;
[2166]51 int samples;
[2167]52 int transcode;
[2140]53} g_conf;
54
[2185]55struct roar_bixcoder transcoder[1];
56
[2124]57void usage (void) {
[2292]58 printf("roarphone [OPTIONS]...\n");
[2124]59
60 printf("\nOptions:\n\n");
61
[2141]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"
[2167]70        "  --transcode          - Use local transcodeing\n"
[2141]71        "  --help               - Show this help\n"
[2124]72       );
73
74}
75
[2128]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
[2215]84#ifdef _SPEEX_API_OLD
[2141]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
[2142]96 ROAR_DBG("anti_echo_speex16(*) = ?");
97
[2141]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
[2142]105 ROAR_DBG("anti_echo_speex16(*) = ?");
106
[2141]107 if ( obuf == NULL ) {
108  if ( (obuf = malloc(2*samples)) == NULL )
109   return -1;
110 }
111
[2142]112 ROAR_DBG("anti_echo_speex16(*) = ?");
113
[2141]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
[2142]122 ROAR_DBG("anti_echo_speex16(*) = 0");
123
[2141]124 return 0;
125}
126#endif
127
128int anti_echo16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) {
[2140]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];
[2293]138    return 0;
[2140]139   break;
[2215]140#ifdef _SPEEX_API_OLD
[2141]141  case AE_SPEEX:
142    return anti_echo_speex16(buf, aebuf, len, info);
143   break;
144#endif
[2293]145  case AE_ROARD:
146    return 0;
147   break;
[2140]148  default:
149    return -1;
150   break;
151 }
152
153 return -1;
154}
155
[2128]156int run_stream (struct roar_vio_calls * s0, struct roar_vio_calls * s1, struct roar_audio_info * info) {
[2129]157 size_t len;
[2140]158 void * outbuf, * micbuf;
159 ssize_t outlen, miclen;
[2129]160
[2312]161 ROAR_DBG("run_stream(*): g_conf.samples = %i, info->bits = %i", g_conf.samples, info->bits);
[2166]162 len = g_conf.samples * info->bits / 8;
[2312]163 ROAR_DBG("run_stream(*): len=%lu", (unsigned long) len);
[2129]164
[2140]165 if ( (outbuf = malloc(2*len)) == NULL )
[2129]166  return -1;
167
[2140]168 micbuf = outbuf + len;
169
[2129]170 while (1) {
[2140]171  if ( (miclen = roar_vio_read(s0, micbuf, len)) <= 0 )
[2129]172   break;
[2185]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 ) {
[2312]182   ROAR_DBG("run_stream(*): outbuf=%p, len=%lu", outbuf, (unsigned long) len);
[2185]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  }
[2140]191
[2162]192  if ( g_conf.antiecho != AE_NONE && info->bits == 16 )
[2141]193   anti_echo16(outbuf, micbuf, ROAR_MIN(miclen, outlen)/2, info);
[2140]194
195  if ( roar_vio_write(s0, outbuf, outlen) != outlen )
[2129]196   break;
197 }
198
[2140]199 free(outbuf);
[2129]200
201 return 0;
[2128]202}
203
[2124]204int main (int argc, char * argv[]) {
[2125]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                               };
[2167]210 struct roar_audio_info dinfo;
[2128]211 struct roar_vio_calls dvio, svio;
[2126]212 char * driver   = DRIVER;
213 char * device   = NULL;
[2124]214 char * server   = NULL;
215 char * k;
216 int    i;
217
[2140]218 memset(&g_conf, 0, sizeof(g_conf));
219
[2293]220 g_conf.antiecho = AE_ROARD;
[2140]221
[2124]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 ) {
[2125]228   info.rate = atoi(argv[++i]);
[2124]229  } else if ( strcmp(k, "--bits") == 0 ) {
[2125]230   info.bits = atoi(argv[++i]);
[2124]231  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) {
[2125]232   info.channels = atoi(argv[++i]);
[2124]233  } else if ( strcmp(k, "--codec") == 0 ) {
[2125]234   info.codec = roar_str2codec(argv[++i]);
[2127]235  } else if ( strcmp(k, "--driver") == 0 ) {
236   driver = argv[++i];
237  } else if ( strcmp(k, "--device") == 0 ) {
238   device = argv[++i];
[2141]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   }
[2167]253  } else if ( strcmp(k, "--transcode") == 0 ) {
254   g_conf.transcode = 1;
[2124]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
[2293]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
[2166]269 g_conf.samples = info.channels * info.rate / TIMEDIV;
270
[2167]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 ) {
[2126]292  return 1;
293 }
294
[2312]295 ROAR_DBG("main(*): CALL open_stream(&svio, server, &info)");
[2128]296 if ( open_stream(&svio, server, &info) == -1 ) {
297  roar_vio_close(&dvio);
298  return 2;
299 }
[2312]300 ROAR_DBG("main(*): RET");
[2128]301
[2185]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
[2196]311  g_conf.samples = 8 * roar_bixcoder_packet_size(transcoder, -1) / dinfo.bits;
[2185]312 }
313
[2312]314 ROAR_DBG("main(*): CALL run_stream(&dvio, &svio, &info);");
[2140]315 run_stream(&dvio, &svio, &info);
[2312]316 ROAR_DBG("main(*): RET");
[2128]317
[2185]318 roar_bixcoder_close(transcoder);
319
[2128]320 roar_vio_close(&svio);
[2126]321 roar_vio_close(&dvio);
322
[2124]323 return 0;
324}
325
326//ll
Note: See TracBrowser for help on using the repository browser.