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