π π A Python library for audio.
C audioaudio-processingaudio-productionaudio-researchaudio-unitaugmentationjucemachine-learningpybind11pythonvst3vst3-host
fix(BrickwallLimiter): make all requested-state fields atomic + fix calibration test docstring
Christopher Jacoby0274fb91e6f8
diff --git a/pedalboard/plugins/BrickwallLimiter.h b/pedalboard/plugins/BrickwallLimiter.hindex 3bcda88..91570e5 100644--- a/pedalboard/plugins/BrickwallLimiter.h+++ b/pedalboard/plugins/BrickwallLimiter.h@@ -40,8 +40,10 @@ 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.+ * All four requested-state parameters (ceiling_db, release_ms, lookahead_ms,+ * true_peak) use std::atomic for safe concurrent access between Python+ * setters and the native audio callback (AudioStream releases the GIL+ * during processing, so plain field access would be a C++ data race).* Coefficients are snapshotted at block boundaries for thread safety.*/template <typename SampleType> class BrickwallLimiter : public Plugin {@@ -68,18 +70,22 @@ public:releaseMs_.store(value, std::memory_order_relaxed);}- float getLookaheadMs() const noexcept { return lookaheadMs_; }+ float getLookaheadMs() const noexcept {+ return lookaheadMs_.load(std::memory_order_relaxed);+ }void setLookaheadMs(float value) {if (value <= 0.0f || value > 100.0f || std::isnan(value) ||std::isinf(value))throw std::range_error("lookahead_ms must be in (0, 100]");- lookaheadMs_ = value;+ lookaheadMs_.store(value, std::memory_order_relaxed);// Takes effect on next prepare() β no reallocation here}- bool getTruePeak() const noexcept { return truePeak_; }+ bool getTruePeak() const noexcept {+ return truePeak_.load(std::memory_order_relaxed);+ }void setTruePeak(bool value) {- truePeak_ = value;+ truePeak_.store(value, std::memory_order_relaxed);// Takes effect on next prepare()}@@ -93,16 +99,18 @@ public:(spec.sampleRate != preparedSpec_.sampleRate ||preparedSpec_.maximumBlockSize < spec.maximumBlockSize ||spec.numChannels != preparedSpec_.numChannels);- bool paramsChanged = (lookaheadMs_ != preparedLookaheadMs_ ||- truePeak_ != preparedTruePeak_);+ float currentLookaheadMs = lookaheadMs_.load(std::memory_order_relaxed);+ bool currentTruePeak = truePeak_.load(std::memory_order_relaxed);+ bool paramsChanged = (currentLookaheadMs != preparedLookaheadMs_ ||+ currentTruePeak != preparedTruePeak_);if (!specChanged && !paramsChanged) {return; // preserve state for reset=False streaming}preparedSpec_ = spec;- preparedLookaheadMs_ = lookaheadMs_;- preparedTruePeak_ = truePeak_;+ preparedLookaheadMs_ = currentLookaheadMs;+ preparedTruePeak_ = currentTruePeak;// OversamplerupsampleDelay_ = 0;@@ -254,13 +262,15 @@ public:virtual int getLatencyHint() override { return activeLookaheadSamples_; }private:- // Requested parameters β atomic for thread safety with AudioStream.- // Written by setters (Python thread), read by process() (audio thread)- // at block boundaries via relaxed loads.+ // Requested parameters β ALL atomic for thread safety with AudioStream.+ // Written by setters (Python thread), read by process()/prepare() (which+ // may run on a different thread). Relaxed ordering is sufficient since we+ // snapshot at block boundaries (process()) or preparation boundaries+ // (prepare()), needing only eventual visibility, not ordering guarantees.std::atomic<float> ceilingDb_{-1.0f};std::atomic<float> releaseMs_{100.0f};- float lookaheadMs_ = 5.0f;- bool truePeak_ = false;+ std::atomic<float> lookaheadMs_{5.0f};+ std::atomic<bool> truePeak_{false};// Active (prepared) state β only written by prepare(), read by process()int activeLookaheadSamples_ = 0;diff --git a/tests/test_brickwall_limiter.py b/tests/test_brickwall_limiter.pyindex b23eda1..f76addd 100644--- a/tests/test_brickwall_limiter.py+++ b/tests/test_brickwall_limiter.py@@ -506,20 +506,26 @@ def test_property_mutation_lookahead_and_true_peak():def test_upsampler_latency_calibration():- """Verify the upsampling-only group-delay compensation (getLatencyInSamples() / 2.0).+ """Verify the upsampler delay compensation is self-consistent.Feeds a single-sample impulse through a true_peak=True plugin with aceiling far above the impulse's amplitude, so the gain envelope neverengages and the impulse is unmodified except for the plugin's reported- latency (base lookahead + upsampling-only group delay). Pedalboard's- automatic latency compensation should then re-align the impulse to its- original input index. If BrickwallLimiter's assumed upsampling-only- delay (half of Oversampling::getLatencyInSamples(), since only- processSamplesUp() is used) were wrong, activeLookaheadSamples_ would- not match the delay line's actual configured delay and/or the value- used to size and drain the delay line, and the impulse would land at- the wrong output index or its amplitude would be smeared/attenuated by- the mismatch.+ latency (base lookahead + upsampler delay). Pedalboard's automatic+ latency compensation should then re-align the impulse to its original+ input index.++ The delay compensation uses ceil(getLatencyInSamples()) β the full+ reported round-trip latency β as a conservative upper bound, since+ JUCE exposes no per-direction latency accessor and the up/down filters+ have different orders. This over-compensates slightly (the delay line+ is a bit longer than strictly needed) but guarantees gain reduction+ always arrives before the peak exits the delay line.++ NOTE: This test validates self-consistency (the impulse lands at the+ right index after latency compensation), not the accuracy of the+ delay split itself β Pedalboard's latency compensation makes any+ self-consistent delay value produce a correctly-aligned impulse."""sr = 44100n = sr@@ -539,8 +545,8 @@ def test_upsampler_latency_calibration():assert out_peak_idx == impulse_pos, (f"Impulse shifted from input index {impulse_pos} to output index "- f"{out_peak_idx}; upsampling-only latency compensation "- "(getLatencyInSamples() / 2.0) appears miscalibrated."+ f"{out_peak_idx}; upsampler delay compensation "+ "(ceil(getLatencyInSamples())) appears miscalibrated.")np.testing.assert_allclose(out_flat[out_peak_idx], 1.0, atol=1e-3)