-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06_saturation.js
44 lines (39 loc) · 1.13 KB
/
06_saturation.js
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
config({ description: 'saturation', inChannels: 2, outChannels: 2 });
let gainIn;
let gainInDb;
let volume;
let volumeDb;
let algorithm;
slider(1, gainInDb, 0, 0, 36, 0.01, 'Gain In (dB)');
slider(2, volumeDb, 0, -18, 18, 0.01, 'Volume (dB)');
selectBox(
3,
algorithm,
'sigmoid',
[
{ name: 'sigmoid', label: 'Sigmoid' },
{ name: 'htan', label: 'Hyperbolic Tangent' },
{ name: 'hclip', label: 'Hard Clip' }
],
'Algorithm'
);
onSlider(() => {
gainIn = Math.pow(10, gainInDb / 20);
volume = Math.pow(10, volumeDb / 20);
});
onSample(() => {
eachChannel((sample, _channel) => {
if (algorithm === 'sigmoid') {
sample = 2 * (1 / (1 + exp(-gainIn * sample))) - 1;
} else if (algorithm === 'htan') {
sample =
(exp(2 * sample * gainIn) - 1) /
(exp(2 * sample * gainIn) + 1) /
((exp(2 * gainIn) - 1) / (exp(2 * gainIn) + 1));
} else if (algorithm === 'hclip') {
sample *= gainIn;
sample = abs(sample) > 0.5 ? 0.5 * sign(sample) : sample;
}
sample *= volume;
});
});