xxxxxxxxxx
64
let mic;
let cols, rows;
let size = 10;
let d = [];
let max;
let colors = ["#f72585","#b5179e","#7209b7","#560bad","#480ca8","#3a0ca3","#3f37c9","#4361ee","#4895ef","#4cc9f0"];
let rings = 2;
let audioSourceIndex = 4; // Set desired audio source index
function setup() {
createCanvas(600, 400);
mic = new p5.AudioIn();
// Get available sources and set to index 4 if available
mic.getSources((deviceList) => {
if (deviceList.length > audioSourceIndex) {
mic.setSource(audioSourceIndex);
console.log("Audio source set to:", deviceList[audioSourceIndex].label);
} else {
console.log("Audio source index out of range. Using default.");
}
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;
}
}
}
function mousePressed() {
if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
let fs = fullscreen();
fullscreen(!fs);
}
}