source: roaraudio/roarclients/roarfilt.c @ 5961:06e7fd9e4c25

Last change on this file since 5961:06e7fd9e4c25 was 5961:06e7fd9e4c25, checked in by phi, 10 years ago

Updates of copyright and license headers

File size: 7.5 KB
Line 
1//roarfilt.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2014
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include <roaraudio.h>
27#include <libroardsp/libroardsp.h>
28
29#ifdef ROAR_HAVE_LIBM
30#include <math.h>
31#endif
32
33#define BUFFERSIZE 1024
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  -R RATE     - Set sample rate\n"
42        "  --bits  -B BITS     - Set bits per sample\n"
43        "  --chans -C CHANNELS - Set number of channels\n"
44        "  --aiprofile PROFILE - Set audio profile\n"
45        "  --help              - Show this help\n"
46        "\n"
47        "  --half              - half the volume\n"
48        "  --double            - double the volume\n"
49        "  --amp VAL           - Set amplification\n"
50        "  --mul VAL           - Set mul\n"
51        "  --div VAL           - Set div\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
64#define _volX(bits,twobits,sf) \
65void vol##bits (void * data, int32_t mul, int32_t div, size_t len) { \
66 int##bits##_t * samples = (int##bits##_t *) data; \
67 size_t i; \
68\
69 len /= sf; \
70\
71 for (i = 0; i < len; i++) \
72  samples[i] = ((int##twobits##_t) samples[i] * mul) / div; \
73}
74
75_volX( 8,16,1)
76_volX(16,32,2)
77_volX(32,64,4)
78
79int main (int argc, char * argv[]) {
80 struct roar_audio_info info;
81 const char  * server   = NULL;
82 const char  * k;
83 int     i;
84 int32_t mul = 1, div = 1;
85 int     filter_id;
86 int32_t tmp;
87 float   tmpfp;
88 char    buf[BUFFERSIZE];
89 struct roardsp_filterchain fc;
90 struct roardsp_filter      filter_real[8];
91 struct roardsp_filter    * filter = filter_real - 1;
92 struct roar_stream         stream;
93 struct roar_vio_calls      svio;
94
95 _LIBROAR_IGNORE_RET(roar_profile2info(&info, "default-server"));
96 info.codec = ROAR_CODEC_DEFAULT;
97
98 roardsp_fchain_init(&fc);
99
100 for (i = 1; i < argc; i++) {
101  k = argv[i];
102
103  if ( strcmp(k, "--server") == 0 || strcmp(k, "-s") == 0 ) {
104   ROAR_CKHAVEARGS(1);
105   server = argv[++i];
106  } else if ( strcmp(k, "--rate") == 0 || strcmp(k, "-R") == 0 || strcmp(k, "-r") == 0 ) {
107   ROAR_CKHAVEARGS(1);
108   info.rate = roar_str2rate(argv[++i]);
109  } else if ( strcmp(k, "--bits") == 0 || strcmp(k, "-B") == 0 ) {
110   ROAR_CKHAVEARGS(1);
111   info.bits = roar_str2bits(argv[++i]);
112  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 || strcmp(k, "-C") == 0 ) {
113   ROAR_CKHAVEARGS(1);
114   info.channels = roar_str2channels(argv[++i]);
115  } else if ( strcmp(k, "-b") == 0 ) {
116   info.bits = 8;
117  } else if ( strcmp(k, "-m") == 0 ) {
118   info.channels = 1;
119  } else if ( !strcmp(k, "--aiprofile") ) {
120   ROAR_CKHAVEARGS(1);
121   if ( roar_profile2info(&info, argv[++i]) == -1 ) {
122    fprintf(stderr, "Error: Can not load audio profile: %s: %s\n", argv[i], roar_error2str(roar_error));
123    return 1;
124   }
125   info.codec = ROAR_CODEC_DEFAULT;
126  } else if ( strcmp(k, "--half") == 0 || strcmp(k, "-half") == 0 ) {
127   div *= 2;
128  } else if ( strcmp(k, "--double") == 0 || strcmp(k, "-double") == 0 ) {
129   mul *= 2;
130  } else if ( strcmp(k, "--amp") == 0 ) {
131   ROAR_CKHAVEARGS(1);
132   mul *= atoi(argv[++i]);
133  } else if ( strcmp(k, "--mul") == 0 ) {
134   ROAR_CKHAVEARGS(1);
135   mul  = atoi(argv[++i]);
136  } else if ( strcmp(k, "--div") == 0 ) {
137   ROAR_CKHAVEARGS(1);
138   div  = atoi(argv[++i]);
139  } else if ( strcmp(k, "--filter") == 0 ) {
140   ROAR_CKHAVEARGS(1);
141   stream.info = info;
142   filter_id = roardsp_filter_str2id(argv[++i]);
143   if ( filter_id == -1 ) {
144    ROAR_WARN("Can not add filter as filter ID is unknown: %s: %s", argv[i], roar_error2str(roar_error));
145   } else {
146    filter++;
147    if ( roardsp_filter_init(filter, &stream, filter_id) == -1 ) {
148     ROAR_WARN("Can not add filter: %s: %s", argv[i], roar_error2str(roar_error));
149     filter--;
150    } else {
151     roardsp_fchain_add(&fc, filter);
152    }
153   }
154  } else if ( strcmp(k, "--ffreq") == 0 ) {
155   ROAR_CKHAVEARGS(1);
156   tmpfp = atof(argv[++i]);
157   roardsp_filter_ctl(filter, ROARDSP_FCTL_FREQ, &tmpfp);
158  } else if ( strcmp(k, "--fmul") == 0 ) {
159   ROAR_CKHAVEARGS(1);
160   tmp = atoi(argv[++i]);
161   roardsp_filter_ctl(filter, ROARDSP_FCTL_MUL, &tmp);
162  } else if ( strcmp(k, "--fdiv") == 0 ) {
163   ROAR_CKHAVEARGS(1);
164   tmp = atoi(argv[++i]);
165   roardsp_filter_ctl(filter, ROARDSP_FCTL_DIV, &tmp);
166  } else if ( strcmp(k, "--fn") == 0 ) {
167   ROAR_CKHAVEARGS(1);
168   tmp = atoi(argv[++i]);
169   roardsp_filter_ctl(filter, ROARDSP_FCTL_N, &tmp);
170  } else if ( strcmp(k, "--fq") == 0 ) {
171   ROAR_CKHAVEARGS(1);
172   tmp = atoi(argv[++i]);
173   roardsp_filter_ctl(filter, ROARDSP_FCTL_Q, &tmp);
174  } else if ( strcmp(k, "--flimit") == 0 ) {
175   ROAR_CKHAVEARGS(1);
176   tmp = atoi(argv[++i]);
177   roardsp_filter_ctl(filter, ROARDSP_FCTL_LIMIT, &tmp);
178  } else if ( strcmp(k, "--fmode") == 0 ) {
179   ROAR_CKHAVEARGS(1);
180   tmp = atoi(argv[++i]);
181   roardsp_filter_ctl(filter, ROARDSP_FCTL_MODE, &tmp);
182  } else if ( strcmp(k, "--help") == 0 || strcmp(k, "-h") == 0 ) {
183   usage();
184   return 0;
185  } else {
186   fprintf(stderr, "Error: unknown argument: %s\n", k);
187   usage();
188   return 1;
189  }
190 }
191
192 if ( roar_vio_simple_stream(&svio,
193                             info.rate, info.channels, info.bits, info.codec,
194                             server, ROAR_DIR_FILTER, "roarfilt", -1) == -1 ) {
195  fprintf(stderr, "Error: can not start playback\n");
196  return 1;
197 }
198
199 if ( mul == div &&
200      roardsp_fchain_num(&fc) == 0 ) {
201  fprintf(stderr, "Error: filter is useless!\n");
202  return 0;
203 }
204
205 switch (info.bits) {
206  case 8:
207    while((i = roar_vio_read(&svio, buf, sizeof(buf)))) {
208     vol8((void*)buf, mul, div, i);
209     roardsp_fchain_calc(&fc, (void*)buf, (8*i)/info.bits);
210     if (roar_vio_write(&svio, buf, i) != i)
211      break;
212    }
213   break;
214  case 16:
215    while((i = roar_vio_read(&svio, buf, sizeof(buf)))) {
216     if ( mul != div )
217      vol16((void*)buf, mul, div, i);
218     roardsp_fchain_calc(&fc, (void*)buf, (8*i)/info.bits);
219     if (roar_vio_write(&svio, buf, i) != i)
220      break;
221    }
222   break;
223  case 32:
224    while((i = roar_vio_read(&svio, buf, sizeof(buf)))) {
225     vol32((void*)buf, mul, div, i);
226     roardsp_fchain_calc(&fc, (void*)buf, (8*i)/info.bits);
227     if (roar_vio_write(&svio, buf, i) != i)
228      break;
229    }
230   break;
231  default:
232    fprintf(stderr, "Error: %i bits per sample is not supported!\n", (int)info.bits);
233    return 1;
234 }
235
236 roar_vio_close(&svio);
237
238 roardsp_fchain_uninit(&fc);
239
240 return 0;
241}
242
243//ll
Note: See TracBrowser for help on using the repository browser.