-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
75 lines (63 loc) · 1.34 KB
/
sketch.js
File metadata and controls
75 lines (63 loc) · 1.34 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
// CODIO SOLUTION BEGIN
let sound, button, sm
function preload() {
sound = loadSound('music/Beat-Culture-Midori.mp3')
}
function setup() {
createCanvas(640, 640);
button = createButton('Play/Stop')
button.mousePressed(() => {
if (sm.sound.isPlaying()) {
sm.sound.stop()
} else {
sm.sound.play()
}
})
noStroke()
fill(0)
sm = new SoundMatrix(sound)
sm.initialize()
}
function draw() {
background(220);
sm.analyze()
sm.show()
}
class SoundMatrix {
constructor(music) {
this.sound = music
this.fft = new p5.FFT()
this.spectrum = []
this.dots = []
}
initialize() {
for (let i = 0; i < 32; i++) {
for (let j = 0; j < 32; j++) {
this.dots.push({
x: j * width / 32 + 8,
y: i * height / 32 + 8,
fftIndex: i + j
})
}
}
let indices = []
for (let i = 0; i < 1024; i++) {
indices.push(this.dots[i].fftIndex)
}
let shuffled = shuffle(indices)
for (let i = 0; i < 1024; i++) {
this.dots[i].fftIndex = shuffled[i]
}
}
analyze() {
this.spectrum = this.fft.analyze()
}
show() {
for (let i = 0; i < 1024; i++) {
let dot = this.dots[i]
let value = map(this.spectrum[dot.fftIndex], 0, 255, 0, 25)
circle(dot.x, dot.y, value)
}
}
}
// CODIO SOLUTION END