source: roaraudio/roarclients/roarphone.c @ 2167:a72f8c925e4b

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

start transcoding code

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