Hi,
I’m learning programming and I’m stuck in one place. According to the code Cassie wrote on the webinar (Creative Coding in p5.js - GitHub Satellite 2020 - YouTube), the sound comes when you click. I want the sound to go on all the time the program detects “open mouth”. How to do it?
And the second question - this code seems to be quite short, but even so, it happens quite often that the preview freezes completely. Only after refreshing the browser window (program restart) does it start to function properly. What it comes from? How can I improve the smooth running of the program?
Below I put the code to the program.
let osc, playing, freq, amp;
let capture;
let ctracker;
let positions;
function setup() {
let cnv = createCanvas(400, 225);
cnv.mousePressed(playOscillator);
osc = new p5.Oscillator("sawtooth");
capture = createCapture(VIDEO);
capture.size(400, 225);
capture.hide();
ctracker = new clm.tracker();
ctracker.init();
ctracker.start(capture.elt);
}
function draw() {
background(600);
image(capture, 0, 0);
positions = ctracker.getCurrentPosition();
let mouthDist;
if (positions) {
positions.forEach((pos) => {
fill(255, 0, 0);
noStroke();
circle(pos[0], pos[1], 3);
});
const mouthTop = createVector(positions[60][0], positions[60][1]);
const mouthBottom = createVector(positions[57][0], positions[57][1]);
mouthDist = mouthTop.sub(mouthBottom).mag();
}
freq = constrain(map(mouthDist, 4, 27, 50, 500), 100, 500);
amp = constrain(map(mouthDist, 4, 27, 0, 0.5), 0, 1);
if (playing) {
osc.freq(freq, 0.1);
osc.amp(amp, 0.1);
}
}
function playOscillator() {
osc.start();
playing = true;
}
function mouseReleased() {
osc.amp(0, 0.5);
playing = false;
}