-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhow-codecs-work.html
More file actions
642 lines (590 loc) · 38.6 KB
/
how-codecs-work.html
File metadata and controls
642 lines (590 loc) · 38.6 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>How Codecs Work — Silk Docs</title>
<link rel="stylesheet" href="css/shared.css">
<style>
.chapter-num{font-family:'Fragment Mono',monospace;font-size:0.7rem;letter-spacing:0.15em;color:var(--text-muted);margin-bottom:0.5rem;display:block;}
.visual-break{height:2px;background:linear-gradient(90deg,var(--accent),transparent);margin:3rem 0;border-radius:1px;}
.quant-demo{background:var(--code-bg);border-radius:var(--radius-lg);padding:1.5rem;margin:1.5rem 0;position:relative;}
.codebook-grid{display:grid;grid-template-columns:repeat(8,1fr);gap:4px;margin:1rem 0;}
.cb-entry{aspect-ratio:1;border-radius:4px;display:flex;align-items:center;justify-content:center;font-family:'Fragment Mono',monospace;font-size:0.6rem;color:rgba(255,255,255,0.6);cursor:pointer;transition:all 0.2s;border:2px solid transparent;}
.cb-entry:hover{transform:scale(1.15);z-index:2;border-color:#555;}
.cb-entry.matched{border-color:#555;box-shadow:0 0 12px rgba(85,85,85,0.4);}
.residual-chain{display:flex;align-items:stretch;gap:0;margin:1.5rem 0;overflow-x:auto;}
.residual-step{flex:1;min-width:120px;background:var(--bg-card);border:1px solid var(--border);padding:1rem;text-align:center;position:relative;}
.residual-step:first-child{border-radius:var(--radius-md) 0 0 var(--radius-md);}
.residual-step:last-child{border-radius:0 var(--radius-md) var(--radius-md) 0;}
.residual-step .step-n{font-family:'Fragment Mono',monospace;font-size:0.65rem;color:var(--text-muted);margin-bottom:0.5rem;}
.residual-step .step-val{font-family:'Fragment Mono',monospace;font-size:1.5rem;font-weight:600;margin-bottom:0.25rem;}
.residual-step .step-desc{font-size:0.7rem;color:var(--text-muted);}
.residual-arrow{display:flex;align-items:center;padding:0 0.25rem;color:var(--text-muted);font-size:1.2rem;}
@media(max-width:768px){.residual-chain{flex-direction:column;}.residual-step{border-radius:0!important;}.residual-arrow{justify-content:center;transform:rotate(90deg);padding:0.25rem 0;}}
</style>
</head>
<body>
<aside class="sidebar">
<div class="sidebar-progress"><div class="sidebar-progress-fill"></div></div>
<div class="sidebar-rail">
<div class="sidebar-brand">S</div>
<nav>
<a href="index.html" data-page="index.html" data-num="01" title="How Voice AI Works"><span class="pip-tooltip">How Voice AI Works</span></a>
<a href="how-codecs-work.html" data-page="how-codecs-work.html" data-num="02" title="How Codecs Work"><span class="pip-tooltip">How Codecs Work</span></a>
<a href="silk-codec.html" data-page="silk-codec.html" data-num="03" title="Latent Space Learning"><span class="pip-tooltip">Latent Space Learning</span></a>
<a href="voice-ai-pipeline.html" data-page="voice-ai-pipeline.html" data-num="04" title="Voice AI Pipeline"><span class="pip-tooltip">Voice AI Pipeline</span></a>
<a href="comparison.html" data-page="comparison.html" data-num="05" title="Silk vs The World"><span class="pip-tooltip">Silk vs The World</span></a>
<a href="geometry.html" data-page="geometry.html" data-num="06" title="Why It Works"><span class="pip-tooltip">Why It Works</span></a>
<div class="quiz-pip"><a href="quiz.html" data-page="quiz.html" data-num="?" title="Test Yourself"><span class="pip-tooltip">Test Yourself</span></a></div>
</nav>
</div>
<div class="sidebar-panel">
<div class="sidebar-logo"><h2>Silk</h2><span>internal briefing</span></div>
<nav>
<a href="index.html" data-page="index.html"><span class="nav-num">01</span> How Voice AI Works</a>
<a href="how-codecs-work.html" data-page="how-codecs-work.html"><span class="nav-num">02</span> How Codecs Work</a>
<a href="silk-codec.html" data-page="silk-codec.html"><span class="nav-num">03</span> Latent Space Learning</a>
<a href="voice-ai-pipeline.html" data-page="voice-ai-pipeline.html"><span class="nav-num">04</span> Voice AI Pipeline</a>
<a href="comparison.html" data-page="comparison.html"><span class="nav-num">05</span> Silk vs The World</a>
<a href="geometry.html" data-page="geometry.html"><span class="nav-num">06</span> Why It Works</a>
<div class="quiz-link"><a href="quiz.html" data-page="quiz.html"><span class="nav-num">?</span> Test Yourself</a></div>
</nav>
</div>
</aside>
<main class="main-content">
<div class="page-content">
<span class="chapter-num">rmk-silk-002 · page 01</span>
<h1>how codecs work today</h1>
<p class="subtitle">every voice AI codec in production uses the same core approach. it works, but it has a fundamental flaw.</p>
<!-- ============ SECTION 1: THE PROBLEM ============ -->
<h2>the compression problem</h2>
<p>raw audio is 24,000 numbers per second. an LLM processes maybe 100-200 <span class="term" data-term="tokens">tokens</span> per second. you can't feed 24,000 numbers/sec into an LLM directly.</p>
<p>every codec compresses. the question: <strong>how do you compress without losing the important stuff?</strong></p>
<!-- Animated: waveform → spectrogram -->
<div class="interactive-block fade-in">
<h3>step 1: waveform → spectrogram</h3>
<p class="description">first, convert the raw <span class="term" data-term="waveform">waveform</span> into a <span class="term" data-term="spectrogram">spectrogram</span> — a "heat map" of which frequencies are active at each moment.</p>
<div style="display:grid;grid-template-columns:1fr 40px 1fr;align-items:center;gap:0;">
<div style="background:var(--code-bg);border-radius:var(--radius-md);overflow:hidden;">
<canvas id="wave-raw" height="140" style="display:block;width:100%;"></canvas>
<div style="padding:0.6rem;font-family:'Fragment Mono',monospace;font-size:0.8rem;color:rgba(255,255,255,0.5);text-align:center;">waveform (time domain)</div>
</div>
<div style="text-align:center;color:var(--accent);font-size:1.5rem;font-weight:600;">→</div>
<div style="background:#0f0f1a;border-radius:var(--radius-md);overflow:hidden;">
<canvas id="spec-demo" height="140" style="display:block;width:100%;"></canvas>
<div style="padding:0.6rem;font-family:'Fragment Mono',monospace;font-size:0.8rem;color:rgba(255,255,255,0.5);text-align:center;">spectrogram (freq × time)</div>
</div>
</div>
</div>
<!-- ============ SECTION 2: ENCODER ============ -->
<div class="interactive-block fade-in">
<h3>step 2: neural network <span class="term" data-term="encoder">encoder</span></h3>
<p class="description">a neural network processes the spectrogram and compresses each time slice into a small vector of numbers.</p>
<div style="background:#1a1a2e;border-radius:var(--radius-lg);padding:2rem 1.5rem;">
<svg id="encoder-nn-svg" viewBox="0 0 700 260" style="width:100%;height:auto;display:block;">
<text x="60" y="18" text-anchor="middle" fill="rgba(85,85,85,0.7)" font-size="11" font-weight="600" font-family="Fragment Mono">input</text>
<text x="640" y="18" text-anchor="middle" fill="rgba(45,138,78,0.6)" font-size="11" font-weight="600" font-family="Fragment Mono">output</text>
<text x="350" y="252" text-anchor="middle" fill="rgba(255,255,255,0.3)" font-size="11" font-family="Fragment Mono">128 mel bands → neural network → 128 continuous numbers</text>
</svg>
</div>
</div>
<p>at this point, discrete and continuous codecs are identical. the encoder produces a <strong>continuous vector</strong> — floating point numbers like <code>[0.73, -1.2, 0.04, ...]</code>.</p>
<p>what happens next is where everything diverges.</p>
<div class="visual-break"></div>
<!-- ============ SECTION 3: QUANTIZATION ============ -->
<span class="chapter-num">the critical step</span>
<h2 style="border:none;padding:0;margin-top:0;">step 3: quantization (the "snap")</h2>
<!-- CODEBOOK EXPLAINER -->
<div class="interactive-block fade-in" style="padding:2.5rem;">
<h3 style="margin-bottom:0.5rem;">what is a codebook?</h3>
<p class="description" style="font-size:0.95rem;max-width:640px;">a <span class="term" data-term="codebook">codebook</span> is a <strong>dictionary of reference sounds</strong>. during training, the network learns a fixed set of "representative" vectors — like a color palette. every new sound must be matched to the closest entry in this palette.</p>
<div style="display:grid;grid-template-columns:1fr 1fr;gap:2rem;margin:2rem 0;">
<!-- Left: the palette analogy -->
<div style="background:var(--bg);border-radius:var(--radius-md);padding:1.75rem;">
<div style="font-family:'Fragment Mono',monospace;font-size:0.8rem;font-weight:600;color:var(--text);margin-bottom:1rem;">the codebook (1,024 entries)</div>
<div id="codebook-palette" style="display:grid;grid-template-columns:repeat(8,1fr);gap:6px;"></div>
<div style="font-size:0.85rem;color:var(--text-muted);margin-top:1rem;line-height:1.6;">each colored block is one codebook entry — a vector that represents a specific type of sound. the network has <strong>only these to choose from.</strong></div>
</div>
<!-- Right: the snapping in action -->
<div style="background:var(--code-bg);border-radius:var(--radius-md);padding:1.75rem;">
<div style="font-family:'Fragment Mono',monospace;font-size:0.8rem;font-weight:600;color:rgba(255,255,255,0.7);margin-bottom:1rem;">what happens to a new sound</div>
<div style="display:flex;flex-direction:column;gap:1.25rem;">
<div style="display:flex;align-items:center;gap:1rem;">
<div style="width:40px;height:40px;border-radius:50%;background:#555;flex-shrink:0;display:flex;align-items:center;justify-content:center;font-size:0.75rem;color:white;font-family:'Fragment Mono',monospace;">in</div>
<div style="font-size:0.85rem;color:rgba(255,255,255,0.6);line-height:1.5;">the encoder outputs a <strong style="color:#555;">continuous vector</strong> — a precise point in space, like [0.73, -1.2, 0.04, ...]</div>
</div>
<div style="text-align:center;font-size:1.5rem;color:rgba(255,255,255,0.3);">↓ <span style="font-size:0.75rem;vertical-align:middle;">find nearest</span> ↓</div>
<div style="display:flex;align-items:center;gap:1rem;">
<div style="width:40px;height:40px;border-radius:8px;background:#d4cfc7;flex-shrink:0;display:flex;align-items:center;justify-content:center;font-size:0.65rem;color:#2c2a26;font-family:'Fragment Mono',monospace;font-weight:600;">#47</div>
<div style="font-size:0.85rem;color:rgba(255,255,255,0.6);line-height:1.5;">snapped to <strong style="color:#d4cfc7;">codebook entry #47</strong> — the closest match. everything between the original point and this entry is <span style="color:#d32f2f;font-weight:600;">lost forever.</span></div>
</div>
</div>
<div style="margin-top:1.25rem;padding:0.75rem 1rem;background:rgba(196,64,64,0.15);border-radius:var(--radius-sm);border-left:3px solid #d32f2f;">
<div style="font-size:0.85rem;color:rgba(255,255,255,0.7);line-height:1.5;">this is like rounding <code style="color:#1a1a1a;">3.7429</code> to <code style="color:#d4cfc7;">4</code>. the decimals — the nuance, the subtlety — are gone.</div>
</div>
</div>
</div>
<!-- Animated codebook lookup -->
<div style="margin-top:1.5rem;">
<div style="font-family:'Fragment Mono',monospace;font-size:0.8rem;font-weight:600;color:var(--text);margin-bottom:0.75rem;">interactive: click any palette block to see how it maps</div>
<div style="background:var(--bg);border-radius:var(--radius-md);padding:1.5rem;">
<canvas id="codebook-visual" height="280" style="display:block;width:100%;cursor:pointer;"></canvas>
<div id="codebook-info" style="text-align:center;margin-top:0.75rem;font-family:'Fragment Mono',monospace;font-size:0.85rem;color:var(--text-muted);min-height:1.5em;">hover over points to see the snap in action</div>
</div>
</div>
</div>
<p style="margin-top:1.5rem;">discrete codecs take that continuous vector and <strong>snap it to the nearest entry in a codebook</strong> — a lookup table of pre-learned vectors.</p>
<div class="interactive-block fade-in">
<h3>interactive: see quantization happen</h3>
<p class="description">the orange dot is the encoder's continuous output. gray dots are codebook entries. drag the slider to change codebook size. watch how the orange dot gets "snapped" to the nearest gray dot.</p>
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:0.5rem;">
<div style="font-family:'Fragment Mono',monospace;font-size:0.8rem;">codebook size: <strong id="cb-label" style="color:var(--accent);">8</strong></div>
<div style="font-family:'Fragment Mono',monospace;font-size:0.8rem;color:var(--text-muted);">error: <span id="q-error" style="color:var(--error);font-weight:600;">—</span></div>
</div>
<div class="slider-control">
<input type="range" id="cb-slider" min="2" max="64" value="8">
</div>
<div class="viz-area" id="quant-viz" style="height:400px;background:var(--bg);cursor:crosshair;">
<svg id="quant-svg" viewBox="0 0 700 400" style="width:100%;height:100%;"></svg>
</div>
<div style="display:flex;justify-content:center;gap:2rem;margin-top:1rem;font-size:0.85rem;color:var(--text-light);">
<span>🟠 original continuous vector</span>
<span>⬜ codebook entry</span>
<span style="color:var(--error);">--- quantization error</span>
</div>
</div>
<!-- ============ SECTION 4: RESIDUAL CHAIN ============ -->
<h2>step 4: the residual trick</h2>
<p>one codebook isn't enough. the error from the first quantization step gets passed to a SECOND codebook. that error's error goes to a THIRD. this is <strong><span class="term" data-term="rvq">Residual Vector Quantization (RVQ)</span></strong>.</p>
<div class="interactive-block fade-in">
<h3>interactive: watch the residual chain</h3>
<p class="description">click "run" to see how each codebook reduces the error from the previous one.</p>
<div style="text-align:center;margin-bottom:1rem;">
<button id="rvq-run" style="padding:0.5rem 1.5rem;background:var(--accent);color:#fff;border:none;border-radius:var(--radius-sm);font-family:'Fragment Mono',monospace;font-size:0.8rem;cursor:pointer;">▶ run RVQ chain</button>
</div>
<div class="residual-chain" id="rvq-chain">
<div class="residual-step" id="rvq-0">
<div class="step-n">original vector</div>
<div class="step-val" style="color:var(--accent);">2.73</div>
<div class="step-desc">encoder output</div>
</div>
<div class="residual-arrow">→</div>
<div class="residual-step" id="rvq-1" style="opacity:0.3;">
<div class="step-n">codebook 1</div>
<div class="step-val">3.00</div>
<div class="step-desc">nearest entry</div>
<div style="font-family:'Fragment Mono',monospace;font-size:0.7rem;color:var(--error);margin-top:0.25rem;">error: 0.27</div>
</div>
<div class="residual-arrow">→</div>
<div class="residual-step" id="rvq-2" style="opacity:0.3;">
<div class="step-n">codebook 2</div>
<div class="step-val">0.25</div>
<div class="step-desc">fixes residual</div>
<div style="font-family:'Fragment Mono',monospace;font-size:0.7rem;color:var(--error);margin-top:0.25rem;">error: 0.02</div>
</div>
<div class="residual-arrow">→</div>
<div class="residual-step" id="rvq-3" style="opacity:0.3;">
<div class="step-n">codebook 3</div>
<div class="step-val">0.02</div>
<div class="step-desc">fixes residual</div>
<div style="font-family:'Fragment Mono',monospace;font-size:0.7rem;color:var(--success);margin-top:0.25rem;">error: ~0.00</div>
</div>
</div>
<div style="text-align:center;margin-top:1rem;">
<div style="font-family:'Fragment Mono',monospace;font-size:0.75rem;color:var(--text-muted);">total output = codebook 1 + codebook 2 + codebook 3 = <span id="rvq-total" style="color:var(--accent);font-weight:600;">—</span></div>
</div>
</div>
<p>8 codebooks × 1024 entries each covers a lot more ground than one codebook alone. but there's a structural problem that no amount of extra codebooks can fix.</p>
<div class="visual-break"></div>
<!-- ============ SECTION 5: THE PROBLEM ============ -->
<span class="chapter-num">the fundamental flaw</span>
<h2 style="border:none;padding:0;margin-top:0;">the information destruction problem</h2>
<p>codebook entries are <strong>learned from training data.</strong> a codebook trained on english learns entries for english sounds. it literally has no entries for sounds it hasn't heard.</p>
<div class="interactive-block fade-in">
<h3>interactive: codebook coverage</h3>
<p class="description">green = sounds the codebook has entries for (training language). red = sounds from another language that get mapped to the WRONG entries. use the toggle to switch languages.</p>
<div class="tab-group">
<button class="tab-btn active" data-tab="en">english (trained)</button>
<button class="tab-btn" data-tab="zh">mandarin (unseen)</button>
<button class="tab-btn" data-tab="ar">arabic (unseen)</button>
</div>
<div style="background:var(--code-bg);border-radius:var(--radius-md);padding:1rem;">
<canvas id="coverage-viz" height="200" style="display:block;width:100%;"></canvas>
</div>
<div id="coverage-desc" style="text-align:center;margin-top:0.75rem;font-size:0.85rem;color:var(--text-muted);"></div>
</div>
<div class="callout insight fade-in">
<div class="callout-title">the color palette analogy</div>
<p>a color palette designed by someone who only saw forests would be heavy on greens and missing reds. try to paint a sunset — everything comes out greenish. that's what an english-trained codebook does to mandarin tones. the tonal information (which changes the meaning of words) gets snapped to flat english intonation entries.</p>
</div>
<!-- ============ SECTION 6: THE LANDSCAPE ============ -->
<h2>the codec landscape</h2>
<div class="interactive-block fade-in">
<h3>token burden per second of audio</h3>
<p class="description">every discrete codec produces hundreds of tokens per second. the LLM has to generate ALL of them to produce speech.</p>
<div id="token-chart" style="min-height:250px;"></div>
</div>
<table class="comparison-table fade-in">
<thead>
<tr><th>codec</th><th>frame rate</th><th>codebooks</th><th>tokens/sec</th><th>type</th></tr>
</thead>
<tbody>
<tr><td><strong>DAC</strong></td><td>~86 Hz</td><td>9</td><td>774</td><td><span style="background:var(--error-light);color:var(--error);font-size:0.7rem;padding:2px 8px;border-radius:3px;font-family:'Fragment Mono',monospace;">DISCRETE</span></td></tr>
<tr><td><strong>EnCodec</strong></td><td>75 Hz</td><td>8</td><td>600</td><td><span style="background:var(--error-light);color:var(--error);font-size:0.7rem;padding:2px 8px;border-radius:3px;font-family:'Fragment Mono',monospace;">DISCRETE</span></td></tr>
<tr><td><strong>Mimi</strong></td><td>12.5 Hz</td><td>8</td><td>100</td><td><span style="background:var(--error-light);color:var(--error);font-size:0.7rem;padding:2px 8px;border-radius:3px;font-family:'Fragment Mono',monospace;">DISCRETE</span></td></tr>
<tr><td><strong>SNAC</strong></td><td>multi-scale</td><td>4</td><td>82.5</td><td><span style="background:var(--error-light);color:var(--error);font-size:0.7rem;padding:2px 8px;border-radius:3px;font-family:'Fragment Mono',monospace;">DISCRETE</span></td></tr>
<tr style="background:var(--accent-light);"><td><strong style="color:var(--accent);">Silk Codec</strong></td><td>2.5 Hz</td><td>19 FSQ groups</td><td style="font-weight:700;color:var(--accent);">47.5</td><td><span style="background:var(--success-light);color:var(--success);font-size:0.7rem;padding:2px 8px;border-radius:3px;font-family:'Fragment Mono',monospace;">FSQ</span></td></tr>
</tbody>
</table>
<!-- Go Deeper -->
<div class="depth-section fade-in">
<button class="depth-toggle" data-target="depth-gradient"><span class="arrow">▸</span> go deeper: the gradient problem with quantization</button>
<div class="depth-content" id="depth-gradient">
<div class="depth-inner">
<h4>you can't differentiate through a lookup table</h4>
<p>the "snap to nearest entry" operation has zero gradient everywhere except at the boundary (where it's undefined). this means during training, the encoder never gets precise feedback about the quantization error. the standard workaround is the <strong>straight-through estimator</strong> — pretend quantization didn't happen during backpropagation.</p>
<p>the Silk Codec uses <span class="term" data-term="fsq">FSQ (Finite Scalar Quantization)</span> instead of VQ. FSQ quantizes each dimension independently to 8 levels, and uses dimension-level straight-through estimation — much more precise than vector-level STE used in RVQ codecs.</p>
</div>
</div>
</div>
<div class="depth-section fade-in">
<button class="depth-toggle" data-target="depth-bandwidth"><span class="arrow">▸</span> go deeper: the bandwidth vs quality tradeoff</button>
<div class="depth-content" id="depth-bandwidth">
<div class="depth-inner">
<h4>more codebooks = better quality but more tokens</h4>
<p>each additional codebook reduces reconstruction error. but each codebook also produces one token per frame. at 75 Hz with 8 codebooks, that's <strong>600 tokens/sec</strong> the downstream LLM must generate — 6x faster than GPT generates text.</p>
<p>SNAC tried multi-scale codebooks. Mimi reduced frame rate to 12.5 Hz. but they're all still discrete, still destroying information at every quantization step.</p>
</div>
</div>
</div>
<!-- Nav -->
<div style="display:flex;justify-content:space-between;margin-top:4rem;padding-top:1.5rem;border-top:1px solid var(--border-light);">
<a href="index.html" style="color:var(--accent);text-decoration:none;font-size:0.9rem;">← how voice AI works</a>
<a href="silk-codec.html" style="color:var(--accent);text-decoration:none;font-size:0.9rem;">LSL & the codec →</a>
</div>
</div>
</main>
<script src="js/shared.js"></script>
<script>
// === WAVEFORM ===
(function(){
const c=document.getElementById('wave-raw');if(!c)return;
const ctx=c.getContext('2d');c.width=c.parentElement.offsetWidth*2;c.height=200;let t=0;
function draw(){ctx.clearRect(0,0,c.width,c.height);ctx.strokeStyle='rgba(100,100,100,0.8)';ctx.lineWidth=1.5;ctx.beginPath();for(let x=0;x<c.width;x++){const y=c.height/2+Math.sin(x*0.02+t*0.04)*30+Math.sin(x*0.05+t*0.06)*15+Math.sin(x*0.008+t*0.02)*10;x===0?ctx.moveTo(x,y):ctx.lineTo(x,y);}ctx.stroke();t++;requestAnimationFrame(draw);}draw();
})();
// === SPECTROGRAM ===
(function(){
const c=document.getElementById('spec-demo');if(!c)return;
const ctx=c.getContext('2d');c.width=c.parentElement.offsetWidth*2;c.height=200;let t=0;
const data=[];for(let i=0;i<100;i++){const col=[];for(let j=0;j<50;j++)col.push(Math.random());data.push(col);}
function draw(){
// Shift data
data.shift();const col=[];for(let j=0;j<50;j++){col.push(0.3*Math.sin(j*0.1+t*0.05)+0.3+Math.random()*0.2+(j<15?0.3:0)*(0.5+0.5*Math.sin(t*0.03)));}data.push(col);
ctx.clearRect(0,0,c.width,c.height);
const cw=c.width/100,ch=c.height/50;
data.forEach((col,x)=>{col.forEach((v,y)=>{const h=240-v*200;ctx.fillStyle=`hsl(${h},70%,${20+v*40}%)`;ctx.fillRect(x*cw,(49-y)*ch,cw+1,ch+1);});});
t++;requestAnimationFrame(draw);
}draw();
})();
// === ENCODER NN (SVG-based) ===
(function(){
const svg=document.getElementById('encoder-nn-svg');if(!svg)return;
const layers=[8,10,8,5,3];
const layerX=[60,210,360,510,640];
function getY(l,i){const n=layers[l];const h=n*26;const start=(240-h)/2+20;return start+i*26;}
let html='';
for(let l=0;l<layers.length-1;l++){
for(let i=0;i<layers[l];i++){
for(let j=0;j<layers[l+1];j++){
html+=`<line x1="${layerX[l]}" y1="${getY(l,i)}" x2="${layerX[l+1]}" y2="${getY(l+1,j)}" stroke="rgba(100,100,100,0.15)" stroke-width="0.5"/>`;
}
}
}
for(let l=0;l<layers.length;l++){
const isLast=l===layers.length-1;const isFirst=l===0;
for(let i=0;i<layers[l];i++){
html+=`<circle cx="${layerX[l]}" cy="${getY(l,i)}" r="9" fill="${isFirst?'rgba(255,255,255,0.15)':isLast?'rgba(45,138,78,0.3)':'rgba(100,100,100,0.2)'}" stroke="${isLast?'rgba(45,138,78,0.5)':'rgba(100,100,100,0.4)'}" stroke-width="1.5"/>`;
}
}
for(let p=0;p<8;p++){
html+=`<circle class="enc-particle" cx="0" cy="0" r="3.5" fill="#555" opacity="0.7" style="display:none;"/>`;
}
svg.innerHTML+=html;
const particles=svg.querySelectorAll('.enc-particle');
let t=0;
function animate(){
particles.forEach((p,idx)=>{
const layer=Math.floor((t*0.008+idx*0.28)%(layers.length-1));
const prog=((t*0.008+idx*0.28)%(layers.length-1))-layer;
const s=idx%layers[layer],d=idx%layers[layer+1];
p.setAttribute('cx',layerX[layer]+(layerX[layer+1]-layerX[layer])*prog);
p.setAttribute('cy',getY(layer,s)+(getY(layer+1,d)-getY(layer,s))*prog);
p.style.display='';
});
t++;requestAnimationFrame(animate);
}
const obs=new IntersectionObserver(e=>{if(e[0].isIntersecting){animate();obs.unobserve(svg);}},{threshold:0.3});
obs.observe(svg);
})();
// === QUANTIZATION VIZ ===
(function(){
const svg=document.getElementById('quant-svg');
const slider=document.getElementById('cb-slider');
const label=document.getElementById('cb-label');
const errLabel=document.getElementById('q-error');
// Random points
const pts=[];for(let i=0;i<25;i++)pts.push({x:40+Math.sin(i*2.5)*280+Math.random()*100,y:30+Math.cos(i*1.7)*120+Math.random()*50+20});
function genCB(n){const e=[];const c=Math.ceil(Math.sqrt(n));const r=Math.ceil(n/c);let idx=0;for(let ri=0;ri<r&&idx<n;ri++)for(let ci=0;ci<c&&idx<n;ci++){e.push({x:60+ci/(c-1||1)*580,y:40+ri/(r-1||1)*230});idx++;}return e;}
function nearest(p,cb){let md=Infinity,b=cb[0];cb.forEach(e=>{const d=Math.hypot(p.x-e.x,p.y-e.y);if(d<md){md=d;b=e;}});return{e:b,d:md};}
function render(){
const n=parseInt(slider.value);label.textContent=n;const cb=genCB(n);let html='';let totalErr=0;
cb.forEach(e=>{html+=`<rect x="${e.x-5}" y="${e.y-5}" width="10" height="10" rx="2" fill="#d4cfc7" opacity="0.5"/>`;});
pts.forEach(p=>{const{e,d}=nearest(p,cb);totalErr+=d;html+=`<line x1="${p.x}" y1="${p.y}" x2="${e.x}" y2="${e.y}" stroke="#d32f2f" stroke-width="1" stroke-dasharray="3,3" opacity="0.5"/>`;});
pts.forEach(p=>{html+=`<circle cx="${p.x}" cy="${p.y}" r="5" fill="#555"/>`;});
svg.innerHTML=html;
errLabel.textContent=(totalErr/pts.length).toFixed(1)+' units';
}
slider.addEventListener('input',render);render();
})();
// === RVQ CHAIN (dynamic) ===
(function(){
const btn=document.getElementById('rvq-run');
const steps=['rvq-1','rvq-2','rvq-3'];
const totalEl=document.getElementById('rvq-total');
const origEl=document.getElementById('rvq-0');
btn.addEventListener('click',()=>{
// Generate fresh random values each run
const original=+(2+Math.random()*3).toFixed(2);
origEl.querySelector('.step-val').textContent=original;
steps.forEach(s=>{const el=document.getElementById(s);el.style.opacity='0.3';el.style.transition='none';});
totalEl.textContent='—';
let remaining=original;
const vals=[];
// Pre-compute codebook matches
for(let i=0;i<3;i++){
const snap=+remaining.toFixed(i===0?0:1); // round to nearest int, then tenth, etc.
const nearest=i===0?Math.round(remaining):+(Math.round(remaining*10)/10).toFixed(1);
const error=+(remaining-nearest).toFixed(4);
vals.push({nearest,error:Math.abs(error)});
remaining=error;
}
steps.forEach((s,i)=>{
setTimeout(()=>{
const el=document.getElementById(s);
el.style.transition='opacity 0.4s';
el.style.opacity='1';
el.querySelector('.step-val').textContent=vals[i].nearest;
const errEl=el.querySelector('[style*="color:var(--error)"], [style*="color:var(--success)"]');
if(errEl){
const err=vals[i].error;
errEl.textContent='error: '+(err<0.005?'~0.00':err.toFixed(2));
errEl.style.color=err<0.01?'var(--success)':'var(--error)';
}
if(i===steps.length-1){
const sum=vals.map(v=>v.nearest).reduce((a,b)=>a+b,0);
totalEl.textContent=vals.map(v=>v.nearest).join(' + ')+' = '+sum.toFixed(2)+' ≈ '+original;
}
},(i+1)*800);
});
});
})();
// === COVERAGE VIZ (improved) ===
(function(){
const c=document.getElementById('coverage-viz');if(!c)return;
const ctx=c.getContext('2d');c.width=c.parentElement.offsetWidth*2;c.height=500;
const W=c.width,H=c.height;
const desc=document.getElementById('coverage-desc');
const tabs=document.querySelectorAll('.tab-btn');
let lang='en';
let animT=0;
// English codebook entries with labels
const enLabels=['ah','ee','oo','eh','ih','s','t','k','p','f','sh','th','ch','v','z','m','n','ng','l','r','w','b','d','g','uh','ey','ow','aw','oy','er'];
const enSounds=[];for(let i=0;i<30;i++){
const angle=i/30*Math.PI*2;const rad=W*0.2+Math.random()*W*0.15;
enSounds.push({x:W*0.5+Math.cos(angle)*rad+(Math.random()-0.5)*W*0.1,y:H*0.5+Math.sin(angle)*rad*(H/W)+(Math.random()-0.5)*H*0.1,label:enLabels[i]});
}
function draw(){
ctx.clearRect(0,0,W,H);
// Soft Voronoi-like backgrounds for codebook regions
enSounds.forEach(s=>{
const grd=ctx.createRadialGradient(s.x,s.y,0,s.x,s.y,W*0.06);
grd.addColorStop(0,'rgba(212,207,199,0.08)');grd.addColorStop(1,'transparent');
ctx.fillStyle=grd;ctx.fillRect(0,0,W,H);
});
// Draw codebook entries (larger, with labels)
enSounds.forEach(s=>{
ctx.beginPath();ctx.arc(s.x,s.y,14,0,Math.PI*2);
ctx.fillStyle='rgba(212,207,199,0.2)';ctx.fill();
ctx.strokeStyle='rgba(212,207,199,0.45)';ctx.lineWidth=1.5;ctx.stroke();
ctx.fillStyle='rgba(212,207,199,0.55)';ctx.font='bold 11px Fragment Mono';ctx.textAlign='center';
ctx.fillText(s.label,s.x,s.y+4);
});
// Language-specific sounds
let sounds=[];let closeCount=0,farCount=0;
if(lang==='en'){
for(let i=0;i<20;i++)sounds.push({x:enSounds[i%enSounds.length].x+(Math.random()-0.5)*25,y:enSounds[i%enSounds.length].y+(Math.random()-0.5)*25,label:enLabels[i]});
}else if(lang==='zh'){
const zhClose=['a','e','i','o','u','s','t','k','p','m','n','l','f','w'];
const zhFar=['mā↑','má↗','mǎ↘↗','mà↘','zh','ch','x','j','q','ü'];
for(let i=0;i<14;i++)sounds.push({x:enSounds[i%enSounds.length].x+(Math.random()-0.5)*25,y:enSounds[i%enSounds.length].y+(Math.random()-0.5)*25,label:zhClose[i]});
for(let i=0;i<10;i++){
const angle=Math.random()*Math.PI*2;const r=W*0.38+Math.random()*W*0.05;
sounds.push({x:W*0.5+Math.cos(angle)*r,y:H*0.5+Math.sin(angle)*r*(H/W),label:zhFar[i],far:true});
}
}else{
const arClose=['a','i','u','s','t','k','m','n','l','b'];
const arFar=['ʕ ain','ħ ha','ʔ','q qaf','ḍ dad','ṣ sad','ṭ ta','ẓ za','ð dhal','ġ ghain','ḫ kha','θ tha','ʃ shin','dʒ jim','r raa'];
for(let i=0;i<10;i++)sounds.push({x:enSounds[i%enSounds.length].x+(Math.random()-0.5)*25,y:enSounds[i%enSounds.length].y+(Math.random()-0.5)*25,label:arClose[i]});
for(let i=0;i<15;i++){
const angle=Math.random()*Math.PI*2;const r=W*0.35+Math.random()*W*0.08;
sounds.push({x:W*0.5+Math.cos(angle)*r,y:H*0.5+Math.sin(angle)*r*(H/W),label:arFar[i],far:true});
}
}
// Draw sounds with snap lines
sounds.forEach(s=>{
let minD=Infinity,best=enSounds[0];
enSounds.forEach(e=>{const d=Math.hypot(s.x-e.x,s.y-e.y);if(d<minD){minD=d;best=e;}});
const isClose=minD<60;
if(isClose)closeCount++;else farCount++;
if(!isClose){
// Pulsing glow for far sounds
const pulse=0.4+0.3*Math.sin(animT*0.05+s.x*0.01);
const glow=ctx.createRadialGradient(s.x,s.y,0,s.x,s.y,20);
glow.addColorStop(0,'rgba(196,64,64,'+pulse+')');glow.addColorStop(1,'transparent');
ctx.fillStyle=glow;ctx.fillRect(s.x-20,s.y-20,40,40);
// Error line
ctx.strokeStyle='rgba(196,64,64,0.35)';ctx.lineWidth=1.5;ctx.setLineDash([4,4]);
ctx.beginPath();ctx.moveTo(s.x,s.y);ctx.lineTo(best.x,best.y);ctx.stroke();ctx.setLineDash([]);
}
ctx.beginPath();ctx.arc(s.x,s.y,isClose?8:9,0,Math.PI*2);
ctx.fillStyle=isClose?'rgba(45,138,78,0.85)':'rgba(196,64,64,0.85)';ctx.fill();
// Labels for sounds
if(s.label){
ctx.fillStyle=isClose?'rgba(45,138,78,0.7)':'rgba(196,64,64,0.7)';
ctx.font='bold 10px Fragment Mono';ctx.textAlign='left';
ctx.fillText(s.label,s.x+13,s.y+4);
}
});
// Coverage percentage bar
const total=closeCount+farCount;
if(total>0){
const pct=Math.round(closeCount/total*100);
const barY=H-40,barX=W*0.15,barW=W*0.7,barH=14;
ctx.fillStyle='rgba(196,64,64,0.2)';
ctx.fillRect(barX,barY,barW,barH);
ctx.fillStyle='rgba(45,138,78,0.6)';
ctx.fillRect(barX,barY,barW*(pct/100),barH);
ctx.fillStyle='rgba(255,255,255,0.6)';ctx.font='bold 12px Fragment Mono';ctx.textAlign='center';
ctx.fillText(pct+'% covered',barX+barW/2,barY+11);
}
// Legend
ctx.font='bold 12px Fragment Mono';ctx.textAlign='left';
ctx.fillStyle='rgba(212,207,199,0.45)';ctx.fillText('○ codebook entries (english)',20,22);
ctx.fillStyle='rgba(45,138,78,0.7)';ctx.fillText('● close match',20,40);
ctx.fillStyle='rgba(196,64,64,0.7)';ctx.fillText('● far (info lost)',W*0.35,40);
// Description
if(lang==='en')desc.textContent='english: '+closeCount+'/'+total+' sounds land near codebook entries. low error, great reconstruction.';
else if(lang==='zh')desc.textContent='mandarin: '+closeCount+' shared sounds are fine, but '+farCount+' tones and phonemes land far from any entry. tonal information destroyed.';
else desc.textContent='arabic: only '+closeCount+' sounds match. '+farCount+' pharyngeals, emphatics, and unique consonants have no nearby entry.';
animT++;requestAnimationFrame(draw);
}
tabs.forEach(btn=>{btn.addEventListener('click',()=>{tabs.forEach(b=>b.classList.remove('active'));btn.classList.add('active');lang=btn.dataset.tab;});});
draw();
})();
// === CODEBOOK PALETTE ===
(function(){
const palette=document.getElementById('codebook-palette');if(!palette)return;
const hues=[0,25,45,120,180,210,260,300,15,35,55,90,150,195,240,280];
for(let i=0;i<32;i++){
const h=hues[i%hues.length]+(i>15?15:0);
const s=50+Math.random()*30;
const l=35+Math.random()*25;
const el=document.createElement('div');
el.style.cssText=`aspect-ratio:1;border-radius:6px;background:hsl(${h},${s}%,${l}%);transition:all 0.2s;cursor:pointer;border:2px solid transparent;`;
el.title=`entry #${i+1}`;
el.addEventListener('mouseenter',()=>{el.style.transform='scale(1.2)';el.style.borderColor='#555';el.style.zIndex='2';});
el.addEventListener('mouseleave',()=>{el.style.transform='';el.style.borderColor='transparent';el.style.zIndex='';});
palette.appendChild(el);
}
})();
// === CODEBOOK VISUAL (2D snap) ===
(function(){
const c=document.getElementById('codebook-visual');if(!c)return;
const ctx=c.getContext('2d');c.width=c.parentElement.offsetWidth*2;c.height=560;
const W=c.width,H=c.height;const info=document.getElementById('codebook-info');
// Codebook entries — fixed positions
const entries=[];
for(let i=0;i<24;i++){
entries.push({x:W*0.08+Math.random()*W*0.84,y:H*0.08+Math.random()*H*0.84,
label:['ah','ee','oo','s','t','k','sh','f','th','p','m','n','eh','ih','uh','v','z','ch','ng','l','r','w','j','b'][i],
hue:[0,20,40,200,210,220,190,215,205,225,170,175,10,15,5,230,235,195,180,165,160,150,155,240][i]});
}
// Free-floating input points
const inputs=[];
for(let i=0;i<12;i++){
inputs.push({x:W*0.1+Math.random()*W*0.8,y:H*0.1+Math.random()*H*0.8});
}
let hovered=-1;
function nearest(p){let md=Infinity,bi=0;entries.forEach((e,i)=>{const d=Math.hypot(p.x-e.x,p.y-e.y);if(d<md){md=d;bi=i;}});return{idx:bi,dist:md};}
function draw(){
ctx.clearRect(0,0,W,H);
// Draw grid background with Voronoi-like regions
entries.forEach(e=>{
const grd=ctx.createRadialGradient(e.x,e.y,0,e.x,e.y,W*0.12);
grd.addColorStop(0,`hsla(${e.hue},50%,50%,0.08)`);grd.addColorStop(1,'transparent');
ctx.fillStyle=grd;ctx.fillRect(0,0,W,H);
});
// Draw snap lines for all inputs
inputs.forEach((p,i)=>{
const{idx,dist}=nearest(p);const e=entries[idx];
ctx.setLineDash([4,4]);ctx.strokeStyle=i===hovered?'rgba(196,64,64,0.7)':'rgba(196,64,64,0.2)';
ctx.lineWidth=i===hovered?2:1;ctx.beginPath();ctx.moveTo(p.x,p.y);ctx.lineTo(e.x,e.y);ctx.stroke();ctx.setLineDash([]);
});
// Draw codebook entries
entries.forEach((e,i)=>{
ctx.beginPath();ctx.arc(e.x,e.y,14,0,Math.PI*2);
ctx.fillStyle=`hsla(${e.hue},50%,50%,0.3)`;ctx.fill();
ctx.strokeStyle=`hsla(${e.hue},50%,50%,0.6)`;ctx.lineWidth=2;ctx.stroke();
ctx.fillStyle='rgba(0,0,0,0.7)';ctx.font='bold 13px Fragment Mono';ctx.textAlign='center';
ctx.fillText(e.label,e.x,e.y+5);
});
// Draw input points
inputs.forEach((p,i)=>{
const r=i===hovered?10:8;
ctx.beginPath();ctx.arc(p.x,p.y,r,0,Math.PI*2);
ctx.fillStyle=i===hovered?'#555':'rgba(100,100,100,0.8)';ctx.fill();
if(i===hovered){ctx.strokeStyle='#fff';ctx.lineWidth=2;ctx.stroke();}
});
}
draw();
c.addEventListener('mousemove',function(ev){
const rect=c.getBoundingClientRect();
const mx=(ev.clientX-rect.left)/rect.width*W;
const my=(ev.clientY-rect.top)/rect.height*H;
hovered=-1;
inputs.forEach((p,i)=>{if(Math.hypot(p.x-mx,p.y-my)<25)hovered=i;});
if(hovered>=0){
const{idx,dist}=nearest(inputs[hovered]);
info.innerHTML=`sound snapped to <strong>"${entries[idx].label}"</strong> — distance: <span style="color:#d32f2f;">${(dist/W*100).toFixed(1)}%</span> of the space lost`;
}else{info.textContent='hover over orange dots to see the snap in action';}
draw();
});
c.addEventListener('mouseleave',()=>{hovered=-1;info.textContent='hover over orange dots to see the snap in action';draw();});
})();
// === TOKEN CHART ===
createBarChart('token-chart',[
{label:'DAC (9×86Hz)',value:774},
{label:'EnCodec (8×75Hz)',value:600},
{label:'Silk (19×2.5Hz)',value:47.5},
{label:'Mimi (8×12.5Hz)',value:100},
{label:'SNAC (multi)',value:82.5},
],{highlight:'DAC (9×86Hz)',yLabel:'tokens per second',height:320});
</script>
<nav class="mobile-nav">
<a href="#" class="mobile-prev">← prev</a>
<span class="current-page"></span>
<a href="#" class="mobile-next">next →</a>
</nav>
</body>
</html>