source: roaraudio/roarclients/roarfilt.c @ 4708:c9d40761088a

Last change on this file since 4708:c9d40761088a was 4708:c9d40761088a, checked in by phi, 13 years ago

updated copyright statements

File size: 7.9 KB
RevLine 
[126]1//roarfilt.c:
2
[669]3/*
[4708]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
[669]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[669]23 *
24 */
25
[126]26#include <roaraudio.h>
[682]27#include <libroardsp/libroardsp.h>
[1103]28
29#ifdef ROAR_HAVE_LIBM
[130]30#include <math.h>
[1103]31#endif
[126]32
33#define BUFSIZE 1024
[1103]34#ifdef ROAR_HAVE_LIBM
[646]35struct {
[648]36 uint16_t a, b;
37 int16_t  old[ROAR_MAX_CHANNELS];
[646]38} g_lowpass;
[1103]39#endif
[126]40
41void usage (void) {
[233]42 printf("roarfilt [OPTIONS]...\n");
[126]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"
[128]51        "\n"
52        "  --half             - half the volume\n"
53        "  --double           - double the volume\n"
[130]54        "  --amp VAL          - Set amplification\n"
55        "  --mul VAL          - Set mul\n"
56        "  --div VAL          - Set div\n"
[1103]57#ifdef ROAR_HAVE_LIBM
[646]58        "  --lowpass freq     - lowpass filter\n"
[1103]59#endif
[682]60        "  --filter  name     - add filter name\n"
61        "  --ffreq   freq     - set filter freq\n"
[884]62        "  --fmul    mult     - set filter multiplier\n"
63        "  --fdiv    div      - set filter divider\n"
[985]64        "  --fn      N        - set filter N parameter\n"
65        "  --flimit  limit    - set filter limit parameter\n"
[1005]66        "  --fmode   mode     - set filter mode parameter\n"
67        "  --fq      Q        - set filter quality\n"
[126]68       );
69
70}
71
[128]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
[130]86 for (i = 0; i < len; i++)
87  samples[i] = ((int) samples[i] * mul) / div;
88}
89
[1103]90#ifdef ROAR_HAVE_LIBM
[140]91void logs2 (void * data, float scale, int len) {
[130]92 int16_t * samples = (int16_t *) data;
93 int i;
94 float div = logf(scale);
[140]95 float scalemul = scale - 1;
[130]96 int neg;
97
[128]98 len /= 2;
99
[140]100 //printf("logs2(data=%p, scale=%f, len=%i): scalemul=%f, div=%f\n", data, scale, len, scalemul, div);
[130]101
102 for (i = 0; i < len; i++) {
103  if ( (neg = (samples[i] < 0)) )
104   samples[i] = abs(samples[i]);
105
106
[140]107  samples[i] = (neg ? 32768.0 : 32767.0)*logf(1 + (scalemul*(float)samples[i]/(neg ? 32768.0 : 32767.0))) / div;
[130]108
109  if ( neg )
110   samples[i] *= -1;
111 }
[128]112}
113
[646]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}
[1103]137#endif
[646]138
[126]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;
[128]147 int    mul = 1, div = 1;
[884]148 int32_t tmp;
[140]149 float  logscale = 0;
[646]150 float  lp       = 0;
[126]151 char buf[BUFSIZE];
[682]152 struct roardsp_filterchain fc;
[884]153 struct roardsp_filter      filter_real[8];
154 struct roardsp_filter    * filter = filter_real - 1;
[682]155 struct roar_stream         stream;
[2846]156 struct roar_vio_calls      svio;
[126]157
[1103]158#ifdef ROAR_HAVE_LIBM
[646]159 memset(&g_lowpass, 0, sizeof(g_lowpass));
[1103]160#endif
[646]161
[682]162 roardsp_fchain_init(&fc);
163
[126]164 for (i = 1; i < argc; i++) {
165  k = argv[i];
166
[1635]167  if ( strcmp(k, "--server") == 0 || strcmp(k, "-s") == 0 ) {
[126]168   server = argv[++i];
[1507]169  } else if ( strcmp(k, "--rate") == 0 || strcmp(k, "-R") == 0 || strcmp(k, "-r") == 0 ) {
[126]170   rate = atoi(argv[++i]);
[1507]171  } else if ( strcmp(k, "--bits") == 0 || strcmp(k, "-B") == 0 ) {
[126]172   bits = atoi(argv[++i]);
[1507]173  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 || strcmp(k, "-C") == 0 ) {
[126]174   channels = atoi(argv[++i]);
[1507]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 ) {
[128]180   div *= 2;
[1507]181  } else if ( strcmp(k, "--double") == 0 || strcmp(k, "-double") == 0 ) {
[128]182   mul *= 2;
[130]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 ) {
[140]190   logscale = atof(argv[++i]);
[1103]191#ifdef ROAR_HAVE_LIBM
[646]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;
[1103]196#endif
[648]197//   printf("lowpass: A=%i, B=%i\n", g_lowpass.a, g_lowpass.b);
[682]198  } else if ( strcmp(k, "--filter") == 0 ) {
199   stream.info.channels = channels;
200   stream.info.bits     = bits;
201   stream.info.rate     = rate;
[884]202   filter++;
203   roardsp_filter_init(filter, &stream, roardsp_filter_str2id(argv[++i]));
204   roardsp_fchain_add(&fc, filter);
[682]205  } else if ( strcmp(k, "--ffreq") == 0 ) {
206   lp = atof(argv[++i]);
[884]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);
[985]214  } else if ( strcmp(k, "--fn") == 0 ) {
215   tmp = atoi(argv[++i]);
216   roardsp_filter_ctl(filter, ROARDSP_FCTL_N, &tmp);
[1005]217  } else if ( strcmp(k, "--fq") == 0 ) {
218   tmp = atoi(argv[++i]);
219   roardsp_filter_ctl(filter, ROARDSP_FCTL_Q, &tmp);
[985]220  } else if ( strcmp(k, "--flimit") == 0 ) {
221   tmp = atoi(argv[++i]);
222   roardsp_filter_ctl(filter, ROARDSP_FCTL_LIMIT, &tmp);
[1005]223  } else if ( strcmp(k, "--fmode") == 0 ) {
224   tmp = atoi(argv[++i]);
225   roardsp_filter_ctl(filter, ROARDSP_FCTL_MODE, &tmp);
[1635]226  } else if ( strcmp(k, "--help") == 0 || strcmp(k, "-h") == 0 ) {
[126]227   usage();
228   return 0;
229  } else {
230   fprintf(stderr, "Error: unknown argument: %s\n", k);
231   usage();
232   return 1;
233  }
234 }
235
[2846]236 if ( roar_vio_simple_stream(&svio, rate, channels, bits, codec, server, ROAR_DIR_FILTER, "roarfilt") == -1 ) {
[126]237  fprintf(stderr, "Error: can not start playback\n");
238  return 1;
239 }
240
[1103]241 if ( mul == div &&
242#ifdef ROAR_HAVE_LIBM
243      logscale == 0 && g_lowpass.a == 0 &&
244#endif
245      roardsp_fchain_num(&fc) == 0 ) {
[128]246  fprintf(stderr, "Error: filter is useless!\n");
247  return 0;
248 }
249
[2847]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);
[1103]255#ifdef ROAR_HAVE_LIBM
[2847]256     if ( logscale )
257      logs2((void*)buf, logscale, i);
258     if ( g_lowpass.a )
259      lowpass2((void*)buf, i, channels);
[1103]260#endif
[2847]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;
[128]276 }
[126]277
[2846]278 roar_vio_close(&svio);
[126]279
[682]280 roardsp_fchain_uninit(&fc);
281
[126]282 return 0;
283}
284
285//ll
Note: See TracBrowser for help on using the repository browser.