-
Notifications
You must be signed in to change notification settings - Fork 0
/
fir.c
448 lines (415 loc) · 13.9 KB
/
fir.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
* This file is part of dsp.
*
* Copyright (c) 2014-2024 Michael Barbour <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <complex.h>
#include <fftw3.h>
#include "fir.h"
#include "util.h"
#include "codec.h"
#define MAX_DIRECT_LEN (1<<4)
struct fir_direct_state {
ssize_t len, mask, p, filter_frames, drain_frames;
sample_t **filter, **buf;
int has_output, is_draining;
};
struct fir_state {
ssize_t len, fr_len, p, filter_frames, drain_pos, drain_frames;
fftw_complex **filter_fr, *tmp_fr;
sample_t **input, **output, **overlap;
fftw_plan *r2c_plan, *c2r_plan;
int has_output, is_draining;
};
sample_t * fir_direct_effect_run(struct effect *e, ssize_t *frames, sample_t *ibuf, sample_t *obuf)
{
struct fir_direct_state *state = (struct fir_direct_state *) e->data;
ssize_t i, k;
for (i = 0; i < *frames; ++i) {
for (k = 0; k < e->istream.channels; ++k) {
sample_t s = (ibuf) ? ibuf[i*e->istream.channels + k] : 0.0;
if (state->buf[k]) {
for (ssize_t n = state->p, m = 0; m < state->len; ++m) {
state->buf[k][n] += s * state->filter[k][m];
n = (n+1) & state->mask;
}
obuf[i*e->ostream.channels + k] = state->buf[k][state->p];
state->buf[k][state->p] = 0.0;
}
else {
obuf[i*e->ostream.channels + k] = s;
}
}
state->p = (state->p+1) & state->mask;
}
if (*frames > 0)
state->has_output = 1;
return obuf;
}
void fir_direct_effect_reset(struct effect *e)
{
int i;
struct fir_direct_state *state = (struct fir_direct_state *) e->data;
state->p = 0;
for (i = 0; i < e->ostream.channels; ++i)
if (state->buf[i])
memset(state->buf[i], 0, state->len * sizeof(sample_t));
}
void fir_direct_effect_drain(struct effect *e, ssize_t *frames, sample_t *obuf)
{
struct fir_direct_state *state = (struct fir_direct_state *) e->data;
if (!state->has_output && state->p == 0)
*frames = -1;
else {
if (!state->is_draining) {
state->drain_frames = state->filter_frames;
state->is_draining = 1;
}
if (state->drain_frames > 0) {
*frames = MINIMUM(*frames, state->drain_frames);
state->drain_frames -= *frames;
e->run(e, frames, NULL, obuf);
}
else
*frames = -1;
}
}
void fir_direct_effect_destroy(struct effect *e)
{
struct fir_direct_state *state = (struct fir_direct_state *) e->data;
for (int i = 0; i < e->ostream.channels; ++i) {
if (state->buf[i]) {
free(state->filter[i]);
free(state->buf[i]);
break;
}
}
free(state->filter);
free(state->buf);
free(state);
}
sample_t * fir_effect_run(struct effect *e, ssize_t *frames, sample_t *ibuf, sample_t *obuf)
{
struct fir_state *state = (struct fir_state *) e->data;
ssize_t i, k, iframes = 0, oframes = 0;
while (iframes < *frames) {
while (state->p < state->len && iframes < *frames) {
for (i = 0; i < e->ostream.channels; ++i) {
#ifdef SYMMETRIC_IO
obuf[oframes * e->ostream.channels + i] = (state->has_output) ? state->output[i][state->p] : 0;
#else
if (state->has_output)
obuf[oframes * e->ostream.channels + i] = state->output[i][state->p];
#endif
if (state->input[i])
state->input[i][state->p] = (ibuf) ? ibuf[iframes * e->ostream.channels + i] : 0;
else
state->output[i][state->p] = (ibuf) ? ibuf[iframes * e->ostream.channels + i] : 0;
}
#ifdef SYMMETRIC_IO
++oframes;
#else
if (state->has_output)
++oframes;
#endif
++iframes;
++state->p;
}
if (state->p == state->len) {
for (i = 0; i < e->ostream.channels; ++i) {
if (state->input[i]) {
fftw_execute(state->r2c_plan[i]);
for (k = 0; k < state->fr_len; ++k)
state->tmp_fr[k] *= state->filter_fr[i][k];
fftw_execute(state->c2r_plan[i]);
for (k = 0; k < state->len * 2; ++k)
state->output[i][k] /= state->len * 2;
for (k = 0; k < state->len; ++k) {
state->output[i][k] += state->overlap[i][k];
state->overlap[i][k] = state->output[i][k + state->len];
}
}
}
state->p = 0;
state->has_output = 1;
}
}
*frames = oframes;
return obuf;
}
ssize_t fir_effect_delay(struct effect *e)
{
struct fir_state *state = (struct fir_state *) e->data;
return (state->has_output) ? state->len : state->p;
}
void fir_effect_reset(struct effect *e)
{
int i;
struct fir_state *state = (struct fir_state *) e->data;
state->p = 0;
state->has_output = 0;
for (i = 0; i < e->ostream.channels; ++i)
if (state->overlap[i])
memset(state->overlap[i], 0, state->len * sizeof(sample_t));
}
void fir_effect_drain(struct effect *e, ssize_t *frames, sample_t *obuf)
{
struct fir_state *state = (struct fir_state *) e->data;
if (!state->has_output && state->p == 0)
*frames = -1;
else {
if (!state->is_draining) {
state->drain_frames = state->filter_frames;
#ifdef SYMMETRIC_IO
state->drain_frames += state->len - state->p;
#else
if (state->has_output)
state->drain_frames += state->len - state->p;
#endif
state->drain_frames += state->p;
state->is_draining = 1;
}
if (state->drain_pos < state->drain_frames) {
fir_effect_run(e, frames, NULL, obuf);
state->drain_pos += *frames;
*frames -= (state->drain_pos > state->drain_frames) ? state->drain_pos - state->drain_frames : 0;
}
else
*frames = -1;
}
}
void fir_effect_destroy(struct effect *e)
{
int i;
struct fir_state *state = (struct fir_state *) e->data;
for (i = 0; i < e->ostream.channels; ++i) {
fftw_free(state->input[i]);
fftw_free(state->output[i]);
fftw_free(state->overlap[i]);
fftw_free(state->filter_fr[i]);
fftw_destroy_plan(state->r2c_plan[i]);
fftw_destroy_plan(state->c2r_plan[i]);
}
free(state->input);
free(state->output);
free(state->overlap);
free(state->filter_fr);
fftw_free(state->tmp_fr);
free(state->r2c_plan);
free(state->c2r_plan);
free(state);
}
struct effect * fir_effect_init_with_filter(const struct effect_info *ei, const struct stream_info *istream, const char *channel_selector, sample_t *filter_data, int filter_channels, ssize_t filter_frames, int force_direct)
{
int i, k, n_channels;
ssize_t j;
struct effect *e;
for (i = n_channels = 0; i < istream->channels; ++i)
if (GET_BIT(channel_selector, i))
++n_channels;
if (filter_channels != 1 && filter_channels != n_channels) {
LOG_FMT(LL_ERROR, "%s: error: channel mismatch: channels=%d filter_channels=%d", ei->name, n_channels, filter_channels);
return NULL;
}
if (filter_frames < 1) {
LOG_FMT(LL_ERROR, "%s: error: filter length must be >= 1", ei->name);
return NULL;
}
e = calloc(1, sizeof(struct effect));
e->name = ei->name;
e->istream.fs = e->ostream.fs = istream->fs;
e->istream.channels = e->ostream.channels = istream->channels;
if (filter_frames <= MAX_DIRECT_LEN || force_direct) {
e->run = fir_direct_effect_run;
e->reset = fir_direct_effect_reset;
e->drain = fir_direct_effect_drain;
e->destroy = fir_direct_effect_destroy;
struct fir_direct_state *state = calloc(1, sizeof(struct fir_direct_state));
e->data = state;
state->filter_frames = filter_frames;
state->len = 1;
while (state->len < filter_frames)
state->len <<= 1;
state->mask = state->len - 1;
LOG_FMT(LL_VERBOSE, "%s: info: filter_frames=%zd direct_len=%zd", ei->name, filter_frames, state->len);
state->filter = calloc(e->ostream.channels, sizeof(sample_t *));
state->buf = calloc(e->ostream.channels, sizeof(sample_t *));
sample_t *lbuf_filter = calloc(state->len * filter_channels, sizeof(sample_t));
sample_t *lbuf = calloc(state->len * n_channels, sizeof(sample_t));
if (filter_channels == 1)
memcpy(lbuf_filter, filter_data, filter_frames * sizeof(sample_t));
for (i = k = 0; i < e->ostream.channels; ++i) {
if (GET_BIT(channel_selector, i)) {
state->filter[i] = lbuf_filter;
state->buf[i] = lbuf;
if (filter_channels > 1) {
for (j = 0; j < filter_frames; ++j)
state->filter[i][j] = filter_data[j*filter_channels + k];
++k;
lbuf_filter += state->len;
}
lbuf += state->len;
}
}
}
else {
e->run = fir_effect_run;
e->delay = fir_effect_delay;
e->reset = fir_effect_reset;
e->drain = fir_effect_drain;
e->destroy = fir_effect_destroy;
fftw_plan filter_plan;
sample_t *filter;
struct fir_state *state = calloc(1, sizeof(struct fir_state));
e->data = state;
state->filter_frames = filter_frames;
state->len = next_fast_fftw_len(filter_frames);
LOG_FMT(LL_VERBOSE, "%s: info: filter_frames=%zd fft_len=%zd", ei->name, filter_frames, state->len);
state->fr_len = state->len + 1;
state->tmp_fr = fftw_malloc(state->fr_len * sizeof(fftw_complex));
state->input = calloc(e->ostream.channels, sizeof(sample_t *));
state->output = calloc(e->ostream.channels, sizeof(sample_t *));
state->overlap = calloc(e->ostream.channels, sizeof(sample_t *));
state->filter_fr = calloc(e->ostream.channels, sizeof(fftw_complex *));
state->r2c_plan = calloc(e->ostream.channels, sizeof(fftw_plan));
state->c2r_plan = calloc(e->ostream.channels, sizeof(fftw_plan));
filter = fftw_malloc(state->len * 2 * sizeof(sample_t));
memset(filter, 0, state->len * 2 * sizeof(sample_t));
filter_plan = fftw_plan_dft_r2c_1d(state->len * 2, filter, state->tmp_fr, FFTW_ESTIMATE);
if (filter_channels == 1) {
memcpy(filter, filter_data, filter_frames * sizeof(sample_t));
fftw_execute(filter_plan);
}
for (i = k = 0; i < e->ostream.channels; ++i) {
state->output[i] = fftw_malloc(state->len * 2 * sizeof(sample_t));
memset(state->output[i], 0, state->len * 2 * sizeof(sample_t));
if (GET_BIT(channel_selector, i)) {
state->input[i] = fftw_malloc(state->len * 2 * sizeof(sample_t));
memset(state->input[i], 0, state->len * 2 * sizeof(sample_t));
state->overlap[i] = fftw_malloc(state->len * sizeof(sample_t));
memset(state->overlap[i], 0, state->len * sizeof(sample_t));
state->filter_fr[i] = fftw_malloc(state->fr_len * sizeof(fftw_complex));
state->r2c_plan[i] = fftw_plan_dft_r2c_1d(state->len * 2, state->input[i], state->tmp_fr, FFTW_ESTIMATE);
state->c2r_plan[i] = fftw_plan_dft_c2r_1d(state->len * 2, state->tmp_fr, state->output[i], FFTW_ESTIMATE);
if (filter_channels == 1)
memcpy(state->filter_fr[i], state->tmp_fr, state->fr_len * sizeof(fftw_complex));
else {
for (j = 0; j < filter_frames; ++j)
filter[j] = filter_data[j * filter_channels + k];
fftw_execute(filter_plan);
memcpy(state->filter_fr[i], state->tmp_fr, state->fr_len * sizeof(fftw_complex));
++k;
}
}
}
fftw_destroy_plan(filter_plan);
fftw_free(filter);
}
return e;
}
sample_t * fir_read_filter(const struct effect_info *ei, const char *dir, const char *path, int fs, int *channels, ssize_t *frames)
{
static const char coefs_str_prefix[] = "coefs:";
static const char file_str_prefix[] = "file:";
sample_t *data = NULL;
if (strncmp(path, coefs_str_prefix, LENGTH(coefs_str_prefix)-1) == 0) {
char *endptr;
path += LENGTH(coefs_str_prefix)-1;
int filter_channels = 1;
ssize_t i = 1, filter_frames = 1;
for (const char *s = path; *s; ++s) {
if (*s == ',') ++i;
else if (*s == '/') {
++filter_channels;
if (i > filter_frames) filter_frames = i;
i = 1;
}
}
if (i > filter_frames) filter_frames = i;
sample_t *ch_data = data = calloc(filter_frames * filter_channels, sizeof(sample_t));
char *coefs_str = strdup(path);
char *ch = coefs_str;
while (*ch != '\0') {
char *next_ch = isolate(ch, '/');
char *coef = ch;
for (i = 0; *coef != '\0'; ++i) {
char *next_coef = isolate(coef, ',');
if (*coef != '\0') {
ch_data[filter_channels * i] = strtod(coef, &endptr);
if (check_endptr(ei->name, coef, endptr, "coefficient")) {
free(data);
free(coefs_str);
return NULL;
}
}
coef = next_coef;
}
ch_data += 1;
ch = next_ch;
}
free(coefs_str);
*channels = filter_channels;
*frames = filter_frames;
}
else {
if (strncmp(path, file_str_prefix, LENGTH(file_str_prefix)-1) == 0)
path += LENGTH(file_str_prefix)-1;
char *p = construct_full_path(dir, path);
struct codec *c = init_codec(p, NULL, NULL, fs, 1, CODEC_ENDIAN_DEFAULT, CODEC_MODE_READ);
if (c == NULL) {
LOG_FMT(LL_ERROR, "%s: error: failed to open filter file: %s", ei->name, p);
free(p);
return NULL;
}
free(p);
*channels = c->channels;
*frames = c->frames;
if (c->fs != fs) {
LOG_FMT(LL_ERROR, "%s: error: sample rate mismatch: fs=%d filter_fs=%d", ei->name, fs, c->fs);
destroy_codec(c);
return NULL;
}
data = calloc(c->frames * c->channels, sizeof(sample_t));
if (c->read(c, data, c->frames) != c->frames) {
LOG_FMT(LL_ERROR, "%s: error: short read", ei->name);
destroy_codec(c);
free(data);
return NULL;
}
destroy_codec(c);
}
return data;
}
struct effect * fir_effect_init(const struct effect_info *ei, const struct stream_info *istream, const char *channel_selector, const char *dir, int argc, const char *const *argv)
{
int filter_channels;
ssize_t filter_frames;
struct effect *e;
sample_t *filter_data;
if (argc != 2) {
LOG_FMT(LL_ERROR, "%s: usage: %s", argv[0], ei->usage);
return NULL;
}
filter_data = fir_read_filter(ei, dir, argv[1], istream->fs, &filter_channels, &filter_frames);
if (filter_data == NULL)
return NULL;
e = fir_effect_init_with_filter(ei, istream, channel_selector, filter_data, filter_channels, filter_frames, 0);
free(filter_data);
return e;
}