xxxxxxxxxx
// Global variable to store the classifier
let classifier;
// Label
let label = 'listening...';
let confidence;
let hahn = 3;
let taube = 5;
let img, img_hahn, img_taube;
// Teachable Machine model URL:
let soundModel = 'https://teachablemachine.withgoogle.com/models/uOFvz1eDY/';
function preload() {
// Load the model
classifier = ml5.soundClassifier(soundModel + 'model.json');
img = loadImage("Vögel_Icons_V1-04.png"); // microphone
img_hahn = loadImage("Vögel_Icons_V1-02.png"); // clap
img_taube = loadImage("Vögel_Icons_V1-03.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<hahn; i++) {
image(img_hahn, i*icon_size, 0, icon_size, icon_size);
}
// next row
translate(0, icon_size*2);
for (i=0; i<taube; i++) {
image(img_taube, 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("clap") > -1 && confidence > 0.9) {
clap += 1;
}
else if (label.indexOf("snap") > -1 && confidence > 0.9) {
snap += 1;
}
}