xxxxxxxxxx
58
/*
----- Sound Mini Series by Patt Vira -----
Name: EP3_p5.AudioIn
Video Tutorial: https://youtu.be/x_aGtSZMEUA
Connect with Patt: @pattvira
https://www.pattvira.com/
----------------------------------------
*/
let mic;
let cols; let rows; let size = 10;
let d = [];
let max;
let colors = ["#f72585","#b5179e","#7209b7","#560bad","#480ca8","#3a0ca3","#3f37c9","#4361ee","#4895ef","#4cc9f0"];
let rings = 2;
function setup() {
createCanvas(600, 400);
mic = new p5.AudioIn();
mic.start();
cols = width/size;
rows = height/size;
max = sqrt(pow(width/2, 2) + pow(height/2, 2));
for(let i=0; i<cols; i++) {
d[i] = [];
for (let j=0; j<rows; j++) {
let x = i*size + size/2;
let y = j*size + size/2;
d[i][j] = dist(x, y, width/2, height/2);
}
}
}
function draw() {
background(255);
let amplitude = mic.getLevel() * 15;
for(let i=0; i<cols; i++) {
for (let j=0; j<rows; j++) {
let x = i*size + size/2;
let y = j*size + size/2;
let index = floor(map(abs(d[i][j]), 0, max, 0, colors.length * rings));
let c = colors[index % colors.length];
fill(c);
strokeWeight(2);
ellipse(x, y, size, size);
d[i][j] -= amplitude;
}
}
}