[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 | |
---|
[2141] | 29 | #ifdef ROAR_HAVE_LIBSPEEX |
---|
| 30 | #include <speex/speex_echo.h> |
---|
| 31 | #endif |
---|
| 32 | |
---|
| 33 | #define TIMEDIV 100 |
---|
[2124] | 34 | |
---|
[2125] | 35 | #define DRIVER "oss" |
---|
| 36 | |
---|
[2140] | 37 | // anti echo: |
---|
| 38 | #define AE_NONE 0 |
---|
| 39 | #define AE_SIMPLE 1 |
---|
| 40 | #define AE_SPEEX 2 |
---|
[2141] | 41 | #define AE_ROARD 3 |
---|
[2140] | 42 | |
---|
| 43 | struct { |
---|
| 44 | int antiecho; |
---|
[2166] | 45 | int samples; |
---|
[2167] | 46 | int transcode; |
---|
[2140] | 47 | } g_conf; |
---|
| 48 | |
---|
[2185] | 49 | struct roar_bixcoder transcoder[1]; |
---|
| 50 | |
---|
[2124] | 51 | void usage (void) { |
---|
| 52 | printf("roarcat [OPTIONS]...\n"); |
---|
| 53 | |
---|
| 54 | printf("\nOptions:\n\n"); |
---|
| 55 | |
---|
[2141] | 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" |
---|
[2167] | 64 | " --transcode - Use local transcodeing\n" |
---|
[2141] | 65 | " --help - Show this help\n" |
---|
[2124] | 66 | ); |
---|
| 67 | |
---|
| 68 | } |
---|
| 69 | |
---|
[2128] | 70 | int 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 | |
---|
[2141] | 78 | #ifdef ROAR_HAVE_LIBSPEEX |
---|
| 79 | int 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 | |
---|
[2142] | 90 | ROAR_DBG("anti_echo_speex16(*) = ?"); |
---|
| 91 | |
---|
[2141] | 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 | |
---|
[2142] | 99 | ROAR_DBG("anti_echo_speex16(*) = ?"); |
---|
| 100 | |
---|
[2141] | 101 | if ( obuf == NULL ) { |
---|
| 102 | if ( (obuf = malloc(2*samples)) == NULL ) |
---|
| 103 | return -1; |
---|
| 104 | } |
---|
| 105 | |
---|
[2142] | 106 | ROAR_DBG("anti_echo_speex16(*) = ?"); |
---|
| 107 | |
---|
[2141] | 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 | |
---|
[2142] | 116 | ROAR_DBG("anti_echo_speex16(*) = 0"); |
---|
| 117 | |
---|
[2141] | 118 | return 0; |
---|
| 119 | } |
---|
| 120 | #endif |
---|
| 121 | |
---|
| 122 | int anti_echo16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) { |
---|
[2140] | 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; |
---|
[2141] | 133 | #ifdef ROAR_HAVE_LIBSPEEX |
---|
| 134 | case AE_SPEEX: |
---|
| 135 | return anti_echo_speex16(buf, aebuf, len, info); |
---|
| 136 | break; |
---|
| 137 | #endif |
---|
[2140] | 138 | default: |
---|
| 139 | return -1; |
---|
| 140 | break; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | return -1; |
---|
| 144 | } |
---|
| 145 | |
---|
[2128] | 146 | int run_stream (struct roar_vio_calls * s0, struct roar_vio_calls * s1, struct roar_audio_info * info) { |
---|
[2129] | 147 | size_t len; |
---|
[2140] | 148 | void * outbuf, * micbuf; |
---|
| 149 | ssize_t outlen, miclen; |
---|
[2129] | 150 | |
---|
[2166] | 151 | len = g_conf.samples * info->bits / 8; |
---|
[2129] | 152 | |
---|
[2140] | 153 | if ( (outbuf = malloc(2*len)) == NULL ) |
---|
[2129] | 154 | return -1; |
---|
| 155 | |
---|
[2140] | 156 | micbuf = outbuf + len; |
---|
| 157 | |
---|
[2129] | 158 | while (1) { |
---|
[2140] | 159 | if ( (miclen = roar_vio_read(s0, micbuf, len)) <= 0 ) |
---|
[2129] | 160 | break; |
---|
[2185] | 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 | } |
---|
[2140] | 178 | |
---|
[2162] | 179 | if ( g_conf.antiecho != AE_NONE && info->bits == 16 ) |
---|
[2141] | 180 | anti_echo16(outbuf, micbuf, ROAR_MIN(miclen, outlen)/2, info); |
---|
[2140] | 181 | |
---|
| 182 | if ( roar_vio_write(s0, outbuf, outlen) != outlen ) |
---|
[2129] | 183 | break; |
---|
| 184 | } |
---|
| 185 | |
---|
[2140] | 186 | free(outbuf); |
---|
[2129] | 187 | |
---|
| 188 | return 0; |
---|
[2128] | 189 | } |
---|
| 190 | |
---|
[2124] | 191 | int main (int argc, char * argv[]) { |
---|
[2125] | 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 | }; |
---|
[2167] | 197 | struct roar_audio_info dinfo; |
---|
[2128] | 198 | struct roar_vio_calls dvio, svio; |
---|
[2126] | 199 | char * driver = DRIVER; |
---|
| 200 | char * device = NULL; |
---|
[2124] | 201 | char * server = NULL; |
---|
| 202 | char * k; |
---|
| 203 | int i; |
---|
| 204 | |
---|
[2140] | 205 | memset(&g_conf, 0, sizeof(g_conf)); |
---|
| 206 | |
---|
[2141] | 207 | g_conf.antiecho = AE_NONE; |
---|
[2140] | 208 | |
---|
[2124] | 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 ) { |
---|
[2125] | 215 | info.rate = atoi(argv[++i]); |
---|
[2124] | 216 | } else if ( strcmp(k, "--bits") == 0 ) { |
---|
[2125] | 217 | info.bits = atoi(argv[++i]); |
---|
[2124] | 218 | } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
[2125] | 219 | info.channels = atoi(argv[++i]); |
---|
[2124] | 220 | } else if ( strcmp(k, "--codec") == 0 ) { |
---|
[2125] | 221 | info.codec = roar_str2codec(argv[++i]); |
---|
[2127] | 222 | } else if ( strcmp(k, "--driver") == 0 ) { |
---|
| 223 | driver = argv[++i]; |
---|
| 224 | } else if ( strcmp(k, "--device") == 0 ) { |
---|
| 225 | device = argv[++i]; |
---|
[2141] | 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 | } |
---|
[2167] | 240 | } else if ( strcmp(k, "--transcode") == 0 ) { |
---|
| 241 | g_conf.transcode = 1; |
---|
[2124] | 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 | |
---|
[2166] | 252 | g_conf.samples = info.channels * info.rate / TIMEDIV; |
---|
| 253 | |
---|
[2167] | 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 ) { |
---|
[2126] | 275 | return 1; |
---|
| 276 | } |
---|
| 277 | |
---|
[2128] | 278 | if ( open_stream(&svio, server, &info) == -1 ) { |
---|
| 279 | roar_vio_close(&dvio); |
---|
| 280 | return 2; |
---|
| 281 | } |
---|
| 282 | |
---|
[2185] | 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 | |
---|
[2196] | 292 | g_conf.samples = 8 * roar_bixcoder_packet_size(transcoder, -1) / dinfo.bits; |
---|
[2185] | 293 | } |
---|
| 294 | |
---|
[2140] | 295 | run_stream(&dvio, &svio, &info); |
---|
[2128] | 296 | |
---|
[2185] | 297 | roar_bixcoder_close(transcoder); |
---|
| 298 | |
---|
[2128] | 299 | roar_vio_close(&svio); |
---|
[2126] | 300 | roar_vio_close(&dvio); |
---|
| 301 | |
---|
[2124] | 302 | return 0; |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | //ll |
---|