-
Notifications
You must be signed in to change notification settings - Fork 0
/
auspan.c
254 lines (214 loc) · 7.39 KB
/
auspan.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "asa.h"
#include <fftw3.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <math.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/stat.h>
#define Y_DBG_MAIN
#include "y_dbg.h"
// TODO: autogenerate
#define PROGRAM "auspan"
#define VERSION "0.1.0"
#define COMPILE PROGRAM " " VERSION " compiled at " __DATE__ " " __TIME__
__attribute__((noreturn))
static void version() {
fprintf(stderr, "%s\n", COMPILE);
exit(0);
}
__attribute__((noreturn))
static void usage(char* msg) {
if (msg) fprintf(stderr, "\e[31;1mError: %s\e[m\n", msg);
fputs(
"Analyse audio and generate spectrums\n"
"Usage: " PROGRAM " [options] [input-file [output-file]]\n"
" where input-file is a s16le pcm source und output-file a file to\n"
" which u8 spectrum data is APPENDED to; s16le is signed 16-bit little\n"
" endian and u8 unsigned 8-bit integer\n"
"\n"
"Options: (x = 2^20 = 1048576, m = 1 + n / 2)\n"
" -v print version default limits\n"
" -s number of samples taken in a sequence 32 1 <= s <= x\n"
" -r number of sequences per spectrum 1 1 <= r <= 999\n"
" -d distance between sequence starts s 1 <= d <= x\n"
" or in % of s 100% 1% <= d <= 10000%\n"
" spectrums output at frequency: sampling frequency / d / r\n"
" -w window function, one of: boxcar hann flattop blackmanharris,\n"
" default hann\n"
" -n fft size with zero padding s s <= n <= x\n"
" -b b0,b1 use bins from b0 to b1 1,m-2 0 <= b0 <= b1 <= m-1\n"
" being used: b = b1 - b0 unsigned 8-bit bins\n"
" -p distribute bins to the power of p 1 1 <= p <= 2\n"
" -l number of spectrum lines b 1 <= l <= b\n"
" if b == l then only p == 1 is allowed\n"
"", stderr
);
exit(127);
}
static void parse_args(int argc, char **argv, asa_t asa) {
asa_param_t p = {
.s = 32, .n = 32, .m = 17,
.b0 = 1, .b1 = 15, .b = 15,
.p = 1.0, .l = 15,
.r = 1, .d = 32,
.w = W_HANN,
};
y_trc("s %d n %d m %d b0 %d b1 %d b %d l %d p %f r %d d %d w %s",
p.s, p.n, p.m, p.b0, p.b1, p.b, p.l, p.p, p.r, p.d, window_names[p.w]);
char opt;
unsigned long result;
int s_set = 0, n_set = 0, d_set = 0, b_set = 0, l_set = 0;
while (-1 != (opt = getopt (argc, argv, "vhs:n:b:p:l:r:d:w:"))) {
y_trc("opt %c optarg '%s' optind %d", opt, optarg, optind);
switch (opt) {
case 'v': version();
case 'h': usage(NULL);
case 's': {
result = strtoull(optarg, NULL, 10);
if (result < 1 || result > x) usage("-s out of limit");
p.s = result;
if (!n_set) p.n = p.s;
if (!d_set) p.d = p.s;
s_set = 1;
} break;
case 'r': {
result = strtoll(optarg, NULL, 10);
if (result < 1 || result > 999) usage("-r out of limit");
p.r = result;
} break;
case 'd': {
if (!s_set) usage("specify -s before -d"); // it's easier this way :-/
char *tail;
result = strtoull(optarg, &tail, 10);
if (tail[0] == '%') {
if (result > 10000) usage("-d out of limit");
p.d = result / 100.0 * p.s;
}
else {
if (result < 1 || result > x) usage("-d out of limit");
p.d = result;
}
} break;
case 'w': {
for (p.w = W_FIRST; p.w <= W_LAST; p.w++)
if (0 == strcmp(optarg, window_names[p.w])) break;
if (p.w > W_LAST) usage("-d invalid window type");
} break;
case 'n': {
result = strtoll(optarg, NULL, 10);
p.n = result;
p.m = 1 + p.n / 2;
n_set = 1;
} break;
case 'b': {
int num = sscanf(optarg, "%u,%u", &p.b0, &p.b1);
if (num != 2) usage("-b malformed");
b_set = 1;
} break;
case 'p': {
int num = sscanf(optarg, "%lf", &p.p);
if (num != 1) usage("-p invalid value");
if (p.p < 1.0 || p.p > 2.0) usage("-p out of limit");
} break;
case 'l': {
result = strtoll(optarg, NULL, 10);
p.l = result;
l_set = 1;
} break;
default: usage("invalid option(s) or missing argument(s)");
}
}
p.m = 1 + p.n / 2;
if (!b_set) p.b1 = p.m - 2;
p.b = 1 + p.b1 - p.b0;
if (!l_set) p.l = p.b;
if (p.n < p.s || p.n > x) usage("-n out of limit");
if (p.b0 > p.b1) usage("-b rule b0 <= b1 broken");
if (p.b1 > p.m - 1) usage("-b rule b1 <= m-1 broken");
if (p.l < 1 || p.l > p.b) usage("-l out of limit");
if (!(p.p == 1.0 || p.l != p.b)) usage("if b == l then only p == 1 allowed");
if (argc - optind > 3) usage("too many parameters");
if (argc - optind == 0) {
asa->fd_in = STDIN_FILENO;
y_info("stdin used as input");
}
else {
char *in = argv[optind + 0];
asa->fd_in = open(in, O_RDONLY);
if (asa->fd_in == -1) y_error("open input: %s", y_strerr);
y_dbg("'%s' opened readonly, fd %d", in, asa->fd_in);
}
if (argc - optind <= 1) {
asa->fd_out = STDOUT_FILENO;
y_info("stdout used as output");
}
else {
char *out = argv[optind + 1];
asa->fd_out = open(out, O_WRONLY|O_CREAT|O_APPEND, 0666);
if (asa->fd_out == -1) y_error("open output: %s", y_strerr);
y_dbg("'%s' opened writeonly append, fd %d", out, asa->fd_out);
}
y_info_o(Y_OUT_START, ""
"Running with these parameters: (f: sampling frequency)\n"
" w %-14s window function\n"
" s %6d number of samples in a sequence%s\n"
" r %6d number of sequences used per generated spectrum\n"
" d %6d distance between sequence starts; spectrums come at\n"
" f / r / d, for 44.1 kHz at %.3f Hz\n"
" n %6d fft input size; frequency resolution is f / n, for\n"
" 44.1 kHz it's %.3f Hz\n"
" m %6d fft output size\n"
" b %6d number of bins total (from %d to %d)\n"
" p %09.7f power distribution (p == 1 means linear)\n"
" l %6d number of lines with distribution from bins as:\n"
""
, window_names[p.w]
, p.s , p.n > p.s ? ", sequence zero-padded" : ""
, p.r, p.d
, 44100.0 / p.r / p.d
, p.n, 44100.0 / p.n
, p.m, p.b, p.b0, p.b1
, p.p, p.l
);
p.g = asa_distribute_bins(p.l, p.b, p.p);
char *indent = " ";
int wb = y_info_o(Y_OUT_CONT, "%s", indent);
for (int j = 0; j < p.l; j++) {
wb += y_info_o(Y_OUT_CONT, " %d", p.g[j]);
if (wb > 70) { wb = 0; y_info_o(Y_OUT_CONT, "\n%s", indent); }
}
y_info_o(Y_OUT_END, "");
y_assert(sum(p.g, p.l) == p.b);
asa->param = p;
}
static struct asa_struct_t static_asa = { 0 };
void exit_handler(void) {
y_dbg("cleaning up");
if (static_asa.s16le) free(static_asa.s16le);
asa_cleanup(&static_asa);
}
int main(int argc, char **argv) {
atexit(exit_handler);
y_set_log_level();
y_info("%s", COMPILE);
asa_t asa = &static_asa;
parse_args(argc, argv, asa);
asa_init_fft(asa);
asa_param_t p = asa->param;
asa->s16le = malloc(sizeof(int16_t) * p.s);
if (!asa->s16le) y_oom();
while (asa_read(asa)) {
asa_pad_and_window(asa);
asa_run_fft(asa);
asa_lines(asa);
asa_write(asa);
}
y_dbg("number of sequences read: %d", asa->num_in);
y_dbg("number of spectrums written: %d", asa->num_out);
}