Clip TTS int16 conversion instead of wrapping in Kokoro, MMS and ChatTTS #849
All Vouch review confidence checks passed
Residual risk 0.11 = Change Risk (0.35) × (1 − Review Confidence (0.68)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.35 · 61 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.35 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.247
Real PR Conversation Timeline
2 items
Approved
Great catch, thank you!
<img src="https://static.klipy.com/ii/d7aec6f6f171607374b2065c836f92f4/a4/d8/AfCHZzC1.gif"/>
andimarafioti
divya0795
Summary
Kokoro, Facebook MMS and ChatTTS (non-streaming) convert float audio to int16 with
(audio 32768).astype(np.int16)and no clip. A sample at or above full scale wraps around instead of saturating, so the speaker gets a full-scale spike of the opposite sign (a loud click) in place of a peak. This PR clips at those four sites the way the other handlers already do.The bug
Sites at
fff079a:TTS/kokoro_handler.py:403(MLX backend) and:468(kokorolibrary backend), afterresample_poly(audio, up=2, down=3)TTS/facebookmms_handler.py:205, afterlibrosa.resample(...)TTS/chatTTS_handler.py:134, thestream=Falsebranch, afterlibrosa.resample(...)Out-of-range float to int16 casts wrap. On numpy 2.4.3:
The cast runs after resampling, and band-limited resampling overshoots near full scale on transients. A 24 kHz signal peaking at 0.97, put through the same
resample_poly(..., 2, 3)call Kokoro uses, peaks at 1.177 and has 183 samples above 1.0. At the highest one the unclipped cast gives-26972where32767is expected.librosa.resampleto 16 kHz behaves the same way (peak 1.18, 229 samples above 1.0). Nothing downstream clamps these blocks before they are played or sent to the client.The rest of the TTS code already clips: Qwen3, Kitten, Irodori, OmniVoice, Supertonic, Phonon, Pocket, the OpenAI-compatible handler, and ChatTTS's own streaming branch (
chatTTS_handler.py:113,122).tests/test_kitten_tts_handler.py::test_clips_pcm_instead_of_wrappingalready tests this. These four sites are the only ones left without it.The change
At each of the four sites,
(x 32768).astype(np.int16)becomesnp.clip(x * 32768, -32768, 32767).astype(np.int16), the same expression the ChatTTS streaming branch uses. In-range samples come out the same, bit for bit. Only values outside [-1, 32767/32768] change, and they now saturate instead of wrapping.Tests
The new tests follow the Kitten test. Each one feeds the handler a resampler output of
[1.0, 1.2, -1.2, 0.5]and expects[32767, 32767, -32768, 16384]:tests/test_kokoro_handler.py::test_clips_pcm_instead_of_wrapping[kokoro|mlx]tests/test_facebookmms_tts_latency.py::test_clips_pcm_instead_of_wrappingtests/test_chattts_streaming.py::test_non_streaming_clips_pcm_instead_of_wrappingAll four fail on
main(for example, Kokoro gives[-32768, -26215, 26215, 16384]) and pass with the fix. Locally on macOS arm64 with Python 3.12:ruff check,ruff format --checkandmypy src/are clean.pytest tests/passes except for a few timing-dependent tests intest_audio_client.py,test_webrtc.pyandtest_openai_stt_lifecycle.py. On this loaded machine they also fail on unmodifiedmain, and they pass when re-run on their own.Not tested against the real Kokoro, MMS or ChatTTS models. The tests stub the provider and the resampler.
🤖 Generated with Claude Code