forked from corbanbrook/spectrotune
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.pde
128 lines (101 loc) · 4.03 KB
/
render.pde
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
void render() {
image(bg, 0, 0); // Render the background
String selectedTab = ui.getWindow().getCurrentTab().getName();
if ( selectedTab == "windowing") {
renderWindowCurve();
renderPeaks();
} else if ( selectedTab == "FFT" ) {
renderFFT();
} else {
renderPeaks();
}
// Render semi transparent UI background
fill(0, 200);
rect(width - 140, 0, width, height);
}
void renderPeaks() {
int keyHeight = height / (keyboardEnd - keyboardStart);
// render key presses for detected peaks
for ( int i = 0; i < notes[frameNumber].length; i++ ) {
Note note = notes[frameNumber][i];
if ( note.isWhiteKey() ) {
image(whiteKey, 10, height - ((note.pitch - keyboardStart) * keyHeight + keyHeight));
} else if ( note.isBlackKey() ) {
image(blackKey, 10, height - ((note.pitch - keyboardStart) * keyHeight + keyHeight));
}
}
// render detected peaks
noStroke();
int keyLength = 10;
int scroll = (frameNumber * keyLength > width) ? frameNumber - width/keyLength: 0;
if ( scroll >= 512 ){
frameNumber = 0;
notes = new Note[bufferSize][0];
pcp = new float[bufferSize][12];
}
for ( int x = frameNumber; x >= scroll; x-- ) {
for ( int i = 0; i < notes[x].length; i++ ) {
Note note = notes[x][i];
color noteColor;
if ( pcp[x][note.pitch % 12] == 1.0 ) {
noteColor = color(255, 100 * note.amplitude / 400, 0);
} else {
noteColor = color(0, 255 * note.amplitude / 400, 200);
}
fill(red(noteColor)/4, green(noteColor)/4, blue(noteColor)/4);
rect(abs(x - frameNumber) * keyLength + 24, height - ((note.pitch - keyboardStart) * keyHeight), abs(x - frameNumber) * keyLength + keyLength + 25 , height - ((note.pitch - keyboardStart) * keyHeight + keyHeight));
fill(noteColor);
rect(abs(x - frameNumber) * keyLength + 24, height - ((note.pitch - keyboardStart) * keyHeight) - 1, abs(x - frameNumber) * keyLength + keyLength + 24 , height - ((note.pitch - keyboardStart) * keyHeight + keyHeight));
}
}
// output semitone text labels
textSize(10);
for ( int i = 0; i < notes[frameNumber].length; i++ ) {
Note note = notes[frameNumber][i];
fill(20);
text(note.label(), 24 + 1, height - ((note.pitch - keyboardStart) * keyHeight + keyHeight + 1));
fill(140);
text(note.label(), 24, height - ((note.pitch - keyboardStart) * keyHeight + keyHeight + 2));
}
}
void renderWindowCurve() {
int windowX = 35;
int windowY = 110;
int windowHeight = 80;
stroke(255, 255, 255, 250);
float[] windowCurve = window.drawCurve();
for (int i = 0; i < windowCurve.length - 1; i++) {
line(i + windowX, windowY - windowCurve[i] * windowHeight, i+1 + windowX, windowY - windowCurve[i+1] * windowHeight);
}
noStroke();
}
void renderFFT() {
noStroke();
int keyHeight = height / (keyboardEnd - keyboardStart);
color noteColor;
float[] amp = new float[128];
int previousPitch = -1;
int currentPitch;
float amplitudeTotal = 0f;
for ( int k = 0; k < spectrum.length; k++ ) {
float freq = k / (float)fftBufferSize * in.sampleRate();
currentPitch = freqToPitch(freq);
if ( currentPitch == previousPitch ) {
amp[currentPitch] = amp[currentPitch] > spectrum[k] ? amp[currentPitch] : spectrum[k];
} else {
amp[currentPitch] = spectrum[k];
previousPitch = currentPitch;
}
}
for ( int i = keyboardStart; i < keyboardEnd; i++) {
noteColor = color(0, 255, 240);
fill(red(noteColor)/4, green(noteColor)/4, blue(noteColor)/4);
rect(24, height - ((i - keyboardStart) * keyHeight), 25 + amp[i], height - ((i - keyboardStart) * keyHeight + keyHeight)); // shadow
fill(noteColor);
rect(24, height - ((i - keyboardStart) * keyHeight) - 1, 24 + amp[i] , height - ((i - keyboardStart) * keyHeight + keyHeight));
}
stroke(255);
ui.getController("labelThreshold").setPosition(PEAK_THRESHOLD + 26, 60);
line(PEAK_THRESHOLD + 24, 0, PEAK_THRESHOLD + 24, height);
noStroke();
}