-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathJSynFFT.java
More file actions
47 lines (38 loc) · 1.34 KB
/
JSynFFT.java
File metadata and controls
47 lines (38 loc) · 1.34 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
package processing.sound;
import java.util.Arrays;
import com.jsyn.data.FloatSample;
import com.jsyn.data.HannWindow;
import com.jsyn.data.SpectralWindow;
import com.jsyn.unitgen.FixedRateMonoWriter;
/**
* This class copies all input to an audio buffer of the given size and performs
* an FFT on it when required.
*/
class JSynFFT extends FixedRateMonoWriter {
private FloatSample buffer;
private float[] real;
private float[] imaginary;
private SpectralWindow window;
protected JSynFFT(int bufferSize) {
super();
this.buffer = new FloatSample(bufferSize);
this.real = new float[bufferSize];
this.imaginary = new float[bufferSize];
this.window = new HannWindow(bufferSize);
// write any connected input into the output buffer ad infinitum
this.dataQueue.queueLoop(this.buffer);
}
protected void calculateMagnitudes(float[] target) {
// get position currently being written to
int pos = (int) this.dataQueue.getFrameCount() % this.buffer.getNumFrames();
this.buffer.read(pos, this.real, 0, this.buffer.getNumFrames() - pos);
this.buffer.read(0, this.real, this.buffer.getNumFrames() - pos, pos);
if (this.window != null) {
for (int i = 0; i < this.real.length; i++) {
this.real[i] *= this.window.get(i);
}
}
Arrays.fill(this.imaginary, 0);
FFT.calculateMagnitudesFromSample(this.real, this.imaginary, target);
}
}