xxxxxxxxxx
84
/*
---------------------------------------------------------------------
Interactive Audio Visualization Workshop (Feb 23, 2025)
by Patt Vira @pattvira & Lamtharn Hantrakul @yaboihanoi
Name: Sound Classifier_Teachable Machines Template (no amplitude)
---------------------------------------------------------------------
*/
// Sound classification variables
let classifier;
let predictedSound = "listening";
let confidence = 0;
let confidenceThreshold = 0.8;
let availableSounds = [];
// Teachable Machine model URL - Replace with your model here:
let soundModelURL = "https://teachablemachine.withgoogle.com/models/qXpO5lDq9/";
function preload() {
// Load the model
classifier = ml5.soundClassifier(soundModelURL);
}
function setup() {
createCanvas(400, 400);
classifier.classifyStart(gotResult);
}
function draw() {
background(0);
if (predictedSound === "Background Noise") {
background(0);
} else {
background(150);
if (availableSounds.length > 0) {
let shapeIndex = availableSounds.indexOf(predictedSound);
let shapeColor = color(255, 0, 200);
fill(color(shapeColor));
drawShape(shapeIndex, 100);
}
}
noStroke();
fill(255);
textSize(24);
textAlign(CENTER, CENTER);
text(predictedSound, width / 2, height / 2);
}
function drawShape(index, s) {
let x = width / 2;
let y = height / 2;
switch (index % 3) {
case 0: ellipse(x, y, s, s); break;
case 1: rectMode(CENTER); rect(x, y, s, s); break;
case 2: triangle(x - s / 2, y + s / 2, x + s / 2, y + s / 2, x, y - s / 2); break;
}
}
function gotResult(results) {
if (results.length > 0) {
let newConfidence = results[0].confidence;
let newSound = results[0].label;
// Only update if confidence exceeds threshold
if (newConfidence > confidenceThreshold) {
predictedSound = newSound;
confidence = newConfidence;
}
// Store all available classes once
if (availableSounds.length === 0) {
availableSounds = results.map(r => r.label)
.filter(label => label !== "Background Noise")
.sort();
print(availableSounds);
}
}
}