xxxxxxxxxx
79
let capture;
let previousPixels;
let sounds = [];
let currentFile;
let hue = 360;
let lastFrameKeyPressed = false;
function preload() {
for (let i = 0; i < 5; i++) {
let fileName = "sounds/" + i + ".mp3";
sounds[i] = loadSound(fileName);
}
}
function copyImage(src, dst) {
let n = src.length;
if (!dst || dst.length != n) dst = new src.constructor(n);
while (n--) dst[n] = src[n];
return dst;
}
function setup() {
createCanvas(600, 600);
currentFile = sounds[Math.floor(random(5))];
colorMode(HSB, 360, 100, 100);
capture = createCapture(VIDEO);
capture.size(640, 360);
capture.hide();
pixelDensity(1);
noStroke();
}
function draw() {
capture.loadPixels();
let total = 0;
if (capture.pixels.length > 0) {
// don't forget this!
if (!previousPixels) {
previousPixels = copyImage(capture.pixels, previousPixels);
} else {
let thresholdAmount = 100;
thresholdAmount *= 3; // 3 for r, g, b
for (let y = 50; y < 100; y++) {
for (let x = 100; x < 150; x++) {
let index = 4 * (x + y * width);
// calculate the differences
capture.pixels[index] = 255;
capture.pixels[index+1] = 255;
capture.pixels[index+2] = 255;
let rdiff = Math.abs(
capture.pixels[index + 0] - previousPixels[index + 0]
);
let gdiff = Math.abs(
capture.pixels[index + 1] - previousPixels[index + 1]
);
let bdiff = Math.abs(
capture.pixels[index + 2] - previousPixels[index + 2]
);
// copy the current pixels to previousPixels
previousPixels[index + 0] = capture.pixels[index + 0];
previousPixels[index + 1] = capture.pixels[index + 1];
previousPixels[index + 2] = capture.pixels[index + 2];
let diffs = rdiff + gdiff + bdiff;
if (diffs > thresholdAmount && !currentFile.isPlaying()) {
currentFile = sounds[Math.floor(random(5))];
currentFile.play();
}
}
}
}
}
capture.updatePixels();
image(capture, 0, 0, 640, 480);
}