π π A Python library for audio.
C audioaudio-processingaudio-productionaudio-researchaudio-unitaugmentationjucemachine-learningpybind11pythonvst3vst3-host
fix(BrickwallLimiter): conservative upsampler delay + atomic params + type stub
Christopher Jacobydf5d78bb4f9d
diff --git a/pedalboard/plugins/BrickwallLimiter.h b/pedalboard/plugins/BrickwallLimiter.hindex d38120b..3bcda88 100644--- a/pedalboard/plugins/BrickwallLimiter.h+++ b/pedalboard/plugins/BrickwallLimiter.h@@ -19,6 +19,7 @@#include "../JuceHeader.h"#include "../Plugin.h"+#include <atomic>#include <cmath>namespace Pedalboard {@@ -39,6 +40,8 @@ namespace Pedalboard {** Design: setters store REQUESTED parameter values. prepare() copies them* to ACTIVE state. process() reads only ACTIVE state.+ * ceiling_db and release_ms use std::atomic<float> for safe concurrent+ * access between Python setters and the native audio callback.* Coefficients are snapshotted at block boundaries for thread safety.*/template <typename SampleType> class BrickwallLimiter : public Plugin {@@ -47,18 +50,22 @@ public:// Setters store requested values. Derived coefficients are snapshotted// in process() at block boundaries for thread safety with AudioStream.- float getCeilingDb() const noexcept { return ceilingDb_; }+ float getCeilingDb() const noexcept {+ return ceilingDb_.load(std::memory_order_relaxed);+ }void setCeilingDb(float value) {if (std::isnan(value) || std::isinf(value))throw std::range_error("ceiling_db must be a finite number");- ceilingDb_ = value;+ ceilingDb_.store(value, std::memory_order_relaxed);}- float getReleaseMs() const noexcept { return releaseMs_; }+ float getReleaseMs() const noexcept {+ return releaseMs_.load(std::memory_order_relaxed);+ }void setReleaseMs(float value) {if (value <= 0.0f || std::isnan(value) || std::isinf(value))throw std::range_error("release_ms must be > 0");- releaseMs_ = value;+ releaseMs_.store(value, std::memory_order_relaxed);}float getLookaheadMs() const noexcept { return lookaheadMs_; }@@ -105,23 +112,20 @@ public:juce::dsp::Oversampling<SampleType>::filterHalfBandFIREquiripple,true); // isMaxQualityoversampler_->initProcessing(spec.maximumBlockSize);- // Upsampling-only group delay approximation: half of- // getLatencyInSamples(), which JUCE reports as the round-trip- // (up+down) latency averaged across stages. JUCE does not expose a- // per-direction (up-only vs. down-only) latency, and for- // filterHalfBandFIREquiripple the up- and down-filters are designed- // to different specs (the up-filter targets a narrower transition- // band and deeper stopband, so its FIR order is typically higher- // than the down-filter's) β so this /2.0 split is an approximation,- // not an exact figure. In practice the error is a small number of- // samples, dwarfed by the base lookahead for typical lookahead_ms- // values, and end-to-end latency self-consistency (delay line delay- // == reported getLatencyHint()) is covered by- // test_upsampler_latency_calibration β that test cannot validate the- // /2.0 split itself, since it only exercises pedalboard's own- // latency-hint-driven output alignment.+ // Conservative upsampler delay compensation: use the FULL reported+ // round-trip latency as the delay line extension.+ //+ // getLatencyInSamples() reports the combined up+down path latency.+ // The actual upsampling-only delay is somewhere between half and the+ // full value (JUCE's equiripple up- and down-filters have different+ // orders, and no per-direction accessor is exposed). Using the full+ // value over-compensates slightly β the delay line is a bit longer+ // than strictly needed β but this guarantees the gain reduction+ // always arrives before the peak exits the delay line, even with+ // very short lookahead_ms values. The extra latency is typically+ // < 10 samples, negligible for most use cases.upsampleDelay_ =- (int)std::round(oversampler_->getLatencyInSamples() / 2.0);+ (int)std::ceil(oversampler_->getLatencyInSamples());} else {oversampler_.reset();}@@ -146,11 +150,17 @@ public:int numSamples = (int)ioBlock.getNumSamples();int numChannels = (int)ioBlock.getNumChannels();- // Snapshot parameters at block boundary for thread safety- float ceilingLinear = std::pow(10.0f, ceilingDb_ / 20.0f);+ // Snapshot parameters at block boundary for thread safety.+ // ceilingDb_ and releaseMs_ are std::atomic<float>, ensuring no data race+ // when setters are called from another thread (e.g., Python property access+ // while AudioStream's native callback is running).+ float ceilingLinear =+ std::pow(10.0f, ceilingDb_.load(std::memory_order_relaxed) / 20.0f);float releaseCoeff = std::exp(-1.0f /- std::max(1, (int)(preparedSpec_.sampleRate * releaseMs_ / 1000.0f)));+ std::max(1, (int)(preparedSpec_.sampleRate *+ releaseMs_.load(std::memory_order_relaxed) /+ 1000.0f)));int lookaheadSamples = activeLookaheadSamples_;// ββ Step 1: Detect peaks ββ@@ -244,9 +254,11 @@ public:virtual int getLatencyHint() override { return activeLookaheadSamples_; }private:- // Requested parameters (written by setters, read by process() at block start)- float ceilingDb_ = -1.0f;- float releaseMs_ = 100.0f;+ // Requested parameters β atomic for thread safety with AudioStream.+ // Written by setters (Python thread), read by process() (audio thread)+ // at block boundaries via relaxed loads.+ std::atomic<float> ceilingDb_{-1.0f};+ std::atomic<float> releaseMs_{100.0f};float lookaheadMs_ = 5.0f;bool truePeak_ = false;diff --git a/pedalboard_native/__init__.pyi b/pedalboard_native/__init__.pyiindex 79523f3..206ae5a 100644--- a/pedalboard_native/__init__.pyi+++ b/pedalboard_native/__init__.pyi@@ -36,6 +36,7 @@ _Shape = typing.Tuple[int, ...]__all__ = ["AudioUnitPlugin",+ "BrickwallLimiter","Bitcrush","Chorus","Clipping",@@ -154,6 +155,56 @@ class Plugin:"""pass+class BrickwallLimiter(Plugin):+ """+ A lookahead brick-wall limiter with configurable ceiling.++ Unlike the built-in Limiter, BrickwallLimiter does not apply makeup gain+ and has a configurable output ceiling. Optionally uses 4x oversampled+ sidechain detection for inter-sample peaks (true_peak=True).+ """++ def __init__(+ self,+ ceiling_db: float = -1.0,+ release_ms: float = 100.0,+ lookahead_ms: float = 5.0,+ true_peak: bool = False,+ ) -> None: ...+ def __repr__(self) -> str: ...+ @property+ def ceiling_db(self) -> float:+ """ """++ @ceiling_db.setter+ def ceiling_db(self, arg1: float) -> None:+ pass++ @property+ def release_ms(self) -> float:+ """ """++ @release_ms.setter+ def release_ms(self, arg1: float) -> None:+ pass++ @property+ def lookahead_ms(self) -> float:+ """ """++ @lookahead_ms.setter+ def lookahead_ms(self, arg1: float) -> None:+ pass++ @property+ def true_peak(self) -> bool:+ """ """++ @true_peak.setter+ def true_peak(self, arg1: bool) -> None:+ pass+ pass+class Bitcrush(Plugin):"""A plugin that reduces the signal to a given bit depth, giving the audio a lo-fi, digitized sound. Floating-point bit depths are supported.