Spaces:
Running

Granary-HiFi / sed_pipeline_explainer.js
sasha-meister's picture
Upload sed_pipeline_explainer.js with huggingface_hub
a1dae35 verified
Raw History Blame Contribute Delete
28.8 kB
(function () {
"use strict";
const DURATION = 48;
const W = 1600;
const H = 900;
const C = {
bg: "#000000",
panel: "#111111",
grid: "#343434",
muted: "#b3b3b3",
white: "#ffffff",
green: "#76b900",
cyan: "#42d3ff",
teal: "#4de2c5",
gold: "#ffd166",
coral: "#ff6b6b",
blue: "#4f8cff",
purple: "#b9a7ff",
orange: "#ff9f43",
};
const CHANNELS = [
["Speech", C.teal],
["Male", C.blue],
["Female", C.purple],
["Child", C.green],
["Conversation", C.orange],
["Narration", C.gold],
["Synthesizer", C.cyan],
];
const EVENTS = [
[0.86, 2.68, C.cyan],
[3.12, 5.28, C.teal],
[6.04, 6.68, C.gold],
];
const clamp = (v, a = 0, b = 1) => Math.max(a, Math.min(b, v));
const smooth = (v) => {
v = clamp(v);
return v * v * (3 - 2 * v);
};
const phase = (t, a, b) => smooth((t - a) / (b - a));
const lerp = (a, b, t) => a + (b - a) * t;
const gauss = (x, m, s) => Math.exp(-Math.pow((x - m) / s, 2));
function waveform(t) {
const e = 0.05 + 0.82 * gauss(t, 1.65, 0.68) +
0.66 * gauss(t, 4.15, 1.10) + 0.30 * gauss(t, 6.35, 0.36);
return e * (0.57 * Math.sin(2 * Math.PI * 4.2 * t) +
0.27 * Math.sin(2 * Math.PI * 9.1 * t) +
0.11 * Math.sin(2 * Math.PI * 15.4 * t));
}
function speechProb(t) {
return clamp(0.035 + 0.80 * gauss(t, 1.68, 0.72) +
0.72 * gauss(t, 4.18, 1.12) + 0.48 * gauss(t, 6.34, 0.34));
}
function channelProb(index, t) {
if (index === 0) return clamp(0.025 + 0.62 * gauss(t, 1.65, 0.74) +
0.55 * gauss(t, 4.15, 1.14) + 0.30 * gauss(t, 6.35, 0.37));
const center = 0.65 + index * 0.82;
const base = 0.025 + (0.055 + index * 0.014) * gauss(t, center, 0.48);
const texture = 0.018 * (1 + Math.sin(t * (2.0 + index * 0.3) + index));
const eventEnergy = (index === 1 || index === 5) ? 0.14 * gauss(t, 6.35, 0.30) : 0;
return clamp(base + texture + eventEnergy, 0.01, 0.64);
}
function font(size, weight = 400) {
return `${weight} ${size}px "NVIDIA Sans", Inter, Arial, sans-serif`;
}
function text(ctx, value, x, y, size, color = C.white, align = "center", weight = 400) {
ctx.save();
ctx.font = font(size, weight);
ctx.fillStyle = color;
ctx.textAlign = align;
ctx.textBaseline = "middle";
ctx.fillText(value, x, y);
ctx.restore();
}
function roundedRect(ctx, x, y, w, h, r, fill, stroke, lineWidth = 1) {
ctx.beginPath();
ctx.roundRect(x, y, w, h, r);
if (fill) {
ctx.fillStyle = fill;
ctx.fill();
}
if (stroke) {
ctx.strokeStyle = stroke;
ctx.lineWidth = lineWidth;
ctx.stroke();
}
}
function pill(ctx, value, x, y, color, size = 20, width) {
ctx.save();
ctx.font = font(size, 700);
const w = width || ctx.measureText(value).width + 30;
ctx.globalAlpha = 0.95;
roundedRect(ctx, x - w / 2, y - 18, w, 36, 10, "#101010", color, 1.5);
text(ctx, value, x, y + 1, size, color, "center", 700);
ctx.restore();
}
function heading(ctx, eyebrow, title, alpha = 1) {
ctx.save();
ctx.globalAlpha = alpha;
text(ctx, eyebrow, W / 2, 45, 19, C.white, "center", 700);
text(ctx, title, W / 2, 91, 42, C.green, "center", 700);
ctx.restore();
}
function transformHeading(ctx, t, start, previous, next) {
const p = phase(t, start, start + .8);
heading(ctx, previous[0], previous[1], 1 - p);
heading(ctx, next[0], next[1], p);
}
function plotRect() {
return { x: 125, y: 155, w: 1350, h: 545 };
}
function drawAxes(ctx, probability = false, alpha = 1) {
const r = plotRect();
ctx.save();
ctx.globalAlpha = alpha;
ctx.strokeStyle = C.grid;
ctx.lineWidth = 1;
for (let i = 0; i <= 8; i++) {
const x = r.x + (i / 8) * r.w;
ctx.beginPath();
ctx.moveTo(x, r.y);
ctx.lineTo(x, r.y + r.h);
ctx.stroke();
text(ctx, `${i} s`, x, r.y + r.h + 26, 15, C.muted);
}
const lines = probability ? 4 : 2;
for (let i = 0; i <= lines; i++) {
const y = r.y + (i / lines) * r.h;
ctx.beginPath();
ctx.moveTo(r.x, y);
ctx.lineTo(r.x + r.w, y);
ctx.stroke();
}
if (probability) {
text(ctx, "1.0", r.x - 24, r.y, 14, C.muted, "right");
text(ctx, "0.5", r.x - 24, r.y + r.h / 2, 14, C.muted, "right");
text(ctx, "0", r.x - 24, r.y + r.h, 14, C.muted, "right");
}
ctx.restore();
}
function drawWave(ctx, color = C.cyan, alpha = 1, ranges) {
const r = plotRect();
ctx.save();
ctx.strokeStyle = color;
ctx.globalAlpha = alpha;
ctx.lineWidth = 3;
ctx.shadowColor = color;
ctx.shadowBlur = 10;
ctx.beginPath();
let started = false;
for (let i = 0; i <= 700; i++) {
const t = 8 * i / 700;
const visible = !ranges || ranges.some(([a, b]) => t >= a && t <= b);
if (!visible) {
started = false;
continue;
}
const x = r.x + r.w * t / 8;
const y = r.y + r.h / 2 - waveform(t) * r.h * 0.42;
if (!started) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
started = true;
}
ctx.stroke();
ctx.restore();
}
function drawLegend(ctx, alpha = 1) {
const y = 854;
const totalW = 1030;
const start = (W - totalW) / 2;
ctx.save();
ctx.globalAlpha = alpha;
roundedRect(ctx, start - 24, y - 25, totalW + 48, 48, 12, "#111111ee", "#333333");
CHANNELS.forEach(([name, color], i) => {
const x = start + i * (totalW / CHANNELS.length);
ctx.strokeStyle = color;
ctx.lineWidth = 7;
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x + 28, y);
ctx.stroke();
text(ctx, name, x + 39, y, 15, C.white, "left", 600);
});
ctx.restore();
}
function drawMel(ctx, amount, alpha = 1) {
const r = plotRect();
const rows = 16;
const cols = 58;
ctx.save();
ctx.globalAlpha = alpha;
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const t = 8 * col / (cols - 1);
const energy = 0.08 + 0.78 * gauss(t, 1.65, 0.76) +
0.62 * gauss(t, 4.15, 1.18) + 0.23 * gauss(t, 6.35, 0.40);
const texture = 0.5 + 0.5 * Math.sin(0.58 * col + 0.83 * row);
const opacity = clamp(energy * (0.24 + 0.76 * texture), 0.04, 0.97);
const targetX = r.x + col * r.w / cols;
const targetY = r.y + (rows - 1 - row) * r.h / rows;
const sourceY = r.y + r.h / 2 - waveform(t) * r.h * 0.42;
const x = targetX;
const y = lerp(sourceY, targetY, amount);
const cellW = lerp(4, r.w / cols + 1, amount);
const cellH = lerp(4, r.h / rows + 1, amount);
ctx.fillStyle = row > 8 ? `rgba(185,167,255,${opacity})` : `rgba(79,140,255,${opacity})`;
ctx.fillRect(x, y, cellW, cellH);
}
}
ctx.restore();
}
function drawMelCollapse(ctx, amount, alpha = 1) {
const r = plotRect();
const rows = 16;
const cols = 58;
ctx.save();
ctx.globalAlpha = alpha;
for (let row = 0; row < rows; row++) {
for (let col = 0; col < cols; col++) {
const t = 8 * col / (cols - 1);
const energy = 0.08 + 0.78 * gauss(t, 1.65, 0.76) +
0.62 * gauss(t, 4.15, 1.18) + 0.23 * gauss(t, 6.35, 0.40);
const texture = 0.5 + 0.5 * Math.sin(0.58 * col + 0.83 * row);
const opacity = clamp(energy * (0.24 + 0.76 * texture), 0.04, 0.97);
const cellW = r.w / cols + 1;
const x = r.x + col * r.w / cols;
const sourceY = r.y + (rows - 1 - row) * r.h / rows;
const targetY = r.y + r.h - 5;
const y = lerp(sourceY, targetY, amount);
const cellH = lerp(r.h / rows + 1, 7, amount);
ctx.fillStyle = row > 8 ? `rgba(185,167,255,${opacity})` : `rgba(79,140,255,${opacity})`;
ctx.fillRect(x, y, cellW, cellH);
}
}
ctx.restore();
}
function drawCurve(ctx, fn, color, width = 2, alpha = 1, clipRanges) {
const r = plotRect();
ctx.save();
ctx.strokeStyle = color;
ctx.lineWidth = width;
ctx.globalAlpha = alpha;
ctx.beginPath();
let started = false;
for (let i = 0; i <= 240; i++) {
const t = 8 * i / 240;
const visible = !clipRanges || clipRanges.some(([a, b]) => t >= a && t <= b);
if (!visible) {
started = false;
continue;
}
const x = r.x + r.w * t / 8;
const y = r.y + r.h * (1 - fn(t));
if (!started) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
started = true;
}
ctx.stroke();
ctx.restore();
}
function drawEvents(ctx, alpha = 1, labels = "speech event") {
const r = plotRect();
ctx.save();
ctx.globalAlpha = alpha;
EVENTS.forEach(([a, b, color], i) => {
const x = r.x + r.w * a / 8;
const w = r.w * (b - a) / 8;
ctx.fillStyle = color + "18";
ctx.fillRect(x, r.y, w, r.h);
ctx.strokeStyle = color;
ctx.lineWidth = 2;
ctx.strokeRect(x, r.y, w, r.h);
text(ctx, labels === "speech event" ? `speech event ${i + 1}` : `E${i + 1} class channels`,
x + w / 2, r.y - 19, labels === "speech event" ? 17 : 20, color, "center", 700);
});
ctx.restore();
}
function drawEventCurvesToStrips(ctx, p, eventAlphas = [1, 1, 1]) {
const r = plotRect();
EVENTS.forEach(([a, b, eventColor], eventIndex) => {
const eventAlpha = eventAlphas[eventIndex];
if (eventAlpha <= 0) return;
const x = r.x + r.w * a / 8;
const w = r.w * (b - a) / 8;
const targetY = r.y + 13;
CHANNELS.forEach(([, color], i) => {
ctx.save();
ctx.globalAlpha = eventAlpha;
ctx.strokeStyle = color;
ctx.lineWidth = lerp(3, 15, p);
ctx.beginPath();
for (let sample = 0; sample <= 30; sample++) {
const q = sample / 30;
const time = lerp(a, b, q);
const sourceX = x + q * w;
const sourceY = r.y + r.h * (1 - channelProb(i, time));
const targetX = x + (i + q) * w / CHANNELS.length;
const px = lerp(sourceX, targetX, p);
const py = lerp(sourceY, targetY, p);
if (sample === 0) ctx.moveTo(px, py);
else ctx.lineTo(px, py);
}
ctx.stroke();
ctx.restore();
});
ctx.save();
ctx.globalAlpha = eventAlpha;
ctx.strokeStyle = eventColor;
ctx.lineWidth = 2;
ctx.strokeRect(x, r.y, w, r.h);
text(ctx, `E${eventIndex + 1} class channels`, x + w / 2, r.y - 25,
20, C.white, "center", 700);
ctx.restore();
});
}
function drawE2StripToMatrix(ctx, p, alpha = 1) {
const r = plotRect();
const g = matrixGeometry();
const [a, b] = EVENTS[1];
const sourceX = r.x + r.w * a / 8;
const sourceW = r.w * (b - a) / 8;
const sourceY = r.y + 13;
const targetX = g.x + g.rowW;
const targetW = COLS.length * g.cellW;
const targetY = g.y - 13;
ctx.save();
ctx.globalAlpha = alpha;
COLS.forEach(([, color], i) => {
const x1 = lerp(sourceX + i * sourceW / COLS.length,
targetX + i * targetW / COLS.length, p);
const x2 = lerp(sourceX + (i + 1) * sourceW / COLS.length,
targetX + (i + 1) * targetW / COLS.length, p);
const y = lerp(sourceY, targetY, p);
ctx.strokeStyle = color;
ctx.lineWidth = 15;
ctx.beginPath();
ctx.moveTo(x1, y);
ctx.lineTo(x2 + 1, y);
ctx.stroke();
});
ctx.restore();
}
const COLS = [
["Speech", C.teal], ["Music", C.purple], ["Natural", C.blue], ["Urban", C.orange],
["Animal", C.green], ["Indoor", C.gold], ["Background", "#4267a8"], ["Vocal", C.coral],
];
const ROWS = ["mean", "std", "p50", "p90", "p95", "max", "frac≥.3"];
const VALUES = [
[.78, .05, .03, .04, .02, .06, .04, .08],
[.10, .03, .02, .03, .02, .04, .02, .05],
[.80, .04, .02, .03, .01, .05, .04, .07],
[.88, .09, .06, .08, .05, .11, .07, .15],
[.90, .11, .08, .10, .07, .13, .09, .18],
[.93, .15, .12, .14, .11, .19, .13, .24],
[.96, .02, .00, .01, .00, .02, .00, .04],
];
function matrixGeometry() {
return { x: 95, y: 190, rowW: 110, cellW: 116, cellH: 59 };
}
function drawMatrix(ctx, alpha = 1, highlights = {}) {
const g = matrixGeometry();
ctx.save();
ctx.globalAlpha = alpha;
roundedRect(ctx, g.x, g.y, g.rowW, g.cellH, 0, C.panel, C.grid);
text(ctx, "metric", g.x + g.rowW / 2, g.y + g.cellH / 2, 15, C.muted, "center", 700);
COLS.forEach(([name, color], col) => {
const x = g.x + g.rowW + col * g.cellW;
ctx.globalAlpha = alpha;
ctx.fillStyle = color + "30";
ctx.fillRect(x, g.y, g.cellW, g.cellH);
ctx.strokeStyle = C.grid;
ctx.strokeRect(x, g.y, g.cellW, g.cellH);
text(ctx, name, x + g.cellW / 2, g.y + g.cellH / 2, 13, color, "center", 700);
});
ROWS.forEach((name, row) => {
const y = g.y + (row + 1) * g.cellH;
ctx.fillStyle = C.panel;
ctx.fillRect(g.x, y, g.rowW, g.cellH);
ctx.strokeStyle = C.grid;
ctx.strokeRect(g.x, y, g.rowW, g.cellH);
text(ctx, name, g.x + g.rowW / 2, y + g.cellH / 2, 15, C.muted, "center", 700);
COLS.forEach(([, color], col) => {
const x = g.x + g.rowW + col * g.cellW;
const key = `${row}:${col}`;
const h = highlights[key] || 0;
ctx.fillStyle = h ? `rgba(118,185,0,${0.25 + 0.65 * h})` : color + "42";
ctx.fillRect(x, y, g.cellW, g.cellH);
ctx.strokeStyle = h ? C.green : C.grid;
ctx.lineWidth = h ? 3 : 1;
ctx.strokeRect(x, y, g.cellW, g.cellH);
text(ctx, VALUES[row][col].toFixed(2), x + g.cellW / 2, y + g.cellH / 2,
16, C.white, "center", h ? 700 : 400);
});
});
ctx.restore();
}
function treeNode(ctx, value, x, y, w, active = 0, muted = false) {
ctx.save();
ctx.globalAlpha *= muted ? 0.28 : 1;
roundedRect(ctx, x - w / 2, y - 29, w, 58, 10,
active ? "#19270a" : C.panel, active ? C.green : "#777777", active ? 4 : 2);
text(ctx, value, x, y, 16, C.white, "center", 700);
ctx.restore();
}
function drawTree(ctx, p) {
const x = 1300;
const root = [x, 255], second = [x, 395], clean = [1198, 555], reject = [1405, 555];
const firstDone = phase(p, .05, .36);
const secondDone = phase(p, .42, .73);
ctx.save();
ctx.lineWidth = 4;
ctx.strokeStyle = firstDone > .6 ? C.green : C.grid;
ctx.beginPath(); ctx.moveTo(root[0], root[1] + 30); ctx.lineTo(second[0], second[1] - 30); ctx.stroke();
ctx.strokeStyle = secondDone > .6 ? C.green : C.grid;
ctx.beginPath(); ctx.moveTo(second[0], second[1] + 30); ctx.lineTo(clean[0], clean[1] - 30); ctx.stroke();
ctx.globalAlpha *= secondDone > .6 ? .25 : 1;
ctx.strokeStyle = C.grid;
ctx.beginPath(); ctx.moveTo(second[0], second[1] + 30); ctx.lineTo(reject[0], reject[1] - 30); ctx.stroke();
ctx.restore();
text(ctx, "one tree in the ensemble", x, 191, 15, C.muted);
treeNode(ctx, "speech mean > .55?", ...root, 220, firstDone);
treeNode(ctx, "music p95 < .25?", ...second, 220, secondDone);
treeNode(ctx, "CLEAN", ...clean, 140, secondDone);
treeNode(ctx, "NOT CLEAN", ...reject, 158, 0, secondDone > .6);
if (p > .76) {
pill(ctx, "E2 clean_speech_event_score = 0.31 → CLEAN", x, 660, C.green, 15, 465);
text(ctx, "classifier threshold = 0.1197503", x, 704, 14, C.muted);
}
return {
"0:0": firstDone,
"4:1": secondDone,
};
}
function drawFinal(ctx, p) {
const r = plotRect();
drawAxes(ctx, false, 0.8);
drawWave(ctx, C.muted, .35);
EVENTS.slice(0, 2).forEach(([a, b]) => {
const x = r.x + r.w * a / 8;
const w = r.w * (b - a) / 8;
ctx.fillStyle = C.green + "18";
ctx.fillRect(x, r.y, w, r.h);
ctx.strokeStyle = C.green;
ctx.lineWidth = 2;
ctx.strokeRect(x, r.y, w, r.h);
pill(ctx, "clean speech", x + w / 2, r.y + 52, C.green, 17);
});
drawWave(ctx, C.green, 1, EVENTS.slice(0, 2));
drawWave(ctx, C.coral, .35, [EVENTS[2]]);
pill(ctx, "rejected", r.x + r.w * 6.35 / 8, r.y + 160, C.coral, 17);
const valuesAlpha = phase(p, .12, .37);
ctx.save();
ctx.globalAlpha = valuesAlpha;
const eventValues = [
[1.77, "₁", "1.82 s", "0.77", "0.27"],
[4.20, "₂", "2.16 s", "0.70", "0.31"],
];
eventValues.forEach(([center, index, duration, evidence, score]) => {
const x = r.x + r.w * center / 8;
text(ctx, `duration (t${index}) = ${duration}`, x, 735, 15, C.white);
text(ctx, `mean p_speech (μ${index}) = ${evidence}`, x, 760, 15, C.teal);
text(ctx, `clean_speech_event_score (q${index}) = ${score}`, x, 785, 15, C.gold);
});
ctx.restore();
const metricsAlpha = phase(p, .43, .72);
ctx.save();
ctx.globalAlpha = metricsAlpha;
const x = 1150;
text(ctx, "SEGMENT-LEVEL METRICS", x, 728, 16, C.muted, "left", 700);
text(ctx, "rₛₚₑₑ꜀ₕ = Σᵢ Δtᵢ / T = 0.498", x, 759, 18, C.white, "left", 500);
text(ctx, "cₘₒdₑₗ = Σᵢ (Δtᵢ / T) μᵢ = 0.364", x, 790, 18, C.teal, "left", 500);
text(ctx, "cɢʙᴛ = Σᵢ (Δtᵢ / T) qᵢ = 0.145", x, 821, 18, C.gold, "left", 500);
ctx.restore();
}
function drawScene(ctx, t) {
ctx.clearRect(0, 0, W, H);
ctx.fillStyle = C.bg;
ctx.fillRect(0, 0, W, H);
if (t < 4) {
heading(ctx, "THE SAME 8-SECOND SEGMENT", "Audio waveform");
drawAxes(ctx);
drawWave(ctx);
pill(ctx, "mono • 16 kHz", W / 2, 810, C.cyan, 17);
return;
}
if (t < 8) {
const p = phase(t, 4, 7);
transformHeading(ctx, t, 4,
["THE SAME 8-SECOND SEGMENT", "Audio waveform"],
["SAME TIME AXIS", "Log-mel spectrogram"]);
drawAxes(ctx, false, 1 - p);
drawWave(ctx, C.cyan, 1 - p);
drawMel(ctx, p);
pill(ctx, "1024-sample window • hop 320 • 64 mel bins", W / 2, 810, C.purple, 17);
return;
}
if (t < 13) {
const collapse = phase(t, 8, 9.6);
const curves = phase(t, 9.0, 11.3);
transformHeading(ctx, t, 8,
["SAME TIME AXIS", "Log-mel spectrogram"],
["CNN14 • SIGMOID • 50 FRAMES/S", "Framewise class probabilities"]);
drawAxes(ctx, true, phase(t, 8.5, 9.8));
drawMelCollapse(ctx, collapse, 1 - phase(t, 9.3, 10.5));
CHANNELS.forEach(([, color], i) =>
drawCurve(ctx, x => channelProb(i, x), color, 2.5, curves));
drawLegend(ctx, curves);
return;
}
if (t < 17) {
const p = phase(t, 13, 15);
transformHeading(ctx, t, 13,
["CNN14 • SIGMOID • 50 FRAMES/S", "Framewise class probabilities"],
["7 SPEECH CLASSES → NOISY-OR", "One speech probability"]);
drawAxes(ctx, true);
CHANNELS.forEach(([, color], i) =>
drawCurve(ctx, x => channelProb(i, x), color, lerp(2.5, 2.2, p), lerp(1, .32, p)));
drawCurve(ctx, speechProb, C.teal, 7, p);
if (p > .35) {
pill(ctx, "pₛₚₑₑ꜀ₕ(t) = 1 − ∏c∈S [1 − p꜀(t)]", 1185, 190, C.teal, 18, 470);
}
drawLegend(ctx);
return;
}
if (t < 22) {
const p = phase(t, 17, 19);
transformHeading(ctx, t, 17,
["7 SPEECH CLASSES → NOISY-OR", "One speech probability"],
["MEDIAN 0.2 s • THRESHOLD 0.2 • MERGE GAP ≤ 0.1 s",
"Speech events on the same timeline"]);
drawAxes(ctx, true);
CHANNELS.forEach(([, color], i) => drawCurve(ctx, x => channelProb(i, x), color, 1.8, .16));
drawCurve(ctx, speechProb, C.teal, 6);
const r = plotRect();
const y = r.y + r.h * .8;
ctx.strokeStyle = C.coral;
ctx.lineWidth = 2;
ctx.beginPath(); ctx.moveTo(r.x, y); ctx.lineTo(r.x + r.w, y); ctx.stroke();
pill(ctx, "0.2", r.x + r.w - 32, y - 26, C.coral, 14, 58);
drawEvents(ctx, p);
drawLegend(ctx);
return;
}
if (t < 26) {
const p = phase(t, 22, 24);
transformHeading(ctx, t, 22,
["MEDIAN 0.2 s • THRESHOLD 0.2 • MERGE GAP ≤ 0.1 s",
"Speech events on the same timeline"],
["INSIDE EACH DETECTED EVENT", "Speech-event evidence from p_speech"]);
drawAxes(ctx, true);
drawCurve(ctx, speechProb, C.teal, 6);
drawEvents(ctx, lerp(1, .45, p));
const r = plotRect();
const a = EVENTS[1][0], b = EVENTS[1][1], mean = .70;
const x1 = r.x + r.w * a / 8, x2 = r.x + r.w * b / 8;
const y = r.y + r.h * (1 - mean);
ctx.strokeStyle = C.white; ctx.lineWidth = 4;
ctx.beginPath(); ctx.moveTo(x1, y); ctx.lineTo(lerp(x1, x2, p), y); ctx.stroke();
if (p > .45) pill(ctx, "speech_event_evidence(E2) = mean pₛₚₑₑ꜀ₕ(t) = 0.70",
(x1 + x2) / 2, y + 45, C.white, 16, 510);
if (p > .72) pill(ctx, "max = 0.91", r.x + r.w * 4.25 / 8, r.y + 35, C.gold, 16);
drawLegend(ctx);
return;
}
if (t < 31) {
const reveal = phase(t, 26, 27.2);
const lift = phase(t, 27.1, 29.7);
transformHeading(ctx, t, 26,
["INSIDE EACH DETECTED EVENT", "Speech-event evidence from p_speech"],
["KEEP CLASS PROBABILITIES ONLY INSIDE EACH EVENT",
"Each speech event becomes a feature strip"]);
drawAxes(ctx, true);
drawCurve(ctx, speechProb, C.teal, 6, 1 - reveal);
drawEventCurvesToStrips(ctx, lift, [reveal, reveal, reveal]);
drawLegend(ctx);
return;
}
if (t < 37) {
const isolate = phase(t, 31, 32.2);
const expand = phase(t, 32.0, 34.3);
const matrixReveal = phase(t, 33.2, 35.2);
transformHeading(ctx, t, 31,
["KEEP CLASS PROBABILITIES ONLY INSIDE EACH EVENT",
"Each speech event becomes a feature strip"],
["E2 STRIP EXPANDS INTO EVENT FEATURES", "Metrics × class probability matrix"]);
drawAxes(ctx, true, 1 - phase(t, 31, 32.4));
drawEventCurvesToStrips(ctx, 1, [1 - isolate, 1 - expand, 1 - isolate]);
drawE2StripToMatrix(ctx, expand, 1 - phase(t, 34.1, 35.1));
drawMatrix(ctx, matrixReveal);
drawLegend(ctx);
return;
}
if (t < 42) {
const move = phase(t, 37, 38.5);
const treeReveal = phase(t, 37.8, 38.8);
const treeProgress = phase(t, 38.4, 41.5);
transformHeading(ctx, t, 37,
["E2 STRIP EXPANDS INTO EVENT FEATURES", "Metrics × class probability matrix"],
["GBT EVALUATES THE FULL MATRIX FOR E2", "Tree decisions select clean segments"]);
const highlights = {
"0:0": phase(treeProgress, .05, .36),
"4:1": phase(treeProgress, .42, .73),
};
ctx.save();
ctx.translate(lerp(0, -40, move), lerp(0, 35, move));
const matrixScale = lerp(1, .74, move);
ctx.scale(matrixScale, matrixScale);
drawMatrix(ctx, 1, highlights);
ctx.restore();
ctx.save();
ctx.globalAlpha = treeReveal;
drawTree(ctx, treeProgress);
ctx.restore();
drawLegend(ctx);
return;
}
const p = phase(t, 42, 47);
heading(ctx, "BACK ON THE ORIGINAL AUDIO TIMELINE", "Selected clean speech segments");
drawFinal(ctx, p);
drawLegend(ctx);
}
// Earlier transitions are true geometry morphs. Only the final return from
// the feature space to the original waveform is intentionally a dissolve.
const TRANSITION_POINTS = [42];
const TRANSITION_DURATION = 1.35;
const previousFrame = document.createElement("canvas");
const currentFrame = document.createElement("canvas");
previousFrame.width = currentFrame.width = W;
previousFrame.height = currentFrame.height = H;
const previousContext = previousFrame.getContext("2d");
const currentContext = currentFrame.getContext("2d");
function drawFrame(ctx, t) {
const boundary = TRANSITION_POINTS.find(
point => t >= point && t < point + TRANSITION_DURATION
);
if (boundary === undefined) {
drawScene(ctx, t);
return;
}
// Dissolve the GBT view back onto the original audio timeline.
previousContext.setTransform(1, 0, 0, 1, 0, 0);
currentContext.setTransform(1, 0, 0, 1, 0, 0);
drawScene(previousContext, boundary - 0.001);
drawScene(currentContext, t);
const mix = phase(t, boundary, boundary + TRANSITION_DURATION);
ctx.clearRect(0, 0, W, H);
ctx.globalAlpha = 1;
ctx.drawImage(previousFrame, 0, 0, W, H);
ctx.globalAlpha = mix;
ctx.drawImage(currentFrame, 0, 0, W, H);
ctx.globalAlpha = 1;
}
class SedExplainer {
constructor(root) {
this.root = root;
this.canvas = root.querySelector("canvas");
this.ctx = this.canvas.getContext("2d");
this.playButton = root.querySelector('[data-action="play"]');
this.restartButton = root.querySelector('[data-action="restart"]');
this.timeline = root.querySelector(".sed-explainer__timeline");
this.timeLabel = root.querySelector(".sed-explainer__time");
this.time = 0;
this.playing = false;
this.lastFrame = 0;
this.visible = false;
this.reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
this.resize = this.resize.bind(this);
this.tick = this.tick.bind(this);
this.toggle = this.toggle.bind(this);
this.restart = this.restart.bind(this);
this.seek = this.seek.bind(this);
this.resizeObserver = new ResizeObserver(this.resize);
this.resizeObserver.observe(root);
this.intersectionObserver = new IntersectionObserver((entries) => {
this.visible = entries[0].isIntersecting;
if (this.visible && !this.reducedMotion && this.time === 0) this.play();
if (!this.visible) this.pause();
}, { threshold: 0.2 });
this.intersectionObserver.observe(root);
this.playButton.addEventListener("click", this.toggle);
this.restartButton.addEventListener("click", this.restart);
this.timeline.addEventListener("input", this.seek);
if (this.reducedMotion) this.time = DURATION;
this.resize();
this.render();
}
resize() {
const viewport = this.root.querySelector(".sed-explainer__viewport");
const bounds = viewport.getBoundingClientRect();
if (!bounds.width || !bounds.height) return;
const dpr = Math.min(window.devicePixelRatio || 1, 2);
this.canvas.width = Math.round(bounds.width * dpr);
this.canvas.height = Math.round(bounds.height * dpr);
this.render();
}
render() {
const sx = this.canvas.width / W;
const sy = this.canvas.height / H;
this.ctx.setTransform(sx, 0, 0, sy, 0, 0);
drawFrame(this.ctx, this.time);
this.timeline.value = String(this.time);
this.timeLabel.textContent = `${Math.floor(this.time / 60)}:${String(Math.floor(this.time % 60)).padStart(2, "0")} / 0:48`;
}
play() {
if (this.time >= DURATION) this.time = 0;
if (this.playing) return;
this.playing = true;
this.playButton.textContent = "Pause";
this.playButton.setAttribute("aria-label", "Pause SED animation");
this.lastFrame = performance.now();
requestAnimationFrame(this.tick);
}
pause() {
this.playing = false;
this.playButton.textContent = "Play";
this.playButton.setAttribute("aria-label", "Play SED animation");
}
toggle() {
if (this.playing) this.pause();
else this.play();
}
restart() {
this.time = 0;
this.render();
if (!this.reducedMotion) this.play();
}
seek(event) {
this.time = Number(event.target.value);
this.render();
}
tick(now) {
if (!this.playing) return;
const delta = Math.min((now - this.lastFrame) / 1000, .1);
this.lastFrame = now;
this.time += delta;
if (this.time >= DURATION) {
this.time = DURATION;
this.pause();
}
this.render();
if (this.playing) requestAnimationFrame(this.tick);
}
}
function initialize(root) {
if (root.__sedExplainer) return;
root.__sedExplainer = new SedExplainer(root);
}
function scan(node) {
if (!(node instanceof Element || node instanceof Document)) return;
if (node.matches && node.matches("[data-sed-explainer]")) initialize(node);
if (node.querySelectorAll) {
node.querySelectorAll("[data-sed-explainer]").forEach(initialize);
}
}
function start() {
scan(document);
new MutationObserver((mutations) => {
mutations.forEach((mutation) => mutation.addedNodes.forEach(scan));
}).observe(document.body, { childList: true, subtree: true });
}
if (document.readyState === "loading") document.addEventListener("DOMContentLoaded", start);
else start();
})();