xxxxxxxxxx
// Global variable to store the classifier
let classifier;
// Label
let label = 'listening...';
let confidence;
let Vibration = 0;
let Klingeln = 0;
let img, img_Vibration, img_Klingeln;
// Teachable Machine model URL:
let soundModel = 'https://teachablemachine.withgoogle.com/models/NdztiyQf4/';
function preload() {
// Load the model
classifier = ml5.soundClassifier(soundModel + 'model.json');
img = loadImage("Handy.png"); // microphone
img_Vibration = loadImage("VHandy.png"); // clap
img_Klingeln = loadImage("KHandy.png"); // snap
}
function setup() {
createCanvas(windowWidth, windowHeight);
// Start classifying
// The sound model will continuously listen to the microphone
classifier.classify(gotResult);
}
function draw() {
background(200);
imageMode(CENTER);
let icon_size = 50;
// start position
translate(100, height/4);
// left to right
for (i=0; i<Klingeln; i++) {
image(img_Klingeln, i*icon_size, 0, icon_size, icon_size);
}
// next row
translate(0, icon_size*2);
for (i=0; i<Vibration; i++) {
image(img_Vibration, i*icon_size, 0, icon_size, icon_size);
}
/*
// colums
let colums = 5;
push();
translate(width/2 - colums*icon_size - icon_size, height/4);
for (i=1; i<=snap; i++) {
image(img_snap, ((i-1)%colums)*icon_size, 0, icon_size, icon_size);
if (i%colums == 0)
translate(0, icon_size); // next row
}
pop();
push();
translate(width/2 + icon_size, height/4);
for (i=1; i<=clap; i++) {
image(img_clap, ((i-1)%colums)*icon_size, 0, icon_size, icon_size);
if (i%colums == 0)
translate(0, icon_size); // next row
}
pop();
*/
//textSize(50);
//textAlign(CENTER, CENTER);
//text("Clap: " + clap, width / 2, height / 3);
//text("Snap: " + snap, width / 2, height / 2);
}
// The model recognizing a sound will trigger this event
function gotResult(error, results) {
if (error) {
console.error(error);
return;
}
// The results are in an array ordered by confidence.
// console.log(results[0]);
label = results[0].label;
confidence = nf(results[0].confidence, 0, 2);
//print(label + " / " + confidence);
if (label.indexOf("Vibration") > -1 && confidence > 0.9) {
Vibration += 1;
//print("clap");
//img = img_clap;
}
else if (label.indexOf("Klingeln") > -1 && confidence > 0.9) {
Klingeln += 1;
//print("snap");
//img = img_snap;
}
}