-
Notifications
You must be signed in to change notification settings - Fork 12
/
render.pde
149 lines (118 loc) · 4.65 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
void render() {
image(bg, 0, 0); // Render the background
// Render octave toggle buttons for active octaves
for ( int i = 0; i < 8; i++ ) {
if ( OCTAVE_TOGGLE[i] ) {
image(octaveBtn, 0, height - (i * 36) - 36);
}
}
String selectedTab = controlP5.window(this).currentTab().name();
if ( selectedTab == "windowing") {
renderWindowCurve();
} else if ( selectedTab == "FFT" ) {
renderFFT();
} else {
renderPeaks();
}
// Update progress bar
if ( isLoaded() ) {
if ( audio.isPlaying() ) {
float percentComplete = audio.position() / (float)audio.length() * 100;
sliderProgress.setValue(audio.position());
sliderProgress.setValueLabel(nf(round(percentComplete), 2) + "%");
}
} else {
sliderProgress.setValueLabel("NO FILE LOADED");
}
// Render semi transparent UI background
fill(0, 200);
rect(width - 140, 0, width, height);
// Render Buildingsky logo on top of it all
image(logo, 25, 254);
}
void renderPeaks() {
int keyHeight = height / (keyboardEnd - keyboardStart);
if ( isLoaded() ) {
// 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;
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;
if ( isLoaded() ) {
for ( int k = 0; k < spectrum.length; k++ ) {
float freq = k / (float)fftBufferSize * audio.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(255, 100 * amp[i] / 400, 0);
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);
labelThreshold.setPosition(PEAK_THRESHOLD + 26, 60);
line(PEAK_THRESHOLD + 24, 0, PEAK_THRESHOLD + 24, height);
noStroke();
}