xxxxxxxxxx
91
let heatmap;
let mic, fft;
let numSamples = 20;
let bandNum;
let barHeight;
let oldX = 500;
let oldY = 500;
let dim;
let theta;
let spectrum;
let c1, c2, c3;
let circles = [];
let spectrumStart = 0.1;
let spectrumWidth = 0.3;
function setup() {
createCanvas(1080, 1080);
heatmap = createGraphics(1920, 1920);
colorMode(HSB, 360, 100, 100, 100);
c1 = color(63, 100, 82, 100);
c2 = color(62, 50, 30);
c3 = color(62, 100, 33);
mic = new p5.AudioIn();
mic.start();
fft = new p5.FFT();
fft.setInput(mic);
}
function draw() {
fill(255, 100);
rect(0, 0, width, height);
fft.smooth(0.8);
spectrum = fft.analyze();
//console.log(spectrum.length);
heatmap.colorMode(HSB, 360, 100, 100, 100);
//heatmap.clear();
heatmap.noStroke();
heatmap.fill(0);
drawDot(heatmap, 64);
blendMode(BLEND);
image(heatmap, 0, 0);
}
function drawDot(layer, sampleCount) {
let maxVal = 0;
let maxIndex = 0;
for (i = 0; i < sampleCount; i++) {
bandNum = Math.floor((spectrum.length * spectrumStart) + (spectrum.length * spectrumWidth) / sampleCount * i);
barHeight = fft.getEnergy(bandNum) * 2;
if (barHeight > maxVal) {
maxVal = barHeight;
maxIndex = i;
}
}
let varience = random(2 * Math.PI / sampleCount)*2;
theta = (2 * Math.PI / sampleCount * maxIndex); // + varience;
let maxR = min(width / 2, height / 2) * 1;
let maxDotR = min(width / 2, height / 2) * 2;
let finalRad = maxVal + random(-240, 40);
let x = (maxR - finalRad) * cos(theta) + width / 2;
let y = (maxR - finalRad) * sin(theta) + height / 2;
drawGradient(layer, x, y, maxVal*0.2, c1);
}
function drawGradient(layer, x, y, rad, col) {
if (rad > 0) {
let xxx = int(x);
let yyy = int(y);
let radius = int(rad);
let c = col;
let alph = 100 / radius * 0.1;
let subLayer = createGraphics(radius * 2, radius * 2);
subLayer.colorMode(HSB, 360, 100, 100, 100);
c.setAlpha(alph);
for (let r = radius; r > 0; --r) {
c.setAlpha(alph);
subLayer.noFill();
subLayer.strokeWeight(1);
subLayer.stroke(c);
subLayer.ellipse(radius, radius, r, r);
alph = alph + 100 / radius * 0.1;
}
layer.blendMode(DODGE);
layer.image(subLayer, x - radius, y - radius);
}
}