xxxxxxxxxx
125
let positions;
let videoInput;
let e1, e2, e3, e4;
let eyes = []
let f = 200; // in Hertz
let scale = [];
let n = 12;
function setup() {
cnv = createCanvas(800, 600);
cnv.position(0, 0)
noStroke();
eyes.push(new Eye(40, 50, 60, [255,0,0]));
eyes.push(new Eye(110, 50, 60, [255,0,0]));
eyes.push(new Eye(164, height-50, 40, [255,255,0]));
eyes.push(new Eye(220, height-50, 40, [255,255,0]));
eyes.push(new Eye(width-50, 50, 20, [0,0,255]));
eyes.push(new Eye(width-80, 50, 20, [0,0,255]));
eyes.push(new Eye(width-100, height-50, 100, [0,255,0]));
eyes.push(new Eye(width-230, height-50, 100, [0,255,0]));
// setup camera capture
videoInput = createCapture(VIDEO);
videoInput.size(800, 600);
videoInput.position(0, 0);
// setup tracker
ctracker = new clm.tracker();
console.log(ctracker)
ctracker.init(pModel);
ctracker.start(videoInput.elt);
// // 1. Derive pentatonic scale
scale[0] = f;
scale[3] = f * 3 / 2;
scale[5] = f * 2;
scale[2] = scale[5] * 2 / 3;
scale[1] = scale[3] * 3 / 4;
scale[4] = scale[1] * 3 / 2;
console.log("pentatonic scale: ", scale);
osc = new p5.Oscillator();
}
function keyPressed(){
let pos = parseInt(key) % n - 1;
console.log("current frequency: ", scale[pos]);
osc.freq(scale[pos]);
osc.setType('sine');
osc.amp(0.5);
osc.start();
}
function keyReleased(){
osc.stop();
}
function draw() {
background(102);
// get array of face marker positions [x, y] format
positions = ctracker.getCurrentPosition();
if (positions) {
outputX = positions[62][0];
outputX = width - outputX;
outputY = positions[62][1];
ellipse(outputX, outputY, 20)
// e1.update(outputX, outputY);
// e2.update(outputX, outputY);
// e3.update(outputX, outputY);
// e4.update(outputX, outputY);
// e1.display();
// e2.display();
// e3.display();
// e4.display();
}
for (const eye of eyes) {
eye.update(mouseX, mouseY);
eye.display();
}
}
function Eye(tx, ty, ts, c) {
this.x = tx;
this.y = ty;
this.size = ts;
this.angle = 0;
this.c = c;
this.update = function(mx, my) {
this.angle = atan2(my - this.y, mx - this.x);
};
this.display = function() {
push();
translate(this.x, this.y);
fill(255);
ellipse(0, 0, this.size, this.size);
rotate(this.angle);
fill(this.c);
ellipse(this.size / 4, 0, this.size / 2, this.size / 2);
pop();
};
}