source: roaraudio/roarclients/roarphone.c @ 2215:956f710f3279

Last change on this file since 2215:956f710f3279 was 2215:956f710f3279, checked in by phi, 15 years ago

test for speex api version

File size: 7.4 KB
Line 
1//roarbidir.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008
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 "driver.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("roarcat [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   break;
139#ifdef _SPEEX_API_OLD
140  case AE_SPEEX:
141    return anti_echo_speex16(buf, aebuf, len, info);
142   break;
143#endif
144  default:
145    return -1;
146   break;
147 }
148
149 return -1;
150}
151
152int run_stream (struct roar_vio_calls * s0, struct roar_vio_calls * s1, struct roar_audio_info * info) {
153 size_t len;
154 void * outbuf, * micbuf;
155 ssize_t outlen, miclen;
156
157 len = g_conf.samples * info->bits / 8;
158
159 if ( (outbuf = malloc(2*len)) == NULL )
160  return -1;
161
162 micbuf = outbuf + len;
163
164 while (1) {
165  if ( (miclen = roar_vio_read(s0, micbuf, len)) <= 0 )
166   break;
167  if ( g_conf.transcode ) {
168   if ( roar_bixcoder_write_packet(transcoder, micbuf, miclen) == -1 )
169    break;
170  } else {
171   if ( roar_vio_write(s1, micbuf, miclen) != miclen )
172    break;
173  }
174
175  if ( g_conf.transcode ) {
176   if ( roar_bixcoder_read_packet(transcoder, outbuf, len) == -1 )
177    break;
178
179   outlen = len;
180  } else {
181   if ( (outlen = roar_vio_read(s1, outbuf, len)) <= 0 )
182    break;
183  }
184
185  if ( g_conf.antiecho != AE_NONE && info->bits == 16 )
186   anti_echo16(outbuf, micbuf, ROAR_MIN(miclen, outlen)/2, info);
187
188  if ( roar_vio_write(s0, outbuf, outlen) != outlen )
189   break;
190 }
191
192 free(outbuf);
193
194 return 0;
195}
196
197int main (int argc, char * argv[]) {
198 struct roar_audio_info info = {.rate     = ROAR_RATE_DEFAULT,
199                                .bits     = ROAR_BITS_DEFAULT,
200                                .channels = ROAR_CHANNELS_DEFAULT,
201                                .codec    = ROAR_CODEC_DEFAULT
202                               };
203 struct roar_audio_info dinfo;
204 struct roar_vio_calls dvio, svio;
205 char * driver   = DRIVER;
206 char * device   = NULL;
207 char * server   = NULL;
208 char * k;
209 int    i;
210
211 memset(&g_conf, 0, sizeof(g_conf));
212
213 g_conf.antiecho = AE_NONE;
214
215 for (i = 1; i < argc; i++) {
216  k = argv[i];
217
218  if ( strcmp(k, "--server") == 0 ) {
219   server = argv[++i];
220  } else if ( strcmp(k, "--rate") == 0 ) {
221   info.rate = atoi(argv[++i]);
222  } else if ( strcmp(k, "--bits") == 0 ) {
223   info.bits = atoi(argv[++i]);
224  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) {
225   info.channels = atoi(argv[++i]);
226  } else if ( strcmp(k, "--codec") == 0 ) {
227   info.codec = roar_str2codec(argv[++i]);
228  } else if ( strcmp(k, "--driver") == 0 ) {
229   driver = argv[++i];
230  } else if ( strcmp(k, "--device") == 0 ) {
231   device = argv[++i];
232  } else if ( strcmp(k, "--antiecho") == 0 ) {
233   k = argv[++i];
234   if ( !strcmp(k, "none") ) {
235    g_conf.antiecho = AE_NONE;
236   } else if ( !strcmp(k, "simple") ) {
237    g_conf.antiecho = AE_SIMPLE;
238   } else if ( !strcmp(k, "speex") ) {
239    g_conf.antiecho = AE_SPEEX;
240   } else if ( !strcmp(k, "roard") ) {
241    g_conf.antiecho = AE_ROARD;
242   } else {
243    fprintf(stderr, "Error: unknown mode: %s\n", k);
244    return 1;
245   }
246  } else if ( strcmp(k, "--transcode") == 0 ) {
247   g_conf.transcode = 1;
248  } else if ( strcmp(k, "--help") == 0 ) {
249   usage();
250   return 0;
251  } else {
252   fprintf(stderr, "Error: unknown argument: %s\n", k);
253   usage();
254   return 1;
255  }
256 }
257
258 g_conf.samples = info.channels * info.rate / TIMEDIV;
259
260 memcpy(&dinfo, &info, sizeof(dinfo));
261
262 if ( g_conf.transcode ) {
263  dinfo.bits  = 16;
264  dinfo.codec = ROAR_CODEC_DEFAULT;
265
266  switch (info.codec) {
267   case ROAR_CODEC_ALAW:
268   case ROAR_CODEC_MULAW:
269     info.bits = 8;
270    break;
271   case ROAR_CODEC_ROAR_CELT:
272     info.bits = 16;
273    break;
274   case ROAR_CODEC_ROAR_SPEEX:
275     info.bits = 16;
276    break;
277  }
278 }
279
280 if ( roar_cdriver_open(&dvio, driver, device, &dinfo, ROAR_DIR_BIDIR) == -1 ) {
281  return 1;
282 }
283
284 if ( open_stream(&svio, server, &info) == -1 ) {
285  roar_vio_close(&dvio);
286  return 2;
287 }
288
289 if ( g_conf.transcode ) {
290  dinfo.codec = info.codec;
291
292  if ( roar_bixcoder_init(transcoder, &dinfo, &svio) == -1 ) {
293   roar_vio_close(&svio);
294   roar_vio_close(&dvio);
295   return 10;
296  }
297
298  g_conf.samples = 8 * roar_bixcoder_packet_size(transcoder, -1) / dinfo.bits;
299 }
300
301 run_stream(&dvio, &svio, &info);
302
303 roar_bixcoder_close(transcoder);
304
305 roar_vio_close(&svio);
306 roar_vio_close(&dvio);
307
308 return 0;
309}
310
311//ll
Note: See TracBrowser for help on using the repository browser.