source: roaraudio/roarclients/roarphone.c @ 2185:428bc7864999

Last change on this file since 2185:428bc7864999 was 2185:428bc7864999, checked in by phi, 15 years ago

first try of transcoding code

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