Skip to content

Commit 248c41d

Browse files
Dimitri BuhonDimitri Buhon
Dimitri Buhon
authored and
Dimitri Buhon
committed
fix beat detection on 0
1 parent deb51cf commit 248c41d

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

0/index.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
let yoff = 0.0;
22
let song;
3-
let peaks = [];
43
let dark = true;
54
let fast = false;
65

76
function preload() {
8-
song = loadSound('../assets/song.mp3', () => song.processPeaks((result) => peaks = result));
7+
song = loadSound('../assets/song.mp3');
98
}
109

1110
function setup() {
1211
createCanvas(windowWidth, windowHeight);
1312
fill(25);
1413
song.loop();
1514
song.play();
15+
16+
amplitude = new p5.Amplitude();
17+
amplitude.setInput(song);
1618
}
1719

1820
function draw() {
19-
if (peaks.find(p => p.toFixed(1) === song.currentTime().toFixed(1))) {
21+
const level = amplitude.getLevel();
22+
if (level >= .3) {
2023
color = randomizeColor(color);
2124
fill(color.r, color.g, color.b);
2225
fast = !fast;
@@ -31,7 +34,7 @@ function draw() {
3134
xoff += 0.01;
3235
}
3336

34-
yoff += fast ? 0.005 : 0.0005;
37+
yoff += fast ? 0.0025 : 0.0005;
3538
vertex(width, height);
3639
vertex(0, height);
3740
endShape();

1/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ function draw() {
6161
}
6262

6363
xoff += fast ? 0.000005 : 0.000001;
64+
6465
}
6566

6667
function x(value) {

index.html

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
p5js tests reacting to sound
2+
<ul>
3+
<li><a href='/0'>Test 1</a></li>
4+
<li><a href='/1'>Test 2</a></li>
5+
<li><a href='/2'>Test 3</a></li>
6+
<li><a href='/3'>Test 4</a></li>
7+
</ul>

0 commit comments

Comments
 (0)