1 | //roarfilt.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 <libroardsp/libroardsp.h> |
---|
27 | |
---|
28 | #ifdef ROAR_HAVE_LIBM |
---|
29 | #include <math.h> |
---|
30 | #endif |
---|
31 | |
---|
32 | #define BUFSIZE 1024 |
---|
33 | #ifdef ROAR_HAVE_LIBM |
---|
34 | struct { |
---|
35 | uint16_t a, b; |
---|
36 | int16_t old[ROAR_MAX_CHANNELS]; |
---|
37 | } g_lowpass; |
---|
38 | #endif |
---|
39 | |
---|
40 | void usage (void) { |
---|
41 | printf("roarfilt [OPTIONS]...\n"); |
---|
42 | |
---|
43 | printf("\nOptions:\n\n"); |
---|
44 | |
---|
45 | printf(" --server SERVER - Set server hostname\n" |
---|
46 | " --rate RATE - Set sample rate\n" |
---|
47 | " --bits BITS - Set bits per sample\n" |
---|
48 | " --chans CHANNELS - Set number of channels\n" |
---|
49 | " --help - Show this help\n" |
---|
50 | "\n" |
---|
51 | " --half - half the volume\n" |
---|
52 | " --double - double the volume\n" |
---|
53 | " --amp VAL - Set amplification\n" |
---|
54 | " --mul VAL - Set mul\n" |
---|
55 | " --div VAL - Set div\n" |
---|
56 | #ifdef ROAR_HAVE_LIBM |
---|
57 | " --lowpass freq - lowpass filter\n" |
---|
58 | #endif |
---|
59 | " --filter name - add filter name\n" |
---|
60 | " --ffreq freq - set filter freq\n" |
---|
61 | " --fmul mult - set filter multiplier\n" |
---|
62 | " --fdiv div - set filter divider\n" |
---|
63 | " --fn N - set filter N parameter\n" |
---|
64 | " --flimit limit - set filter limit parameter\n" |
---|
65 | " --fmode mode - set filter mode parameter\n" |
---|
66 | " --fq Q - set filter quality\n" |
---|
67 | ); |
---|
68 | |
---|
69 | } |
---|
70 | |
---|
71 | void vol2 (void * data, int mul, int div, int len) { |
---|
72 | int16_t * samples = (int16_t *) data; |
---|
73 | int i; |
---|
74 | |
---|
75 | len /= 2; |
---|
76 | |
---|
77 | for (i = 0; i < len; i++) |
---|
78 | samples[i] = ((int) samples[i] * mul) / div; |
---|
79 | } |
---|
80 | |
---|
81 | void vol1 (void * data, int mul, int div, int len) { |
---|
82 | int8_t * samples = (int8_t *) data; |
---|
83 | int i; |
---|
84 | |
---|
85 | for (i = 0; i < len; i++) |
---|
86 | samples[i] = ((int) samples[i] * mul) / div; |
---|
87 | } |
---|
88 | |
---|
89 | #ifdef ROAR_HAVE_LIBM |
---|
90 | void logs2 (void * data, float scale, int len) { |
---|
91 | int16_t * samples = (int16_t *) data; |
---|
92 | int i; |
---|
93 | float div = logf(scale); |
---|
94 | float scalemul = scale - 1; |
---|
95 | int neg; |
---|
96 | |
---|
97 | len /= 2; |
---|
98 | |
---|
99 | //printf("logs2(data=%p, scale=%f, len=%i): scalemul=%f, div=%f\n", data, scale, len, scalemul, div); |
---|
100 | |
---|
101 | for (i = 0; i < len; i++) { |
---|
102 | if ( (neg = (samples[i] < 0)) ) |
---|
103 | samples[i] = abs(samples[i]); |
---|
104 | |
---|
105 | |
---|
106 | samples[i] = (neg ? 32768.0 : 32767.0)*logf(1 + (scalemul*(float)samples[i]/(neg ? 32768.0 : 32767.0))) / div; |
---|
107 | |
---|
108 | if ( neg ) |
---|
109 | samples[i] *= -1; |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | void lowpass2 (void * data, int len, int channels) { |
---|
114 | int16_t * samples = (int16_t *) data; |
---|
115 | register int32_t s; |
---|
116 | int i, c; |
---|
117 | |
---|
118 | if ( channels > ROAR_MAX_CHANNELS ) |
---|
119 | return; |
---|
120 | |
---|
121 | len /= 2 * channels; |
---|
122 | |
---|
123 | // * output[N] = input[N] * A + output[N-1] * B |
---|
124 | |
---|
125 | for (i = 0; i < len; i++) { |
---|
126 | for (c = 0; c < channels; c++) { |
---|
127 | s = samples[i*channels + c] * g_lowpass.a + g_lowpass.old[c] * g_lowpass.b; |
---|
128 | |
---|
129 | s /= 65536; |
---|
130 | |
---|
131 | samples[i*channels + c] = s; |
---|
132 | g_lowpass.old[ c] = s; |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | #endif |
---|
137 | |
---|
138 | int main (int argc, char * argv[]) { |
---|
139 | int rate = 44100; |
---|
140 | int bits = 16; |
---|
141 | int channels = 2; |
---|
142 | int codec = ROAR_CODEC_DEFAULT; |
---|
143 | char * server = NULL; |
---|
144 | char * k; |
---|
145 | int fh; |
---|
146 | int i; |
---|
147 | int mul = 1, div = 1; |
---|
148 | int32_t tmp; |
---|
149 | float logscale = 0; |
---|
150 | float lp = 0; |
---|
151 | char buf[BUFSIZE]; |
---|
152 | struct roardsp_filterchain fc; |
---|
153 | struct roardsp_filter filter_real[8]; |
---|
154 | struct roardsp_filter * filter = filter_real - 1; |
---|
155 | struct roar_stream stream; |
---|
156 | |
---|
157 | #ifdef ROAR_HAVE_LIBM |
---|
158 | memset(&g_lowpass, 0, sizeof(g_lowpass)); |
---|
159 | #endif |
---|
160 | |
---|
161 | roardsp_fchain_init(&fc); |
---|
162 | |
---|
163 | for (i = 1; i < argc; i++) { |
---|
164 | k = argv[i]; |
---|
165 | |
---|
166 | if ( strcmp(k, "--server") == 0 ) { |
---|
167 | server = argv[++i]; |
---|
168 | } else if ( strcmp(k, "--rate") == 0 ) { |
---|
169 | rate = atoi(argv[++i]); |
---|
170 | } else if ( strcmp(k, "--bits") == 0 ) { |
---|
171 | bits = atoi(argv[++i]); |
---|
172 | } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
173 | channels = atoi(argv[++i]); |
---|
174 | } else if ( strcmp(k, "--half") == 0 ) { |
---|
175 | div *= 2; |
---|
176 | } else if ( strcmp(k, "--double") == 0 ) { |
---|
177 | mul *= 2; |
---|
178 | } else if ( strcmp(k, "--amp") == 0 ) { |
---|
179 | mul *= atoi(argv[++i]); |
---|
180 | } else if ( strcmp(k, "--mul") == 0 ) { |
---|
181 | mul = atoi(argv[++i]); |
---|
182 | } else if ( strcmp(k, "--div") == 0 ) { |
---|
183 | div = atoi(argv[++i]); |
---|
184 | } else if ( strcmp(k, "--log") == 0 ) { |
---|
185 | logscale = atof(argv[++i]); |
---|
186 | #ifdef ROAR_HAVE_LIBM |
---|
187 | } else if ( strcmp(k, "--lowpass") == 0 ) { |
---|
188 | lp = exp(-2 * M_PI * atof(argv[++i]) / rate) * 65536; |
---|
189 | g_lowpass.b = lp; |
---|
190 | g_lowpass.a = 65536 - lp; |
---|
191 | #endif |
---|
192 | // printf("lowpass: A=%i, B=%i\n", g_lowpass.a, g_lowpass.b); |
---|
193 | } else if ( strcmp(k, "--filter") == 0 ) { |
---|
194 | stream.info.channels = channels; |
---|
195 | stream.info.bits = bits; |
---|
196 | stream.info.rate = rate; |
---|
197 | filter++; |
---|
198 | roardsp_filter_init(filter, &stream, roardsp_filter_str2id(argv[++i])); |
---|
199 | roardsp_fchain_add(&fc, filter); |
---|
200 | } else if ( strcmp(k, "--ffreq") == 0 ) { |
---|
201 | lp = atof(argv[++i]); |
---|
202 | roardsp_filter_ctl(filter, ROARDSP_FCTL_FREQ, &lp); |
---|
203 | } else if ( strcmp(k, "--fmul") == 0 ) { |
---|
204 | tmp = atoi(argv[++i]); |
---|
205 | roardsp_filter_ctl(filter, ROARDSP_FCTL_MUL, &tmp); |
---|
206 | } else if ( strcmp(k, "--fdiv") == 0 ) { |
---|
207 | tmp = atoi(argv[++i]); |
---|
208 | roardsp_filter_ctl(filter, ROARDSP_FCTL_DIV, &tmp); |
---|
209 | } else if ( strcmp(k, "--fn") == 0 ) { |
---|
210 | tmp = atoi(argv[++i]); |
---|
211 | roardsp_filter_ctl(filter, ROARDSP_FCTL_N, &tmp); |
---|
212 | } else if ( strcmp(k, "--fq") == 0 ) { |
---|
213 | tmp = atoi(argv[++i]); |
---|
214 | roardsp_filter_ctl(filter, ROARDSP_FCTL_Q, &tmp); |
---|
215 | } else if ( strcmp(k, "--flimit") == 0 ) { |
---|
216 | tmp = atoi(argv[++i]); |
---|
217 | roardsp_filter_ctl(filter, ROARDSP_FCTL_LIMIT, &tmp); |
---|
218 | } else if ( strcmp(k, "--fmode") == 0 ) { |
---|
219 | tmp = atoi(argv[++i]); |
---|
220 | roardsp_filter_ctl(filter, ROARDSP_FCTL_MODE, &tmp); |
---|
221 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
222 | usage(); |
---|
223 | return 0; |
---|
224 | } else { |
---|
225 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
226 | usage(); |
---|
227 | return 1; |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | if ( (fh = roar_simple_filter(rate, channels, bits, codec, server, "roarifilt")) == -1 ) { |
---|
232 | fprintf(stderr, "Error: can not start playback\n"); |
---|
233 | return 1; |
---|
234 | } |
---|
235 | |
---|
236 | if ( mul == div && |
---|
237 | #ifdef ROAR_HAVE_LIBM |
---|
238 | logscale == 0 && g_lowpass.a == 0 && |
---|
239 | #endif |
---|
240 | roardsp_fchain_num(&fc) == 0 ) { |
---|
241 | fprintf(stderr, "Error: filter is useless!\n"); |
---|
242 | return 0; |
---|
243 | } |
---|
244 | |
---|
245 | if ( bits == 16 ) { |
---|
246 | while((i = read(fh, buf, BUFSIZE))) { |
---|
247 | if ( mul != div ) |
---|
248 | vol2((void*)buf, mul, div, i); |
---|
249 | #ifdef ROAR_HAVE_LIBM |
---|
250 | if ( logscale ) |
---|
251 | logs2((void*)buf, logscale, i); |
---|
252 | if ( g_lowpass.a ) |
---|
253 | lowpass2((void*)buf, i, channels); |
---|
254 | #endif |
---|
255 | roardsp_fchain_calc(&fc, (void*)buf, (8*i)/bits); |
---|
256 | if (write(fh, buf, i) != i) |
---|
257 | break; |
---|
258 | } |
---|
259 | } else if ( bits == 8 ) { |
---|
260 | while((i = read(fh, buf, BUFSIZE))) { |
---|
261 | vol1((void*)buf, mul, div, i); |
---|
262 | if (write(fh, buf, i) != i) |
---|
263 | break; |
---|
264 | } |
---|
265 | } else { |
---|
266 | fprintf(stderr, "Error: %i bits per sample is not supported!\n", bits); |
---|
267 | return 1; |
---|
268 | } |
---|
269 | |
---|
270 | roar_simple_close(fh); |
---|
271 | |
---|
272 | roardsp_fchain_uninit(&fc); |
---|
273 | |
---|
274 | return 0; |
---|
275 | } |
---|
276 | |
---|
277 | //ll |
---|