source: roaraudio/roarclients/roarphone.c @ 2142:95f833ec51ea

Last change on this file since 2142:95f833ec51ea was 2142:95f833ec51ea, checked in by phi, 15 years ago

added debug lions

File size: 6.0 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 ROAR_DBG("anti_echo_speex16(*) = ?");
85
86 if ( state == NULL ) {
87  if ( (state = speex_echo_state_init(samples, 100*samples)) == NULL )
88   return -1;
89
90  // todo: set sample rate.
91 }
92
93 ROAR_DBG("anti_echo_speex16(*) = ?");
94
95 if ( obuf == NULL ) {
96  if ( (obuf = malloc(2*samples)) == NULL )
97   return -1;
98 }
99
100 ROAR_DBG("anti_echo_speex16(*) = ?");
101
102/*
103 speex_echo_cancellation(state, buf, aebuf, obuf);
104*/
105
106 speex_echo_cancel(state, buf, aebuf, obuf, NULL);
107
108 memcpy(buf, obuf, 2*samples);
109
110 ROAR_DBG("anti_echo_speex16(*) = 0");
111
112 return 0;
113}
114#endif
115
116int anti_echo16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) {
117 size_t i;
118
119 switch (g_conf.antiecho) {
120  case AE_NONE:
121    return 0;
122   break;
123  case AE_SIMPLE:
124    for (i = 0; i < len; i++)
125     buf[i] -= aebuf[i];
126   break;
127#ifdef ROAR_HAVE_LIBSPEEX
128  case AE_SPEEX:
129    return anti_echo_speex16(buf, aebuf, len, info);
130   break;
131#endif
132  default:
133    return -1;
134   break;
135 }
136
137 return -1;
138}
139
140int run_stream (struct roar_vio_calls * s0, struct roar_vio_calls * s1, struct roar_audio_info * info) {
141 size_t len;
142 void * outbuf, * micbuf;
143 ssize_t outlen, miclen;
144
145 len = (info->rate / TIMEDIV) * info->channels * info->bits / 8;
146
147 if ( (outbuf = malloc(2*len)) == NULL )
148  return -1;
149
150 micbuf = outbuf + len;
151
152 while (1) {
153  if ( (miclen = roar_vio_read(s0, micbuf, len)) <= 0 )
154   break;
155  if ( roar_vio_write(s1, micbuf, miclen) != miclen )
156   break;
157  if ( (outlen = roar_vio_read(s1, outbuf, len)) <= 0 )
158   break;
159
160  if ( g_conf.antiecho != AE_NONE )
161   anti_echo16(outbuf, micbuf, ROAR_MIN(miclen, outlen)/2, info);
162
163  if ( roar_vio_write(s0, outbuf, outlen) != outlen )
164   break;
165 }
166
167 free(outbuf);
168
169 return 0;
170}
171
172int main (int argc, char * argv[]) {
173 struct roar_audio_info info = {.rate     = ROAR_RATE_DEFAULT,
174                                .bits     = ROAR_BITS_DEFAULT,
175                                .channels = ROAR_CHANNELS_DEFAULT,
176                                .codec    = ROAR_CODEC_DEFAULT
177                               };
178 struct roar_vio_calls dvio, svio;
179 char * driver   = DRIVER;
180 char * device   = NULL;
181 char * server   = NULL;
182 char * k;
183 int    i;
184
185 memset(&g_conf, 0, sizeof(g_conf));
186
187 g_conf.antiecho = AE_NONE;
188
189 for (i = 1; i < argc; i++) {
190  k = argv[i];
191
192  if ( strcmp(k, "--server") == 0 ) {
193   server = argv[++i];
194  } else if ( strcmp(k, "--rate") == 0 ) {
195   info.rate = atoi(argv[++i]);
196  } else if ( strcmp(k, "--bits") == 0 ) {
197   info.bits = atoi(argv[++i]);
198  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) {
199   info.channels = atoi(argv[++i]);
200  } else if ( strcmp(k, "--codec") == 0 ) {
201   info.codec = roar_str2codec(argv[++i]);
202  } else if ( strcmp(k, "--driver") == 0 ) {
203   driver = argv[++i];
204  } else if ( strcmp(k, "--device") == 0 ) {
205   device = argv[++i];
206  } else if ( strcmp(k, "--antiecho") == 0 ) {
207   k = argv[++i];
208   if ( !strcmp(k, "none") ) {
209    g_conf.antiecho = AE_NONE;
210   } else if ( !strcmp(k, "simple") ) {
211    g_conf.antiecho = AE_SIMPLE;
212   } else if ( !strcmp(k, "speex") ) {
213    g_conf.antiecho = AE_SPEEX;
214   } else if ( !strcmp(k, "roard") ) {
215    g_conf.antiecho = AE_ROARD;
216   } else {
217    fprintf(stderr, "Error: unknown mode: %s\n", k);
218    return 1;
219   }
220  } else if ( strcmp(k, "--help") == 0 ) {
221   usage();
222   return 0;
223  } else {
224   fprintf(stderr, "Error: unknown argument: %s\n", k);
225   usage();
226   return 1;
227  }
228 }
229
230 if ( roar_cdriver_open(&dvio, driver, device, &info, ROAR_DIR_BIDIR) == -1 ) {
231  return 1;
232 }
233
234 if ( open_stream(&svio, server, &info) == -1 ) {
235  roar_vio_close(&dvio);
236  return 2;
237 }
238
239 run_stream(&dvio, &svio, &info);
240
241 roar_vio_close(&svio);
242 roar_vio_close(&dvio);
243
244 return 0;
245}
246
247//ll
Note: See TracBrowser for help on using the repository browser.