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