From 24ea32e47cecff8efec9720546ed068b4efef2f7 Mon Sep 17 00:00:00 2001 From: Joshua Parmenter Date: Tue, 3 Nov 2026 12:25:29 -0800 Subject: [PATCH] v2.2.0 release: live-parameter ramp origin primed at synth add A @UIParam/@LiveParam value assigned after construction but before the first render is now delivered flat on the first block instead of ramping from the property- declaration default (fixes e.g. a NoiseGate restored from saved rig state latching open and ignoring its threshold). Backed by a new internal-facing LiveParameterValue._primeRampOrigin() (default no-op, source-compatible); live changes after start still ramp. See RELEASES.md for the full behavior-change note. Co-Authored-By: Claude Opus 4.8 --- RELEASES.md | 51 ++++++++ .../Lyrebird/Lyrebird.docc/ParameterInputs.md | 6 + .../Lyrebird.docc/SynthesizerProtocol.md | 5 +- Sources/Lyrebird/Parameters/LiveParam.swift | 26 ++++ .../Lyrebird/Parameters/ParameterNode.swift | 6 + Sources/Lyrebird/Parameters/UIParamNode.swift | 7 + .../Synthesizer/SynthesizerBase.swift | 21 ++- Sources/LyrebirdCLI/Version.swift | 2 +- .../Parameters/LiveParamRampOriginTests.swift | 120 ++++++++++++++++++ 9 files changed, 238 insertions(+), 6 deletions(-) create mode 100644 Tests/LyrebirdTests/Parameters/LiveParamRampOriginTests.swift diff --git a/RELEASES.md b/RELEASES.md index a73e688..a8ea1d0 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -7,6 +7,57 @@ functionality in a backward-compatible way. --- +## 2.2.0 — Live-parameter ramp origin primed at synth add + +A small-but-behavioral release. One fix to how `@UIParam` / `@LiveParam` values +are delivered on a synth's first rendered block, plus the internal hook that +backs it. The public API is source-compatible — the one new protocol member is +internal-facing and ships with a default implementation — so this is drop-in +over 2.1.x, but the delivery change is observable and is called out below. + +**Fix — a parameter set before the first render is now delivered flat.** When a +value is assigned to an `@UIParam` / `@LiveParam` *after the synth is +constructed but before it first renders* — most commonly when restoring saved +state onto a freshly built synth — the parameter is now delivered as a flat +block at that value from sample 0. Previously the first rendered block ramped +from the property-declaration default up to the assigned value over one +(sub-)block, because the property-wrapper setter advances the atomic target but +not the interpolation origin, which stayed at the construction-time default. + +For most parameters that one-block (~a few ms) glide was benign. For a value +feeding a decision stage it was not: a `Schmidt` trigger fed a threshold that +swept up from the default would latch on the transient and, if the steady signal +sat inside the hysteresis band, never reset — e.g. a `NoiseGate` restored from a +saved rig would open on load and ignore its threshold. Priming the origin at add +removes the sweep. + +Mechanically: `SynthesizerBase`, as part of the `Mirror`-based live-parameter +auto-registration it already runs at add time (`rebuildGraph()`, main thread, +before the node is visible to the RT thread), now also primes each parameter's +ramp origin to its current value via a new `LiveParameterValue._primeRampOrigin()`. +`ParameterNode` and `UIParamNode` implement it; external conformers inherit a +no-op default, so nothing is source-broken. A re-add (`remove` → change value → +`add`) likewise re-primes to the then-current value. + +**Behavior change to be aware of.** Two deliveries changed, both toward "the +value you set before starting is the value you get": + +- A value assigned between construction and first render is **flat** on block + one, not a ramp from the declaration default. Code that (knowingly or not) + relied on that opening glide will no longer see it. If you want an opening + ramp, drive the parameter from a source/envelope or change it after start. +- A synth removed, re-assigned while out of the tree, then re-added starts + **flat at the new value** rather than gliding from the value it last rendered. + +Live changes made **while the synth is running** are unchanged — they still ramp +per (sub-)block from the last rendered value, as before. The source-slot / +crossfade path is unaffected (the source overwrites the origin each block). +Pinned by `LiveParamRampOriginTests` (flat delivery for `@UIParam` and +`@LiveParam`, live-change-still-ramps, remove/change/re-add, and source-slot +non-interference). + +--- + ## 2.1.1 — Delay-click fix + internal consolidation A patch release: one audible bug fix in the delay/comb/allpass family, plus a diff --git a/Sources/Lyrebird/Lyrebird.docc/ParameterInputs.md b/Sources/Lyrebird/Lyrebird.docc/ParameterInputs.md index d783b04..4a992c1 100644 --- a/Sources/Lyrebird/Lyrebird.docc/ParameterInputs.md +++ b/Sources/Lyrebird/Lyrebird.docc/ParameterInputs.md @@ -82,6 +82,12 @@ at the end of `rebuildGraph()`. The property name (with the leading `_` of prope backing storage stripped) becomes the parameter key for `updateParameter` and ``LyrebirdMessageContext/set(_:to:on:)``. No `declareParameter` call is needed. +Auto-registration also **primes each parameter's ramp origin** to its current value (before the +RT thread sees the node), so a value assigned between construction and the synth's first render — +for example restoring saved state onto a freshly built synth — is delivered **flat on the first +block**, not as a ramp up from the property-declaration default. Changes made after the synth is +running still ramp as usual. + ### Block-rate vs sub-block-rate ramping When `controlBlockSize == blockSize` (the engine default), the framework calls diff --git a/Sources/Lyrebird/Lyrebird.docc/SynthesizerProtocol.md b/Sources/Lyrebird/Lyrebird.docc/SynthesizerProtocol.md index eb60f99..8c74ab0 100644 --- a/Sources/Lyrebird/Lyrebird.docc/SynthesizerProtocol.md +++ b/Sources/Lyrebird/Lyrebird.docc/SynthesizerProtocol.md @@ -245,7 +245,10 @@ Do not call `engine.remove` from within the RT callback. - `parameterTable.inputs` is populated by `_autoRegisterLiveParams()` at the end of `rebuildGraph()` (main thread, before add) and read during `evaluate()` (RT thread, - after add). No concurrent access. + after add). No concurrent access. The same pass also primes each live parameter's ramp + origin to its current value, so a value assigned before the first render (e.g. restored + state) is delivered flat; this write is main-thread-only and precedes the add, so it never + races the RT thread. - `updateParameter` issues a releasing atomic store on the projected `ParameterNode` / `UIParamNode`; the RT thread picks up the new value via an acquiring atomic load at the start of the next (sub-)block — no queue, no dictionary mutation on the RT thread. diff --git a/Sources/Lyrebird/Parameters/LiveParam.swift b/Sources/Lyrebird/Parameters/LiveParam.swift index 5f7bbed..ec4e0b4 100644 --- a/Sources/Lyrebird/Parameters/LiveParam.swift +++ b/Sources/Lyrebird/Parameters/LiveParam.swift @@ -34,6 +34,32 @@ public protocol LiveParameterValue: AnyObject { /// `UIParamNode` (`@Observable` stored property) satisfy this requirement via their /// existing public `value` accessor. var value: Double { get } + + /// Moves the interpolation origin (`previousOutputValue`) to the current target value, so the + /// next rendered block is a flat buffer at that value with no ramp. + /// + /// `SynthesizerBase` calls this on every live parameter when a synth is (re-)added to the engine + /// — on the main thread, before the RT thread sees the node. Any value assigned to the parameter + /// **before the first render** (e.g. restoring saved state) is therefore delivered flat, while a + /// value changed **after** the synth is running still ramps from the last rendered value. + /// + /// Without this, assigning a parameter after construction leaves the ramp origin at the + /// property-declaration default, so the first block ramps default → assigned. For most + /// parameters that is a brief, benign glide; for a decision threshold feeding a `Schmidt` + /// trigger it can latch the trigger into the wrong state. + /// + /// - Important: NOT RT-safe — must be called from the control side only, and only while the + /// node is not yet observed by the RT thread. It writes `previousOutputValue` (a plain + /// `Double`) without synchronisation, which is safe because `SynthesizerBase` calls it before + /// the add event is enqueued. A node shared with a synth that is already active must not be + /// primed. + func _primeRampOrigin() +} + +public extension LiveParameterValue { + /// Default no-op so external conformers are not source-broken by this requirement. The two + /// in-tree conformers (`ParameterNode`, `UIParamNode`) provide real implementations. + func _primeRampOrigin() {} } /// A property wrapper that backs a synthesizer parameter with a `ParameterNode`. diff --git a/Sources/Lyrebird/Parameters/ParameterNode.swift b/Sources/Lyrebird/Parameters/ParameterNode.swift index 95ee95f..8fe5a42 100644 --- a/Sources/Lyrebird/Parameters/ParameterNode.swift +++ b/Sources/Lyrebird/Parameters/ParameterNode.swift @@ -320,6 +320,12 @@ public final class ParameterNode: GeneratorBase, ControlGenerator, LiveParameter value = newValue } + /// `LiveParameterValue` conformance — resets the ramp origin to the current target so the next + /// block is flat (no ramp). See the protocol requirement for when `SynthesizerBase` calls this. + public func _primeRampOrigin() { + previousOutputValue = value + } + // MARK: - Init-time priming /// Resets both the atomic backing and the ramp origin (`previousOutputValue`) to diff --git a/Sources/Lyrebird/Parameters/UIParamNode.swift b/Sources/Lyrebird/Parameters/UIParamNode.swift index 5925809..1b8b6fa 100644 --- a/Sources/Lyrebird/Parameters/UIParamNode.swift +++ b/Sources/Lyrebird/Parameters/UIParamNode.swift @@ -119,6 +119,13 @@ public final class UIParamNode: GeneratorBase, ControlGenerator, LiveParameterVa update(newValue) } + /// `LiveParameterValue` conformance — resets the ramp origin to the current target so the next + /// block is flat (no ramp). Delegates to `_node`, which owns the RT interpolation state the + /// render thread reads. See the protocol requirement for when `SynthesizerBase` calls this. + public func _primeRampOrigin() { + _node._primeRampOrigin() + } + // MARK: - Source property (delegates to _node) /// Generator wired into this parameter slot. When non-nil, the parameter's diff --git a/Sources/Lyrebird/Synthesizer/SynthesizerBase.swift b/Sources/Lyrebird/Synthesizer/SynthesizerBase.swift index f1b194f..b6f9908 100644 --- a/Sources/Lyrebird/Synthesizer/SynthesizerBase.swift +++ b/Sources/Lyrebird/Synthesizer/SynthesizerBase.swift @@ -666,9 +666,13 @@ open class SynthesizerBase: Synthesizer { /// /// **Value survival across re-add.** `parameterTable.inputs` is repopulated by /// `_autoRegisterLiveParams()` on every call, but each `@LiveParam` / `@UIParam` property - /// wrapper holds its projected node (`ParameterNode` / `UIParamNode`) as a `let` — - /// the same instance is re-registered, so its atomic value and ramp state are preserved - /// across `engine.remove` + `engine.add` cycles. + /// wrapper holds its projected node (`ParameterNode` / `UIParamNode`) as a `let` — the same + /// instance is re-registered, so its **atomic value** is preserved across `engine.remove` + + /// `engine.add` cycles. The **ramp origin** is not preserved: `_autoRegisterLiveParams()` + /// re-primes it to that current value on every add (see + /// ``LiveParameterValue/_primeRampOrigin()``), so the first block after an add is flat at the + /// value in effect — a value assigned between construction (or a remove) and the add is + /// delivered without a ramp, while changes made after the synth is running still ramp. /// /// - Precondition: the Synthesizer must NOT currently be active in the node tree. /// Calling this while `evaluate()` may be running on the RT thread is a data race on @@ -708,7 +712,16 @@ open class SynthesizerBase: Synthesizer { guard let label: String = child.label else { continue } let key: String = label.hasPrefix("_") ? String(label.dropFirst()) : label if let wrapper: any LiveParameterWrapper = child.value as? LiveParameterWrapper { - parameterTable.inputs[key] = wrapper._projectedAsValidInput + let node: any ValidGeneratorInput = wrapper._projectedAsValidInput + parameterTable.inputs[key] = node + // Flatten the ramp origin to the value held right now (before the RT thread first + // sees this node). A value assigned after construction but before the engine + // starts — e.g. restoring saved state — is otherwise delivered as a first-block + // ramp from the property-declaration default, which can latch a downstream + // Schmidt/decision stage into the wrong state. Live changes after start still ramp. + if let primeable: any LiveParameterValue = node as? LiveParameterValue { + primeable._primeRampOrigin() + } } } } diff --git a/Sources/LyrebirdCLI/Version.swift b/Sources/LyrebirdCLI/Version.swift index ac6723c..2dfac1f 100644 --- a/Sources/LyrebirdCLI/Version.swift +++ b/Sources/LyrebirdCLI/Version.swift @@ -5,7 +5,7 @@ // SPDX-License-Identifier: MIT /// The current Lyrebird framework and CLI version. -public let lyrebirdVersion: String = "2.1.1" +public let lyrebirdVersion: String = "2.2.0" /// The banner printed when the interactive shell or REPL session starts. public let welcomeBanner: String = """ diff --git a/Tests/LyrebirdTests/Parameters/LiveParamRampOriginTests.swift b/Tests/LyrebirdTests/Parameters/LiveParamRampOriginTests.swift new file mode 100644 index 0000000..ad170c8 --- /dev/null +++ b/Tests/LyrebirdTests/Parameters/LiveParamRampOriginTests.swift @@ -0,0 +1,120 @@ +// LiveParamRampOriginTests.swift +// Lyrebird +// +// Copyright (c) 2026 Joshua Parmenter / realizedsound +// SPDX-License-Identifier: MIT + +import Testing +@testable @_spi(LyrebirdExtensions) import LyrebirdSynth + +/// A synth whose only output is a `@UIParam` written straight to a bus, so a rendered block +/// reveals the parameter's delivered ramp. Declaration default `0.1`. +@MainActor +private final class UIParamProbe: SynthesizerBase { + @UIParam var p: Double = 0.1 + let bus: Int + init(bus: Int = 0) { self.bus = bus; super.init() } + override func build() { + output(bus: bus, source: $p) + } +} + +/// Same, but backed by `@LiveParam` (`ParameterNode`) — a separate priming code path from +/// `@UIParam`'s `UIParamNode` delegation. +@MainActor +private final class LiveParamProbe: SynthesizerBase { + @LiveParam var p: Double = 0.1 + let bus: Int + init(bus: Int = 0) { self.bus = bus; super.init() } + override func build() { + output(bus: bus, source: $p) + } +} + +@MainActor +@Suite("Live parameter ramp origin") +struct LiveParamRampOriginTests { + + let engine: SynthesisEngine = makeTestEngine(blockSize: 16) + + init() throws { + try engine.start() + } + + @Test func uiParamAssignedAfterConstructionIsDeliveredFlat() throws { + // Assign AFTER construction (declaration default 0.1) but BEFORE the first render. Without + // priming at add time the first block would ramp 0.1 → 0.9; the fix primes the ramp origin + // when the synth is added, so the block is flat at 0.9 from sample 0. + let probe: UIParamProbe = UIParamProbe(bus: 0) + probe.p = 0.9 + try engine.add(probe) + engine.processBlock(inputSamples: []) + for i: Int in 0..