xxxxxxxxxx
54
let classifier;
// Label
let label = 'listening...';
// Teachable Machine model URL:
let soundModel = 'https://teachablemachine.withgoogle.com/models/CRxZak9cc/';
let helloVoice, byeVoice;
function preload() {
// Load the model
classifier = ml5.soundClassifier(soundModel + 'model.json');
helloVoice = loadSound("voice/helloVoice.m4a");
byeVoice = loadSound("voice/byeVoice.m4a");
helloVoice.playMode("restart");
byeVoice.playMode("restart");
}
function setup() {
createCanvas(320, 240);
// Start classifying
// The sound model will continuously listen to the microphone
classifier.classify(gotResult);
}
function draw() {
background(0);
// Draw the label in the canvas
fill(255);
textSize(32);
textAlign(CENTER, CENTER);
text(label, width / 2, height / 2);
if(label === "hello") {
helloVoice.play();
} else if(label === "bye") {
byeVoice.play();
}
}
// 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;
}