1 | //roarfilt.c: |
---|
2 | |
---|
3 | #include <roaraudio.h> |
---|
4 | #include <math.h> |
---|
5 | |
---|
6 | #define BUFSIZE 1024 |
---|
7 | |
---|
8 | void usage (void) { |
---|
9 | printf("roarfilt [OPTIONS]...\n"); |
---|
10 | |
---|
11 | printf("\nOptions:\n\n"); |
---|
12 | |
---|
13 | printf(" --server SERVER - Set server hostname\n" |
---|
14 | " --rate RATE - Set sample rate\n" |
---|
15 | " --bits BITS - Set bits per sample\n" |
---|
16 | " --chans CHANNELS - Set number of channels\n" |
---|
17 | " --help - Show this help\n" |
---|
18 | "\n" |
---|
19 | " --half - half the volume\n" |
---|
20 | " --double - double the volume\n" |
---|
21 | " --amp VAL - Set amplification\n" |
---|
22 | " --mul VAL - Set mul\n" |
---|
23 | " --div VAL - Set div\n" |
---|
24 | ); |
---|
25 | |
---|
26 | } |
---|
27 | |
---|
28 | void vol2 (void * data, int mul, int div, int len) { |
---|
29 | int16_t * samples = (int16_t *) data; |
---|
30 | int i; |
---|
31 | |
---|
32 | len /= 2; |
---|
33 | |
---|
34 | for (i = 0; i < len; i++) |
---|
35 | samples[i] = ((int) samples[i] * mul) / div; |
---|
36 | } |
---|
37 | |
---|
38 | void vol1 (void * data, int mul, int div, int len) { |
---|
39 | int8_t * samples = (int8_t *) data; |
---|
40 | int i; |
---|
41 | |
---|
42 | for (i = 0; i < len; i++) |
---|
43 | samples[i] = ((int) samples[i] * mul) / div; |
---|
44 | } |
---|
45 | |
---|
46 | void logs2 (void * data, float scale, int len) { |
---|
47 | int16_t * samples = (int16_t *) data; |
---|
48 | int i; |
---|
49 | float div = logf(scale); |
---|
50 | float scalemul = scale - 1; |
---|
51 | int neg; |
---|
52 | |
---|
53 | len /= 2; |
---|
54 | |
---|
55 | //printf("logs2(data=%p, scale=%f, len=%i): scalemul=%f, div=%f\n", data, scale, len, scalemul, div); |
---|
56 | |
---|
57 | for (i = 0; i < len; i++) { |
---|
58 | if ( (neg = (samples[i] < 0)) ) |
---|
59 | samples[i] = abs(samples[i]); |
---|
60 | |
---|
61 | |
---|
62 | samples[i] = (neg ? 32768.0 : 32767.0)*logf(1 + (scalemul*(float)samples[i]/(neg ? 32768.0 : 32767.0))) / div; |
---|
63 | |
---|
64 | if ( neg ) |
---|
65 | samples[i] *= -1; |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | int main (int argc, char * argv[]) { |
---|
70 | int rate = 44100; |
---|
71 | int bits = 16; |
---|
72 | int channels = 2; |
---|
73 | int codec = ROAR_CODEC_DEFAULT; |
---|
74 | char * server = NULL; |
---|
75 | char * k; |
---|
76 | int fh; |
---|
77 | int i; |
---|
78 | int mul = 1, div = 1; |
---|
79 | float logscale = 0; |
---|
80 | char buf[BUFSIZE]; |
---|
81 | |
---|
82 | for (i = 1; i < argc; i++) { |
---|
83 | k = argv[i]; |
---|
84 | |
---|
85 | if ( strcmp(k, "--server") == 0 ) { |
---|
86 | server = argv[++i]; |
---|
87 | } else if ( strcmp(k, "--rate") == 0 ) { |
---|
88 | rate = atoi(argv[++i]); |
---|
89 | } else if ( strcmp(k, "--bits") == 0 ) { |
---|
90 | bits = atoi(argv[++i]); |
---|
91 | } else if ( strcmp(k, "--channels") == 0 || strcmp(k, "--chans") == 0 ) { |
---|
92 | channels = atoi(argv[++i]); |
---|
93 | } else if ( strcmp(k, "--half") == 0 ) { |
---|
94 | div *= 2; |
---|
95 | } else if ( strcmp(k, "--double") == 0 ) { |
---|
96 | mul *= 2; |
---|
97 | } else if ( strcmp(k, "--amp") == 0 ) { |
---|
98 | mul *= atoi(argv[++i]); |
---|
99 | } else if ( strcmp(k, "--mul") == 0 ) { |
---|
100 | mul = atoi(argv[++i]); |
---|
101 | } else if ( strcmp(k, "--div") == 0 ) { |
---|
102 | div = atoi(argv[++i]); |
---|
103 | } else if ( strcmp(k, "--log") == 0 ) { |
---|
104 | logscale = atof(argv[++i]); |
---|
105 | } else if ( strcmp(k, "--help") == 0 ) { |
---|
106 | usage(); |
---|
107 | return 0; |
---|
108 | } else { |
---|
109 | fprintf(stderr, "Error: unknown argument: %s\n", k); |
---|
110 | usage(); |
---|
111 | return 1; |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | if ( (fh = roar_simple_filter(rate, channels, bits, codec, server, "roarifilt")) == -1 ) { |
---|
116 | fprintf(stderr, "Error: can not start playback\n"); |
---|
117 | return 1; |
---|
118 | } |
---|
119 | |
---|
120 | if ( mul == div && logscale == 0 ) { |
---|
121 | fprintf(stderr, "Error: filter is useless!\n"); |
---|
122 | return 0; |
---|
123 | } |
---|
124 | |
---|
125 | if ( bits == 16 ) { |
---|
126 | while((i = read(fh, buf, BUFSIZE))) { |
---|
127 | if ( mul != div ) |
---|
128 | vol2((void*)buf, mul, div, i); |
---|
129 | if ( logscale ) |
---|
130 | logs2((void*)buf, logscale, i); |
---|
131 | if (write(fh, buf, i) != i) |
---|
132 | break; |
---|
133 | } |
---|
134 | } else if ( bits == 8 ) { |
---|
135 | while((i = read(fh, buf, BUFSIZE))) { |
---|
136 | vol1((void*)buf, mul, div, i); |
---|
137 | if (write(fh, buf, i) != i) |
---|
138 | break; |
---|
139 | } |
---|
140 | } else { |
---|
141 | fprintf(stderr, "Error: %i bits per sample is not supported!\n", bits); |
---|
142 | return 1; |
---|
143 | } |
---|
144 | |
---|
145 | roar_simple_close(fh); |
---|
146 | |
---|
147 | return 0; |
---|
148 | } |
---|
149 | |
---|
150 | //ll |
---|