-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeatDetect.js
More file actions
229 lines (182 loc) · 5.46 KB
/
beatDetect.js
File metadata and controls
229 lines (182 loc) · 5.46 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
var file = 'amilli.mp3'
var source_file; // sound file
var src_length; // hold its duration
var fft;
var pg; // to draw waveform
var playing = false;
var button;
// detectors
var onsetLow, beatLow;
var onsetLowMid, beatLowMid;
var onsetMid, beatMid;
function preload() {
source_file = loadSound(file); // preload the sound
}
function setup() {
createCanvas(windowWidth, 250);
textAlign(CENTER);
src_length = source_file.duration();
source_file.playMode('restart');
console.log("source duration: ", src_length);
// draw the waveform to an off-screen graphic
console.log('this is source_file: ', source_file);
var peaks = source_file.getPeaks(); // get an array of peaks
pg = createGraphics(width, 150);
pg.background(100);
pg.translate(0, 75);
pg.noFill();
pg.stroke(0);
for (var i = 0; i < peaks.length; i++) {
var x = map(i, 0, peaks.length, 0, width);
var y = map(peaks[i], 0, 1, 0, 150);
pg.line(x, 0, x, y);
pg.line(x, 0, x, -y);
}
// FFT
fft = new p5.FFT();
// instanciation of onset and beat detection from fft
// low band : 40Hz-120Hz
onsetLow = new OnsetDetect(40, 120, "bass", 0.025);
beatLow = new BeatDetect(40, 120, "bass", 0.95);
// lowMid band : 140Hz-400Hz
onsetLowMid = new OnsetDetect(140, 400, "lowMid", 0.025);
beatLowMid = new BeatDetect(140, 400, "lowMid", 0.80);
// mid band : 400Hz-2.6kHz
onsetMid = new OnsetDetect(400, 2600, "Mid", 0.025);
beatMid = new BeatDetect(400, 2600, "Mid", 0.6);
// gui
button = createButton('play');
button.position(3, 3);
button.mousePressed(play);
}
function draw() {
background(180);
image(pg, 0, 100); // display our waveform representation
// draw playhead position
fill(255, 255, 180, 150);
noStroke();
rect(map(source_file.currentTime(), 0, src_length, 0, windowWidth), 100, 3, 150);
//display current time
text("current time: " + nfc(source_file.currentTime(), 1) + " s", 60, 50);
// we need to call fft.analyse() before the update functions of our class
// this is because we use the getEnergy method inside our class.
var spectrum = fft.analyze();
// display and update our detector objects
text("onset detection", 350, 15);
text("amplitude treshold", 750, 15);
onsetLow.display(250, 50);
onsetLow.update(fft);
beatLow.display(650, 50);
beatLow.update(fft);
onsetLowMid.display(350, 50);
onsetLowMid.update(fft);
beatLowMid.display(750, 50);
beatLowMid.update(fft);
onsetMid.display(450, 50);
onsetMid.update(fft);
beatMid.display(850, 50);
beatMid.update(fft);
if (source_file.currentTime() >= src_length - 0.05) {
source_file.pause();
}
}
function mouseClicked() {
if (mouseY > 100 && mouseY < 350) {
var playpos = constrain(map(mouseX, 0, windowWidth, 0, src_length), 0, src_length);
source_file.play();
source_file.play(0, 1, 1, playpos, src_length);
playing = true;
button.html('pause');
}
return false;//callback for p5js
}
function keyTyped() {
if (key == ' ') {
play();
}
return false; // callback for p5js
}
function play() {
if (playing) {
source_file.pause();
button.html('play');
playing = false;
}
else {
source_file.play();
button.html('pause');
playing = true;
}
}
function OnsetDetect(f1, f2, str, thresh) {
this.isDetected = false;
this.f1 = f1;
this.f2 = f2;
this.str = str;
this.treshold = thresh;
this.energy = 0;
this.penergy = 0;
this.siz = 10;
this.sensitivity = 500;
}
OnsetDetect.prototype.display = function (x, y) {
if (this.isDetected == true) {
this.siz = lerp(this.siz, 40, 0.99);
}
else if (this.isDetected == false) {
this.siz = lerp(this.siz, 15, 0.99);
}
fill(255, 0, 0);
ellipse(x, y, this.siz, this.siz);
fill(0);
text(this.str, x, y);
text("( " + this.f1 + " - " + this.f2 + "Hz )", x, y + 10);
}
OnsetDetect.prototype.update = function (fftObject) {
this.energy = fftObject.getEnergy(this.f1, this.f2) / 255;
if (this.isDetected == false) {
if (this.energy - this.penergy > this.treshold) {
this.isDetected = true;
var self = this;
setTimeout(function () {
self.isDetected = false;
}, this.sensitivity);
}
}
this.penergy = this.energy;
}
function BeatDetect(f1, f2, str, thresh) {
this.isDetected = false;
this.f1 = f1;
this.f2 = f2;
this.str = str;
this.treshold = thresh;
this.energy = 0;
this.siz = 10;
this.sensitivity = 500;
}
BeatDetect.prototype.display = function (x, y) {
if (this.isDetected == true) {
this.siz = lerp(this.siz, 40, 0.99);
}
else if (this.isDetected == false) {
this.siz = lerp(this.siz, 15, 0.99);
}
fill(255, 0, 0);
ellipse(x, y, this.siz, this.siz);
fill(0);
text(this.str, x, y);
text("( " + this.f1 + " - " + this.f2 + "Hz )", x, y + 10);
}
BeatDetect.prototype.update = function (fftObject) {
this.energy = fftObject.getEnergy(this.f1, this.f2) / 255;
if (this.isDetected == false) {
if (this.energy > this.treshold) {
this.isDetected = true;
var self = this;
setTimeout(function () {
self.isDetected = false;
}, this.sensitivity);
}
}
}