source: roaraudio/roarclients/roarfilt.c @ 884:9447d31faa46

Last change on this file since 884:9447d31faa46 was 884:9447d31faa46, checked in by phi, 15 years ago

added --fmul and --fdiv

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