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 | #define BUFSIZE 1024 |
---|
29 | |
---|
30 | #define DRIVER "oss" |
---|
31 | |
---|
32 | // anti echo: |
---|
33 | #define AE_NONE 0 |
---|
34 | #define AE_SIMPLE 1 |
---|
35 | #define AE_SPEEX 2 |
---|
36 | |
---|
37 | struct { |
---|
38 | int antiecho; |
---|
39 | } g_conf; |
---|
40 | |
---|
41 | void usage (void) { |
---|
42 | printf("roarcat [OPTIONS]...\n"); |
---|
43 | |
---|
44 | printf("\nOptions:\n\n"); |
---|
45 | |
---|
46 | printf(" --server SERVER - Set server hostname\n" |
---|
47 | " --rate RATE - Set sample rate\n" |
---|
48 | " --bits BITS - Set bits per sample\n" |
---|
49 | " --chans CHANNELS - Set number of channels\n" |
---|
50 | " --codec CODEC - Set the codec\n" |
---|
51 | " --driver DRIVER - Set the driver\n" |
---|
52 | " --device DEVICE - Set the device\n" |
---|
53 | " --help - Show this help\n" |
---|
54 | ); |
---|
55 | |
---|
56 | } |
---|
57 | |
---|
58 | int open_stream (struct roar_vio_calls * vio, char * server, struct roar_audio_info * info) { |
---|
59 | return roar_vio_simple_stream(vio, |
---|
60 | info->rate, info->channels, info->bits, info->codec, |
---|
61 | server, |
---|
62 | ROAR_DIR_BIDIR, |
---|
63 | "roarphone"); |
---|
64 | } |
---|
65 | |
---|
66 | int anti_echo16(int16_t * buf, int16_t * aebuf, size_t len) { |
---|
67 | size_t i; |
---|
68 | |
---|
69 | switch (g_conf.antiecho) { |
---|
70 | case AE_NONE: |
---|
71 | return 0; |
---|
72 | break; |
---|
73 | case AE_SIMPLE: |
---|
74 | for (i = 0; i < len; i++) |
---|
75 | buf[i] -= aebuf[i]; |
---|
76 | break; |
---|
77 | default: |
---|
78 | return -1; |
---|
79 | break; |
---|
80 | } |
---|
81 | |
---|
82 | return -1; |
---|
83 | } |
---|
84 | |
---|
85 | int run_stream (struct roar_vio_calls * s0, struct roar_vio_calls * s1, struct roar_audio_info * info) { |
---|
86 | size_t len; |
---|
87 | void * outbuf, * micbuf; |
---|
88 | ssize_t outlen, miclen; |
---|
89 | |
---|
90 | len = (info->rate / 100) * info->channels * info->bits / 8; |
---|
91 | |
---|
92 | if ( (outbuf = malloc(2*len)) == NULL ) |
---|
93 | return -1; |
---|
94 | |
---|
95 | micbuf = outbuf + len; |
---|
96 | |
---|
97 | while (1) { |
---|
98 | if ( (miclen = roar_vio_read(s0, micbuf, len)) <= 0 ) |
---|
99 | break; |
---|
100 | if ( roar_vio_write(s1, micbuf, miclen) != miclen ) |
---|
101 | break; |
---|
102 | if ( (outlen = roar_vio_read(s1, outbuf, len)) <= 0 ) |
---|
103 | break; |
---|
104 | |
---|
105 | if ( g_conf.antiecho != AE_NONE ) |
---|
106 | anti_echo16(outbuf, micbuf, ROAR_MIN(miclen, outlen)/2); |
---|
107 | |
---|
108 | if ( roar_vio_write(s0, outbuf, outlen) != outlen ) |
---|
109 | break; |
---|
110 | } |
---|
111 | |
---|
112 | free(outbuf); |
---|
113 | |
---|
114 | return 0; |
---|
115 | } |
---|
116 | |
---|
117 | int main (int argc, char * argv[]) { |
---|
118 | struct roar_audio_info info = {.rate = ROAR_RATE_DEFAULT, |
---|
119 | .bits = ROAR_BITS_DEFAULT, |
---|
120 | .channels = ROAR_CHANNELS_DEFAULT, |
---|
121 | .codec = ROAR_CODEC_DEFAULT |
---|
122 | }; |
---|
123 | struct roar_vio_calls dvio, svio; |
---|
124 | char * driver = DRIVER; |
---|
125 | char * device = NULL; |
---|
126 | char * server = NULL; |
---|
127 | char * k; |
---|
128 | int i; |
---|
129 | |
---|
130 | memset(&g_conf, 0, sizeof(g_conf)); |
---|
131 | |
---|
132 | g_conf.antiecho = AE_SIMPLE; |
---|
133 | |
---|
134 | for (i = 1; i < argc; i++) { |
---|
135 | k = argv[i]; |
---|
136 | |
---|
137 | if ( strcmp(k, "--server") == 0 ) { |
---|
138 | server = argv[++i]; |
---|
139 | } else if ( strcmp(k, "--rate") == 0 ) { |
---|
140 | info.rate = atoi(argv[++i]); |
---|
141 | } else if ( strcmp(k, "--bits") == 0 ) { |
---|
142 | info.bits = atoi(argv[++i]); |
---|
143 | } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
144 | info.channels = atoi(argv[++i]); |
---|
145 | } else if ( strcmp(k, "--codec") == 0 ) { |
---|
146 | info.codec = roar_str2codec(argv[++i]); |
---|
147 | } else if ( strcmp(k, "--driver") == 0 ) { |
---|
148 | driver = argv[++i]; |
---|
149 | } else if ( strcmp(k, "--device") == 0 ) { |
---|
150 | device = argv[++i]; |
---|
151 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
152 | usage(); |
---|
153 | return 0; |
---|
154 | } else { |
---|
155 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
156 | usage(); |
---|
157 | return 1; |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | if ( roar_cdriver_open(&dvio, driver, device, &info, ROAR_DIR_BIDIR) == -1 ) { |
---|
162 | return 1; |
---|
163 | } |
---|
164 | |
---|
165 | if ( open_stream(&svio, server, &info) == -1 ) { |
---|
166 | roar_vio_close(&dvio); |
---|
167 | return 2; |
---|
168 | } |
---|
169 | |
---|
170 | run_stream(&dvio, &svio, &info); |
---|
171 | |
---|
172 | roar_vio_close(&svio); |
---|
173 | roar_vio_close(&dvio); |
---|
174 | |
---|
175 | return 0; |
---|
176 | } |
---|
177 | |
---|
178 | //ll |
---|