-
Notifications
You must be signed in to change notification settings - Fork 0
/
px_equalizer.h
461 lines (391 loc) · 15.3 KB
/
px_equalizer.h
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
449
450
451
452
453
454
455
456
457
458
459
460
461
#include "px_biquad.h"
#include "px_vector.h"
#include "px_globals.h"
#ifndef PX_EQUALIZER_H
#define PX_EQUALIZER_H
#define MAX_BANDS 24
typedef struct px_mono_equalizer
{
px_vector filter_bank; // type-generic vector filled with px_mono_biquad filters
float sample_rate;
int num_bands;
} px_mono_equalizer;
typedef struct px_stereo_equalizer
{
px_mono_equalizer left;
px_mono_equalizer right;
} px_stereo_equalizer;
typedef struct px_ms_equalizer
{
px_mono_equalizer mid;
px_mono_equalizer side;
} px_ms_equalizer;
// ----------------------------------------------------------------------------------------------------
#ifdef PX_USE_GENERIC
#define px_equalizer_process(a,b,...) _Generic((a), \
px_mono_equalizer*: px_equalizer_mono_process, \
px_stereo_equalizer*: px_equalizer_stereo_process, \
px_ms_equalizer*: px_equalizer_ms_process) \
(a,b,__VA_ARGS__) \
#define px_equalizer_initialize(a,b) _Generic((a), \
px_mono_equalizer*: px_equalizer_mono_initialize, \
px_stereo_equalizer*: px_equalizer_stereo_initialize, \
px_ms_equalizer*: px_equalizer_ms_initialize) \
(a,b)
#define px_equalizer_add_band(a,b,c,d,e) _Generic((a), \
px_mono_equalizer*: px_equalizer_mono_add_band, \
px_stereo_equalizer*: px_equalizer_stereo_add_band, \
px_ms_equalizer*: px_equalizer_ms_add_band) \
(a,b,c,d,e) \
#define px_equalizer_remove_band(a,b) _Generic((a), \
px_mono_equalizer*: px_equalizer_mono_remove_band, \
px_stereo_equalizer*: px_equalizer_stereo_remove_band, \
px_ms_equalizer*: px_equalizer_ms_remove_band) \
(a,b)
#define px_equalizer_set_frequency(a,b,c,...) _Generic((a), \
px_mono_equalizer*: px_equalizer_mono_set_frequency, \
px_stereo_equalizer*: px_equalizer_stereo_set_frequency, \
px_ms_equalizer*: px_equalizer_ms_set_frequency) \
(a,b,c,__VA_ARGS__)
#define px_equalizer_set_quality(a,b,c,...) _Generic((a), \
px_mono_equalizer*: px_equalizer_mono_set_quality, \
px_stereo_equalizer*: px_equalizer_stereo_set_quality, \
px_ms_equalizer*: px_equalizer_ms_set_quality) \
(a,b,c,__VA_ARGS__)
#define px_equalizer_set_gain(a,b,c,...) _Generic((a), \
px_mono_equalizer*: px_equalizer_mono_set_gain, \
px_stereo_equalizer*: px_equalizer_stereo_set_gain, \
px_ms_equalizer*: px_equalizer_ms_set_quality) \
(a,b,c,__VA_ARGS__)
#define px_equalizer_set_type(a,b,c,...) _Generic((a), \
px_mono_equalizer*: px_equalizer_mono_set_type, \
px_stereo_equalizer*: px_equalizer_stereo_set_type, \
px_ms_equalizer*: px_equalizer_ms_set_type) \
(a,b,c,__VA_ARGS__)
#endif
// mono
static void px_equalizer_mono_process(px_mono_equalizer* equalizer, float* input);
static void px_equalizer_mono_initialize(px_mono_equalizer* equalizer, float sample_rate);
static void px_equalizer_mono_add_band(px_mono_equalizer* equalizer, float frequency, float quality, float gain, BIQUAD_FILTER_TYPE type);
static void px_equalizer_mono_remove_band(px_mono_equalizer* equalizer, size_t index);
static void px_equalizer_mono_set_frequency(px_mono_equalizer* equalizer, size_t index, float in_frequency);
static void px_equalizer_mono_set_quality(px_mono_equalizer* equalizer, size_t index, float in_quality);
static void px_equalizer_mono_set_gain(px_mono_equalizer* equalizer, size_t index, float in_gain);
static void px_equalizer_mono_set_type(px_mono_equalizer* equalizer, size_t index, BIQUAD_FILTER_TYPE in_type);
// stereo
static void px_equalizer_stereo_process(px_stereo_equalizer* stereo_equalizer, float* input_left, float* input_right);
static void px_equalizer_stereo_initialize(px_stereo_equalizer* stereo_equalizer, float sample_rate);
static void px_equalizer_stereo_add_band(px_stereo_equalizer* stereo_equalizer, float frequency, float quality, float gain, BIQUAD_FILTER_TYPE type);
static void px_equalizer_stereo_remove_band(px_stereo_equalizer* stereo_equalizer, size_t index);
static void px_equalizer_stereo_set_frequency(px_stereo_equalizer* stereo_equalizer, size_t index, float in_frequency, CHANNEL_FLAG channel);
static void px_equalizer_stereo_set_quality(px_stereo_equalizer* stereo_equalizer, size_t index, float in_quality, CHANNEL_FLAG channel);
static void px_equalizer_stereo_set_gain(px_stereo_equalizer* stereo_equalizer, size_t index, float in_gain, CHANNEL_FLAG channel);
static void px_equalizer_stereo_set_type(px_stereo_equalizer* stereo_equalizer, size_t index, BIQUAD_FILTER_TYPE in_type, CHANNEL_FLAG channel);
// mid/side
static void px_equalizer_ms_process(px_ms_equalizer* ms_equalizer, float* input_left, float* input_right);
static px_ms_encoded px_equalizer_ms_process_and_return(px_ms_equalizer* ms_equalizer, float input_left, float input_right);
static void px_equalizer_ms_initialize(px_ms_equalizer* ms_equalizer, float sample_rate);
static void px_equalizer_ms_add_band(px_ms_equalizer* ms_equalizer, float frequency, float quality, float gain, BIQUAD_FILTER_TYPE type);
static void px_equalizer_ms_remove_band(px_ms_equalizer* ms_equalizer, size_t index);
static void px_equalizer_ms_set_frequency(px_ms_equalizer* ms_equalizer, size_t index, float in_frequency, CHANNEL_FLAG channel);
static void px_equalizer_ms_set_quality(px_ms_equalizer* ms_equalizer, size_t index, float in_quality, CHANNEL_FLAG channel);
static void px_equalizer_ms_set_gain(px_ms_equalizer* ms_equalizer, size_t index, float in_gain, CHANNEL_FLAG channel);
static void px_equalizer_ms_set_type(px_ms_equalizer* ms_equalizer, size_t index, BIQUAD_FILTER_TYPE in_type, CHANNEL_FLAG channel);
// ----------------------------------------------------------------------------------------------------
static void px_equalizer_mono_process(px_mono_equalizer* equalizer, float* input)
{
px_assert(equalizer, input);
for (int i = 0; i < equalizer->num_bands; ++i)
{
px_biquad_process((px_biquad*)px_vector_get(&equalizer->filter_bank, i), input);
}
}
static void px_equalizer_stereo_process(px_stereo_equalizer* stereo_equalizer, float* input_left, float* input_right)
{
px_assert(stereo_equalizer, input_left, input_right);
px_equalizer_mono_process(&stereo_equalizer->left, input_left);
px_equalizer_mono_process(&stereo_equalizer->right, input_right);
}
static void px_equalizer_ms_process(px_ms_equalizer* ms_equalizer, float* input_left, float* input_right)
{
px_assert(ms_equalizer, input_left, input_right);
px_ms_decoded decoded = { 0.f, 0.f };
decoded.left = *input_left;
decoded.right = *input_right;
px_ms_encoded encoded = px_ms_encode(decoded);
px_equalizer_mono_process(&ms_equalizer->mid, &encoded.mid);
px_equalizer_mono_process(&ms_equalizer->side, &encoded.side);
decoded = px_ms_decode(encoded);
*input_left = decoded.left;
*input_right = decoded.right;
}
static px_ms_encoded px_equalizer_ms_process_and_return(px_ms_equalizer* ms_equalizer, float input_left, float input_right)
{
assert(ms_equalizer);
px_ms_decoded decoded = { 0.f, 0.f };
decoded.left = input_left;
decoded.right = input_right;
px_ms_encoded encoded = px_ms_encode(decoded);
px_equalizer_mono_process(&ms_equalizer->mid, &encoded.mid);
px_equalizer_mono_process(&ms_equalizer->side, &encoded.side);
return encoded;
}
static void px_equalizer_mono_initialize(px_mono_equalizer* equalizer, float sample_rate)
{
assert(equalizer);
equalizer->sample_rate = sample_rate;
equalizer->num_bands = 0;
px_vector_initialize(&equalizer->filter_bank);
}
static void px_equalizer_stereo_initialize(px_stereo_equalizer* stereo_equalizer, float sample_rate)
{
assert(stereo_equalizer);
px_equalizer_mono_initialize(&stereo_equalizer->left, sample_rate);
px_equalizer_mono_initialize(&stereo_equalizer->right, sample_rate);
}
static void px_equalizer_ms_initialize(px_ms_equalizer* ms_equalizer, float sample_rate)
{
assert(ms_equalizer);
px_equalizer_mono_initialize(&ms_equalizer->mid, sample_rate);
px_equalizer_mono_initialize(&ms_equalizer->side, sample_rate);
}
static void px_equalizer_mono_add_band(px_mono_equalizer* equalizer, float frequency, float quality, float gain, BIQUAD_FILTER_TYPE type)
{
assert(equalizer);
px_biquad* new_filter = px_biquad_create(equalizer->sample_rate, type);
px_biquad_set_frequency(new_filter, frequency);
px_biquad_set_quality(new_filter, quality);
px_biquad_set_gain(new_filter, gain);
px_vector_push(&equalizer->filter_bank, new_filter);
equalizer->num_bands++;
}
static void px_equalizer_stereo_add_band(px_stereo_equalizer* stereo_equalizer, float frequency, float quality, float gain, BIQUAD_FILTER_TYPE type)
{
assert(stereo_equalizer);
px_equalizer_mono_add_band(&stereo_equalizer->left, frequency, quality, gain, type);
px_equalizer_mono_add_band(&stereo_equalizer->right, frequency, quality, gain, type);
}
static void px_equalizer_ms_add_band(px_ms_equalizer* ms_equalizer, float frequency, float quality, float gain, BIQUAD_FILTER_TYPE type)
{
assert(ms_equalizer);
px_equalizer_mono_add_band(&ms_equalizer->mid, frequency, quality, gain, type);
px_equalizer_mono_add_band(&ms_equalizer->side, frequency, quality, gain, type);
}
static void px_equalizer_mono_remove_band(px_mono_equalizer* equalizer, size_t index)
{
if (index < equalizer->num_bands)
{
px_biquad_destroy((px_biquad*)px_vector_get(&equalizer->filter_bank, index));
px_vector_remove(&equalizer->filter_bank, index);
equalizer->num_bands--;
}
}
static void px_equalizer_stereo_remove_band(px_stereo_equalizer* stereo_equalizer, size_t index)
{
assert(stereo_equalizer);
px_equalizer_mono_remove_band(&stereo_equalizer->left, index);
px_equalizer_mono_remove_band(&stereo_equalizer->right, index);
}
static void px_equalizer_ms_remove_band(px_ms_equalizer* ms_equalizer, size_t index)
{
assert(ms_equalizer);
px_equalizer_mono_remove_band(&ms_equalizer->mid, index);
px_equalizer_mono_remove_band(&ms_equalizer->side, index);
}
static void px_equalizer_mono_set_frequency(px_mono_equalizer* equalizer, size_t index, float in_frequency)
{
assert(equalizer);
if (index < equalizer->num_bands)
{
px_biquad* filter = (px_biquad*)px_vector_get(&equalizer->filter_bank, index);
px_biquad_set_frequency(filter, in_frequency);
}
}
static void px_equalizer_stereo_set_frequency(px_stereo_equalizer* stereo_equalizer, size_t index, float in_frequency, CHANNEL_FLAG channel)
{
assert(stereo_equalizer);
switch (channel)
{
case BOTH:
{
px_equalizer_mono_set_frequency(&stereo_equalizer->left, index, in_frequency);
px_equalizer_mono_set_frequency(&stereo_equalizer->right, index, in_frequency);
}
case LEFT:
{
px_equalizer_mono_set_frequency(&stereo_equalizer->left, index, in_frequency);
}
case RIGHT:
{
px_equalizer_mono_set_frequency(&stereo_equalizer->right, index, in_frequency);
}
}
}
static void px_equalizer_ms_set_frequency(px_ms_equalizer* ms_equalizer, size_t index, float in_frequency, CHANNEL_FLAG channel)
{
assert(ms_equalizer);
switch (channel)
{
case BOTH:
{
px_equalizer_mono_set_frequency(&ms_equalizer->mid, index, in_frequency);
px_equalizer_mono_set_frequency(&ms_equalizer->side, index, in_frequency);
}
case LEFT:
{
px_equalizer_mono_set_frequency(&ms_equalizer->mid, index, in_frequency);
}
case RIGHT:
{
px_equalizer_mono_set_frequency(&ms_equalizer->side, index, in_frequency);
}
}
}
static void px_equalizer_mono_set_quality(px_mono_equalizer* equalizer, size_t index, float in_quality)
{
assert(equalizer);
if (index < equalizer->num_bands)
{
px_biquad* filter = (px_biquad*)px_vector_get(&equalizer->filter_bank, index);
px_biquad_set_quality(filter, in_quality);
}
}
static void px_equalizer_stereo_set_quality(px_stereo_equalizer* stereo_equalizer, size_t index, float in_quality, CHANNEL_FLAG channel)
{
assert(stereo_equalizer);
switch (channel)
{
case BOTH:
{
px_equalizer_mono_set_quality(&stereo_equalizer->left, index, in_quality);
px_equalizer_mono_set_quality(&stereo_equalizer->right, index, in_quality);
}
case LEFT:
{
px_equalizer_mono_set_quality(&stereo_equalizer->left, index, in_quality);
}
case RIGHT:
{
px_equalizer_mono_set_quality(&stereo_equalizer->right, index, in_quality);
}
}
}
static void px_equalizer_ms_set_quality(px_ms_equalizer* ms_equalizer, size_t index, float in_quality, CHANNEL_FLAG channel)
{
assert(ms_equalizer);
switch (channel)
{
case BOTH:
{
px_equalizer_mono_set_quality(&ms_equalizer->mid, index, in_quality);
px_equalizer_mono_set_quality(&ms_equalizer->side, index, in_quality);
}
case MID:
{
px_equalizer_mono_set_quality(&ms_equalizer->mid, index, in_quality);
}
case SIDE:
{
px_equalizer_mono_set_quality(&ms_equalizer->side, index, in_quality);
}
}
}
static void px_equalizer_mono_set_gain(px_mono_equalizer* equalizer, size_t index, float in_gain)
{
assert(equalizer);
if (index < equalizer->num_bands)
{
px_biquad* filter = (px_biquad*)px_vector_get(&equalizer->filter_bank, index);
px_biquad_set_gain(filter, in_gain);
}
}
static void px_equalizer_stereo_set_gain(px_stereo_equalizer* stereo_equalizer, size_t index, float in_gain, CHANNEL_FLAG channel)
{
assert(stereo_equalizer);
switch (channel)
{
case BOTH:
{
px_equalizer_mono_set_gain(&stereo_equalizer->left, index, in_gain);
px_equalizer_mono_set_gain(&stereo_equalizer->right, index, in_gain);
}
case LEFT:
{
px_equalizer_mono_set_gain(&stereo_equalizer->left, index, in_gain);
}
case RIGHT:
{
px_equalizer_mono_set_gain(&stereo_equalizer->right, index, in_gain);
}
}
}
static void px_equalizer_ms_set_gain(px_ms_equalizer* ms_equalizer, size_t index, float in_gain, CHANNEL_FLAG channel)
{
assert(ms_equalizer);
switch (channel)
{
case BOTH:
{
px_equalizer_mono_set_gain(&ms_equalizer->mid, index, in_gain);
px_equalizer_mono_set_gain(&ms_equalizer->side, index, in_gain);
}
case MID:
{
px_equalizer_mono_set_gain(&ms_equalizer->mid, index, in_gain);
}
case SIDE:
{
px_equalizer_mono_set_gain(&ms_equalizer->side, index, in_gain);
}
}
}
static void px_equalizer_mono_set_type(px_mono_equalizer* equalizer, size_t index, BIQUAD_FILTER_TYPE in_type)
{
assert(equalizer);
if (index < equalizer->num_bands)
{
px_biquad* filter = (px_biquad*)px_vector_get(&equalizer->filter_bank, index);
px_biquad_set_type(filter, in_type);
}
}
static void px_equalizer_stereo_set_type(px_stereo_equalizer* stereo_equalizer, size_t index, BIQUAD_FILTER_TYPE in_type, CHANNEL_FLAG channel)
{
assert(stereo_equalizer);
switch (channel)
{
case BOTH:
{
px_equalizer_mono_set_type(&stereo_equalizer->left, index, in_type);
px_equalizer_mono_set_type(&stereo_equalizer->right, index, in_type);
}
case LEFT:
{
px_equalizer_mono_set_type(&stereo_equalizer->left, index, in_type);
}
case RIGHT:
{
px_equalizer_mono_set_type(&stereo_equalizer->right, index, in_type);
}
}
}
static void px_equalizer_ms_set_type(px_ms_equalizer* ms_equalizer, size_t index, BIQUAD_FILTER_TYPE in_type, CHANNEL_FLAG channel)
{
assert(ms_equalizer);
switch (channel)
{
case BOTH:
{
px_equalizer_mono_set_type(&ms_equalizer->mid, index, in_type);
px_equalizer_mono_set_type(&ms_equalizer->side, index, in_type);
}
case MID:
{
px_equalizer_mono_set_type(&ms_equalizer->mid, index, in_type);
}
case SIDE:
{
px_equalizer_mono_set_type(&ms_equalizer->side, index, in_type);
}
}
}
#endif