-
Notifications
You must be signed in to change notification settings - Fork 0
/
beatDetection.js
576 lines (478 loc) · 17.5 KB
/
beatDetection.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
"use strict";
const SKETCH_1_HEIGHT = 250;
// global detectors
// instanciation of onset and beat detection from fft
// low band : 40Hz-120Hz
let onsetLow = new OnsetDetect(40, 120, "bass", 0.025);
let beatLow = new BeatDetect(40, 120, "bass", 0.95);
// lowMid band : 140Hz-400Hz
let onsetLowMid = new OnsetDetect(140, 400, "lowMid", 0.025);
let beatLowMid = new BeatDetect(140, 400, "lowMid", 0.80);
// mid band : 400Hz-2.6kHz
let onsetMid = new OnsetDetect(400, 2600, "mid", 0.025);
let beatMid = new BeatDetect(400, 2600, "mid", 0.6);
let isInGame = true;
// ***~*~*~*~*~*~*~*~*~*~*~*~ BEGIN SKETCHES **~*~*~*~*~*~*~*~**~*~*~*~*~**~
// sketch 1
var sketch1 = function (p) {
p.file = 'astral.mp3'
p.source_file; // sound file
p.src_length; // hold its duration
p.fft;
p.pg; // to draw waveform
p.playing = false;
p.button;
p.preload = function () {
p.source_file = p.loadSound(p.file); // preload the sound
};
p.setup = function () {
p.createCanvas(p.windowWidth, SKETCH_1_HEIGHT);
p.textAlign(p.CENTER);
p.src_length = p.source_file.duration();
p.source_file.playMode('restart');
// draw the waveform to an off-screen graphic
let peaks = p.source_file.getPeaks([600]); // get an array of peaks
p.pg = p.createGraphics(p.width, 150);
p.pg.background(100);
p.pg.translate(0, 75);
p.pg.noFill();
p.pg.stroke(0);
for (let i = 0; i < peaks.length; i++) {
let x = p.map(i, 0, peaks.length, 0, p.width);
let y = p.map(peaks[i], 0, 1, 0, 150);
p.pg.line(x, 0, x, y);
p.pg.line(x, 0, x, -y);
}
// FFT
p.fft = new p5.FFT();
// gui
p.button = p.createButton('play');
p.button.position(3, 3);
p.button.mousePressed(p.play);
};
p.draw = function () {
p.background(180);
p.image(p.pg, 0, 100); // display our waveform representation
// draw playhead position
p.fill(255, 255, 180, 150);
p.noStroke();
p.rect(p.map(p.source_file.currentTime(), 0, p.src_length, 0, p.windowWidth), 100, 3, 150);
//display current time
p.text("current time: " + p.nfc(p.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.
let spectrum = p.fft.analyze();
// display and update our detector objects
p.text("onset detection", 350, 15);
p.text("amplitude treshold", 750, 15);
onsetLow.display(p, 250, 50);
onsetLow.update(p, p.fft);
beatLow.display(p, 650, 50);
beatLow.update(p, p.fft);
onsetLowMid.display(p, 350, 50);
onsetLowMid.update(p, p.fft);
beatLowMid.display(p, 750, 50);
beatLowMid.update(p, p.fft);
onsetMid.display(p, 450, 50);
onsetMid.update(p, p.fft);
beatMid.display(p, 850, 50);
beatMid.update(p, p.fft);
if (p.source_file.currentTime() >= p.src_length - 0.05) {
p.source_file.pause();
}
};
p.mouseClicked = function () {
if (p.mouseY > 100 && p.mouseY < 350) {
let mapper = p.map(p.mouseX, 0, p.windowWidth, 0, p.src_length);
let playpos = p.constrain(mapper, 0, p.src_length);
p.source_file.play();
p.source_file.play(0, 1, 1, playpos, p.src_length);
p.playing = true;
p.button.html('pause');
}
return false;//callback for p5js
}
p.keyTyped = function () {
if (p.key == ' ') {
p.play();
}
return false; // callback for p5js
}
p.play = function () {
if (p.playing) {
p.source_file.pause();
p.button.html('play');
p.playing = false;
}
else {
p.source_file.play();
p.button.html('pause');
p.playing = true;
}
}
};
// give the id of the html div as the second parameter
var mySketch1 = new p5(sketch1, "sketch1");
// sketch 2 -> main game
var sketch2 = function (p) {
p.CIRCLE = 0;
p.TRIANGLE = 1;
p.BULLET = 2;
p.SQUARE = 3;
const PLAYER_VELOCITY = 150;
const PLAYER_EASING = 0.05;
const BACKGROUND_COLOR_CHANGE_SENSITIVITY = 500;
const ENEMY_COLOR_CHANGE_SENSITIVITY = 400;
const ENEMY_PARTICLE_SPEED = 400;
const ENEMY_SPAWN_RATE = 200
const WEB_ENEMY_SIZE = 15;
const WEB_ENEMY_Y_VEL_VARIANCE = 80;
const WEB_ENEMY_SPEED = 80;
const WEB_ENEMY_SPAWN_RATE = 400;
const WEB_ENEMY_VARIANCE = 100;
const SQUARE_ENEMY_SPAWN_RATE = 500;
const SQUARE_ENEMY_SPEED = 40;
const SQUARE_ENEMY_SIZE = 30;
// in milliseconds
const SHOOT_DELAY = 250;
const SHOT_SPEED = 500;
p.blurTime = 0;
p.isFadingBlur = false;
// particles and enemies
p.shots = new Particles([]);
p.deadEnemyParticles = new Particles([]);
p.enemies = new Particles([]);
p.squareEnemies = new Particles([]);
p.webEnemies = [];
p.player = new Player(p.createVector(PLAYER_VELOCITY, PLAYER_VELOCITY), p.createVector(p.floor(p.width / 2), p.floor(p.height / 2)), 50.0, p.color(255, 255, 255), PLAYER_EASING);
let img;
p.preload = function () {
img = p.loadImage("124conch.png");
}
// SETUP FUNCTION
p.setup = function () {
p.createCanvas(p.windowWidth, p.windowHeight - SKETCH_1_HEIGHT);
p.image(img, 0, 0);
};
// DRAW FUNCTINO
p.draw = function () {
p.background(img);
// shooting mechanic
if (p.mouseIsPressed && p.player.canShoot) {
p.shots.addParticle(p, p.createVector(SHOT_SPEED, 0), p.createVector(p.player.position.x + p.player.radius / 2, p.player.position.y), 5, p.color(255, 0, 0), p.BULLET, 0);
// wait until can change color again
p.player.canShoot = false;
setTimeout(function () {
p.player.canShoot = true;
}, SHOOT_DELAY);
}
p.shots.updateParticlePositions(p);
p.shots.drawParticles(p);
p.shots.shoot(p, p.enemies);
p.shots.deleteOldParticles(p);
// dead enemy particles
p.deadEnemyParticles.updateParticlePositions(p);
p.noStroke();
p.deadEnemyParticles.drawParticles(p);
p.stroke(0);
p.deadEnemyParticles.deleteOldParticles(p);
// beat detection
// lows
let elem = document.querySelectorAll("canvas");
if (beatLow.isDetected) {
if (!p.isFadingBlur) {
for (let el of elem) {
el.style.filter = "hue-rotate(180deg)";
}
p.isFadingBlur = true;
p.blurTime = 180;
}
}
if (p.isFadingBlur) {
p.blurTime -= 4;
for (let el of elem) {
// el.classList.remove("hue-rotate");
el.style.filter = "hue-rotate(" + p.blurTime + "deg)";
}
if (p.blurTime == 0) {
for (let el of elem) {
el.classList.remove("hue-rotate");
}
p.isFadingBlur = false;
}
}
// mids
if (beatMid.isDetected && !p.changingEnemyColor) {
let enemyColor = p.color(Math.random() * 255, Math.random() * 255, Math.random() * 255);
p.enemies.changeColor(p, enemyColor);
enemyColor = p.color(Math.random() * 255, Math.random() * 255, Math.random() * 255);
p.squareEnemies.changeColor(p, enemyColor);
// wait until can change color again
p.changingEnemyColor = true;
setTimeout(function () {
p.changingEnemyColor = false;
}, ENEMY_COLOR_CHANGE_SENSITIVITY);
}
// low mids
if (beatLowMid.isDetected && !p.changingWebColor) {
let enemyColor = p.color(Math.random() * 255, Math.random() * 255, Math.random() * 255);
for (let i = 0; i < p.webEnemies.length; i++) {
let webParticles = p.webEnemies[i];
webParticles.changeColor(p, enemyColor);
}
// wait until can change color again
p.changingWebColor = true;
setTimeout(function () {
p.changingWebColor = false;
}, ENEMY_COLOR_CHANGE_SENSITIVITY);
}
// enemies
if (p.frameCount % ENEMY_SPAWN_RATE == 0) {
p.enemies.addEnemyParticle(p, ENEMY_PARTICLE_SPEED, undefined, undefined, 50, p.color(0, 0, 255), p.CIRCLE, 0);
}
p.enemies.updateEnemyPositions(p);
p.enemies.drawParticles(p);
// web enemies
if (p.frameCount % WEB_ENEMY_SPAWN_RATE == 0) {
let newParticles = new Particles([]);
let seedHeight = Math.random() * (p.height - WEB_ENEMY_SIZE - WEB_ENEMY_VARIANCE) + WEB_ENEMY_SIZE + WEB_ENEMY_VARIANCE;
for (let i = 0; i < 5; i++) {
let x = -WEB_ENEMY_SPEED;
let y = ((Math.random() * 2) - 1) * WEB_ENEMY_Y_VEL_VARIANCE;
let velocity = p.createVector(x, y);
newParticles.addEnemyParticle(p, WEB_ENEMY_SPEED, velocity, getRandomSpawnPositionAroundSeed(p, WEB_ENEMY_SIZE, seedHeight, WEB_ENEMY_VARIANCE), WEB_ENEMY_SIZE, p.color(0, 0, 255), p.TRIANGLE, 1);
}
p.webEnemies.push(newParticles);
}
for (let i = 0; i < p.webEnemies.length; i++) {
let webParticles = p.webEnemies[i];
webParticles.updateWebEnemyPositions(p);
webParticles.drawWebParticles(p);
}
for (let i = 0; i < p.webEnemies.length; i++) {
let webParticles = p.webEnemies[i];
p.shots.shootWebbies(p, webParticles);
}
// square enemies
if (p.frameCount % SQUARE_ENEMY_SPAWN_RATE == 0) {
p.squareEnemies.addEnemyParticle(p, SQUARE_ENEMY_SPEED, undefined, undefined, 100, p.color(0, 0, 255), p.SQUARE, 0);
}
p.squareEnemies.updateEnemyPositions(p);
p.squareEnemies.drawParticles(p);
p.shots.shootSquare(p, p.squareEnemies);
// player
p.player.move(p);
};
p.resetGame = function () {
p.shots = new Particles([]);
p.deadEnemyParticles = new Particles([]);
p.enemies = new Particles([]);
p.webEnemies = undefined;
p.webEnemies = [];
p.player.canShoot = true;
showGameOver();
};
};
var mySketch2 = new p5(sketch2, "sketch2");
// sketch 3 -> Game Over screen
var sketch3 = function (p) {
let prev_pos_x = 0;
let prev_pos_y = 0;
let x_vel;// = 3*Math.random()*200;
let y_vel;// = 3*Math.random()*80;
// might need to change these guys when we merge this whole thing??
const PARTICLE_SPEED = 400;
const FONT_SIZE = 150;
const TEXT_X = p.windowWidth / 2;
const TEXT_Y = p.windowHeight / 2;
const TEXT_TOP = p.windowHeight / 2;
const TEXT_BOTTOM = p.windowHeight / 2.9;
p.mouse_particles = new Particles([]);
p.backgroundColor = p.color(0);
p.changingColor = false;
// SETUP FUNCTION
p.setup = function () {
p.createCanvas(p.windowWidth, p.windowHeight - SKETCH_1_HEIGHT);
};
// DRAW FUNCTION
p.draw = function () {
//p.background(p.backgroundColor);
p.background(0);
if (p.mouseIsPressed && !isInGame) {
showGame();
}
// make particles follow mouse
// set their velocity in the opposite direction of where the mouse is going
let x_dir = p.mouseX - prev_pos_x;
let y_dir = p.mouseY - prev_pos_y;
if (x_dir > 0) {
x_vel = -2 * Math.random() * 200;
}
if (x_dir < 0) {
x_vel = 2 * Math.random() * 200;
}
if (y_dir > 0) {
y_vel = -2 * Math.random() * 80;
}
if (y_dir < 0) {
y_vel = 2 * Math.random() * 80;
}
if (x_dir == 0 && y_dir == 0) {
x_vel = x_vel * Math.random() * 50;
y_vel = y_vel * Math.random() * 50;
}
// get a random color: red/purple/blue
let red_color = Math.random() * 255;
let green_color;
let blue_color = Math.random() * 255;
// add, update and draw the particles
p.mouse_particles.addParticle(p, p.createVector(x_vel, y_vel), p.createVector(p.mouseX, p.mouseY), 5, p.color(red_color, 0, blue_color), p.CIRCLE, 0);
p.mouse_particles.updateParticlePositions(p);
p.mouse_particles.drawParticles(p);
p.mouse_particles.deleteOldParticles(p);
// save the previous position of the particles
prev_pos_x = p.mouseX;
prev_pos_y = p.mouseY;
p.textSize(FONT_SIZE);
let aString = 'GAME OVER';
let sWidth = p.textWidth(aString);
p.fill(255, 0, 0);
p.textFont('Impact');
//p.text(aString, 400, 405);
// p.line(sWidth, 50, sWidth, 100);
let tw = p.textWidth(aString);
let text_min_x = p.width / 2 - tw / 2;
// let text_min_y = TEXT_BOTTOM;
let text_max_x = p.width / 2 + tw / 2;
// let text_max_y = TEXT_TOP;
//&& p.mouseY < text_max_y && p.mouseY > text_min_y) {
if (p.mouseX < text_max_x && p.mouseX > text_min_x && p.mouseY < TEXT_TOP && p.mouseY > TEXT_BOTTOM) {
p.textAlign(p.CENTER);
p.text(aString, TEXT_X + Math.random() * 5 - 5, TEXT_Y + Math.random() * 5 - 5);
} else {
p.textAlign(p.CENTER);
p.text(aString, TEXT_X, TEXT_Y);
}
// Try again text
p.textSize(30);
let bString = 'Click anywhere to try again';
let bWidth = p.textWidth(bString);
p.fill(255, 240, 240);
p.textFont('Impact');
p.textAlign(p.CENTER);
p.text(bString, TEXT_X, TEXT_Y * 1.15);
};
};
// ***~*~*~*~*~*~*~*~*~*~*~*~ END SKETCHES **~*~*~*~*~*~*~*~**~*~*~*~**~
function showGameOver() {
mySketch2 = undefined;
document.getElementById("sketch2").innerHTML = "";
mySketch2 = new p5(sketch3, "sketch2");
isInGame = false;
}
function showGame() {
mySketch2 = undefined;
document.getElementById("sketch2").innerHTML = "";
mySketch2 = new p5(sketch2, "sketch2");
isInGame = true;
}
function OnsetDetect(f1, f2, str, thresh) {
this.isDetected = false;
// f1 and f2 are the low and high frequencies for this detection band
this.f1 = f1;
this.f2 = f2;
// str is the string for the band... like "bass", or "mid"
this.str = str;
// threshold is the threshold to trigger a detection. energy - penergy
this.treshold = thresh;
// energy is current energy, penergy is the energy on the previous frame
this.energy = 0;
this.penergy = 0;
this.siz = 10;
this.sensitivity = 400;
}
OnsetDetect.prototype.display = function (p, x, y) {
if (this.isDetected == true) {
this.siz = p.lerp(this.siz, 40, 0.99);
}
else if (this.isDetected == false) {
this.siz = p.lerp(this.siz, 15, 0.99);
}
p.fill(255, 0, 0);
p.ellipse(x, y, this.siz, this.siz);
p.fill(0);
p.text(this.str, x, y);
p.text("( " + this.f1 + " - " + this.f2 + "Hz )", x, y + 10);
}
OnsetDetect.prototype.update = function (p, fftObject) {
this.energy = fftObject.getEnergy(this.f1, this.f2) / 255;
if (this.isDetected == false) {
if (this.energy - this.penergy > this.treshold) {
this.isDetected = true;
let 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 (p, x, y) {
if (this.isDetected == true) {
this.siz = p.lerp(this.siz, 40, 0.99);
}
else if (this.isDetected == false) {
this.siz = p.lerp(this.siz, 15, 0.99);
}
p.fill(255, 0, 0);
p.ellipse(x, y, this.siz, this.siz);
p.fill(0);
p.text(this.str, x, y);
p.text("( " + this.f1 + " - " + this.f2 + "Hz )", x, y + 10);
}
BeatDetect.prototype.update = function (p, fftObject) {
this.energy = fftObject.getEnergy(this.f1, this.f2) / 255;
if (this.isDetected == false) {
if (this.energy > this.treshold) {
this.isDetected = true;
let self = this;
setTimeout(function () {
self.isDetected = false;
}, this.sensitivity);
}
}
}
BeatDetect.prototype.update = function (p, fftObject) {
this.energy = fftObject.getEnergy(this.f1, this.f2) / 255;
if (this.isDetected == false) {
if (this.energy > this.treshold) {
this.isDetected = true;
let self = this;
setTimeout(function () {
self.isDetected = false;
}, this.sensitivity);
}
}
}
// **~*~*~*~*~*~*~*~**~ PRIVATE HELPERS *~*~**~*~*~~**~*~
function getRandomSpawnPositionAroundSeed(p, radius, seedHeight, posVariance) {
let widthVar = Math.random() * posVariance;
let heightVar = Math.random() * posVariance;
let finalHeight = seedHeight + heightVar;
if (finalHeight < 8 * radius) {
finalHeight = 8 * radius;
} else if (finalHeight > p.height - 8 * radius) {
finalHeight = p.height - 8 * radius;
}
return p.createVector(p.width + widthVar, finalHeight);
}