[126] | 1 | //roarfilt.c: |
---|
| 2 | |
---|
| 3 | #include <roaraudio.h> |
---|
[130] | 4 | #include <math.h> |
---|
[126] | 5 | |
---|
| 6 | #define BUFSIZE 1024 |
---|
| 7 | |
---|
| 8 | void usage (void) { |
---|
| 9 | printf("roarcat [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" |
---|
[128] | 18 | "\n" |
---|
| 19 | " --half - half the volume\n" |
---|
| 20 | " --double - double the volume\n" |
---|
[130] | 21 | " --amp VAL - Set amplification\n" |
---|
| 22 | " --mul VAL - Set mul\n" |
---|
| 23 | " --div VAL - Set div\n" |
---|
[126] | 24 | ); |
---|
| 25 | |
---|
| 26 | } |
---|
| 27 | |
---|
[128] | 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 | |
---|
[130] | 42 | for (i = 0; i < len; i++) |
---|
| 43 | samples[i] = ((int) samples[i] * mul) / div; |
---|
| 44 | } |
---|
| 45 | |
---|
[140] | 46 | void logs2 (void * data, float scale, int len) { |
---|
[130] | 47 | int16_t * samples = (int16_t *) data; |
---|
| 48 | int i; |
---|
| 49 | float div = logf(scale); |
---|
[140] | 50 | float scalemul = scale - 1; |
---|
[130] | 51 | int neg; |
---|
| 52 | |
---|
[128] | 53 | len /= 2; |
---|
| 54 | |
---|
[140] | 55 | //printf("logs2(data=%p, scale=%f, len=%i): scalemul=%f, div=%f\n", data, scale, len, scalemul, div); |
---|
[130] | 56 | |
---|
| 57 | for (i = 0; i < len; i++) { |
---|
| 58 | if ( (neg = (samples[i] < 0)) ) |
---|
| 59 | samples[i] = abs(samples[i]); |
---|
| 60 | |
---|
| 61 | |
---|
[140] | 62 | samples[i] = (neg ? 32768.0 : 32767.0)*logf(1 + (scalemul*(float)samples[i]/(neg ? 32768.0 : 32767.0))) / div; |
---|
[130] | 63 | |
---|
| 64 | if ( neg ) |
---|
| 65 | samples[i] *= -1; |
---|
| 66 | } |
---|
[128] | 67 | } |
---|
| 68 | |
---|
[126] | 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; |
---|
[128] | 78 | int mul = 1, div = 1; |
---|
[140] | 79 | float logscale = 0; |
---|
[126] | 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]); |
---|
[128] | 93 | } else if ( strcmp(k, "--half") == 0 ) { |
---|
| 94 | div *= 2; |
---|
| 95 | } else if ( strcmp(k, "--double") == 0 ) { |
---|
| 96 | mul *= 2; |
---|
[130] | 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 ) { |
---|
[140] | 104 | logscale = atof(argv[++i]); |
---|
[126] | 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 | |
---|
[130] | 120 | if ( mul == div && logscale == 0 ) { |
---|
[128] | 121 | fprintf(stderr, "Error: filter is useless!\n"); |
---|
| 122 | return 0; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | if ( bits == 16 ) { |
---|
| 126 | while((i = read(fh, buf, BUFSIZE))) { |
---|
[130] | 127 | if ( mul != div ) |
---|
| 128 | vol2((void*)buf, mul, div, i); |
---|
| 129 | if ( logscale ) |
---|
| 130 | logs2((void*)buf, logscale, i); |
---|
[128] | 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 | } |
---|
[126] | 144 | |
---|
| 145 | roar_simple_close(fh); |
---|
| 146 | |
---|
| 147 | return 0; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | //ll |
---|