Python Dynamics and Repair API
Every dynamics and repair stage of a mastering chain as a one-shot module-level function, with the matching measurement functions that report defects without changing the audio. The chains that run these stages in order, and the config keys they share, are on Python Mastering API; the assistant that recommends stages is on Python Mastering API.
Standalone dynamics and repair
Every named stage is also a one-shot module-level function, so you can run a single processor without assembling a chain. Parameters are keyword-only and mirror the corresponding MasteringChainConfig keys in snake_case. The dynamics processors return a DynamicsProcessorResult (samples and an int latency_samples); the repair processors return a result record with the repaired samples and a report.
| Function | Returns |
|---|---|
mastering_dynamics_compressor() | DynamicsProcessorResult |
mastering_dynamics_gate() | DynamicsProcessorResult |
mastering_dynamics_transient_shaper() | DynamicsProcessorResult |
mastering_repair_declick() | DeclickResult |
mastering_repair_declip() | DeclipResult |
mastering_repair_decrackle() | DecrackleResult |
mastering_repair_dehum() | DehumResult |
mastering_repair_denoise_classical() | DenoiseResult |
mastering_repair_dereverb_classical() | DereverbResult |
mastering_repair_trim_silence() | TrimSilenceResult |
sidechain_high_pass_hz and key_high_pass_hz take None for off. clip_threshold_db of mastering_repair_declip() defaults to the level of a linear 0.98. mastering_repair_trim_silence() takes padding_samples or padding_sec, one of them.
# Apply the offline feed-forward compressor.
def mastering_dynamics_compressor(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
threshold_db: float = -18.0,
ratio: float = 2.0,
attack_ms: float = 10.0,
release_ms: float = 100.0,
knee_db: float = 0.0,
makeup_gain_db: float = 0.0,
auto_makeup: bool = False,
detector: CompressorDetector = 'rms',
sidechain_high_pass_hz: float | None = None,
pdr_time_ms: float = 0.0,
pdr_release_scale: float = 1.0,
) -> DynamicsProcessorResult: ...
# Apply the offline noise gate.
def mastering_dynamics_gate(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
threshold_db: float = -50.0,
attack_ms: float = 2.0,
release_ms: float = 80.0,
range_db: float = -80.0,
hold_ms: float = 0.0,
close_threshold_db: float = -50.0,
key_high_pass_hz: float | None = None,
) -> DynamicsProcessorResult: ...
# Apply the offline envelope-difference transient shaper.
def mastering_dynamics_transient_shaper(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
attack_gain_db: float = 3.0,
sustain_gain_db: float = 0.0,
fast_attack_ms: float = 0.0,
fast_release_ms: float = 20.0,
slow_attack_ms: float = 15.0,
slow_release_ms: float = 200.0,
sensitivity: float = 1.0,
max_gain_db: float = 12.0,
gain_smoothing_ms: float = 0.0,
lookahead_ms: float = 0.0,
) -> DynamicsProcessorResult: ...# Offline LPC-based declicker.
def mastering_repair_declick(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
threshold: float = 0.8,
neighbor_ratio: float = 4.0,
max_click_samples: int = 8,
lpc_order: int = 20,
residual_ratio: float = 8.0,
) -> DeclickResult: ...
# Offline LPC-based declipper.
def mastering_repair_declip(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
clip_threshold_db: float = _DEFAULT_CLIP_THRESHOLD_DB,
lpc_order: int = 36,
iterations: int = 2,
) -> DeclipResult: ...
# Offline crackle suppressor (median or wavelet-shrinkage).
def mastering_repair_decrackle(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
threshold: float = 0.4,
mode: DecrackleMode = 'median',
levels: int = 4,
) -> DecrackleResult: ...
# Offline mains-hum remover.
def mastering_repair_dehum(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
fundamental_hz: float = 50.0,
harmonics: int = 4,
q: float = 20.0,
adaptive: bool = False,
search_range_hz: float = 2.0,
adaptation: float = 0.25,
frame_size: int = 2048,
pll_bandwidth: float = 0.01,
mode: DehumMode = 'subtract',
) -> DehumResult: ...
# Offline STFT-domain classical denoiser.
def mastering_repair_denoise_classical(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
mode: DenoiseClassicalMode = 'logMmse',
noise_estimator: DenoiseClassicalNoiseEstimator = 'quantile',
n_fft: int = 1024,
hop_length: int = 256,
dd_alpha: float = 0.98,
reduction_db: float = 26.0,
over_subtraction: float = 2.0,
spectral_floor: float = 0.05,
noise_estimation_quantile: float = 0.1,
speech_presence_gain: bool = True,
gain_smoothing: bool = True,
) -> DenoiseResult: ...
# Offline classical dereverberator (spectral subtraction + optional WPE).
def mastering_repair_dereverb_classical(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
threshold: float = 0.0,
attenuation: float = 1.0,
n_fft: int = 1024,
hop_length: int = 256,
t60_sec: float = 0.4,
late_delay_ms: float = 50.0,
over_subtraction: float = 1.0,
spectral_floor: float = 0.08,
wpe_enabled: bool = False,
wpe_iterations: int = 2,
wpe_taps: int = 3,
wpe_strength: float = 0.7,
) -> DereverbResult: ...
# Offline silence trimmer (peak threshold or LUFS-gated).
def mastering_repair_trim_silence(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
threshold_db: float = -60.0,
padding_samples: int | None = None,
padding_sec: float | None = None,
mode: TrimSilenceMode = 'peak',
gate_lufs: float = -60.0,
window_ms: float = 400.0,
) -> TrimSilenceResult: ...The whole-signal repair stages (declick, declip, decrackle, dehum, dereverb) are rejected by StreamingMasteringChain. Run them with these one-shot helpers or inside master_audio*. In the realtime engine, decrackle, dehum, the classical denoiser and the classical dereverb are also available as inserts under their catalog ids; declick and declip stay offline.
Each repair helper also has a _stereo form, and the denoiser and dereverb a _linked form that treats the channels as one program.
# Declicks a stereo pair, repairing the union of both channels' runs.
def mastering_repair_declick_stereo(
left: Sequence[float] | list[float] | np.ndarray,
right: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
threshold: float = 0.8,
neighbor_ratio: float = 4.0,
max_click_samples: int = 8,
lpc_order: int = 20,
residual_ratio: float = 8.0,
) -> DeclickStereoResult: ...
# Declips a stereo pair.
def mastering_repair_declip_stereo(
left: Sequence[float] | list[float] | np.ndarray,
right: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
clip_threshold_db: float = _DEFAULT_CLIP_THRESHOLD_DB,
lpc_order: int = 36,
iterations: int = 2,
link_channels: bool = False,
) -> DeclipStereoResult: ...
# Decrackles a stereo pair, each channel on its own.
def mastering_repair_decrackle_stereo(
left: Sequence[float] | list[float] | np.ndarray,
right: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
threshold: float = 0.4,
mode: DecrackleMode = 'median',
levels: int = 4,
) -> DecrackleStereoResult: ...
# Dehums a stereo pair, sharing the tracked fundamental when tracking is on.
def mastering_repair_dehum_stereo(
left: Sequence[float] | list[float] | np.ndarray,
right: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
fundamental_hz: float = 50.0,
harmonics: int = 4,
q: float = 20.0,
adaptive: bool = False,
search_range_hz: float = 2.0,
adaptation: float = 0.25,
frame_size: int = 2048,
pll_bandwidth: float = 0.01,
mode: DehumMode = 'subtract',
) -> DehumStereoResult: ...
# Denoises a stereo pair with one channel-linked gain mask.
def mastering_repair_denoise_classical_stereo(
left: Sequence[float] | list[float] | np.ndarray,
right: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
mode: DenoiseClassicalMode = 'logMmse',
noise_estimator: DenoiseClassicalNoiseEstimator = 'quantile',
n_fft: int = 1024,
hop_length: int = 256,
dd_alpha: float = 0.98,
reduction_db: float = 26.0,
over_subtraction: float = 2.0,
spectral_floor: float = 0.05,
noise_estimation_quantile: float = 0.1,
speech_presence_gain: bool = True,
gain_smoothing: bool = True,
) -> DenoiseStereoResult: ...
# Denoises any number of channels with one channel-linked gain mask.
def mastering_repair_denoise_classical_linked(
channels: Sequence[Sequence[float] | np.ndarray] | np.ndarray,
sample_rate: int,
*,
mode: DenoiseClassicalMode = 'logMmse',
noise_estimator: DenoiseClassicalNoiseEstimator = 'quantile',
n_fft: int = 1024,
hop_length: int = 256,
dd_alpha: float = 0.98,
reduction_db: float = 26.0,
over_subtraction: float = 2.0,
spectral_floor: float = 0.05,
noise_estimation_quantile: float = 0.1,
speech_presence_gain: bool = True,
gain_smoothing: bool = True,
) -> DenoiseLinkedResult: ...
# Dereverberates a stereo pair with one channel-linked mask.
def mastering_repair_dereverb_classical_stereo(
left: Sequence[float] | list[float] | np.ndarray,
right: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
threshold: float = 0.0,
attenuation: float = 1.0,
n_fft: int = 1024,
hop_length: int = 256,
t60_sec: float = 0.4,
late_delay_ms: float = 50.0,
over_subtraction: float = 1.0,
spectral_floor: float = 0.08,
wpe_enabled: bool = False,
wpe_iterations: int = 2,
wpe_taps: int = 3,
wpe_strength: float = 0.7,
) -> DereverbStereoResult: ...
# Dereverberates any number of channels with one channel-linked mask.
def mastering_repair_dereverb_classical_linked(
channels: Sequence[Sequence[float] | np.ndarray] | np.ndarray,
sample_rate: int,
*,
threshold: float = 0.0,
attenuation: float = 1.0,
n_fft: int = 1024,
hop_length: int = 256,
t60_sec: float = 0.4,
late_delay_ms: float = 50.0,
over_subtraction: float = 1.0,
spectral_floor: float = 0.08,
wpe_enabled: bool = False,
wpe_iterations: int = 2,
wpe_taps: int = 3,
wpe_strength: float = 0.7,
) -> DereverbLinkedResult: ...
# Trims a stereo pair to one shared range.
def mastering_repair_trim_silence_stereo(
left: Sequence[float] | list[float] | np.ndarray,
right: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
threshold_db: float = -60.0,
padding_samples: int | None = None,
padding_sec: float | None = None,
mode: TrimSilenceMode = 'peak',
gate_lufs: float = -60.0,
window_ms: float = 400.0,
) -> TrimSilenceStereoResult: ...The mastering_repair_detect_* functions measure a defect (clicks, noise floor, clipping, crackle, hum, reverb, trim range) without changing the audio. mastering_repair_detect_trim_range_stereo() measures the one range a stereo trim pass would keep, the union of both channels' ranges. mastering_repair_noise_band_bins() returns the bin boundaries of the grid a detected noise floor is reported on.
# Measure clicks without repairing them.
def mastering_repair_detect_clicks(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
threshold: float = 0.8,
neighbor_ratio: float = 4.0,
max_click_samples: int = 8,
lpc_order: int = 20,
residual_ratio: float = 8.0,
) -> ClickDetection: ...
# Measure clipping without repairing it.
def mastering_repair_detect_clipping(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
clip_threshold_db: float = _DEFAULT_CLIP_THRESHOLD_DB,
lpc_order: int = 36,
iterations: int = 2,
) -> ClipDetection: ...
# Measure crackle without repairing it.
def mastering_repair_detect_crackle(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
threshold: float = 0.4,
mode: DecrackleMode = 'median',
levels: int = 4,
) -> CrackleDetection: ...
# Measure mains hum without filtering it.
def mastering_repair_detect_hum(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
fundamental_hz: float = 50.0,
harmonics: int = 4,
q: float = 20.0,
adaptive: bool = False,
search_range_hz: float = 2.0,
adaptation: float = 0.25,
frame_size: int = 2048,
pll_bandwidth: float = 0.01,
mode: DehumMode = 'subtract',
) -> HumDetection: ...
# Measure the noise floor without denoising.
def mastering_repair_detect_noise_floor(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
mode: DenoiseClassicalMode = 'logMmse',
noise_estimator: DenoiseClassicalNoiseEstimator = 'quantile',
n_fft: int = 1024,
hop_length: int = 256,
dd_alpha: float = 0.98,
reduction_db: float = 26.0,
over_subtraction: float = 2.0,
spectral_floor: float = 0.05,
noise_estimation_quantile: float = 0.1,
speech_presence_gain: bool = True,
gain_smoothing: bool = True,
) -> NoiseDetection: ...
# Measure reverberation without dereverberating.
def mastering_repair_detect_reverb(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
threshold: float = 0.0,
attenuation: float = 1.0,
n_fft: int = 1024,
hop_length: int = 256,
t60_sec: float = 0.4,
late_delay_ms: float = 50.0,
over_subtraction: float = 1.0,
spectral_floor: float = 0.08,
wpe_enabled: bool = False,
wpe_iterations: int = 2,
wpe_taps: int = 3,
wpe_strength: float = 0.7,
) -> ReverbDetection: ...
# Measure the range a trim pass would keep, without trimming.
def mastering_repair_detect_trim_range(
samples: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
threshold_db: float = -60.0,
padding_samples: int | None = None,
padding_sec: float | None = None,
mode: TrimSilenceMode = 'peak',
gate_lufs: float = -60.0,
window_ms: float = 400.0,
) -> TrimRange: ...
# Measure the one range a stereo trim pass would cut both channels to.
def mastering_repair_detect_trim_range_stereo(
left: Sequence[float] | list[float] | np.ndarray,
right: Sequence[float] | list[float] | np.ndarray,
sample_rate: int,
*,
threshold_db: float = -60.0,
padding_samples: int | None = None,
padding_sec: float | None = None,
mode: TrimSilenceMode = 'peak',
gate_lufs: float = -60.0,
window_ms: float = 400.0,
) -> TrimRange: ...
# Bin boundaries of the grid a denoise noise floor is reported on.
def mastering_repair_noise_band_bins(sample_rate: int, n_fft: int = 1024) -> np.ndarray: ...mastering_repair_dereverb_config_for_room() turns a RoomEstimate into a complete dereverb config.
# Point a dereverb config at a measured room.
def mastering_repair_dereverb_config_for_room(
estimate: RoomEstimate,
*,
threshold: float = 0.0,
attenuation: float = 1.0,
n_fft: int = 1024,
hop_length: int = 256,
t60_sec: float = 0.4,
late_delay_ms: float = 50.0,
over_subtraction: float = 1.0,
spectral_floor: float = 0.08,
wpe_enabled: bool = False,
wpe_iterations: int = 2,
wpe_taps: int = 3,
wpe_strength: float = 0.7,
) -> DereverbClassicalConfig: ...To repair audio of any channel count in two calls, mastering_repair_analyze() measures the defects per channel and returns the stages the assistant would run (recommended, ready to pass on; dereverb is never recommended), and mastering_repair_apply(channels, sample_rate, stages=[...]) runs them. Stages always run in the fixed order declip, declick, decrackle, dehum, denoise, dereverb whatever order the list gives; a repeated stage or an unknown setting is refused. The result carries the repaired channels and one report per stage (per channel, or once for the linked denoise and dereverb), and on_progress / cancel work as in Progress callbacks.
# Measure every channel's repair defects and recommend stages.
def mastering_repair_analyze(
channels: Sequence[Sequence[float] | np.ndarray] | np.ndarray,
sample_rate: int,
*,
prefer_streaming_safe: bool = True,
) -> MasteringRepairAnalysis: ...analysis = sonare.mastering_repair_analyze([left, right], 48000)
result = sonare.mastering_repair_apply([left, right], 48000, stages=analysis.recommended)streaming_loudness_gain() and streaming_loudness_gain_stereo() measure, at the loudness stage's input, the numbers a StreamingMasteringChain is constructed with. The returned StreamingLoudnessGain has loudness_static_gain_db (equal to the gain the offline chain applies, ceiling clamp included; 0 for silence), true_peak_dbtp and integrated_lufs; pass the first two as the constructor's loudness_static_gain_db and loudness_static_gain_peak_dbtp. See Dynamics and Repair.
# Measure the loudness numbers a :class:`StreamingMasteringChain` needs.
def streaming_loudness_gain(
samples: Sequence[float] | list[float],
sample_rate: int,
config: MasteringChainConfigInput | None = None,
) -> StreamingLoudnessGain: ...
# Stereo counterpart of :func:`streaming_loudness_gain`.
def streaming_loudness_gain_stereo(
left: Sequence[float] | list[float],
right: Sequence[float] | list[float],
sample_rate: int,
config: MasteringChainConfigInput | None = None,
) -> StreamingLoudnessGain: ...