[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> |
---|
[2126] | 26 | #include "driver.h" |
---|
[2124] | 27 | |
---|
[2141] | 28 | #ifdef ROAR_HAVE_LIBSPEEX |
---|
| 29 | #include <speex/speex_echo.h> |
---|
| 30 | #endif |
---|
| 31 | |
---|
| 32 | #define TIMEDIV 100 |
---|
[2124] | 33 | |
---|
[2125] | 34 | #define DRIVER "oss" |
---|
| 35 | |
---|
[2140] | 36 | // anti echo: |
---|
| 37 | #define AE_NONE 0 |
---|
| 38 | #define AE_SIMPLE 1 |
---|
| 39 | #define AE_SPEEX 2 |
---|
[2141] | 40 | #define AE_ROARD 3 |
---|
[2140] | 41 | |
---|
| 42 | struct { |
---|
| 43 | int antiecho; |
---|
[2166] | 44 | int samples; |
---|
[2167] | 45 | int transcode; |
---|
[2140] | 46 | } g_conf; |
---|
| 47 | |
---|
[2124] | 48 | void usage (void) { |
---|
| 49 | printf("roarcat [OPTIONS]...\n"); |
---|
| 50 | |
---|
| 51 | printf("\nOptions:\n\n"); |
---|
| 52 | |
---|
[2141] | 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" |
---|
[2167] | 61 | " --transcode - Use local transcodeing\n" |
---|
[2141] | 62 | " --help - Show this help\n" |
---|
[2124] | 63 | ); |
---|
| 64 | |
---|
| 65 | } |
---|
| 66 | |
---|
[2128] | 67 | int 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 | |
---|
[2141] | 75 | #ifdef ROAR_HAVE_LIBSPEEX |
---|
| 76 | int 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 | |
---|
[2142] | 87 | ROAR_DBG("anti_echo_speex16(*) = ?"); |
---|
| 88 | |
---|
[2141] | 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 | |
---|
[2142] | 96 | ROAR_DBG("anti_echo_speex16(*) = ?"); |
---|
| 97 | |
---|
[2141] | 98 | if ( obuf == NULL ) { |
---|
| 99 | if ( (obuf = malloc(2*samples)) == NULL ) |
---|
| 100 | return -1; |
---|
| 101 | } |
---|
| 102 | |
---|
[2142] | 103 | ROAR_DBG("anti_echo_speex16(*) = ?"); |
---|
| 104 | |
---|
[2141] | 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 | |
---|
[2142] | 113 | ROAR_DBG("anti_echo_speex16(*) = 0"); |
---|
| 114 | |
---|
[2141] | 115 | return 0; |
---|
| 116 | } |
---|
| 117 | #endif |
---|
| 118 | |
---|
| 119 | int anti_echo16(int16_t * buf, int16_t * aebuf, size_t len, struct roar_audio_info * info) { |
---|
[2140] | 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; |
---|
[2141] | 130 | #ifdef ROAR_HAVE_LIBSPEEX |
---|
| 131 | case AE_SPEEX: |
---|
| 132 | return anti_echo_speex16(buf, aebuf, len, info); |
---|
| 133 | break; |
---|
| 134 | #endif |
---|
[2140] | 135 | default: |
---|
| 136 | return -1; |
---|
| 137 | break; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | return -1; |
---|
| 141 | } |
---|
| 142 | |
---|
[2128] | 143 | int run_stream (struct roar_vio_calls * s0, struct roar_vio_calls * s1, struct roar_audio_info * info) { |
---|
[2129] | 144 | size_t len; |
---|
[2140] | 145 | void * outbuf, * micbuf; |
---|
| 146 | ssize_t outlen, miclen; |
---|
[2129] | 147 | |
---|
[2166] | 148 | len = g_conf.samples * info->bits / 8; |
---|
[2129] | 149 | |
---|
[2140] | 150 | if ( (outbuf = malloc(2*len)) == NULL ) |
---|
[2129] | 151 | return -1; |
---|
| 152 | |
---|
[2140] | 153 | micbuf = outbuf + len; |
---|
| 154 | |
---|
[2129] | 155 | while (1) { |
---|
[2140] | 156 | if ( (miclen = roar_vio_read(s0, micbuf, len)) <= 0 ) |
---|
[2129] | 157 | break; |
---|
[2140] | 158 | if ( roar_vio_write(s1, micbuf, miclen) != miclen ) |
---|
| 159 | break; |
---|
| 160 | if ( (outlen = roar_vio_read(s1, outbuf, len)) <= 0 ) |
---|
[2129] | 161 | break; |
---|
[2140] | 162 | |
---|
[2162] | 163 | if ( g_conf.antiecho != AE_NONE && info->bits == 16 ) |
---|
[2141] | 164 | anti_echo16(outbuf, micbuf, ROAR_MIN(miclen, outlen)/2, info); |
---|
[2140] | 165 | |
---|
| 166 | if ( roar_vio_write(s0, outbuf, outlen) != outlen ) |
---|
[2129] | 167 | break; |
---|
| 168 | } |
---|
| 169 | |
---|
[2140] | 170 | free(outbuf); |
---|
[2129] | 171 | |
---|
| 172 | return 0; |
---|
[2128] | 173 | } |
---|
| 174 | |
---|
[2124] | 175 | int main (int argc, char * argv[]) { |
---|
[2125] | 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 | }; |
---|
[2167] | 181 | struct roar_audio_info dinfo; |
---|
[2128] | 182 | struct roar_vio_calls dvio, svio; |
---|
[2126] | 183 | char * driver = DRIVER; |
---|
| 184 | char * device = NULL; |
---|
[2124] | 185 | char * server = NULL; |
---|
| 186 | char * k; |
---|
| 187 | int i; |
---|
| 188 | |
---|
[2140] | 189 | memset(&g_conf, 0, sizeof(g_conf)); |
---|
| 190 | |
---|
[2141] | 191 | g_conf.antiecho = AE_NONE; |
---|
[2140] | 192 | |
---|
[2124] | 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 ) { |
---|
[2125] | 199 | info.rate = atoi(argv[++i]); |
---|
[2124] | 200 | } else if ( strcmp(k, "--bits") == 0 ) { |
---|
[2125] | 201 | info.bits = atoi(argv[++i]); |
---|
[2124] | 202 | } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
[2125] | 203 | info.channels = atoi(argv[++i]); |
---|
[2124] | 204 | } else if ( strcmp(k, "--codec") == 0 ) { |
---|
[2125] | 205 | info.codec = roar_str2codec(argv[++i]); |
---|
[2127] | 206 | } else if ( strcmp(k, "--driver") == 0 ) { |
---|
| 207 | driver = argv[++i]; |
---|
| 208 | } else if ( strcmp(k, "--device") == 0 ) { |
---|
| 209 | device = argv[++i]; |
---|
[2141] | 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 | } |
---|
[2167] | 224 | } else if ( strcmp(k, "--transcode") == 0 ) { |
---|
| 225 | g_conf.transcode = 1; |
---|
[2124] | 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 | |
---|
[2166] | 236 | g_conf.samples = info.channels * info.rate / TIMEDIV; |
---|
| 237 | |
---|
[2167] | 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 ) { |
---|
[2126] | 259 | return 1; |
---|
| 260 | } |
---|
| 261 | |
---|
[2128] | 262 | if ( open_stream(&svio, server, &info) == -1 ) { |
---|
| 263 | roar_vio_close(&dvio); |
---|
| 264 | return 2; |
---|
| 265 | } |
---|
| 266 | |
---|
[2140] | 267 | run_stream(&dvio, &svio, &info); |
---|
[2128] | 268 | |
---|
| 269 | roar_vio_close(&svio); |
---|
[2126] | 270 | roar_vio_close(&dvio); |
---|
| 271 | |
---|
[2124] | 272 | return 0; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | //ll |
---|