-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDucker.java
More file actions
86 lines (79 loc) · 2.03 KB
/
Ducker.java
File metadata and controls
86 lines (79 loc) · 2.03 KB
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
import beads.*;
import java.lang.Math.*;
class Ducker extends Function {
private float[] buffer;
private int bufferIndex;
private float gain;
private float targetGain;
private int delaySinceClip;
private int attack;
private int sustain;
private float decay;
// Create a Ducker with a given target maximum value
public Ducker(UGen input) {
super(input);
this.gain = 1;
this.targetGain = 1;
this.delaySinceClip = 0;
this.attack = 1024;
this.sustain = 8192;
this.decay = 0.5f / context.getSampleRate();
this.buffer = new float[attack];
}
public float calculate() {
// Gather input
float input = processInput();
// Detect clipping, and set target gain accordingly.
if (input * targetGain > 1.0) {
this.targetGain = 1.0f / input;
this.delaySinceClip = 0;
}
// Modify real gain towards or away from target gain.
if (gain != targetGain) {
// Gain needs to be modified
if (gain > targetGain) {
// Use attack velocity
float velocity = (targetGain - 1) / attack; // Should be negative
if((gain+velocity) < targetGain){
gain = targetGain;
}else{
gain += velocity;
}
}else{
// Use decay velocity
if(gain+decay > targetGain){
gain = targetGain;
}else{
gain += decay;
}
}
} else {
if(delaySinceClip == attack+sustain){
targetGain = 1;
}
}
// Apply delay
float sample = this.buffer[bufferIndex];
this.buffer[bufferIndex] = input;
// Iterate buffer index
this.bufferIndex ++;
if (this.bufferIndex == buffer.length) {
this.bufferIndex = 0;
}
delaySinceClip++;
// Return delayed sample modified by undelayed gain.
return sample * gain;
}
// Compile all inputs into a single channel
private float processInput() {
if (x.length == 1) {
return x[0];
} else {
float sum = 0;
for (float f : x) {
sum += f;
}
return sum;
}
}
}