source: roaraudio/roarclients/roarfilt.c @ 985:e9e58ccee447

Last change on this file since 985:e9e58ccee447 was 985:e9e58ccee447, checked in by phi, 15 years ago

added support for N and Limit parameter of filters

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