|
| 1 | +//! Clipping probe for any fixed playback gain applied after Pocket TTS synth. |
| 2 | +//! |
| 3 | +//! Synthesises a spread of sentences (short/long, calm/energetic) and reports |
| 4 | +//! the raw peak of each, the post-gain peak, and the fraction of samples that |
| 5 | +//! would hit a ±1.0 clamp — i.e. how much a fixed gain would flat-top the |
| 6 | +//! waveform ("blown out" distortion). |
| 7 | +//! |
| 8 | +//! History: the production pipeline briefly shipped a fixed 9.3× gain |
| 9 | +//! calibrated on a single bench utterance that peaked at 0.076. This probe |
| 10 | +//! showed real output peaks at 0.4–0.97, so that gain clipped 13–34% of all |
| 11 | +//! samples (the 2026-06-12 "blown out" report). Production now applies no |
| 12 | +//! gain — run this probe before reintroducing one. |
| 13 | +//! |
| 14 | +//! Run with model files in ~/.buzz/models/pocket-tts (override with arg 1): |
| 15 | +//! cargo run --release --example pocket_clip_probe |
| 16 | +
|
| 17 | +use std::path::PathBuf; |
| 18 | + |
| 19 | +use sherpa_onnx::{ |
| 20 | + self, GenerationConfig, OfflineTts, OfflineTtsConfig, OfflineTtsModelConfig, |
| 21 | + OfflineTtsPocketModelConfig, Wave, |
| 22 | +}; |
| 23 | + |
| 24 | +/// Candidate gain under test (the regressed production value). |
| 25 | +const GAIN: f32 = 9.3; |
| 26 | + |
| 27 | +const PROMPTS: &[&str] = &[ |
| 28 | + "Hello, this is a test of the new Pocket TTS engine running on sherpa-onnx.", |
| 29 | + "Yep, I can hear you.", |
| 30 | + "Absolutely! That sounds fantastic, let's do it right now!", |
| 31 | + "The quick brown fox jumps over the lazy dog near the riverbank.", |
| 32 | + "I found three problems in the code: a race condition, a memory leak, and an off-by-one error in the loop bounds.", |
| 33 | + "No.", |
| 34 | + "Warning! The build failed because seventeen tests crashed unexpectedly!", |
| 35 | + "Sure, I can walk you through the whole pipeline step by step whenever you're ready.", |
| 36 | +]; |
| 37 | + |
| 38 | +fn main() { |
| 39 | + let model_dir = std::env::args().nth(1).unwrap_or_else(|| { |
| 40 | + dirs::home_dir() |
| 41 | + .expect("home dir") |
| 42 | + .join(".buzz/models/pocket-tts") |
| 43 | + .to_string_lossy() |
| 44 | + .into_owned() |
| 45 | + }); |
| 46 | + eprintln!("Model dir: {model_dir}"); |
| 47 | + |
| 48 | + let dir = PathBuf::from(&model_dir); |
| 49 | + let p = |name: &str| dir.join(name).to_string_lossy().into_owned(); |
| 50 | + |
| 51 | + let cfg = OfflineTtsConfig { |
| 52 | + model: OfflineTtsModelConfig { |
| 53 | + pocket: OfflineTtsPocketModelConfig { |
| 54 | + lm_main: Some(p("lm_main.int8.onnx")), |
| 55 | + lm_flow: Some(p("lm_flow.int8.onnx")), |
| 56 | + encoder: Some(p("encoder.onnx")), |
| 57 | + decoder: Some(p("decoder.int8.onnx")), |
| 58 | + text_conditioner: Some(p("text_conditioner.onnx")), |
| 59 | + vocab_json: Some(p("vocab.json")), |
| 60 | + token_scores_json: Some(p("token_scores.json")), |
| 61 | + voice_embedding_cache_capacity: 16, |
| 62 | + }, |
| 63 | + num_threads: 1, |
| 64 | + debug: false, |
| 65 | + ..Default::default() |
| 66 | + }, |
| 67 | + ..Default::default() |
| 68 | + }; |
| 69 | + let engine = OfflineTts::create(&cfg).expect("engine create"); |
| 70 | + |
| 71 | + let voice_path = dir.join("reference_sample.wav"); |
| 72 | + let wave = Wave::read(voice_path.to_str().unwrap()).expect("voice WAV"); |
| 73 | + let voice_samples = wave.samples().to_vec(); |
| 74 | + let voice_sr = wave.sample_rate(); |
| 75 | + |
| 76 | + let gen = || GenerationConfig { |
| 77 | + speed: 1.05, |
| 78 | + num_steps: 1, |
| 79 | + silence_scale: 1.0, |
| 80 | + reference_audio: Some(voice_samples.clone()), |
| 81 | + reference_sample_rate: voice_sr, |
| 82 | + ..Default::default() |
| 83 | + }; |
| 84 | + |
| 85 | + // Warmup. |
| 86 | + let _ = engine.generate_with_config("warmup.", &gen(), None::<fn(&[f32], f32) -> bool>); |
| 87 | + |
| 88 | + println!( |
| 89 | + "{:<46} | {:>8} | {:>9} | {:>9} | {:>10}", |
| 90 | + "prompt", "raw peak", "raw RMS", "post-gain", "% clipped" |
| 91 | + ); |
| 92 | + println!("{}", "-".repeat(95)); |
| 93 | + |
| 94 | + let mut worst_clip = 0.0f32; |
| 95 | + for prompt in PROMPTS { |
| 96 | + let out = engine |
| 97 | + .generate_with_config(prompt, &gen(), None::<fn(&[f32], f32) -> bool>) |
| 98 | + .expect("synth"); |
| 99 | + let samples = out.samples(); |
| 100 | + |
| 101 | + let peak = samples.iter().fold(0.0f32, |m, s| m.max(s.abs())); |
| 102 | + let rms = (samples.iter().map(|s| s * s).sum::<f32>() / samples.len() as f32).sqrt(); |
| 103 | + let post = peak * GAIN; |
| 104 | + let clipped = samples.iter().filter(|s| s.abs() * GAIN > 1.0).count(); |
| 105 | + let clip_pct = 100.0 * clipped as f32 / samples.len() as f32; |
| 106 | + worst_clip = worst_clip.max(clip_pct); |
| 107 | + |
| 108 | + let label: String = prompt.chars().take(44).collect(); |
| 109 | + println!("{label:<46} | {peak:>8.4} | {rms:>9.4} | {post:>9.3} | {clip_pct:>9.3}%"); |
| 110 | + } |
| 111 | + |
| 112 | + println!(); |
| 113 | + println!( |
| 114 | + "Verdict: worst-case clipped fraction {worst_clip:.3}% — {}", |
| 115 | + if worst_clip > 0.1 { |
| 116 | + "AUDIBLE DISTORTION LIKELY (gain too hot)" |
| 117 | + } else if worst_clip > 0.0 { |
| 118 | + "marginal — occasional transient clipping" |
| 119 | + } else { |
| 120 | + "no clipping at this gain" |
| 121 | + } |
| 122 | + ); |
| 123 | +} |
0 commit comments