Skip to content

Prevent hard clipping across audio output backends - #90

Open
brxs wants to merge 3 commits into
Stability-AI:mainfrom
brxs:codex/fix-audio-peak-clipping
Open

Prevent hard clipping across audio output backends#90
brxs wants to merge 3 commits into
Stability-AI:mainfrom
brxs:codex/fix-audio-peak-clipping

Conversation

@brxs

@brxs brxs commented Jul 31, 2026

Copy link
Copy Markdown

Summary

Fixes #78.

Replace destructive output hard clipping with a shared, no-boost peak-protection policy across the PyTorch, MLX, TensorRT, and TFLite paths.

  • Preserve in-range audio unchanged.
  • When decoded audio exceeds the selected PCM ceiling, attenuate the entire waveform by one gain factor so sample ratios are preserved.
  • Reject non-finite waveforms instead of silently converting corrupted samples.
  • Trim decoder padding before measuring the peak.
  • Expose --peak-ceiling-dbfs across the CLIs, defaulting to 0 dBFS.
  • Allow API callers to select output_peak_policy="raw" when they need unmodified floating-point output for downstream mastering.
  • Route optimized CLI and Gradio WAV exports through the same conversion behavior.

This avoids boosting quiet generations while preventing out-of-range samples from being flattened at full scale.

Implementation

The common PyTorch path now centralizes peak detection, gain calculation, PCM16 conversion, and WAV serialization in stable_audio_3.audio_output. The optimized backends use small, self-contained equivalents so their standalone bootstrap bundles do not depend on the root package.

TensorRT

The decoder ONNX rewrite now removes the baked [-1, 1] clip, PCM scale, and integer cast, exposing sample-major FP32 audio_unbounded. Both eager and CUDA-graph inference can therefore validate and peak-protect the decoded waveform before PCM scaling or narrowing hides non-finite and extreme values.

The rewrite also publishes uniquely named external-data sidecars before atomically replacing the ONNX model. This avoids sidecar growth on repeated rewrites and prevents readers from observing a model that references incomplete weights.

Existing TensorRT engines with the legacy clipped pcm output remain usable and produce a compatibility warning. Engines must be rebuilt and republished to gain the new unbounded-output behavior.

Configuration and compatibility

  • Default ceiling: 0 dBFS, matching the full PCM16 range without boosting.
  • Lower ceilings can reserve mastering headroom, for example --peak-ceiling-dbfs -1.
  • Sample rate remains derived from each model/backend configuration; peak protection does not hardcode 44.1 kHz.
  • Standalone MLX, TFLite, and TensorRT bundle/import behavior is preserved.

Validation

  • 70 focused tests passed across shared peak protection, CLI plumbing, and optimized backends.
  • 9 TensorRT ONNX/ONNX Runtime rewrite tests passed, including non-finite/extreme-value preservation, repeated rewrites, and injected model-swap failure.
  • 11 TFLite cross-platform compatibility tests passed.
  • MLX and TFLite Gradio import/help smoke tests passed.
  • Standalone MLX, TFLite, and TensorRT CLI import/help smoke tests passed.
  • Targeted Ruff, formatting, byte-compilation, bootstrap syntax, and diff checks passed.

CUDA/TensorRT engine replay was not available on the development Mac, so GPU execution remains for upstream CI or maintainer validation.

@brxs
brxs force-pushed the codex/fix-audio-peak-clipping branch from 1af5120 to 9d0f559 Compare July 31, 2026 23:50
@brxs
brxs marked this pull request as ready for review July 31, 2026 23:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MLX WAV serializer silently hard-clips out-of-range generated audio

1 participant