-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEarlyReflections.java
More file actions
245 lines (206 loc) · 6.36 KB
/
EarlyReflections.java
File metadata and controls
245 lines (206 loc) · 6.36 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
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
import beads.*;
import java.lang.Math.*;
import java.util.ArrayList;
import java.util.Random;
// Able to handle any room up to the size of a football field.
class EarlyReflections extends Function {
private static final double SOUNDSPD = 1125.33;
private static final int BUFFERSIZE = 16384;
// Pseudo-constants
private final double samplesPerFoot;
// Buffer variables
private float[] buffer;
private int bufferIndex;
// Delay arrays
private int[] delays;
private int[] delays2;
private float refDelay;
private float transition;
// Exporting for modular reverb
ArrayList<Integer> resonances;
public float[] reverbWindow;
// Room statistics
private float l, w, h;
private float goalL, goalW, goalH;
private float echoFactor;
BiquadComponent attenuation;
// For transitioning between room sizes
private float reverbVelocity;
private float velocity;
private boolean aToB;
// Stereo width stuff
private float fbJitter;
private float lrJitter;
// Feedback
private float prevOut;
public EarlyReflections(UGen input, int l, int w, int h, float stereoWidth, BiquadComponent attenuation) {
super(input);
this.buffer = new float[BUFFERSIZE];
this.delays = new int[6];
this.delays2 = new int[6];
// Quasi-constants
this.reverbVelocity = 0.005f;
this.samplesPerFoot = SOUNDSPD / context.getSampleRate();
this.velocity = 40 / context.getSampleRate(); // Set velocity to match 1/40th of a second.
this.refDelay = (float)(100 / samplesPerFoot);
this.l = l;
this.w = w;
this.h = h;
this.goalL = l;
this.goalW = w;
this.goalH = h;
this.echoFactor = 1.0f;
this.resonances = new ArrayList<Integer>();
this.reverbWindow = new float[2];
this.attenuation = attenuation.register(this);
Random r = new Random();
this.fbJitter = (float)((r.nextFloat() - 0.5) * stereoWidth * 2 + 0.5);
this.lrJitter = (float)((r.nextFloat() - 0.5) * stereoWidth * 2 + 0.5);
this.aToB = true;
initDelays();
this.aToB = false;
initDelays();
}
public float calculate() {
// Buffer inputt
float in = processInput();
buffer[bufferIndex] = in;
// Sample both echo tracks
float sample = 0;
float sample2 = 0;
// Determine if fading between delay sequences
if (transition < 1) {
for (int delay : delays) {
sample += get(-delay) / Math.pow((refDelay + delay) / refDelay, 2);
}
}
if (transition > 0) {
for (int delay : delays2) {
sample2 += get(-delay) / Math.pow((refDelay + delay) / refDelay, 2);
}
}
if(transition < 1 && transition > 0){
// Transitioning. Change the mix.
if(aToB){
transition += velocity;
if(transition > 1){
transition = 1;
}
}else{
transition -= velocity;
if(transition < 0){
transition = 0;
}
}
}
// Equal power mix the delay sets
sample *= Math.pow(1-transition, 0.5);
sample2 *= Math.pow(transition, 0.5);
// Combine echoes into single track
sample = sample + sample2;
sample = attenuation.calculate(sample, this);
// Iterate buffer
bufferIndex++;
if (bufferIndex == buffer.length) {
bufferIndex = 0;
}
// Determine if changing reverb window
boolean changed = false;
if (goalL != l) {
l += MathUtils.constrain(goalL - l, -reverbVelocity, reverbVelocity);
changed = true;
}
if (goalW != w) {
w += MathUtils.constrain(goalW - w, -reverbVelocity, reverbVelocity);
changed = true;
}
if (goalH != h) {
h += MathUtils.constrain(goalH - h, -reverbVelocity, reverbVelocity);
changed = true;
}
if (changed) {
updateReverbWindow();
}
sample = (sample + prevOut) / 2;
prevOut = sample;
return ((sample * echoFactor) + in);
}
public void setSize(int l, int w, int h) {
this.goalL = Math.max(l, 1);
this.goalW = Math.max(w, 1);
this.goalH = Math.max(h, 1);
aToB = !aToB;
if(aToB){
transition += velocity;
}else{
transition -= velocity;
}
initDelays();
}
public void setFactor(float factor){
this.echoFactor = factor;
}
public ArrayList<Integer> getDelays() {
return resonances;
}
public float minDelay() {
return (float)(Math.min(Math.min(goalL, goalW), goalH) / SOUNDSPD);
}
public float maxDelay() {
return (float)(Math.max(Math.max(goalL, goalW), goalH) / SOUNDSPD);
}
private float processInput() {
if (x.length == 1) {
return x[0];
} else {
float sum = 0;
for (float f : x) {
sum += f;
}
return sum;
}
}
private void initDelays() {
int[] using = new int[6];
while(resonances.size() < 3){
resonances.add(0);
}
// Floor / ceiling delay
float pos = Math.min(goalH-1, 6);
using[0] = (int)(Math.round(pos / samplesPerFoot) * 2);
using[1] = (int)(Math.round((goalH - pos) / samplesPerFoot) * 2);
this.resonances.set(0, (int)Math.round(goalH / samplesPerFoot));
// Front / Back wall delays
pos = goalL * fbJitter;
using[2] = (int)Math.round(pos / samplesPerFoot) * 2;
using[3] = (int)Math.round((goalL - pos) / samplesPerFoot) * 2;
this.resonances.set(1, (int)Math.round(goalL / samplesPerFoot));
// Left / right wall delays
pos = goalW * lrJitter;
using[4] =(int)Math.round(pos / samplesPerFoot) * 2;
using[5] = (int)Math.round((goalW - pos) / samplesPerFoot) * 2;
this.resonances.set(2, (int)Math.round(goalW / samplesPerFoot));
if (aToB) {
this.delays2 = using;
} else {
this.delays = using;
}
updateReverbWindow();
}
// Gets a value from the circular buffer a given offset from the current index.
// Only supports negative offsets, as this system does not support forward-looking filters.
private float get(int offset) {
int index = bufferIndex + offset;
if (index < 0) {
index += buffer.length;
}
return buffer[index];
}
// Update the minimum and maximum delays for the corresponding reverb
private void updateReverbWindow() {
this.reverbWindow[0] = (float)(((l + w + h) / 3.0) / samplesPerFoot);
//this.reverbWindow[1] = ((l+w+h) / samplesPerFoot) * 2;
this.reverbWindow[1] = (float)(Math.pow(l*w*h, 0.3) * 10 / samplesPerFoot);
//println(this.reverbWindow);
}
}