source: roaraudio/roarclients/roarfilt.c @ 3812:acdaec327ece

Last change on this file since 3812:acdaec327ece was 3812:acdaec327ece, checked in by phi, 14 years ago

fixed some copyright statements

File size: 7.9 KB
Line 
1//roarfilt.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
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 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 struct roar_vio_calls      svio;
157
158#ifdef ROAR_HAVE_LIBM
159 memset(&g_lowpass, 0, sizeof(g_lowpass));
160#endif
161
162 roardsp_fchain_init(&fc);
163
164 for (i = 1; i < argc; i++) {
165  k = argv[i];
166
167  if ( strcmp(k, "--server") == 0 || strcmp(k, "-s") == 0 ) {
168   server = argv[++i];
169  } else if ( strcmp(k, "--rate") == 0 || strcmp(k, "-R") == 0 || strcmp(k, "-r") == 0 ) {
170   rate = atoi(argv[++i]);
171  } else if ( strcmp(k, "--bits") == 0 || strcmp(k, "-B") == 0 ) {
172   bits = atoi(argv[++i]);
173  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 || strcmp(k, "-C") == 0 ) {
174   channels = atoi(argv[++i]);
175  } else if ( strcmp(k, "-b") == 0 ) {
176   bits = 8;
177  } else if ( strcmp(k, "-m") == 0 ) {
178   channels = 1;
179  } else if ( strcmp(k, "--half") == 0 || strcmp(k, "-half") == 0 ) {
180   div *= 2;
181  } else if ( strcmp(k, "--double") == 0 || strcmp(k, "-double") == 0 ) {
182   mul *= 2;
183  } else if ( strcmp(k, "--amp") == 0 ) {
184   mul *= atoi(argv[++i]);
185  } else if ( strcmp(k, "--mul") == 0 ) {
186   mul  = atoi(argv[++i]);
187  } else if ( strcmp(k, "--div") == 0 ) {
188   div  = atoi(argv[++i]);
189  } else if ( strcmp(k, "--log") == 0 ) {
190   logscale = atof(argv[++i]);
191#ifdef ROAR_HAVE_LIBM
192  } else if ( strcmp(k, "--lowpass") == 0 ) {
193   lp = exp(-2 * M_PI * atof(argv[++i]) / rate) * 65536;
194   g_lowpass.b = lp;
195   g_lowpass.a = 65536 - lp;
196#endif
197//   printf("lowpass: A=%i, B=%i\n", g_lowpass.a, g_lowpass.b);
198  } else if ( strcmp(k, "--filter") == 0 ) {
199   stream.info.channels = channels;
200   stream.info.bits     = bits;
201   stream.info.rate     = rate;
202   filter++;
203   roardsp_filter_init(filter, &stream, roardsp_filter_str2id(argv[++i]));
204   roardsp_fchain_add(&fc, filter);
205  } else if ( strcmp(k, "--ffreq") == 0 ) {
206   lp = atof(argv[++i]);
207   roardsp_filter_ctl(filter, ROARDSP_FCTL_FREQ, &lp);
208  } else if ( strcmp(k, "--fmul") == 0 ) {
209   tmp = atoi(argv[++i]);
210   roardsp_filter_ctl(filter, ROARDSP_FCTL_MUL, &tmp);
211  } else if ( strcmp(k, "--fdiv") == 0 ) {
212   tmp = atoi(argv[++i]);
213   roardsp_filter_ctl(filter, ROARDSP_FCTL_DIV, &tmp);
214  } else if ( strcmp(k, "--fn") == 0 ) {
215   tmp = atoi(argv[++i]);
216   roardsp_filter_ctl(filter, ROARDSP_FCTL_N, &tmp);
217  } else if ( strcmp(k, "--fq") == 0 ) {
218   tmp = atoi(argv[++i]);
219   roardsp_filter_ctl(filter, ROARDSP_FCTL_Q, &tmp);
220  } else if ( strcmp(k, "--flimit") == 0 ) {
221   tmp = atoi(argv[++i]);
222   roardsp_filter_ctl(filter, ROARDSP_FCTL_LIMIT, &tmp);
223  } else if ( strcmp(k, "--fmode") == 0 ) {
224   tmp = atoi(argv[++i]);
225   roardsp_filter_ctl(filter, ROARDSP_FCTL_MODE, &tmp);
226  } else if ( strcmp(k, "--help") == 0 || strcmp(k, "-h") == 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
236 if ( roar_vio_simple_stream(&svio, rate, channels, bits, codec, server, ROAR_DIR_FILTER, "roarfilt") == -1 ) {
237  fprintf(stderr, "Error: can not start playback\n");
238  return 1;
239 }
240
241 if ( mul == div &&
242#ifdef ROAR_HAVE_LIBM
243      logscale == 0 && g_lowpass.a == 0 &&
244#endif
245      roardsp_fchain_num(&fc) == 0 ) {
246  fprintf(stderr, "Error: filter is useless!\n");
247  return 0;
248 }
249
250 switch (bits) {
251  case 16:
252    while((i = roar_vio_read(&svio, buf, BUFSIZE))) {
253     if ( mul != div )
254      vol2((void*)buf, mul, div, i);
255#ifdef ROAR_HAVE_LIBM
256     if ( logscale )
257      logs2((void*)buf, logscale, i);
258     if ( g_lowpass.a )
259      lowpass2((void*)buf, i, channels);
260#endif
261     roardsp_fchain_calc(&fc, (void*)buf, (8*i)/bits);
262     if (roar_vio_write(&svio, buf, i) != i)
263      break;
264    }
265   break;
266  case 8:
267    while((i = roar_vio_read(&svio, buf, BUFSIZE))) {
268     vol1((void*)buf, mul, div, i);
269     if (roar_vio_write(&svio, buf, i) != i)
270      break;
271    }
272   break;
273  default:
274    fprintf(stderr, "Error: %i bits per sample is not supported!\n", bits);
275    return 1;
276 }
277
278 roar_vio_close(&svio);
279
280 roardsp_fchain_uninit(&fc);
281
282 return 0;
283}
284
285//ll
Note: See TracBrowser for help on using the repository browser.