source: roaraudio/roarclients/roarfilt.c @ 1005:058e7ecae45c

Last change on this file since 1005:058e7ecae45c was 1005:058e7ecae45c, checked in by phi, 15 years ago

added --fmode and --fq

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