-
Notifications
You must be signed in to change notification settings - Fork 0
/
shaping_clipper.cpp
460 lines (407 loc) · 18.1 KB
/
shaping_clipper.cpp
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
#include "shaping_clipper.h"
#include <algorithm>
#include <complex>
#include <cmath>
shaping_clipper::shaping_clipper(int sample_rate, int fft_size, float clip_level, int max_oversample) {
this->sample_rate = sample_rate;
this->size = fft_size;
max_oversample = 1 << (int)log2(std::max(1, max_oversample));
this->max_oversample = max_oversample;
this->oversample = 1;
this->clip_level = clip_level;
this->iterations = 6;
this->adaptive_distortion_strength = 1.0;
this->overlap = fft_size / 4;
this->pffft = pffft_new_setup(fft_size, PFFFT_REAL);
this->pffft_oversampled = NULL;
// The psy masking calculation is O(n^2),
// so skip it for frequencies not covered by base sampling rantes (i.e. 44k)
if (sample_rate <= 50000) {
this->num_psy_bins = fft_size / 2;
} else if (sample_rate <= 100000) {
this->num_psy_bins = fft_size / 4;
} else {
this->num_psy_bins = fft_size / 8;
}
this->window.resize(fft_size * max_oversample);
this->inv_window.resize(fft_size * max_oversample);
generate_hann_window();
this->in_frame.resize(fft_size);
this->out_dist_frame.resize(fft_size);
this->margin_curve.resize(fft_size / 2 + 1);
// normally I use __builtin_ctz for this but the intrinsic is different for
// different compilers.
// 2 spread_table entries per octave.
int spread_table_rows = log2(this->num_psy_bins) * 2;
this->spread_table.resize(spread_table_rows * this->num_psy_bins);
this->spread_table_range.resize(spread_table_rows);
this->spread_table_index.resize(this->num_psy_bins);
// default curve
int points[][2] = { {0,14}, {125,14}, {250,16}, {500,18}, {1000,20}, {2000,20}, {4000,20}, {8000,17}, {16000,14}, {20000,-10} };
int num_points = 10;
set_margin_curve(points, num_points);
generate_spread_table();
}
shaping_clipper::~shaping_clipper() {
pffft_destroy_setup(this->pffft);
if (this->pffft_oversampled) {
pffft_destroy_setup(this->pffft_oversampled);
}
}
int shaping_clipper::get_feed_size() {
return this->overlap;
}
void shaping_clipper::set_clip_level(float clip_level) {
this->clip_level = clip_level;
}
void shaping_clipper::set_iterations(int iterations) {
this->iterations = iterations;
}
void shaping_clipper::set_adaptive_distortion_strength(float strength) {
this->adaptive_distortion_strength = strength;
}
void shaping_clipper::set_oversample(int oversample) {
oversample = std::max(1, std::min(this->max_oversample, oversample));
oversample = 1 << (int)log2(oversample);
if (oversample != this->oversample) {
if (this->pffft_oversampled) {
pffft_destroy_setup(this->pffft_oversampled);
this->pffft_oversampled = NULL;
}
if (oversample > 1) {
this->pffft_oversampled = pffft_new_setup(this->size * oversample, PFFFT_REAL);
}
}
this->oversample = oversample;
}
void shaping_clipper::feed(const float* in_samples, float* out_samples, bool diff_only, float* total_margin_shift) {
// shift in/out buffers
for (int i = 0; i < this->size - this->overlap; i++) {
this->in_frame[i] = this->in_frame[i + this->overlap];
this->out_dist_frame[i] = this->out_dist_frame[i + this->overlap];
}
for (int i = 0; i < this->overlap; i++) {
this->in_frame[i + this->size - this->overlap] = in_samples[i];
this->out_dist_frame[i + this->size - this->overlap] = 0;
}
float peak;
float* windowed_frame = (float*)alloca(sizeof(float) * this->size * this->oversample);
float* clipping_delta = (float*)alloca(sizeof(float) * this->size * this->oversample);
float* spectrum_buf = (float*)alloca(sizeof(float) * this->size * this->oversample);
float* mask_curve = (float*)alloca(sizeof(float) * (this->size / 2 + 1));
apply_window(this->in_frame.data(), windowed_frame);
pffft_transform_ordered(this->pffft, windowed_frame, spectrum_buf, NULL, PFFFT_FORWARD);
calculate_mask_curve(spectrum_buf, mask_curve);
int clipping_samples = this->size * this->oversample;
int window_stride = this->max_oversample / this->oversample;
PFFFT_Setup* clipping_pffft = this->pffft;
if (this->oversample > 1) {
clipping_pffft = this->pffft_oversampled;
// use IFFT to oversample the windowed frame
spectrum_buf[this->size] = spectrum_buf[1] / 2;
spectrum_buf[1] = 0;
for (int i = this->size + 1; i < this->size * this->oversample; i++) {
spectrum_buf[i] = 0;
}
pffft_transform_ordered(this->pffft_oversampled, spectrum_buf, windowed_frame, NULL, PFFFT_BACKWARD);
float fft_ifft_scale = this->size;
for (int i = 0; i < clipping_samples; i++) {
windowed_frame[i] /= fft_ifft_scale;
}
for (int i = 0; i < this->size / 2 + 1; i++) {
mask_curve[i] *= this->oversample;
}
}
float orig_peak = 0;
for (int sample_idx = 0, window_idx = 0; sample_idx < clipping_samples; sample_idx++, window_idx += window_stride) {
orig_peak = std::max<float>(orig_peak, std::abs(windowed_frame[sample_idx] * inv_window[window_idx]));
}
orig_peak /= this->clip_level;
peak = orig_peak;
// clear clipping_delta
for (int i = 0; i < clipping_samples; i++) {
clipping_delta[i] = 0;
}
if (total_margin_shift) {
*total_margin_shift = 1.0;
}
// repeat clipping-filtering process a few times to control both the peaks and the spectrum
for (int i = 0; i < this->iterations; i++) {
// The last 1/3 of rounds have boosted delta to help reach the peak target faster
float delta_boost = 1.0;
if (i >= this->iterations - this->iterations / 3) {
// boosting the delta when largs peaks are still present is dangerous
if (peak < 2.0) {
delta_boost = 2.0;
}
}
clip_to_window(windowed_frame, clipping_delta, delta_boost);
pffft_transform_ordered(clipping_pffft, clipping_delta, spectrum_buf, NULL, PFFFT_FORWARD);
if (this->oversample > 1) {
// Zero out all frequency bins above the base nyquist rate.
// limit_clip_spectrum doesn't handle these bins.
spectrum_buf[1] = 0;
for (int i = this->size + 1; i < this->size * this->oversample; i++) {
spectrum_buf[i] = 0;
}
}
limit_clip_spectrum(spectrum_buf, mask_curve);
pffft_transform_ordered(clipping_pffft, spectrum_buf, clipping_delta, NULL, PFFFT_BACKWARD);
// see pffft.h
for (int i = 0; i < clipping_samples; i++) {
clipping_delta[i] /= clipping_samples;
}
peak = 0;
for (int sample_idx = 0, window_idx = 0; sample_idx < clipping_samples; sample_idx++, window_idx += window_stride) {
peak = std::max<float>(peak, std::abs((windowed_frame[sample_idx] + clipping_delta[sample_idx]) * inv_window[window_idx]));
}
peak /= this->clip_level;
// Automatically adjust mask_curve as necessary to reach peak target
float mask_curve_shift = 1.122; // 1.122 is 1dB
if (orig_peak > 1.0 && peak > 1.0) {
float diff_achieved = orig_peak - peak;
if (i + 1 < this->iterations - this->iterations / 3 && diff_achieved > 0) {
float diff_needed = orig_peak - 1.0;
float diff_ratio = diff_needed / diff_achieved;
// If a good amount of peak reduction was already achieved,
// don't shift the mask_curve by the full peak value
// On the other hand, if only a little peak reduction was achieved,
// don't shift the mask_curve by the enormous diff_ratio.
diff_ratio = std::min<float>(diff_ratio, peak);
mask_curve_shift = std::max<float>(mask_curve_shift, diff_ratio);
} else {
// If the peak got higher than the input or we are in the last 1/3 rounds,
// go back to the heavy-handed peak heuristic.
mask_curve_shift = std::max<float>(mask_curve_shift, peak);
}
}
mask_curve_shift = 1.0 + (mask_curve_shift - 1.0) * this->adaptive_distortion_strength;
if (total_margin_shift && peak > 1.01 && i < this->iterations - 1) {
*total_margin_shift *= mask_curve_shift;
}
// Be less strict in the next iteration.
// This helps with peak control.
for (int i = 0; i < this->size / 2 + 1; i++) {
mask_curve[i] *= mask_curve_shift;
}
}
if (this->oversample > 1) {
// Downsample back to original rate.
// We already zeroed out all frequency bins above the base nyquist rate so we can simply drop samples here.
for (int i = 0; i < this->size; i++) {
clipping_delta[i] = clipping_delta[i * this->oversample];
}
}
// do overlap & add
apply_window(clipping_delta, this->out_dist_frame.data(), true);
for (int i = 0; i < this->overlap; i++) {
// 4 times overlap with squared hanning window results in 1.5 time increase in amplitude
out_samples[i] = this->out_dist_frame[i] / 1.5;
}
if (this->oversample > 1) {
// When oversampling is active, the clipping delta can be non-zero even if all the samples in the frame are well below the threshold.
// This is likely due to the aliasing introduced when windowing near-nyquist frequencies.
// For purity, detect and remove these unwanted changes to the input signal.
// The effect on peak control is < 0.1% or -60dB. Oversampling is not exact anyway.
float max_out_diff = 0;
for (int i = 0; i < this->overlap; i++) {
max_out_diff = std::max(max_out_diff, std::abs(out_samples[i]));
}
if (max_out_diff < this->clip_level * 0.001) {
for (int i = 0; i < this->overlap; i++) {
out_samples[i] = 0;
}
}
}
if (!diff_only) {
for (int i = 0; i < this->overlap; i++) {
out_samples[i] += this->in_frame[i];
}
}
}
void shaping_clipper::generate_hann_window() {
double pi = acos(-1);
int window_size = this->size * this->max_oversample;
for (int i = 0; i < window_size; i++) {
float value = 0.5 * (1 - cos(2 * pi * i / window_size));
this->window[i] = value;
// 1/window to calculate unwindowed peak.
this->inv_window[i] = value > 0.1 ? 1.0 / value : 0;
}
}
void shaping_clipper::generate_spread_table() {
// Calculate tent-shape function in log-log scale.
// As an optimization, only consider bins close to "bin"
// This reduces the number of multiplications needed in calculate_mask_curve
// The masking contribution at faraway bins is negligeable
// Another optimization to save memory and speed up the calculation of the
// spread table is to calculate and store only 2 spread functions per
// octave, and reuse the same spread function for multiple bins.
int table_index = 0;
int bin = 0;
int increment = 1;
while (bin < this->num_psy_bins) {
float sum = 0;
int base_idx = table_index * this->num_psy_bins;
int start_bin = bin * 3 / 4;
int end_bin = std::min(this->num_psy_bins, ((bin + 1) * 4 + 2) / 3);
for (int j = start_bin; j < end_bin; j++) {
// add 0.5 so i=0 doesn't get log(0)
float rel_idx_log = std::abs(log((j + 0.5) / (bin + 0.5)));
float value;
if (j >= bin) {
// mask up
value = exp(-rel_idx_log * 40);
} else {
// mask down
value = exp(-rel_idx_log * 80);
}
// the spreading function is centred in the row
sum += value;
this->spread_table[base_idx + this->num_psy_bins / 2 + j - bin] = value;
}
// now normalize it
for (int j = start_bin; j < end_bin; j++) {
this->spread_table[base_idx + this->num_psy_bins / 2 + j - bin] /= sum;
}
this->spread_table_range[table_index] = std::make_pair(start_bin - bin, end_bin - bin);
int next_bin;
if (bin <= 1) {
next_bin = bin + 1;
} else {
if ((bin & (bin - 1)) == 0) {
// power of 2
increment = bin / 2;
}
next_bin = bin + increment;
}
// set bins between "bin" and "next_bin" to use this table_index
for (int i = bin; i < next_bin; i++) {
this->spread_table_index[i] = table_index;
}
bin = next_bin;
table_index++;
}
}
void shaping_clipper::set_margin_curve(int points[][2], int num_points) {
this->margin_curve[0] = points[0][1];
int j = 0;
for (int i = 0; i < num_points - 1; i++) {
while (j < this->size / 2 + 1 && j * this->sample_rate / this->size < points[i + 1][0]) {
// linearly interpolate between points
int binHz = j * this->sample_rate / this->size;
margin_curve[j] = points[i][1] + (binHz - points[i][0]) * (points[i + 1][1] - points[i][1]) / (points[i + 1][0] - points[i][0]);
j++;
}
}
// handle bins after the last point
while (j < this->size / 2 + 1) {
margin_curve[j] = points[num_points - 1][1];
j++;
}
// convert margin curve to linear amplitude scale
for (j = 0; j < this->size / 2 + 1; j++) {
margin_curve[j] = pow(10, margin_curve[j] / 20);
}
}
void shaping_clipper::apply_window(const float* in_frame, float* out_frame, const bool add_to_out_frame) {
const float* window = this->window.data();
int total_samples = this->size;
int window_stride = this->max_oversample;
for (int i = 0; i < total_samples; i++) {
if (add_to_out_frame) {
out_frame[i] += in_frame[i] * *window;
} else {
out_frame[i] = in_frame[i] * *window;
}
window += window_stride;
}
}
void shaping_clipper::clip_to_window(const float* windowed_frame, float* clipping_delta, float delta_boost) {
const float* window = this->window.data();
int total_samples = this->size * this->oversample;
int window_stride = this->max_oversample / this->oversample;
for (int i = 0; i < total_samples; i++) {
float limit = this->clip_level * *window;
float effective_value = windowed_frame[i] + clipping_delta[i];
if (effective_value > limit) {
clipping_delta[i] += (limit - effective_value) * delta_boost;
} else if (effective_value < -limit) {
clipping_delta[i] += (-limit - effective_value) * delta_boost;
}
window += window_stride;
}
}
void shaping_clipper::calculate_mask_curve(const float* spectrum, float* mask_curve) {
for (int i = 0; i < this->size / 2 + 1; i++) {
mask_curve[i] = 0;
}
for (int i = 0; i < this->num_psy_bins; i++) {
float magnitude;
if (i == 0) {
magnitude = std::abs(spectrum[0]);
} else if (i == this->size / 2) {
magnitude = std::abs(spectrum[1]);
} else {
// although the negative frequencies are omitted because they are redundant,
// the magnitude of the positive frequencies are not doubled.
// Multiply the magnitude by 2 to simulate adding up the + and - frequencies.
magnitude = abs(std::complex<float>(spectrum[2 * i], spectrum[2 * i + 1])) * 2;
}
int table_idx = this->spread_table_index[i];
std::pair<int, int> range = this->spread_table_range[table_idx];
int base_idx = table_idx * this->num_psy_bins;
int start_bin = std::max(0, i + range.first);
int end_bin = std::min(this->num_psy_bins, i + range.second);
for (int j = start_bin; j < end_bin; j++) {
mask_curve[j] += this->spread_table[base_idx + this->num_psy_bins / 2 + j - i] * magnitude;
}
}
// for ultrasonic frequencies, skip the O(n^2) spread calculation and just copy the magnitude
for (int i = this->num_psy_bins; i < this->size / 2 + 1; i++) {
float magnitude;
if (i == this->size / 2) {
magnitude = std::abs(spectrum[1]);
} else {
// although the negative frequencies are omitted because they are redundant,
// the magnitude of the positive frequencies are not doubled.
// Multiply the magnitude by 2 to simulate adding up the + and - frequencies.
magnitude = abs(std::complex<float>(spectrum[2 * i], spectrum[2 * i + 1])) * 2;
}
mask_curve[i] = magnitude;
}
for (int i = 0; i < this->size / 2 + 1; i++) {
mask_curve[i] = mask_curve[i] / this->margin_curve[i];
}
}
void shaping_clipper::limit_clip_spectrum(float* clip_spectrum, const float* mask_curve) {
// bin 0
float relative_distortion_level = std::abs(clip_spectrum[0]) / mask_curve[0];
if (relative_distortion_level > 1.0) {
clip_spectrum[0] /= relative_distortion_level;
}
// bin 1..N/2-1
// When oversampling is on, the base nyquist bin is handled in this loop
int i = 1;
int bins_below_nyquist = std::min(this->size * this->oversample / 2, this->size / 2 + 1);
for (; i < bins_below_nyquist; i++) {
float real = clip_spectrum[i * 2];
float imag = clip_spectrum[i * 2 + 1];
// although the negative frequencies are omitted because they are redundant,
// the magnitude of the positive frequencies are not doubled.
// Multiply the magnitude by 2 to simulate adding up the + and - frequencies.
relative_distortion_level = abs(std::complex<float>(real, imag)) * 2 / mask_curve[i];
if (relative_distortion_level > 1.0) {
clip_spectrum[i * 2] /= relative_distortion_level;
clip_spectrum[i * 2 + 1] /= relative_distortion_level;
}
}
// bin N/2
// When oversampling is off, the base nyquist bins needs to be handled here.
if (i == this->size / 2) {
relative_distortion_level = std::abs(clip_spectrum[1]) / mask_curve[this->size / 2];
if (relative_distortion_level > 1.0) {
clip_spectrum[1] /= relative_distortion_level;
}
}
}