xxxxxxxxxx
116
let cam;
let mic, fft;
let a = 0;
let xScale;
let yScale;
let inTouch = false;
let clicked = false;
let spectrum;
function setup() {
createCanvas(1280, 960);
mic = new p5.AudioIn();
fft = new p5.FFT();
fft.setInput(mic);
fill(map(mouseX,0,width,45,50),0,0);
cam = createCapture(VIDEO);
cam.size(45, 50);
cam.hide();
xScale = width / cam.width;
yScale = height / cam.height;
}
function mouseClicked() {
mic.start();
}
function draw() {
//background(220);
if (clicked == false)
{
spectrum = fft.analyze();
beginShape();
for (i = 0; i < spectrum.length; i++) {
vertex(i, map(spectrum[i], 0, 200, height, 10));
//print(spectrum[i]);
}
endShape();
let spectrumTwo = fft.analyze();
beginShape();
for (i = -500; i < spectrum.length; i++) {
vertex(i, map(spectrum[i], 0, 200, width, 10));
}
endShape();
}
cam.loadPixels();
for (let y = 0; y < cam.height; y++) {
for (let x = cam.width; x >= 0; x--) {
let i = (x + y * cam.width) * 4;
let r = cam.pixels[i + 0];
let g = cam.pixels[i + 1];
let b = cam.pixels[i + 2];
// noStroke();
let avg = (r + g + b) / 3
push();
rotate(a);
translate(x,y);
// fill(map(mouseX,0,width/2,0,r),50,r);
//blendMode(SCREEN);
stroke(avg);
line((cam.width - x - 1) * random(15) * xScale) + xScale * 2,
(y * yScale) + yScale ,
map(mouseX, random(5), width, 5, xScale + 1 ),
map(mouseY, 0, height, random(45), yScale - random(20));
triangle((cam.width - x - 5) *xScale, y * yScale,
map(mouseX, 0, width, 3, xScale - random(10)) * random(2)*xScale,
map(mouseY, 0, height,0.5, xScale + 1) * yScale);
// fill(map(mouseY,0,width/2,0,b),10,60);
fill(avg);
triangle((cam.width - x + 15) * xScale, y * yScale,
map(mouseX, 0, width, random(3), xScale - random(15)) * xScale,
map(mouseY, 0, height, 4, yScale - 2) * random(4) * yScale);
fill(avg);
line((cam.width - x - 25) * 12 * xScale) + xScale ,
(y * yScale) + yScale ,
map(mouseX, 0, width, 5, xScale + 1 ),
map(mouseY, 0, height, random(25), yScale - 2);
pop();
}
}
function touchStarted() {
inTouch = true;
return false;
}
function touchEnded() {
inTouch = false;
return false;
clear();
}
cam.updatePixels();
}