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
RevLine 
[126]1//roarfilt.c:
2
[669]3/*
[5961]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2014
[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
[5178]33#define BUFFERSIZE 1024
34
[126]35void usage (void) {
[233]36 printf("roarfilt [OPTIONS]...\n");
[126]37
38 printf("\nOptions:\n\n");
39
[5533]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"
[128]46        "\n"
[5533]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"
[126]60       );
61
62}
63
[5180]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; \
[128]73}
74
[5180]75_volX( 8,16,1)
76_volX(16,32,2)
77_volX(32,64,4)
[130]78
[126]79int main (int argc, char * argv[]) {
[5180]80 struct roar_audio_info info;
[5534]81 const char  * server   = NULL;
82 const char  * k;
[5180]83 int     i;
84 int32_t mul = 1, div = 1;
85 int     filter_id;
[884]86 int32_t tmp;
[5950]87 float   tmpfp;
[5180]88 char    buf[BUFFERSIZE];
[682]89 struct roardsp_filterchain fc;
[884]90 struct roardsp_filter      filter_real[8];
91 struct roardsp_filter    * filter = filter_real - 1;
[682]92 struct roar_stream         stream;
[2846]93 struct roar_vio_calls      svio;
[126]94
[5180]95 _LIBROAR_IGNORE_RET(roar_profile2info(&info, "default-server"));
96 info.codec = ROAR_CODEC_DEFAULT;
97
[682]98 roardsp_fchain_init(&fc);
99
[126]100 for (i = 1; i < argc; i++) {
101  k = argv[i];
102
[1635]103  if ( strcmp(k, "--server") == 0 || strcmp(k, "-s") == 0 ) {
[5950]104   ROAR_CKHAVEARGS(1);
[126]105   server = argv[++i];
[1507]106  } else if ( strcmp(k, "--rate") == 0 || strcmp(k, "-R") == 0 || strcmp(k, "-r") == 0 ) {
[5950]107   ROAR_CKHAVEARGS(1);
[5180]108   info.rate = roar_str2rate(argv[++i]);
[1507]109  } else if ( strcmp(k, "--bits") == 0 || strcmp(k, "-B") == 0 ) {
[5950]110   ROAR_CKHAVEARGS(1);
[5180]111   info.bits = roar_str2bits(argv[++i]);
[1507]112  } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 || strcmp(k, "-C") == 0 ) {
[5950]113   ROAR_CKHAVEARGS(1);
[5180]114   info.channels = roar_str2channels(argv[++i]);
[1507]115  } else if ( strcmp(k, "-b") == 0 ) {
[5180]116   info.bits = 8;
[1507]117  } else if ( strcmp(k, "-m") == 0 ) {
[5180]118   info.channels = 1;
[5533]119  } else if ( !strcmp(k, "--aiprofile") ) {
[5950]120   ROAR_CKHAVEARGS(1);
[5533]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;
[1507]126  } else if ( strcmp(k, "--half") == 0 || strcmp(k, "-half") == 0 ) {
[128]127   div *= 2;
[1507]128  } else if ( strcmp(k, "--double") == 0 || strcmp(k, "-double") == 0 ) {
[128]129   mul *= 2;
[130]130  } else if ( strcmp(k, "--amp") == 0 ) {
[5950]131   ROAR_CKHAVEARGS(1);
[130]132   mul *= atoi(argv[++i]);
133  } else if ( strcmp(k, "--mul") == 0 ) {
[5950]134   ROAR_CKHAVEARGS(1);
[130]135   mul  = atoi(argv[++i]);
136  } else if ( strcmp(k, "--div") == 0 ) {
[5950]137   ROAR_CKHAVEARGS(1);
[130]138   div  = atoi(argv[++i]);
[682]139  } else if ( strcmp(k, "--filter") == 0 ) {
[5950]140   ROAR_CKHAVEARGS(1);
[5180]141   stream.info = info;
[5172]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   }
[682]154  } else if ( strcmp(k, "--ffreq") == 0 ) {
[5950]155   ROAR_CKHAVEARGS(1);
156   tmpfp = atof(argv[++i]);
157   roardsp_filter_ctl(filter, ROARDSP_FCTL_FREQ, &tmpfp);
[884]158  } else if ( strcmp(k, "--fmul") == 0 ) {
[5950]159   ROAR_CKHAVEARGS(1);
[884]160   tmp = atoi(argv[++i]);
161   roardsp_filter_ctl(filter, ROARDSP_FCTL_MUL, &tmp);
162  } else if ( strcmp(k, "--fdiv") == 0 ) {
[5950]163   ROAR_CKHAVEARGS(1);
[884]164   tmp = atoi(argv[++i]);
165   roardsp_filter_ctl(filter, ROARDSP_FCTL_DIV, &tmp);
[985]166  } else if ( strcmp(k, "--fn") == 0 ) {
[5950]167   ROAR_CKHAVEARGS(1);
[985]168   tmp = atoi(argv[++i]);
169   roardsp_filter_ctl(filter, ROARDSP_FCTL_N, &tmp);
[1005]170  } else if ( strcmp(k, "--fq") == 0 ) {
[5950]171   ROAR_CKHAVEARGS(1);
[1005]172   tmp = atoi(argv[++i]);
173   roardsp_filter_ctl(filter, ROARDSP_FCTL_Q, &tmp);
[985]174  } else if ( strcmp(k, "--flimit") == 0 ) {
[5950]175   ROAR_CKHAVEARGS(1);
[985]176   tmp = atoi(argv[++i]);
177   roardsp_filter_ctl(filter, ROARDSP_FCTL_LIMIT, &tmp);
[1005]178  } else if ( strcmp(k, "--fmode") == 0 ) {
[5950]179   ROAR_CKHAVEARGS(1);
[1005]180   tmp = atoi(argv[++i]);
181   roardsp_filter_ctl(filter, ROARDSP_FCTL_MODE, &tmp);
[1635]182  } else if ( strcmp(k, "--help") == 0 || strcmp(k, "-h") == 0 ) {
[126]183   usage();
184   return 0;
185  } else {
186   fprintf(stderr, "Error: unknown argument: %s\n", k);
187   usage();
188   return 1;
189  }
190 }
191
[5180]192 if ( roar_vio_simple_stream(&svio,
193                             info.rate, info.channels, info.bits, info.codec,
[5289]194                             server, ROAR_DIR_FILTER, "roarfilt", -1) == -1 ) {
[126]195  fprintf(stderr, "Error: can not start playback\n");
196  return 1;
197 }
198
[1103]199 if ( mul == div &&
200      roardsp_fchain_num(&fc) == 0 ) {
[128]201  fprintf(stderr, "Error: filter is useless!\n");
202  return 0;
203 }
204
[5180]205 switch (info.bits) {
[5176]206  case 8:
[5178]207    while((i = roar_vio_read(&svio, buf, sizeof(buf)))) {
[5180]208     vol8((void*)buf, mul, div, i);
209     roardsp_fchain_calc(&fc, (void*)buf, (8*i)/info.bits);
[5176]210     if (roar_vio_write(&svio, buf, i) != i)
211      break;
212    }
213   break;
[2847]214  case 16:
[5178]215    while((i = roar_vio_read(&svio, buf, sizeof(buf)))) {
[2847]216     if ( mul != div )
[5180]217      vol16((void*)buf, mul, div, i);
218     roardsp_fchain_calc(&fc, (void*)buf, (8*i)/info.bits);
[2847]219     if (roar_vio_write(&svio, buf, i) != i)
220      break;
221    }
222   break;
[5176]223  case 32:
[5178]224    while((i = roar_vio_read(&svio, buf, sizeof(buf)))) {
[5180]225     vol32((void*)buf, mul, div, i);
226     roardsp_fchain_calc(&fc, (void*)buf, (8*i)/info.bits);
[2847]227     if (roar_vio_write(&svio, buf, i) != i)
228      break;
229    }
230   break;
231  default:
[5180]232    fprintf(stderr, "Error: %i bits per sample is not supported!\n", (int)info.bits);
[2847]233    return 1;
[128]234 }
[126]235
[2846]236 roar_vio_close(&svio);
[126]237
[682]238 roardsp_fchain_uninit(&fc);
239
[126]240 return 0;
241}
242
243//ll
Note: See TracBrowser for help on using the repository browser.