← Back to Pull requests

Clip TTS int16 conversion instead of wrapping in Kokoro, MMS and ChatTTS #849

Merged
divya0795 merged 6 files (61 lines) on Nov 23, 2026 09:19 UTC
View on GitHub
divya0795 divya0795 commented on Nov 18, 2026 10:12 UTC
Author

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 (kokoro library backend), after resample_poly(audio, up=2, down=3)

  • TTS/facebookmms_handler.py:205, after librosa.resample(...)

  • TTS/chatTTS_handler.py:134, the stream=False branch, after librosa.resample(...)
  • Out-of-range float to int16 casts wrap. On numpy 2.4.3:

    float           1.0      1.2     -1.2     -1.0001
    unclipped    -32768   -26215    26215     32765
    clipped       32767    32767   -32768    -32768
    

    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 -26972 where 32767 is expected. librosa.resample to 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_wrapping already 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) becomes np.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_wrapping

  • tests/test_chattts_streaming.py::test_non_streaming_clips_pcm_instead_of_wrapping
  • All 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 --check and mypy src/ are clean. pytest tests/ passes except for a few timing-dependent tests in test_audio_client.py, test_webrtc.py and test_openai_stt_lifecycle.py. On this loaded machine they also fail on unmodified main, 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

    Diff: 61 lines (+57 / -4) across 6 files Reviewer: andimarafioti Review duration: 7147m
    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
    0.35
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.35 · 1 substantive comment(s)
    0.35
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    67%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    andimarafioti andimarafioti approved these changes on Nov 23, 2026 09:19 UTC
    Approved Reviewer
    Reviewers
    Assignees
    divya0795 divya0795
    Labels
    risk: low confidence: 67.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 35% (MEDIUM) · Higher = larger blast radius
    35%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 67.5% · Lower = rushed or shallow review
    67.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 11.4% · Safe to merge
    11.4%