xxxxxxxxxx
74
class VoiceListener {
constructor() {
this.options = {
probabilityThreshold: 0.9
};
// Teachable Machine model URL:
this.soundModel = 'https://teachablemachine.withgoogle.com/models/edw86v-Sl/';
// load model
this.classifier = ml5.soundClassifier(this.soundModel + 'model.json', this.options);
// start to classify
this.classify();
}
classify() {
this.classifier.classify(this.makeResult);
}
// The model recognizing a sound will trigger this event
makeResult(error, results) {
/*
이 함수는 객체 내부 함수 접근이 안됨
*/
if (error) {
console.error(error);
return;
}
// The results are in an array ordered by confidence.
// console.log(results[0]);
voiceLabel = results[0].label;
print("label: " + voiceLabel);
print("confidence: " + results[0].confidence);
// player move
player1Move(voiceLabel);
}
show() {
// // Draw the label in the canvas
fill(255);
textSize(32);
textAlign(CENTER, CENTER);
text(voiceLabel, width / 2, height / 2);
}
}
function player1Move(label) {
if (label === "Background Noise") {
return;
}
let dir;
switch (label) {
case "up":
dir = 0;
break;
case "right":
dir = 1;
break;
case "down":
dir = 2;
break;
case "left":
dir = 3;
break;
}
player1.move(dir);
}