-
Notifications
You must be signed in to change notification settings - Fork 81
/
sketch.js
177 lines (142 loc) · 4.47 KB
/
sketch.js
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
/**
* Pitch Detection using Auto Correlation.
*
* Auto correlation multiplies each sample in a buffer by all
* of the other samples. This emphasizes the fundamental
* frequency.
*
* Running the signal through a low pass filter prior to
* autocorrelation helps bring out the fundamental frequency.
*
* The visualization is a correlogram, which plots
* the autocorrelations.
*
* We calculate the pitch by counting the number of samples
* between peaks.
*
* Example by Jason Sigal and Golan Levin.
*/
var source, fft, lowPass;
// center clip nullifies samples below a clip amount
var doCenterClip = false;
var centerClipThreshold = 0.0;
// normalize pre / post autocorrelation
var preNormalize = true;
var postNormalize = true;
function setup() {
createCanvas(windowWidth, windowHeight);
noFill();
source = new p5.AudioIn();
source.start();
lowPass = new p5.LowPass();
lowPass.disconnect();
source.connect(lowPass);
fft = new p5.FFT();
fft.setInput(lowPass);
}
function draw() {
background(200);
// array of values from -1 to 1
var timeDomain = fft.waveform(1024, 'float32');
var corrBuff = autoCorrelate(timeDomain);
beginShape();
for (var i = 0; i < corrBuff.length; i++) {
var w = map(i, 0, corrBuff.length, 0, width);
var h = map(corrBuff[i], -1, 1, height, 0);
curveVertex(w, h);
}
endShape();
fill(0);
text ('Center Clip: ' + centerClipThreshold, 20, 20);
line (0, height/2, width, height/2);
var freq = findFrequency(corrBuff);
text ('Fundamental Frequency: ' + freq.toFixed(2), 20, 50);
}
// accepts a timeDomainBuffer and multiplies every value
function autoCorrelate(timeDomainBuffer) {
var nSamples = timeDomainBuffer.length;
// pre-normalize the input buffer
if (preNormalize){
timeDomainBuffer = normalize(timeDomainBuffer);
}
// zero out any values below the centerClipThreshold
if (doCenterClip) {
timeDomainBuffer = centerClip(timeDomainBuffer);
}
var autoCorrBuffer = [];
for (var lag = 0; lag < nSamples; lag++){
var sum = 0;
for (var index = 0; index < nSamples; index++){
var indexLagged = index+lag;
if (indexLagged < nSamples){
var sound1 = timeDomainBuffer[index];
var sound2 = timeDomainBuffer[indexLagged];
var product = sound1 * sound2;
sum += product;
}
}
// average to a value between -1 and 1
autoCorrBuffer[lag] = sum/nSamples;
}
// normalize the output buffer
if (postNormalize){
autoCorrBuffer = normalize(autoCorrBuffer);
}
return autoCorrBuffer;
}
// Find the biggest value in a buffer, set that value to 1.0,
// and scale every other value by the same amount.
function normalize(buffer) {
var biggestVal = 0;
var nSamples = buffer.length;
for (var index = 0; index < nSamples; index++){
if (abs(buffer[index]) > biggestVal){
biggestVal = abs(buffer[index]);
}
}
for (var index = 0; index < nSamples; index++){
// divide each sample of the buffer by the biggest val
buffer[index] /= biggestVal;
}
return buffer;
}
// Accepts a buffer of samples, and sets any samples whose
// amplitude is below the centerClipThreshold to zero.
// This factors them out of the autocorrelation.
function centerClip(buffer) {
var nSamples = buffer.length;
// center clip removes any samples whose abs is less than centerClipThreshold
centerClipThreshold = map(mouseY, 0, height, 0,1);
if (centerClipThreshold > 0.0) {
for (var i = 0; i < nSamples; i++) {
var val = buffer[i];
buffer[i] = (Math.abs(val) > centerClipThreshold) ? val : 0;
}
}
return buffer;
}
// Calculate the fundamental frequency of a buffer
// by finding the peaks, and counting the distance
// between peaks in samples, and converting that
// number of samples to a frequency value.
function findFrequency(autocorr) {
var nSamples = autocorr.length;
var valOfLargestPeakSoFar = 0;
var indexOfLargestPeakSoFar = -1;
for (var index = 1; index < nSamples; index++){
var valL = autocorr[index-1];
var valC = autocorr[index];
var valR = autocorr[index+1];
var bIsPeak = ((valL < valC) && (valR < valC));
if (bIsPeak){
if (valC > valOfLargestPeakSoFar){
valOfLargestPeakSoFar = valC;
indexOfLargestPeakSoFar = index;
}
}
}
var distanceToNextLargestPeak = indexOfLargestPeakSoFar - 0;
// convert sample count to frequency
var fundamentalFrequency = sampleRate() / distanceToNextLargestPeak;
return fundamentalFrequency;
}