xxxxxxxxxx
100
let capture;
let previousPixels;
let sounds = [];
let currentFile;
let hue = 360;
let lastFrameKeyPressed = false;
let xrec= 0;
let yrec= 0;
function preload() {
for (let i = 0; i < 5; i++) {
let fileName = "sounds/" + i + ".mp3";
print(fileName);
sounds[i] = loadSound(fileName);
}
}
function setup() {
createCanvas(640, 450);
capture = createCapture(VIDEO);
capture.size(640, 360);
capture.hide();
pixelDensity(1);
noStroke();
currentFile = sounds[Math.floor(random(5))];
}
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 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 = 50;
thresholdAmount *= 3; // 3 for r, g, b
for (let y = 0; y < capture.height; y++) {
for (let x = 0; x < capture.width; x++) {
let index = 4 * (x + y * width);
// calculate the differences
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]
);
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;
let output = 0;
if (diffs > thresholdAmount) {
output = 255;
total += diffs;
xrec = x;
yrec = y;
// console.log(x,y)
}
capture.pixels[index] = output;
capture.pixels[index + 1] = output;
capture.pixels[index + 2] = output;
}
}
}
}
// need this because sometimes the frames are repeated
if (total > 0) {
capture.updatePixels();
image(capture, 0, 0, 640, 480);
}
let avgMotion = total / capture.pixels.length;
// rect(0, height - avgMotion * 50, 100, height);
// console.log(avgMotion);
if (avgMotion >= 0.51 && !currentFile.isPlaying()) {
currentFile = sounds[Math.floor(random(5))];
currentFile.play();
hue = random(360);
fill(0,255,0);
rect(xrec, yrec, 100, 100, 100);
// console.log(xrec,yrec);
print(xrec,yrec);
}
}