source: roaraudio/roarclients/roarphone.c @ 2141:ae202c734bd4

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

added old style speex AEC support

File size: 5.9 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 "driver.h"
27
28#ifdef ROAR_HAVE_LIBSPEEX
29#include <speex/speex_echo.h>
30#endif
31
32#define TIMEDIV  100
33
34#define DRIVER  "oss"
35
36// anti echo:
37#define AE_NONE      0
38#define AE_SIMPLE    1
39#define AE_SPEEX     2
40#define AE_ROARD     3
41
42struct {
43 int antiecho;
44} g_conf;
45
46void usage (void) {
47 printf("roarcat [OPTIONS]...\n");
48
49 printf("\nOptions:\n\n");
50
51 printf("  --server   SERVER    - Set server hostname\n"
52        "  --rate     RATE      - Set sample rate\n"
53        "  --bits     BITS      - Set bits per sample\n"
54        "  --chans    CHANNELS  - Set number of channels\n"
55        "  --codec    CODEC     - Set the codec\n"
56        "  --driver   DRIVER    - Set the driver\n"
57        "  --device   DEVICE    - Set the device\n"
58        "  --antiecho AEMODE    - Set the anti echo mode\n"
59        "  --help               - Show this help\n"
60       );
61
62}
63
64int open_stream (struct roar_vio_calls * vio, char * server, struct roar_audio_info * info) {
65 return roar_vio_simple_stream(vio,
66                               info->rate, info->channels, info->bits, info->codec,
67                               server,
68                               ROAR_DIR_BIDIR,
69                               "roarphone");
70}
71
72#ifdef ROAR_HAVE_LIBSPEEX
73int anti_echo_speex16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) {
74 static SpeexEchoState * state = NULL;
75 size_t samples = info->rate / TIMEDIV;
76 static int16_t * obuf = NULL;
77
78 if ( info->channels != 1 )
79  return -1;
80
81 if (len != samples)
82  return -1;
83
84 if ( state == NULL ) {
85  if ( (state = speex_echo_state_init(samples, 100*samples)) == NULL )
86   return -1;
87
88  // todo: set sample rate.
89 }
90
91 if ( obuf == NULL ) {
92  if ( (obuf = malloc(2*samples)) == NULL )
93   return -1;
94 }
95
96/*
97 speex_echo_cancellation(state, buf, aebuf, obuf);
98*/
99
100 speex_echo_cancel(state, buf, aebuf, obuf, NULL);
101
102 memcpy(buf, obuf, 2*samples);
103
104 return 0;
105}
106#endif
107
108int anti_echo16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) {
109 size_t i;
110
111 switch (g_conf.antiecho) {
112  case AE_NONE:
113    return 0;
114   break;
115  case AE_SIMPLE:
116    for (i = 0; i < len; i++)
117     buf[i] -= aebuf[i];
118   break;
119#ifdef ROAR_HAVE_LIBSPEEX
120  case AE_SPEEX:
121    return anti_echo_speex16(buf, aebuf, len, info);
122   break;
123#endif
124  default:
125    return -1;
126   break;
127 }
128
129 return -1;
130}
131
132int run_stream (struct roar_vio_calls * s0, struct roar_vio_calls * s1, struct roar_audio_info * info) {
133 size_t len;
134 void * outbuf, * micbuf;
135 ssize_t outlen, miclen;
136
137 len = (info->rate / TIMEDIV) * info->channels * info->bits / 8;
138
139 if ( (outbuf = malloc(2*len)) == NULL )
140  return -1;
141
142 micbuf = outbuf + len;
143
144 while (1) {
145  if ( (miclen = roar_vio_read(s0, micbuf, len)) <= 0 )
146   break;
147  if ( roar_vio_write(s1, micbuf, miclen) != miclen )
148   break;
149  if ( (outlen = roar_vio_read(s1, outbuf, len)) <= 0 )
150   break;
151
152  if ( g_conf.antiecho != AE_NONE )
153   anti_echo16(outbuf, micbuf, ROAR_MIN(miclen, outlen)/2, info);
154
155  if ( roar_vio_write(s0, outbuf, outlen) != outlen )
156   break;
157 }
158
159 free(outbuf);
160
161 return 0;
162}
163
164int main (int argc, char * argv[]) {
165 struct roar_audio_info info = {.rate     = ROAR_RATE_DEFAULT,
166                                .bits     = ROAR_BITS_DEFAULT,
167                                .channels = ROAR_CHANNELS_DEFAULT,
168                                .codec    = ROAR_CODEC_DEFAULT
169                               };
170 struct roar_vio_calls dvio, svio;
171 char * driver   = DRIVER;
172 char * device   = NULL;
173 char * server   = NULL;
174 char * k;
175 int    i;
176
177 memset(&g_conf, 0, sizeof(g_conf));
178
179 g_conf.antiecho = AE_NONE;
180
181 for (i = 1; i < argc; i++) {
182  k = argv[i];
183
184  if ( strcmp(k, "--server") == 0 ) {
185   server = argv[++i];
186  } else if ( strcmp(k, "--rate") == 0 ) {
187   info.rate = atoi(argv[++i]);
188  } else if ( strcmp(k, "--bits") == 0 ) {
189   info.bits = atoi(argv[++i]);
190  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) {
191   info.channels = atoi(argv[++i]);
192  } else if ( strcmp(k, "--codec") == 0 ) {
193   info.codec = roar_str2codec(argv[++i]);
194  } else if ( strcmp(k, "--driver") == 0 ) {
195   driver = argv[++i];
196  } else if ( strcmp(k, "--device") == 0 ) {
197   device = argv[++i];
198  } else if ( strcmp(k, "--antiecho") == 0 ) {
199   k = argv[++i];
200   if ( !strcmp(k, "none") ) {
201    g_conf.antiecho = AE_NONE;
202   } else if ( !strcmp(k, "simple") ) {
203    g_conf.antiecho = AE_SIMPLE;
204   } else if ( !strcmp(k, "speex") ) {
205    g_conf.antiecho = AE_SPEEX;
206   } else if ( !strcmp(k, "roard") ) {
207    g_conf.antiecho = AE_ROARD;
208   } else {
209    fprintf(stderr, "Error: unknown mode: %s\n", k);
210    return 1;
211   }
212  } else if ( strcmp(k, "--help") == 0 ) {
213   usage();
214   return 0;
215  } else {
216   fprintf(stderr, "Error: unknown argument: %s\n", k);
217   usage();
218   return 1;
219  }
220 }
221
222 if ( roar_cdriver_open(&dvio, driver, device, &info, ROAR_DIR_BIDIR) == -1 ) {
223  return 1;
224 }
225
226 if ( open_stream(&svio, server, &info) == -1 ) {
227  roar_vio_close(&dvio);
228  return 2;
229 }
230
231 run_stream(&dvio, &svio, &info);
232
233 roar_vio_close(&svio);
234 roar_vio_close(&dvio);
235
236 return 0;
237}
238
239//ll
Note: See TracBrowser for help on using the repository browser.