Spaces:
Running
Running
File size: 28,792 Bytes
8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a993140 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 8da2711 a1dae35 a993140 a1dae35 a993140 8da2711 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 | (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();
})();
|