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