xxxxxxxxxx
// Global variable to store the classifier
let classifier;
// Label
let label = 'listening...';
let confidence;
let Tastatur = 1;
let Stift = 1;
let img, img_Tastatur, img_Stift;
// Teachable Machine model URL:
let soundModel = 'https://teachablemachine.withgoogle.com/models/UJRwGJgh7/';
function preload() {
// Load the model
classifier = ml5.soundClassifier(soundModel + 'model.json');
//img = loadImage("1F3A4_black.png"); // microphone
img_Tastatur = loadImage("Piktogramm_Tastatur.png"); // clap
img_Stift = loadImage("Piktogramm_Kulli.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 = 150;
// start position
translate(0, height/4);
// left to right
for (i=0; i<Stift; i++) {
let Zeile = floor(i/3);
let Spalte = i-(Zeile*3);
image(img_Stift, Spalte*(icon_size+10)+750, Zeile*180, icon_size, icon_size);
}
// next row
//translate(0, height/4);
for (i=0; i<Tastatur; i++) {
let Zeile = floor(i/3);
let Spalte = i-(Zeile*3);
image(img_Tastatur, Spalte*(icon_size+10)+100, Zeile*180, 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("Tastatur") > -1 && confidence > 0.7) {
Tastatur += 1;
}
else if (label.indexOf("Stift") > -1 && confidence > 0.75) {
Stift += 1;
}
}