xxxxxxxxxx
353
let canvas;
let mano_x = [];
let mano_y = [];
let index_dita = [4, 8, 12, 16, 20]; //pollice, indice, medio, anulare, mignolo
let campione = [5];
let punto_corda_x = [];
let punto_corda_y = [];
let quiete_x = [];
let quiete_y = [];
let amp_x = [];
let amp_y = [];
let conta_pi = [];
let vibra = 4;
let amp_vibrazione = 30;
let soglia_trigger = 0.25;
let trigger = []
let trigger_pre = [];
let lunghezza = [];
let freq = [];
let durata = [];
let volume = 0.2;
let incr_freq = [0, 0.1, 0.2, 0.3, 0.5];
let ipotenusa;
let audio_attivo = false;
let sketch = function(p) {
p.setup = function() {
canvas = p.createCanvas(window.innerWidth, window.innerHeight);
canvas.id("canvas");
canvas.mouseClicked(p.attiva_audio);
p.getAudioContext().suspend();
p.colorMode(p.HSB);
for (let i = 0; i < 3; i++) {
mano_x[i] = [];
mano_y[i] = [];
for (let j = 0; j < 22; j++) {
mano_x[i][j] = 0;
mano_y[i][j] = 0;
}
}
for(let i = 0; i < 5; i++) {
campione[i] = p.loadSound('Chit_js.mp3');
conta_pi[i] = p.PI;
trigger[i] = 0;
trigger_pre[i] = 0;
amp_x[i] = 0;
amp_y[i] = 0;
}
ipotenusa = p.sqrt(p.width * p.width + p.height * p.height) * 0.8;
} //fine del setup
p.draw = function() {
p.clear();
if (audio_attivo == false) {
p.fill(255);
p.strokeWeight(1);
p.stroke(0);
p.rectMode(p.CENTER);
p.rect(p.width * 0.5, p.height * 0.5 - 12, p.width * 0.5, 40);
p.fill(0);
p.stroke(0);
p.strokeWeight(1);
p.textSize(24);
p.textFont('Helvetica Neue');
p.textAlign(p.CENTER);
p.text("CLICCA SULLO SCHERMO PER ATTIVARE L'AUDIO", p.width * 0.5, p.height * 0.5);
}
if(detections != undefined) {
if(detections.multiHandLandmarks != undefined) {
//p.drawHands();
//p.drawParts();
p.drawLines([0, 5, 9, 13, 17, 0]);//palmo
p.drawLines([0, 1, 2, 3 ,4]);//pollice
p.drawLines([5, 6, 7, 8]);//indice
p.drawLines([9, 10, 11, 12]);//medio
p.drawLines([13, 14, 15, 16]);//anulare
p.drawLines([17, 18, 19, 20]);//mignolo
p.drawLandmarks([0, 1], 0);//palmo
p.drawLandmarks([1, 5], 60);//pollice
p.drawLandmarks([5, 9], 120);//indice
p.drawLandmarks([9, 13], 180);//medio
p.drawLandmarks([13, 17], 240);//anulare
p.drawLandmarks([17, 21], 300);//mignolo
//------- memorizzo variabili dei punti delle mani---------//
for(let i = 0; i < detections.multiHandLandmarks.length; i++) {
for(let j = 0; j < 21; j++) {
mano_x[i][j] = detections.multiHandLandmarks[i][j].x * p.width;
mano_y[i][j] = detections.multiHandLandmarks[i][j].y * p.height;
}
}
//----------sintesi video-----------//
p.strokeWeight(1);
p.fill(255, 255, 255);
p.stroke(255, 255, 255);
p.ellipse(p.width * 0.5, p.height * 0.35, 30, 30);
if (detections.multiHandLandmarks.length == 2) { //se l'algoritmo legge due mani
p.stroke(0, 0, 255);
p.strokeWeight(2);
for (let i = 0; i < 5; i++) {
p.stroke(0, 0, 255);
p.strokeWeight(2);
p.fill(1);
p.stroke(255, 255 * trigger[i], 255);
quiete_x[i] = (mano_x[0][index_dita[i]] + mano_x[1][index_dita[i]]) * 0.5;
quiete_y[i] = (mano_y[0][index_dita[i]] + mano_y[1][index_dita[i]]) * 0.5;
amp_x[i] = amp_x[i] * 0.98;
amp_y[i] = amp_y[i] * 0.98;
conta_pi[i]++;
if (trigger[i] == 1 && trigger_pre[i] == 0) {
amp_x[i] = amp_vibrazione;
amp_y[i] = amp_vibrazione;
conta_pi[i] = p.PI;
}
if (trigger[i] == 1) {
punto_corda_x[i] = quiete_x[i] + amp_vibrazione;
punto_corda_y[i] = quiete_y[i] + amp_vibrazione;
amp_x[i] = (punto_corda_x[i] - quiete_x[i])
amp_y[i] = (punto_corda_y[i] - quiete_y[i])
conta_pi[i] = p.PI;
}
punto_corda_x[i] = quiete_x[i] + amp_x[i] * p.sin(vibra * conta_pi[i]);
punto_corda_y[i] = quiete_y[i] + amp_y[i] * p.sin(vibra * conta_pi[i]);
p.noFill();
p.beginShape();
p.curveVertex(mano_x[0][index_dita[i]], mano_y[0][index_dita[i]]);
p.curveVertex(mano_x[0][index_dita[i]], mano_y[0][index_dita[i]]);
p.curveVertex(punto_corda_x[i], punto_corda_y[i]);
p.curveVertex(mano_x[1][index_dita[i]], mano_y[1][index_dita[i]]);
p.curveVertex(mano_x[1][index_dita[i]], mano_y[1][index_dita[i]]);
p.endShape();
lunghezza[i] = p.calcola_lunghezza(mano_x[0][index_dita[i]], mano_x[1][index_dita[i]],
mano_y[0][index_dita[i]], mano_y[1][index_dita[i]]);
//----------------------- sintesi audio -------------------//
//da rendere non lineare
freq[i] = p.map(lunghezza[i], 0, ipotenusa, 1.2, 0.2);
//da aggiustare in base al numero di trigger attivi
volume = 1/5;
//se voglio la freq con effetto detuning
campione[i].rate(freq[i] + incr_freq[i]);
if (trigger[i] == 1 && trigger_pre[i] == 0) { //&& campione[i].isPlaying() == false) {
campione[i].setVolume(volume);
//campione[i].rate(freq[i]); //se voglio la freq fissa
campione[i].play();
}
trigger_pre[i] = trigger[i];
trigger[i] = 0;
}// fine for dita
//-------------calcolo punto su linea----------------//
for (let i = 0; i < 5; i++) {
trigger[i] = p.calcola_punto_retta(p.floor(mano_x[0][index_dita[i]]), p.floor(mano_y[0][index_dita[i]]),
p.floor(mano_x[1][index_dita[i]]), p.floor(mano_y[1][index_dita[i]]),
p.floor(p.width * 0.5), p.floor(p.height * 0.35),
trigger[i]);
}
} //fine if due mani
}//fine if mani presenti
} //fine if algoritmo funzionante
//p.print(trigger);
} //fine del draw
p.drawHands = function() { //stampo i numeri relativi ai punti delle dita
for (let i = 0; i < detections.multiHandLandmarks.length; i++) {
for (let j = 0; j < detections.multiHandLandmarks[i].length; j++) {
let x = detections.multiHandLandmarks[i][j].x * p.width;
let y = detections.multiHandLandmarks[i][j].y * p.height;
let z = detections.multiHandLandmarks[i][j].z;
p.strokeWeight(0);
p.textFont('Helvetica Neue');
p.text(j, x, y);
p.stroke(255);
p.strokeWeight(10);
p.point(x, y);
}
}
}
p.drawLandmarks = function(indexArray, hue) {
p.noFill();
p.strokeWeight(8);
p.colorMode(p.HSB);
for(let i = 0; i < detections.multiHandLandmarks.length; i++) {
for(let j = indexArray[0]; j < indexArray[1]; j++) {
let x = detections.multiHandLandmarks[i][j].x * p.width;
let y = detections.multiHandLandmarks[i][j].y * p.height;
// let z = detections.multiHandLandmarks[i][j].z;
p.stroke(hue, 40, 255);
p.ellipse(x, y, 1, 1);
}
}
}
p.drawLines = function(index) {
p.stroke(0, 0, 255);
p.strokeWeight(0.5);
p.colorMode(p.HSB);
for(let i=0; i<detections.multiHandLandmarks.length; i++){
for(let j=0; j<index.length-1; j++){
let x = detections.multiHandLandmarks[i][index[j]].x * p.width;
let y = detections.multiHandLandmarks[i][index[j]].y * p.height;
let _x = detections.multiHandLandmarks[i][index[j+1]].x * p.width;
let _y = detections.multiHandLandmarks[i][index[j+1]].y * p.height;
p.line(x, y, _x, _y);
}
}
}
p.attiva_audio = function () {
if (audio_attivo == false) {
audio_attivo = true;
if (p.getAudioContext().state !== 'running') {
p.userStartAudio();
}
}
else {
audio_attivo = false;
p.getAudioContext().suspend();
}
}
p.calcola_lunghezza = function(x1, x2, y1, y2) {
return p.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
p.calcola_punto_retta = function(x0, y0, x1, y1, x2, y2, trigga) {
let A = (x2 - x1) / (x0 - x1);
let B = (y2 - y1) / (y0 - y1);
if (trigga == 0) {
if ((p.abs(x2 - x1) <= soglia_trigger) && (p.abs(x0 - x1) == 0)) {
// il segmento tende a essere verticale
if (y0 < y1) {
if (y2 > y0 && y2 < y1) {
trigga = 1;
}
else {
if (y2 < y0 && y2 > y1) {
trigga = 1;
}
}
}
}
if ((p.abs(y2 - y1) <= soglia_trigger) && (p.abs(y0 - y1) == 0)) {
// il segmento tende a essere orizzontale
if (x0 < x1) {
if (x2 > x0 && x2 < x1) {
trigga = 1;
}
else {
if (x2 < x0 && x2 > x1) {
trigga = 1;
}
}
}
}
if (p.abs(A - B) <= soglia_trigger) {
if (x0 < x1) {
if ((x2 > x0) && (x2 < x1)) {
trigga = 1;
}
}
else {
if ((x2 < x0) && (x2 > x1)) {
trigga = 1;
}
}
}
}
return trigga;
}
} //fine sketch
let myp5 = new p5(sketch);