source: roaraudio/roarclients/roarfilt.c @ 669:90348e6a785f

Last change on this file since 669:90348e6a785f was 669:90348e6a785f, checked in by phi, 16 years ago

added license statements

File size: 5.3 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 <math.h>
27
28#define BUFSIZE 1024
29struct {
30 uint16_t a, b;
31 int16_t  old[ROAR_MAX_CHANNELS];
32} g_lowpass;
33
34void usage (void) {
35 printf("roarfilt [OPTIONS]...\n");
36
37 printf("\nOptions:\n\n");
38
39 printf("  --server SERVER    - Set server hostname\n"
40        "  --rate   RATE      - Set sample rate\n"
41        "  --bits   BITS      - Set bits per sample\n"
42        "  --chans  CHANNELS  - Set number of channels\n"
43        "  --help             - Show this help\n"
44        "\n"
45        "  --half             - half the volume\n"
46        "  --double           - double the volume\n"
47        "  --amp VAL          - Set amplification\n"
48        "  --mul VAL          - Set mul\n"
49        "  --div VAL          - Set div\n"
50        "  --lowpass freq     - lowpass filter\n"
51       );
52
53}
54
55void vol2 (void * data, int mul, int div, int len) {
56 int16_t * samples = (int16_t *) data;
57 int i;
58
59 len /= 2;
60
61 for (i = 0; i < len; i++)
62  samples[i] = ((int) samples[i] * mul) / div;
63}
64
65void vol1 (void * data, int mul, int div, int len) {
66 int8_t * samples = (int8_t *) data;
67 int i;
68
69 for (i = 0; i < len; i++)
70  samples[i] = ((int) samples[i] * mul) / div;
71}
72
73void logs2 (void * data, float scale, int len) {
74 int16_t * samples = (int16_t *) data;
75 int i;
76 float div = logf(scale);
77 float scalemul = scale - 1;
78 int neg;
79
80 len /= 2;
81
82 //printf("logs2(data=%p, scale=%f, len=%i): scalemul=%f, div=%f\n", data, scale, len, scalemul, div);
83
84 for (i = 0; i < len; i++) {
85  if ( (neg = (samples[i] < 0)) )
86   samples[i] = abs(samples[i]);
87
88
89  samples[i] = (neg ? 32768.0 : 32767.0)*logf(1 + (scalemul*(float)samples[i]/(neg ? 32768.0 : 32767.0))) / div;
90
91  if ( neg )
92   samples[i] *= -1;
93 }
94}
95
96void lowpass2 (void * data, int len, int channels) {
97 int16_t * samples = (int16_t *) data;
98 register int32_t s;
99 int i, c;
100
101 if ( channels > ROAR_MAX_CHANNELS )
102  return;
103
104 len /= 2 * channels;
105
106//  *      output[N] = input[N] * A + output[N-1] * B
107
108 for (i = 0; i < len; i++) {
109  for (c = 0; c < channels; c++) {
110   s = samples[i*channels + c] * g_lowpass.a + g_lowpass.old[c] * g_lowpass.b;
111
112   s /= 65536;
113
114   samples[i*channels + c] = s;
115   g_lowpass.old[       c] = s;
116  }
117 }
118}
119
120int main (int argc, char * argv[]) {
121 int    rate     = 44100;
122 int    bits     = 16;
123 int    channels = 2;
124 int    codec    = ROAR_CODEC_DEFAULT;
125 char * server   = NULL;
126 char * k;
127 int    fh;
128 int    i;
129 int    mul = 1, div = 1;
130 float  logscale = 0;
131 float  lp       = 0;
132 char buf[BUFSIZE];
133
134 memset(&g_lowpass, 0, sizeof(g_lowpass));
135
136 for (i = 1; i < argc; i++) {
137  k = argv[i];
138
139  if ( strcmp(k, "--server") == 0 ) {
140   server = argv[++i];
141  } else if ( strcmp(k, "--rate") == 0 ) {
142   rate = atoi(argv[++i]);
143  } else if ( strcmp(k, "--bits") == 0 ) {
144   bits = atoi(argv[++i]);
145  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) {
146   channels = atoi(argv[++i]);
147  } else if ( strcmp(k, "--half") == 0 ) {
148   div *= 2;
149  } else if ( strcmp(k, "--double") == 0 ) {
150   mul *= 2;
151  } else if ( strcmp(k, "--amp") == 0 ) {
152   mul *= atoi(argv[++i]);
153  } else if ( strcmp(k, "--mul") == 0 ) {
154   mul  = atoi(argv[++i]);
155  } else if ( strcmp(k, "--div") == 0 ) {
156   div  = atoi(argv[++i]);
157  } else if ( strcmp(k, "--log") == 0 ) {
158   logscale = atof(argv[++i]);
159  } else if ( strcmp(k, "--lowpass") == 0 ) {
160   lp = exp(-2 * M_PI * atof(argv[++i]) / rate) * 65536;
161   g_lowpass.b = lp;
162   g_lowpass.a = 65536 - lp;
163//   printf("lowpass: A=%i, B=%i\n", g_lowpass.a, g_lowpass.b);
164  } else if ( strcmp(k, "--help") == 0 ) {
165   usage();
166   return 0;
167  } else {
168   fprintf(stderr, "Error: unknown argument: %s\n", k);
169   usage();
170   return 1;
171  }
172 }
173
174 if ( (fh = roar_simple_filter(rate, channels, bits, codec, server, "roarifilt")) == -1 ) {
175  fprintf(stderr, "Error: can not start playback\n");
176  return 1;
177 }
178
179 if ( mul == div && logscale == 0 && lp == 0 ) {
180  fprintf(stderr, "Error: filter is useless!\n");
181  return 0;
182 }
183
184 if ( bits == 16 ) {
185  while((i = read(fh, buf, BUFSIZE))) {
186   if ( mul != div )
187    vol2((void*)buf, mul, div, i);
188   if ( logscale )
189    logs2((void*)buf, logscale, i);
190   if ( g_lowpass.a )
191    lowpass2((void*)buf, i, channels);
192   if (write(fh, buf, i) != i)
193    break;
194  }
195 } else if ( bits == 8 ) {
196  while((i = read(fh, buf, BUFSIZE))) {
197   vol1((void*)buf, mul, div, i);
198   if (write(fh, buf, i) != i)
199    break;
200  }
201 } else {
202  fprintf(stderr, "Error: %i bits per sample is not supported!\n", bits);
203  return 1;
204 }
205
206 roar_simple_close(fh);
207
208 return 0;
209}
210
211//ll
Note: See TracBrowser for help on using the repository browser.