-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
105 lines (72 loc) · 2.05 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
let mic;
let sound;
let button;
function mousePressed() {
if (mouseX > 0 && mouseX < 100 && mouseY > 0 && mouseY < 100) {
let fs = fullscreen();
fullscreen(!fs);
}
}
function setup() {
// Create a start button
button = createButton('Start3 Audio');
button.mousePressed(() => {
if (getAudioContext().state !== 'running') {
getAudioContext().resume();
}
// Create a new audio in
// create a new Amplitude analyzer
mic = new p5.AudioIn();
mic.start();
sound = new p5.Amplitude();
sound.setInput(mic);
fft = new p5.FFT();
fft.setInput(mic);
fullscreen(true);
setTimeout(() => {
createCanvas(windowWidth, windowHeight);
colorMode(RGB);
}, 500)
// Remove the start button
button.remove();
});
}
// input:
const inputResponseLevel = 1
const inputResponseFrequency = 60
const power = 5
// grid:
const gridSize = 12
// noise:
const noiseDensity = 0.3
const noiseSpeedFactor = 0.01
let frame = 0
function draw() {
noStroke();
if(!sound) return
background('rgba(0,0,0,0.3)');
fft.analyze();
level = fft.getEnergy(inputResponseFrequency) / 255 * inputResponseLevel
level = Math.pow(level, power);
// Determine the size of the grid
let gridWidth = width / gridSize;
let gridHeight = height / gridSize;
const ratio = gridHeight / gridWidth * 1.5
// Draw a 4x4 grid of ellipses
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
// Incorporate Perlin noise into the diameter of the ellipse
let noiseFactor = noise(i * noiseDensity + frame * noiseSpeedFactor, j * noiseDensity + noiseSpeedFactor);
let size = map(level * noiseFactor,
0, 1, 0, gridHeight
)
let x = gridWidth / 2 + i * gridWidth;
let y = gridHeight / 2 + j * gridHeight;
// fill(color(255, 255, 255, diam * 100))
fill(color(255, 255, 255, 255))
rectMode(CENTER)
rect(x, y, size / ratio, size);
}
}
frame ++
}