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
//! thresholds the analyzer measures and reads with, all data the caller passes
use std::fmt;
/// a cutoff at or above this fraction of the band's top counts as reaching it, when a window is
/// measured and when it is read
const FULL_BAND: f64 = 0.9;
/// Thresholds each window is measured with.
///
/// Changing one means measuring the audio again.
///
/// No value is checked against a range. One outside what its field describes, or not a number,
/// gives measures that mean nothing, and no panic.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "serde",
serde(default, deny_unknown_fields, rename_all = "camelCase")
)]
pub struct MeasureThresholds {
/// Level in dBFS under which a window is near-silent.
///
/// A near-silent window has no cutoff.
pub silence_dbfs: f64,
/// Range in dB under the loudest band that the cutoff lies in.
///
/// The cutoff is the highest band within this range.
pub cutoff_range_db: f64,
/// Margin in dB of the cutoff line over quantization noise.
///
/// The line never sits closer to the noise of the samples' bit depth, so a quiet passage's
/// quantization noise does not read as content.
pub floor_margin_db: f64,
/// Gap in Hz to the bands compared across a frequency.
pub shelf_gap_hz: f64,
/// Width in Hz of the bands compared across a frequency.
pub shelf_span_hz: f64,
/// Fraction of the top a cutoff needs for an inner step search.
///
/// A window is searched for a step inside its band when its cutoff is at or above this
/// fraction of the band's top.
pub full_band: f64,
/// Lowest frequency in Hz an inner step is looked for at.
pub inner_shelf_from_hz: f64,
/// Start in Hz of the band gaps are counted in.
///
/// It is where MP3's top scale-factor band sits.
pub holes_from_hz: f64,
/// Top in Hz of the band gaps are counted in.
pub holes_to_hz: f64,
/// Depth in dB under its neighbors at which a bin is a gap.
pub hole_depth_db: f64,
/// Whether MP3 and AAC are scanned for at the other rate too.
///
/// It applies to a file at 44.1 or 48 kHz. The scan at the other of the two rates finds a
/// decode that was converted between them ([`OtherRate`](crate::grid::OtherRate)). It takes
/// about as long as the scan for MP3 and AAC at the file's own rate.
pub scan_other_rate: bool,
/// Whether WMA is scanned for.
///
/// The WMA scan takes about a quarter as long as all the others together. With it off, a
/// WMA decode can still read as possibly lossy by its cutoff.
pub scan_wma: bool,
}
impl Default for MeasureThresholds {
fn default() -> Self {
Self {
silence_dbfs: -70.0,
cutoff_range_db: 60.0,
floor_margin_db: 10.0,
shelf_gap_hz: 100.0,
shelf_span_hz: 500.0,
full_band: FULL_BAND,
inner_shelf_from_hz: 10_000.0,
holes_from_hz: 16_000.0,
holes_to_hz: 20_000.0,
hole_depth_db: 20.0,
scan_other_rate: true,
scan_wma: true,
}
}
}
/// Thresholds the measures are read with.
///
/// A reading is quick, so these can be swept over audio measured once.
///
/// No value is checked against a range. One outside what its field describes, or not a number,
/// gives a reading that means nothing, and no panic.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "serde",
serde(default, deny_unknown_fields, rename_all = "camelCase")
)]
pub struct ReadingThresholds {
/// Least drop in dB across the cutoff for an encoder's low-pass.
///
/// A smaller drop is a natural roll-off.
pub sharp_shelf_db: f64,
/// Fraction of the top from which a cutoff counts as full band.
///
/// A cutoff at or above this fraction of the band's top counts as reaching it. Set below
/// [`MeasureThresholds::full_band`], it counts as full some windows that measuring never
/// searched for inner steps.
pub full_band: f64,
/// Fraction of the top under which a gentle roll-off is not judged.
///
/// Content that rolls off gently below it is band-limited by nature, and the reading says it
/// cannot tell.
pub natural_limit: f64,
/// Least inner step in dB that reads as a band-limited layer.
///
/// A step of at least this size inside a full band is a band-limited layer under it.
pub inner_shelf_db: f64,
/// Least step in dB at a top-bitrate cutoff that casts doubt.
///
/// Such a step under a band that reaches the top reads as something the analyzer cannot tell,
/// which costs an honest file nothing. Honest spectra step by a decibel or two there.
pub high_step_db: f64,
/// Height in dB over the cutoff line that makes a band content.
///
/// The band above a step is content only at this height or more. Nearer the line it is the
/// recording's noise floor, hiss or dither.
pub clear_db: f64,
/// Fewest non-silent windows a reading needs.
pub min_windows: usize,
/// Fewest seconds of non-silent audio a reading needs.
pub min_seconds: f64,
/// Seconds past `min_seconds` with little to go on.
///
/// Within this many seconds over `min_seconds` a reading says it has little to go on. There
/// each second moves the shares its rules compare by about 2.5 percent, so a few odd
/// seconds can tip it.
pub few_seconds_over: f64,
/// Share of windows at the top of the band for consistent content.
///
/// At least this share of windows must reach the top for the content to be consistent with
/// its claim.
pub consistent_share: f64,
/// Share of windows that stop at one encoder cutoff for a transcode.
pub transcode_share: f64,
/// Distance in Hz at which a cutoff still shares the common one.
pub cluster_hz: f64,
/// Percentile of a lossy file's cutoffs read as its encoder's ceiling.
pub ceiling: f64,
/// Share of windows each of two sources has for mixed sources.
pub mixed_share: f64,
/// Band tops in Hz of the sample rates audio is commonly made at.
///
/// Content that stops with a cliff at one, in a file whose band reaches past it, was made at
/// that rate.
pub lower_rate_edges: Vec<f64>,
/// Band edges of rates some old equipment recorded at.
///
/// They are left out of `lower_rate_edges` because they sit on encoder cutoffs (18.9 kHz
/// beside 192 kbps MP3's). A cliff at one reads as that encoder, with a limitation naming the
/// rate.
pub other_rate_edges: Vec<RateEdge>,
/// Correlation above which two channels hold the same audio.
///
/// It applies together with a side channel at or below `mono_side_db`.
pub mono_correlation: f64,
/// Side-to-mid level in dB at or below which channels match.
///
/// At or below this level of the side channel relative to the mid channel, the two channels
/// hold the same audio.
pub mono_side_db: f64,
/// Highest confidence a reading has, from 0 to 1.
///
/// A reading by a grid has this confidence itself, so a cap that is negative or not a number
/// is the confidence of such a reading.
pub confidence_cap: f64,
/// Median window level in dBFS under which a file is quiet.
///
/// A quiet file makes a reading less sure.
pub quiet_dbfs: f64,
/// Bitrate in kbit/s from which a cutoff may be a filter.
///
/// Profiles at this bitrate and above cut where some recordings are filtered when they are
/// made, so a sharp shelf there reads as something the analyzer cannot tell, never as a
/// transcode.
pub high_bitrate_kbps: u32,
/// Cutoffs of common lossy encoder settings, lowest first.
pub profiles: Vec<EncoderProfile>,
/// Robust z over which the AAC scan finds a frame grid.
///
/// It is compared with [`CodecScan::z`](crate::grid::CodecScan::z). A lower value finds more
/// grids in decodes that hide them, such as clipped ones, and finds them in more lossless
/// files whose counts happen to dip at one offset.
pub aac_grid_z: f64,
/// Fewest segments that put their lowest count at the file's offset, for an AAC grid.
///
/// It is compared with [`CodecScan::segments_agreeing`]. A decode carries one grid from start
/// to end, so every segment finds it at the same offset. The segments of a lossless file can
/// agree on an offset too, so agreement alone is no grid: it only narrows what the z
/// threshold finds. A file that holds a lossy section only in part falls short, and so does
/// a Vorbis decode, whose grid the AAC scan finds at an offset that moves between segments.
///
/// [`CodecScan::segments_agreeing`]: crate::grid::CodecScan::segments_agreeing
pub aac_grid_segments_agreeing: usize,
/// Robust z over which the MP3 scan finds a frame grid.
///
/// It is compared with [`CodecScan::z`](crate::grid::CodecScan::z), as `aac_grid_z` is.
pub mp3_grid_z: f64,
/// Fewest segments that put their lowest count at the file's offset, for an MP3 grid.
///
/// It is compared with [`CodecScan::segments_agreeing`], as `aac_grid_segments_agreeing` is.
///
/// [`CodecScan::segments_agreeing`]: crate::grid::CodecScan::segments_agreeing
pub mp3_grid_segments_agreeing: usize,
/// Fewest segments that agree, for a grid in part of the file at an uncut decode's offset.
///
/// An encoder's delay puts the grid of its decode at a fixed offset when the start was not
/// cut ([`Family`](crate::grid::Family)), a libvorbis stream's long blocks sit at one place
/// within each 128 samples, and an AC-3 stream's first block starts at the first sample.
/// Fewer segments than the codec's own threshold asks for (`aac_grid_segments_agreeing` and
/// those of MP3, Vorbis and AC-3) show a grid in part of the file there, with the same z, as
/// the segments of a lossless file rarely agree on that one offset.
pub uncut_grid_segments_agreeing: usize,
/// Robust z over which the MP2 scan finds a frame grid.
///
/// It is compared with [`CodecScan::z`](crate::grid::CodecScan::z) of
/// [`Scan::mp2`](crate::grid::Scan::mp2).
pub mp2_grid_z: f64,
/// Robust z over which an MP2 count every segment agrees on finds a frame grid.
///
/// The MP2 scan counts at 32 phases, so three segments share one by chance far more often
/// than three share an offset of another codec's grid, and four far less often than three.
pub mp2_all_agreeing_grid_z: f64,
/// Robust z over which an MP2 count at an uncut decode's phase finds a frame grid.
///
/// An MP2 decode whose start was not cut has its count at one phase of the 32, which the
/// segments of a lossless file rarely agree on. An MP3 decode's count can sit there too, so
/// it applies where MP3's quantizer steps show in no segment.
pub mp2_uncut_grid_z: f64,
/// Fewest segments that put their lowest count at the file's phase, for an MP2 grid.
///
/// It is compared with [`CodecScan::segments_agreeing`], as `mp3_grid_segments_agreeing` is.
///
/// [`CodecScan::segments_agreeing`]: crate::grid::CodecScan::segments_agreeing
pub mp2_grid_segments_agreeing: usize,
/// Robust z over which the Vorbis scan finds a frame grid.
///
/// It is compared with [`CodecScan::z`](crate::grid::CodecScan::z) of
/// [`Scan::vorbis`](crate::grid::Scan::vorbis).
pub vorbis_grid_z: f64,
/// Fewest segments whose lowest count sits a whole number of steps from the file's offset.
///
/// A Vorbis stream's short blocks move its long ones by 128 samples each, so its segments
/// rarely share an offset and always share one modulo 128. It is compared with the
/// segments of [`CodecScan::segment_offset`](crate::grid::CodecScan::segment_offset) that
/// do.
pub vorbis_grid_segments_agreeing: usize,
/// Robust z over which the WMA scan finds a frame grid.
///
/// It is compared with [`CodecScan::z`](crate::grid::CodecScan::z) of
/// [`Scan::wma`](crate::grid::Scan::wma).
pub wma_grid_z: f64,
/// Fewest segments that put their lowest count at the file's offset, for a WMA grid.
///
/// It is compared with [`CodecScan::segments_agreeing`], as `mp3_grid_segments_agreeing` is.
///
/// [`CodecScan::segments_agreeing`]: crate::grid::CodecScan::segments_agreeing
pub wma_grid_segments_agreeing: usize,
/// Robust z over which the AC-3 scan finds a frame grid.
///
/// It is compared with [`CodecScan::z`](crate::grid::CodecScan::z) of
/// [`Scan::ac3`](crate::grid::Scan::ac3).
pub ac3_grid_z: f64,
/// Fewest segments that put their lowest count at the file's offset, for an AC-3 grid.
///
/// It is compared with [`CodecScan::segments_agreeing`], as `mp3_grid_segments_agreeing` is.
///
/// [`CodecScan::segments_agreeing`]: crate::grid::CodecScan::segments_agreeing
pub ac3_grid_segments_agreeing: usize,
/// Robust z a segment's own count passes to show a grid in part of the file.
///
/// It is compared with each of [`CodecScan::segment_z`](crate::grid::CodecScan::segment_z)
/// whose segment puts its lowest count at the file's offset, where no grid shows in the
/// file as a whole.
pub part_grid_z: f64,
/// Fewest segments that pass `part_grid_z`, for a grid in part of the file, one at least.
pub part_grid_segments: usize,
/// Robust z one segment's own count passes to show a grid in part of the file by itself.
///
/// It applies where the file's offset is one an uncut decode has, as
/// `uncut_grid_segments_agreeing` does. One segment of a lossless file can stand far out at
/// some offset, and rarely at that one.
///
/// The scan at the other rate takes one such segment too, for a decode converted afterwards.
pub lone_grid_z: f64,
/// Fewest segments whose subband count is lowest at a block-aligned phase, for a grid.
///
/// It is compared with the segments of [`Scan::mp2`](crate::grid::Scan::mp2) whose phase is
/// one an uncut MP3 or Vorbis decode has. Their blocks start a whole number of the
/// filterbank's 32 samples apart, so every segment's count is lowest at the same phase,
/// however little it stands out. The reading names Vorbis where a segment sits at the
/// phase only its decodes have, and MP3 otherwise.
///
/// It is compared too with the segments at the phase an uncut MP2 decode has, which read
/// as MP2 where MP3's quantizer steps show in none.
pub block_phase_segments: usize,
/// Robust z over which the AAC scan at the other rate finds a frame grid.
///
/// It is compared with the z of [`OtherRate::aac`](crate::grid::OtherRate::aac) and of
/// [`OtherRate::aac_sine`](crate::grid::OtherRate::aac_sine), with the agreement
/// `aac_grid_segments_agreeing` asks for. Decodes of other codecs dip further at the other
/// rate than lossless files do at their own, so it stands over `aac_grid_z`.
pub other_rate_aac_z: f64,
/// Fewest scalefactor bands on whole steps in a segment, for an MP3 grid.
///
/// It is compared with each of
/// [`Mp3Steps::segment_whole`](crate::grid::Mp3Steps::segment_whole), the bands on whole
/// steps of MP3's quantizer, where no count stands out enough for a grid: as many segments
/// as `mp3_grid_segments_agreeing` must reach it, one at least. A band of lossless audio
/// lands on a whole step by chance a few times in a file.
pub mp3_whole_steps: usize,
/// Fewest segments that reach `mp3_whole_steps`, for an MP3 grid in part of the file.
///
/// One at least. It applies where fewer segments reach it than a grid in the whole file
/// asks for. One segment is enough where its lowest count is at the file's offset and that
/// offset is one an uncut decode has.
pub mp3_whole_steps_segments: usize,
/// Fewest scalefactor bands on whole steps in a segment, for an AAC grid.
///
/// It is compared with each of
/// [`AacSteps::segment_whole`](crate::grid::AacSteps::segment_whole), the bands on whole
/// steps of AAC's quantizer, under each window. The steps show a grid no count stands out
/// enough for, and name the window of one a count found.
pub aac_whole_steps: usize,
/// Fewest segments that reach `aac_whole_steps`, for an AAC grid, one at least.
pub aac_whole_steps_segments: usize,
/// Fewest frames on Opus's lattice at one offset, for an Opus grid.
///
/// It is compared with [`OpusScan::frames`](crate::grid::OpusScan::frames), the count over
/// the four segments. A frame of lossless audio lands on the lattice by chance at some
/// offset in some files, never at the same offset often.
pub opus_grid_frames: usize,
/// Fewest frames on Opus's lattice in the windows, for an Opus grid.
///
/// It is compared with [`OpusScan::uncut_frames`](crate::grid::OpusScan::uncut_frames), the
/// count on the one grid a decode has when nothing was cut from its start.
pub opus_uncut_frames: usize,
/// Fewest frames on Opus's lattice at another level, at one offset.
///
/// It is compared with [`OpusShifted::frames`](crate::grid::OpusShifted::frames), the count
/// over the four segments, where neither count at the decoder's own level makes an Opus
/// grid. The search for the level gives chance more frames than `opus_grid_frames` meets.
pub opus_shifted_frames: usize,
/// Fewest frames on Opus's lattice at another level in the windows.
///
/// It is compared with
/// [`OpusShifted::uncut_frames`](crate::grid::OpusShifted::uncut_frames), as
/// `opus_shifted_frames` is with its count.
pub opus_shifted_uncut_frames: usize,
/// Fewest frames on Opus's lattice once the pitch post-filter is taken off, in the windows.
///
/// It is compared with [`OpusUnfiltered::frames`](crate::grid::OpusUnfiltered::frames),
/// where no count at the decoder's level, at another level or of frames under 20 ms makes
/// an Opus grid. The search for the filter gives chance more frames than `opus_uncut_frames`
/// meets.
pub opus_unfiltered_frames: usize,
/// Fewest frames under 20 ms on Opus's lattice at one offset.
///
/// It is compared with [`OpusShort::frames`](crate::grid::OpusShort::frames) of each
/// length, the count over the four segments, where no count of 20 ms frames makes an Opus
/// grid.
pub opus_short_frames: usize,
/// Fewest frames under 20 ms on Opus's lattice in the windows.
///
/// It is compared with [`OpusShort::uncut_frames`](crate::grid::OpusShort::uncut_frames)
/// of each length, as `opus_short_frames` is with its count.
pub opus_short_uncut_frames: usize,
/// Weights that read an MP3 grid at offset 1 as Fraunhofer's encoder.
pub fraunhofer_steps: FraunhoferSteps,
/// Fewest different pairs of steps among Opus's hybrid frames.
///
/// It is compared with [`OpusHybrid::steps`](crate::grid::OpusHybrid::steps) and with
/// [`OpusHybrid::uncut_steps`](crate::grid::OpusHybrid::uncut_steps), where no other count
/// makes an Opus grid. The frames counted must meet `opus_hybrid_share` too.
pub opus_hybrid_steps: usize,
/// Fewest hybrid frames as a share of the frames tested, from 0 to 1.
///
/// The two bands a hybrid frame is tested on land on the lattice by chance in a few frames
/// of a hundred, which in a long file is more pairs of steps than `opus_hybrid_steps`.
pub opus_hybrid_share: f64,
}
/// Weights that read the steps of an MP3 decode as Fraunhofer's encoder.
///
/// Encoders that state no delay all leave their grid at offset 1. They differ in how they
/// move a band's quantizer step from the band under it, which
/// [`Mp3Neighbours`](crate::grid::Mp3Neighbours) counts. Each weight multiplies one share of
/// those counts, and a decode whose sum is over `over` reads as Fraunhofer's.
///
/// The weights were fitted on decodes of the codec Windows ships against Shine, BladeEnc,
/// GOGO, Apple, Helix and LAME. Another version of Fraunhofer's encoder may fall under the
/// threshold.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "serde",
serde(default, deny_unknown_fields, rename_all = "camelCase")
)]
pub struct FraunhoferSteps {
/// Fewest pairs of neighbouring bands, over the three regions.
pub min_pairs: usize,
/// Fewest pairs in each region.
pub min_region_pairs: usize,
/// Weight of the pairs whose higher band is two quarter powers finer.
///
/// The pairs count as a share of all.
pub two_finer: f64,
/// Weight of the pairs whose higher band is two quarter powers coarser.
///
/// The pairs count as a share of all.
pub two_coarser: f64,
/// Weight of each region's pairs with one step.
///
/// The pairs count as a share of the region's.
pub same: [f64; 3],
/// Weight of each region's pairs whose higher band is finer.
///
/// The pairs count as a share of the region's.
pub finer: [f64; 3],
/// Weight of the mean logarithm of the largest value in bands 0 to 5.
pub low_log: f64,
/// Weight of the mean logarithm of the largest value in bands 12 up.
pub high_log: f64,
/// Weight of the lowest region's pairs, as a share of all.
pub low_share: f64,
/// Weight of the highest region's pairs, as a share of all.
pub high_share: f64,
/// Sum over which the steps read as Fraunhofer's.
///
/// A higher value names fewer of its decodes and fewer of another encoder's.
pub over: f64,
}
impl Default for FraunhoferSteps {
fn default() -> Self {
// fitted in the tuning on 445 decodes of Fraunhofer's codec against 2,989 of other
// encoders. each master left out in turn, the threshold names 361 of the 445, none of
// 468 decodes of the other encoders at offset 1 and 3 of 2,521 of LAME and Helix
Self {
min_pairs: 200,
min_region_pairs: 30,
two_finer: 18.913,
two_coarser: -46.687,
same: [-10.995, 14.846, 24.374],
finer: [-2.863, 37.678, 15.258],
low_log: -2.315,
high_log: 1.054,
low_share: -1.271,
high_share: -6.886,
over: 26.001,
}
}
}
/// Band top of a rate some old equipment recorded at.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct RateEdge {
/// Half the rate, in Hz.
pub hz: f64,
/// Rate and who used it, as a limitation names it.
pub rate: String,
}
/// Lossy encoder setting known by where it cuts off.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct EncoderProfile {
/// Cutoff at 44.1 and 48 kHz, in Hz.
pub hz: f64,
/// Codec that cuts here, spelled as a grid's codec is written.
pub codec: String,
/// Setting of that codec that cuts here, in words.
pub setting: String,
/// Another codec that cuts here too.
#[cfg_attr(feature = "serde", serde(default))]
pub also: Option<String>,
/// Highest nominal bitrate cut here, in kbit/s.
pub kbps: u32,
}
impl EncoderProfile {
fn mp3(hz: f64, setting: &str, kbps: u32) -> Self {
Self {
hz,
codec: "MP3".to_string(),
setting: setting.to_string(),
also: None,
kbps,
}
}
}
/// Writes the profile's name: the codec, its setting, then the other codec after "or".
impl fmt::Display for EncoderProfile {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {}", self.codec, self.setting)?;
match &self.also {
Some(also) => write!(f, " or {also}"),
None => Ok(()),
}
}
}
impl Default for ReadingThresholds {
fn default() -> Self {
Self {
sharp_shelf_db: 30.0,
full_band: FULL_BAND,
natural_limit: 0.75,
inner_shelf_db: 20.0,
high_step_db: 10.0,
clear_db: 10.0,
min_windows: 20,
min_seconds: 30.0,
few_seconds_over: 10.0,
consistent_share: 0.9,
transcode_share: 0.85,
cluster_hz: 500.0,
ceiling: 0.9,
mixed_share: 0.15,
// 22.05, 32, 44.1 and 48 kHz
lower_rate_edges: vec![11_025.0, 16_000.0, 22_050.0, 24_000.0],
other_rate_edges: [
(18_900.0, "37.8 kHz (PlayStation and CD-i audio)"),
(15_000.0, "30 kHz (some older samplers)"),
(13_020.0, "26.04 kHz (some older samplers)"),
]
.map(|(hz, rate)| RateEdge {
hz,
rate: rate.to_string(),
})
.to_vec(),
mono_correlation: 0.9999,
mono_side_db: -90.0,
confidence_cap: 0.98,
quiet_dbfs: -40.0,
high_bitrate_kbps: 320,
// LAME's defaults are the commonest; other MP3 encoders and AAC cut a little lower at the
// same bitrate
profiles: vec![
EncoderProfile::mp3(11_000.0, "~64 kbps", 64),
EncoderProfile::mp3(13_500.0, "~80 kbps", 80),
EncoderProfile::mp3(15_100.0, "~96 kbps", 96),
EncoderProfile::mp3(16_000.0, "~128 kbps", 128),
EncoderProfile::mp3(17_000.0, "~128 kbps", 128),
EncoderProfile::mp3(17_500.0, "~160 kbps", 160),
EncoderProfile::mp3(18_600.0, "~192 kbps", 192),
EncoderProfile::mp3(19_400.0, "~224 kbps", 224),
// LAME 3.100 cuts 224 kbps here too, so the name says both
EncoderProfile::mp3(19_530.0, "~224-256 kbps", 256),
EncoderProfile {
also: Some("AAC".to_string()),
..EncoderProfile::mp3(20_000.0, "~320 kbps", 320)
},
EncoderProfile::mp3(20_500.0, "~320 kbps", 320),
],
// of 409 lossless files of the tuning, AAC reaches z 5.2 and its decodes start at 14.9
// with three segments agreeing. an MP3 decode's AAC count passes that in one segment, and
// a lossless file's MP3 count with an MP3 section in two
aac_grid_z: 6.0,
aac_grid_segments_agreeing: 3,
mp3_grid_z: 7.0,
mp3_grid_segments_agreeing: 3,
// of 5,400 lossless files of the corpus none has two segments agreeing at such an offset
// with a z over its codec's
uncut_grid_segments_agreeing: 2,
// the lossless files reach 5.0 with two segments on the step at most
vorbis_grid_z: 6.0,
vorbis_grid_segments_agreeing: 3,
// the lossless files reach 8.8 in one segment, and WMA decodes start at 208
wma_grid_z: 12.0,
wma_grid_segments_agreeing: 3,
// a segment of the held-out lossless files reaches 6.6, and lower thresholds find few
// more decodes
ac3_grid_z: 8.0,
ac3_grid_segments_agreeing: 3,
// of 1,026 lossless files none has three segments agreeing, and of 332 MP2 decodes
// 327 have four, from z 4.1
mp2_grid_z: 12.0,
mp2_all_agreeing_grid_z: 4.0,
// of 5,400 lossless files of the corpus none has three segments at that phase
mp2_uncut_grid_z: 6.0,
mp2_grid_segments_agreeing: 3,
// a lossless file's segment reaches 20 alone, and 7.1 beside another at its offset
part_grid_z: 7.0,
part_grid_segments: 2,
// a segment of the corpus's lossless files reaches 8.3 at such an offset
lone_grid_z: 12.0,
// four segments sit at two phases of 32 by chance once in 65,536 files, and none of the
// corpus's 5,400 lossless files does
block_phase_segments: 4,
// the tuning's files with no AAC behind a conversion reach 9.63 there, and its converted
// AAC decodes that show a grid start at 16.7
other_rate_aac_z: 13.0,
// the second stretch of the corpus's files that were never MP3 reaches eight whole
// steps, of 4,490 files
mp3_whole_steps: 15,
mp3_whole_steps_segments: 2,
// a stretch of the tuning's Vorbis decodes reaches 138 whole steps under one window, and of
// its files that were never AAC 11
aac_whole_steps: 200,
aac_whole_steps_segments: 1,
// the tuning's files that were never Opus reach one frame in the stretches and one in
// the windows
opus_grid_frames: 3,
opus_uncut_frames: 3,
// with a level searched for they reach four frames in the stretches and one in the
// windows, apart from one master that carries such frames on an uncut decode's grid
opus_shifted_frames: 6,
opus_shifted_uncut_frames: 4,
// of frames under 20 ms they reach one in the stretches and one in the windows
opus_short_frames: 3,
opus_short_uncut_frames: 3,
// of 3,837 lossless files of the corpus none has such a frame, of 40 pairs searched
opus_unfiltered_frames: 4,
fraunhofer_steps: FraunhoferSteps::default(),
// the pilot files that were never Opus reach nine pairs of steps at this share, and a
// share of 0.04 with this many pairs
opus_hybrid_steps: 30,
opus_hybrid_share: 0.1,
}
}
}
impl ReadingThresholds {
/// fewer seconds of non-silent audio than this give a reading little to go on, which it says
pub(crate) fn few_seconds(&self) -> f64 {
self.min_seconds + self.few_seconds_over
}
/// the profile whose cutoff is nearest `hz`, when one is within `cluster_hz`
pub(crate) fn profile_at(&self, hz: f64) -> Option<&EncoderProfile> {
self
.profiles
.iter()
.filter(|p| (p.hz - hz).abs() <= self.cluster_hz)
.min_by(|a, b| (a.hz - hz).abs().total_cmp(&(b.hz - hz).abs()))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn thresholds_read_from_json_keep_the_defaults_they_do_not_name() {
let measure: MeasureThresholds = serde_json::from_str(r#"{"cutoffRangeDb": 50.0}"#).unwrap();
assert_eq!(
measure,
MeasureThresholds {
cutoff_range_db: 50.0,
..MeasureThresholds::default()
}
);
let reading: ReadingThresholds = serde_json::from_str(r#"{"sharpShelfDb": 40.0}"#).unwrap();
assert_eq!(
reading,
ReadingThresholds {
sharp_shelf_db: 40.0,
..ReadingThresholds::default()
}
);
}
fn refused<T: serde::de::DeserializeOwned>(json: &str) {
assert!(
serde_json::from_str::<T>(json).is_err(),
"{json} is refused"
);
}
#[test]
fn a_key_no_threshold_has_is_refused_at_every_level() {
refused::<MeasureThresholds>(r#"{"cutoffRange": 50.0}"#);
refused::<ReadingThresholds>(r#"{"sharpShelf": 40.0}"#);
refused::<ReadingThresholds>(
r#"{"profiles": [{"hz": 16000.0, "codec": "a", "setting": "b", "kbps": 128, "guid": "A"}]}"#,
);
refused::<ReadingThresholds>(
r#"{"otherRateEdges": [{"hz": 15000.0, "rate": "30 kHz", "source": "a"}]}"#,
);
}
}