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
//! Long-clip chunking geometry: [`WindowPlan`] turns a clip length into the
//! list of [`Span`]s the identifier scores one at a time.
//!
//! # windit engine + this door's own tail rule
//!
//! The window GEOMETRY is the generic `windit` engine — [`Span`] is
//! `windit::plan::Span` and the *head* (every full-length window) is planned by
//! `windit::plan::WindowPlan` under `DropBelowMin(window)`. What windit does
//! with the leftover is replaced wholesale, because this graph's tail options
//! are not the ones windit (or `audio::ced`) has: its time axis is a
//! `RangeDims`, so a short tail can be scored AT ITS OWN LENGTH rather than
//! padded, and a full-length window can be slid backwards over audio already
//! read. See [`TailPolicy`].
//!
//! windit's *aggregation* engine is deliberately NOT used either — see the
//! sibling `aggregate` module for why this domain needs its own.
//!
//! # Exactly one tail span, deliberately
//!
//! `audio::ced` continues striding past the first ragged tail, emitting
//! progressively shorter ones, because soundevents' `chunk_slices` defines
//! that. This door has no upstream chunker to mirror, so it stops at one: after
//! the head, ONE tail span (or none) covers everything the head left, and every
//! sample is still inside at least one span. A second, shorter tail over audio
//! the first already covered would add an inference and a noisier vector for no
//! new evidence.
//!
//! # Geometry is a knob here, not model shape
//!
//! `audio::ced`'s window is fixed at the graph's only accepted input length.
//! This graph accepts [`MIN_FRAMES`]..=[`MAX_FRAMES`], so the window IS a
//! choice — [`DEFAULT_WINDOW_SAMPLES`] is a measured default, not a constant of
//! the model, and it can move without reshaping this API.
//!
//! # Resource cap
//!
//! [`WindowPlan::spans`] counts its plan in O(1) and refuses one exceeding
//! [`WindowPlan::max_windows`] ([`DEFAULT_MAX_WINDOWS`], default-on) with a
//! typed [`WinditError::TooManyWindows`] BEFORE materializing any span — so a
//! serde-supplied `hop_samples: 1` over a modest clip is a typed refusal, not
//! an OOM and not a flood of inferences.
//!
//! [`MIN_FRAMES`]: crate::audio::lid::MIN_FRAMES
//! [`MAX_FRAMES`]: crate::audio::lid::MAX_FRAMES
use crate;
/// windit's window span (`windit::plan::Span`), re-exported as this module's
/// geometry unit — the half-open sample range `[start, end)` a [`WindowPlan`]
/// plans and the identifier scores. Every span a plan produces carries
/// `window() == `[`WindowPlan::window_samples`], so [`Span::coverage`] is the
/// `real length / window` fraction — `1.0` for every span except a
/// [`TailPolicy::Partial`] tail and the sole span of a clip shorter than one
/// window.
pub use Span;
/// Default [`WindowPlan::window_samples`]: 160 000 samples — **10 s** at 16 kHz,
/// which is 1 001 mel frames.
///
/// Chosen on two measurements, in this order:
///
/// 1. **It is the frame count [`prewarm`] already specializes.** Every unseen
/// frame count costs a one-off 55–97 ms graph specialization against a 9–23 ms
/// steady state (see the module docs' performance notes). A fixed window
/// means the long path pays that ONCE, and pinning the default at the length
/// `prewarm` warms means it is paid off the first real request for free.
/// `default_window_is_the_length_prewarm_warms` holds the two together.
/// 2. **Self-consistency improves with window length up to it, and
/// code-switch resolution gets worse above it.** Reproducing the single-shot
/// top-1 from windows scored 81 % at 3 s windows, 87 % at 5 s and 91 % at
/// 10 s; at 30 s windows a third of a clip in another language stops winning
/// any window at all. The module docs carry the full table.
///
/// Both halves are contingent, which is why this is a `pub const` and
/// [`WindowPlan::with_geometry`] exists: a future export with enumerated shapes
/// would change the specialization economics, and a caller who needs finer
/// code-switch resolution should shorten it.
///
/// [`prewarm`]: crate::audio::lid::Identifier::prewarm
pub const DEFAULT_WINDOW_SAMPLES: u32 = 160_000;
/// Default [`WindowPlan::hop_samples`]: one full window, so the default plan
/// tiles a clip into back-to-back 10 s chunks with no overlap and no sample
/// scored twice.
pub const DEFAULT_HOP_SAMPLES: u32 = DEFAULT_WINDOW_SAMPLES;
/// Default [`WindowPlan::max_windows`]: 100 000 windows.
///
/// A resource rail, not a latency policy: each planned window costs one full
/// CoreML inference, and the per-window path retains a
/// [`NUM_LANGUAGES`](crate::audio::lid::NUM_LANGUAGES)-float row (428 B), so
/// 100 000 caps that retention at ~41 MiB. It admits every realistic clip — at
/// the default 10 s hop it is ~11.5 days of audio — while still refusing
/// hop-abuse: at `hop_samples == 1` any clip longer than one window plans more
/// windows than there are samples in a second of audio, and a 30 s one plans
/// 320 001. Latency-sensitive services should lower it; raising it is a
/// deliberate opt-in to more memory and inference work.
pub const DEFAULT_MAX_WINDOWS: u32 = 100_000;
/// What [`WindowPlan`] does with the audio a plan's full-length windows leave
/// uncovered at the end of a clip.
///
/// All three variants are unit-shaped on purpose: the payload a threshold would
/// carry has exactly one defensible value here ([`MIN_SAMPLES`], the shortest
/// clip the graph accepts at all), so it is a constant of the model rather than
/// a knob.
///
/// # Why "pad the tail" is not among them
///
/// `audio::ced` pads, because its graph accepts one input length and there is
/// nothing else to do. This one accepts a range, and padding is measurably
/// worse than both alternatives below. The fused in-graph mean subtraction
/// reduces over the time axis and therefore SEES the padding: measured against
/// the same audio scored honestly, padding a tail up to the 10 s default window
/// shifts log-probabilities by 2.5 nats at a 9 s tail and 19 nats at a 1 s one,
/// and **changes the reported language** for every tail of 3 s or less (module
/// docs, "Clips longer than 30 s"). [`Self::SlideBack`] gets a full-length
/// window with no padding at all, and [`Self::Partial`] gets an honest short
/// one; neither pays that shift.
///
/// `Display` (rust-type-conventions) prints the SAME snake_case word `serde`
/// does (`rename_all = "snake_case"` below): `"slide_back"`, `"partial"`,
/// `"drop"`. It composes into [`WindowPlan`]'s own persisted-fingerprint
/// spelling — see that impl's doc — so it is pinned exactly there too, and a
/// respelling here is exactly as much a break as one introduced in
/// `WindowPlan` itself.
/// Whether `window_samples` is a length the graph can score:
/// [`MIN_SAMPLES`]..=[`MAX_SAMPLES`]. A window outside it could never be
/// predicted on, so a plan built from one would fail per window rather than at
/// configuration time.
const
/// Whether `hop_samples` is valid against `window_samples`: positive (a zero
/// hop never advances) and no larger than one window (a hop past the window
/// would stride over un-scored audio — a sparse-skim mode is a recorded
/// non-goal).
const
/// Whether `max_windows` is a usable cap: strictly positive. A zero cap would
/// admit no plan at all, even the single-span short clip.
const
/// Long-clip chunking plan: a jointly validated window/hop geometry, a
/// [`TailPolicy`], and a [`Self::max_windows`] resource cap
/// (rust-options-pattern).
///
/// [`Self::spans`] is the pure-geometry core — it maps a clip length to the
/// list of [`Span`]s to score, with no audio and no model involved, so offsets
/// and coverages are hermetically testable. `max_windows` bounds that count in
/// O(1) BEFORE any span is materialized, so an untrusted length plus a small
/// hop cannot expand into an out-of-memory allocation or a flood of inferences.
///
/// # One geometry setter, not two
///
/// `window_samples` and `hop_samples` are validated against each other, so they
/// are set together by [`Self::with_geometry`]. A per-field window setter would
/// make the most natural call on a default plan
/// (`WindowPlan::new().with_window_samples(48_000)`, shrinking the window below
/// the default 160 000-sample hop) a panic, and a per-field setter that
/// silently moved the other field would be worse.
///
/// # Validated deserialization
///
/// `Deserialize` routes through a private `WindowPlanRepr` via
/// `serde(try_from)`, holding a config-file `WindowPlan` to the SAME invariants
/// the checked setters enforce: `{"hop_samples": 0}` would loop forever,
/// `{"hop_samples": 320000}` at the default window would skip audio,
/// `{"window_samples": 1000000}` could never be predicted on, and
/// `{"max_windows": 0}` could never score anything. All four fail to
/// deserialize instead. Every field is optional and fills its `DEFAULT_*`, so
/// the cap is default-on for every deserialized plan.
///
/// UNKNOWN KEYS ARE REFUSED. Defaulted fields and a tolerated stray key compose
/// into a silent hole: `{"max_window": 1}` — the plural dropped — would
/// otherwise deserialize with the typo discarded and `max_windows` filled from
/// [`DEFAULT_MAX_WINDOWS`], so a caller capping this door at ONE window would
/// get 100 000 and a misspelled RESOURCE LIMIT would silently become up to
/// 100 000 CoreML inferences; a misspelled `window_samples`, `hop_samples` or
/// `tail` would silently change the scored geometry the same way. The
/// misspelling is a hard error naming the key instead.
///
/// That refusal makes this type UNFLATTENABLE: serde's `deny_unknown_fields`
/// and `flatten` do not compose (a flattened field sees the outer struct's
/// other keys and rejects them), so a config type composing a plan must NEST it
/// under a key of its own — `window_plan = { … }` — not `#[serde(flatten)]` it
/// into itself.
/// The plain wire form [`WindowPlan`]'s `Deserialize` deserializes FIRST
/// (carrying the field defaults), before [`WindowPlan::try_from`] applies the
/// range checks. Its whole purpose is to make the validated setters
/// unbypassable via serde — it is never constructed or exposed otherwise.
///
/// `deny_unknown_fields` lives HERE rather than on [`WindowPlan`], because this
/// is the type whose fields serde actually visits: the public plan's
/// `Deserialize` is a `try_from` wrapper around this one.
/// **This spelling is persisted by downstream derivation fingerprints — change
/// it only with a major bump.**
///
/// `key=value` pairs in declaration order, joined by `,`: [`Self::window_samples`],
/// [`Self::hop_samples`] and [`Self::max_windows`] as `{}` of `u32`;
/// [`Self::tail_policy`] composing [`TailPolicy`]'s OWN `Display` verbatim —
/// that type is the one place ITS spelling can change, and a drift there fails
/// the same pinning test this one does.
///
/// For example, `WindowPlan::new()` prints
/// `window_samples=160000,hop_samples=160000,tail=slide_back,max_windows=100000`.