source: roaraudio/roarclients/roarfilt.c @ 5176:0066db242e12

Last change on this file since 5176:0066db242e12 was 5176:0066db242e12, checked in by phi, 13 years ago

support 32 bit mode, corrected 8 bit mode

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