<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Real-Time SVG Lip-Sync Demo</title>
<style>
body {
font-family: sans-serif;
display: flex;
flex-direction: column;
align-items: center;
padding: 2rem;
background: #f0f0f0;
}
#controls {
margin-bottom: 1rem;
}
button {
padding: 0.5rem 1rem;
font-size: 1rem;
cursor: pointer;
}
#status {
margin-left: 1rem;
font-style: italic;
}
svg {
width: 200px;
height: 200px;
background: #fff;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div id="controls">
<button id="startBtn">Start</button>
<span id="status">Ready</span>
</div>
<!-- Simple head + mouth -->
<svg viewBox="0 0 100 100">
<!-- Head circle -->
<circle cx="50" cy="50" r="48" fill="#ffe0b3" stroke="#333" stroke-width="1" />
<!-- Mouth path; we'll swap its 'd' attribute -->
<path id="mouth"
d=""
fill="none"
stroke="#333"
stroke-width="2"
stroke-linecap="round"/>
</svg>
<script>
(function(){
// Viseme path data for closed, narrow, wide, round, neutral
const visemes = {
closed: "M30,60 Q50,60 70,60", // flat line
narrow: "M40,55 Q50,65 60,55", // skinny smile
wide: "M20,50 Q50,80 80,50", // big smile
round: "M40,55 A10,10 0 1,0 60,55 A10,10 0 1,0 40,55", // circle
neutral: "M25,60 Q50,65 75,60" // slight curve
};
const mouthEl = document.getElementById("mouth");
const statusEl = document.getElementById("status");
const startBtn = document.getElementById("startBtn");
let audioCtx, analyser, dataArray;
let lastViseme = null;
startBtn.addEventListener("click", () => {
startBtn.disabled = true;
statusEl.textContent = "Requesting mic…";
navigator.mediaDevices.getUserMedia({ audio: true })
.then(stream => {
statusEl.textContent = "Listening…";
audioCtx = new (window.AudioContext || window.webkitAudioContext)();
analyser = audioCtx.createAnalyser();
analyser.fftSize = 1024;
const source = audioCtx.createMediaStreamSource(stream);
source.connect(analyser);
dataArray = new Uint8Array(analyser.frequencyBinCount);
requestAnimationFrame(animate);
})
.catch(err => {
console.error(err);
statusEl.textContent = "Error: " + err.message;
});
});
function animate() {
analyser.getByteFrequencyData(dataArray);
// Frequency bin resolution
const binHz = audioCtx.sampleRate / analyser.fftSize; // ~46.875 Hz
// Define band ranges
const lowEnd = Math.floor(300 / binHz); // up to ~300Hz
const midStart = lowEnd + 1;
const midEnd = Math.floor(2000 / binHz); // up to ~2000Hz
const highStart= midEnd + 1;
let lowSum=0, midSum=0, highSum=0;
// Sum amplitudes in each band
for(let i=0; i<dataArray.length; i++){
if(i <= lowEnd) lowSum += dataArray[i];
else if(i <= midEnd) midSum += dataArray[i];
else if(i <= analyser.frequencyBinCount) highSum += dataArray[i];
}
// Averages
const lowAvg = lowSum / (lowEnd || 1);
const midAvg = midSum / (midEnd - midStart +1 || 1);
const highAvg = highSum / (dataArray.length - highStart || 1);
// Simple thresholds (tweak as needed)
const silenceThreshold = 10; // very quiet = closed mouth
let chosen;
if (lowAvg + midAvg + highAvg < silenceThreshold) {
chosen = "closed";
}
else if (highAvg > midAvg * 1.2) {
chosen = "narrow"; // i/e sounds have more high freq
}
else if (midAvg > lowAvg * 1.2) {
chosen = "wide"; // a/æ are strong mid bands
}
else if (lowAvg > midAvg * 1.2) {
chosen = "round"; // o/u emphasize low frequencies
}
else {
chosen = "neutral"; // fallback
}
// Swap only if changed (can interpolate here instead)
if (chosen !== lastViseme) {
mouthEl.setAttribute("d", visemes[chosen]);
lastViseme = chosen;
}
requestAnimationFrame(animate);
}
})();
</script>
</body>
</html>
Feel free to refine the band thresholds, add smoothing/interpolation between shapes, or swap in your detailed SVG mouth-shape artwork.