xxxxxxxxxx
52
let video;
let mic;
let slider;
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO);
video.size(width, height);
mic = new p5.AudioIn();
mic.start();
slider = createSlider(0, 255, 100);
slider.position(10, height + 10);
slider.style('width', width - 20 + 'px');
}
function draw() {
background(220);
video.loadPixels();
loadPixels();
let volume = mic.getLevel();
let threshold = map(volume, 0, 1, 0, 255);
slider.value(threshold);
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
let index = (x + y * width) * 4;
let r = video.pixels[index + 0];
let g = video.pixels[index + 1];
let b = video.pixels[index + 2];
let a = video.pixels[index + 3];
let bright = (r + g + b) / 3;
if (bright > threshold) {
pixels[index + 0] = r;
pixels[index + 1] = g;
pixels[index + 2] = b;
pixels[index + 3] = a;
} else {
pixels[index + 0] = 0;
pixels[index + 1] = 0;
pixels[index + 2] = 0;
pixels[index + 3] = a;
}
}
}
updatePixels();
}
function keyPressed() {
if (key == ' ') {
saveCanvas('myCanvas', 'png');
}
}