v1.7.2 release: sample-rate-invariant TriodeStage coupling + Mesa/Soldano tone stacks

- TriodeStage coupling DC-blocker corner is now held constant in Hz across sample
  rates (was a fixed pole whose corner drifted up with Fs, thinning low end at high
  rates); evaluated at init, off the RT thread.
- New ToneStackModel cases .mesa and .soldano (Faust tonestacks-library values).
- GrayNoise output clamped to [-1, 1] (Int32.min edge).
This commit is contained in:
2026-08-03 15:57:54 -07:00
parent ea0aa84ca7
commit 77580ee98e
7 changed files with 135 additions and 11 deletions
@@ -9,6 +9,17 @@
/// Maps an Int32 counter to [-1, 1].
private let grayNoiseScalingFactor: Double = 1.0 / Double(Int32.max)
/// Scales a Gray-code counter to `[-1, 1]`, clamping the one out-of-range edge.
///
/// `Int32.max` scales to exactly `1.0`, but `Int32.min` (magnitude one greater) scales to
/// ≈ `-1.0000000005`, just below the contract — so the result is clamped. Only that single
/// counter value is affected; every other passes through unchanged, preserving the
/// bitwise-quantized property (the clamped `-1.0 × Int32.max = -Int32.max` is still integral).
/// Factored out so the boundary is unit-testable directly.
func grayNoiseClampedScale(_ counter: Int32) -> Double {
Swift.max(-1.0, Swift.min(1.0, Double(counter) * grayNoiseScalingFactor))
}
/// Bit mask to pick a random bit position in 0…31 from the raw PRNG word.
private let grayNoiseBitMask: UInt64 = 31
@@ -44,7 +55,7 @@ public enum GrayNoise {
let raw: UInt64 = xorshift64NextRaw(&rngState)
let bitPos: Int = Int(raw & grayNoiseBitMask)
counter ^= Int32(bitPattern: UInt32(1) << bitPos)
return Double(counter) * grayNoiseScalingFactor
return grayNoiseClampedScale(counter)
}
}
@@ -58,13 +58,36 @@ public final class TriodeStage: GeneratorBase, AudioGenerator {
private var _dcX1: Double = 0.0
private var _dcY1: Double = 0.0
/// Cached coupling pole and the sample rate it was computed for, so the `exp` is evaluated only
/// when the rate changes (never mid-render), not per block.
private var _cachedCouplingPole: Double = 0.0
private var _cachedCouplingSampleRate: Double = 0.0
/// Coupling-cap corner in Hz, evaluated once at `init` (off the RT thread) into a plain stored
/// field so `_computeSamples` never touches a lazily-initialised `static`/global (which would
/// pay a `swift_once` lock on first access, on the RT thread).
private let _couplingCornerHz: Double = TriodeStage.couplingCornerHz()
// MARK: - Constants
/// Coupling-cap DC-blocker pole. Shared with `LeakDC`'s audio default so the two stay in
/// sync if it is ever retuned. The corner is sample-rate-dependent (≈ a few tens of Hz at
/// 44.1 / 48 kHz), matching the `LeakDC` convention.
private let couplingPole: Double = leakDCDefaultAudioCoef
/// Reference sample rate at which the coupling corner is pinned to the legacy fixed pole.
private static let couplingReferenceSampleRate: Double = 44_100.0
/// Coupling-cap DC-blocker corner in Hz, chosen so the one-pole coefficient equals
/// `leakDCDefaultAudioCoef` at ``couplingReferenceSampleRate`` — i.e. identical to the old fixed
/// pole at 44.1 kHz (≈ 35 Hz). Unlike the old fixed coefficient, whose corner drifted *up* with
/// the sample rate (thinning the low end at 88.2 / 96 kHz+), this corner is held constant in Hz
/// across sample rates by recomputing the pole from the live rate (see `_computeSamples`). A pure
/// function so no lazy `static` storage is read on the RT thread.
static func couplingCornerHz() -> Double {
-log(leakDCDefaultAudioCoef) * couplingReferenceSampleRate / (2.0 * Double.pi)
}
/// The DC-blocker pole for a given sample rate, holding the corner at ``couplingCornerHz()``.
/// `exp(-2π·fc/Fs)` — equals `leakDCDefaultAudioCoef` at the reference rate. For tests /
/// introspection; the render path uses the cached `_couplingCornerHz` instead.
static func couplingPole(sampleRate: Double) -> Double {
exp(-2.0 * Double.pi * couplingCornerHz() / sampleRate)
}
// MARK: - Init
@@ -114,7 +137,15 @@ public final class TriodeStage: GeneratorBase, AudioGenerator {
_biasInput, isGenerator: _biasIsGenerator, graph: graph, count: count)
// Subtract the static operating point so only the dynamic (asymmetric) response passes.
let biasOffset: Double = tanh(bias)
let pole: Double = couplingPole
// Recompute the coupling pole only when the sample rate changes, so the DC-blocker corner
// stays fixed in Hz across rates (no low-end thinning at high sample rates).
let sampleRate: Double = graph.engine.sampleRate
if sampleRate != _cachedCouplingSampleRate {
// Uses the instance `_couplingCornerHz` (evaluated at init) — no lazy static/global read.
_cachedCouplingPole = exp(-2.0 * Double.pi * _couplingCornerHz / sampleRate)
_cachedCouplingSampleRate = sampleRate
}
let pole: Double = _cachedCouplingPole
let useFourX: Bool = _oversampleFactor == 4
let inputSamples: [[Double]] = _input.calculatedSamples(graph: graph, count: count)
+12 -3
View File
@@ -7,17 +7,20 @@
/// A passive guitar-amplifier tone-stack topology, selecting the resistor/capacitor values
/// used by ``ToneStack``.
///
/// All three classic stacks share the same circuit; only the component values differ. The
/// values here match the widely-used Yeh/Smith reference set (the same set used by the Faust
/// `tonestacks` library).
/// All stacks share the same circuit; only the component values differ. The values here match the
/// widely-used Yeh/Smith reference set (the same set used by the Faust `tonestacks` library).
///
/// - `fender`: Fender Bassman / Twin — scooped mids, bright.
/// - `marshall`: Marshall JCM800 — more mid, tighter low end.
/// - `vox`: Vox AC30 — a different taper, chimey top.
/// - `mesa`: Mesa/Boogie Mark — Fender-derived but a deeper mid scoop and fatter low end.
/// - `soldano`: Soldano SLO — Marshall-derived with a tighter slope for high-gain lead.
public enum ToneStackModel: String, CaseIterable, Sendable {
case fender
case marshall
case vox
case mesa
case soldano
/// Human-readable name for pickers.
public var displayName: String {
@@ -25,6 +28,8 @@ public enum ToneStackModel: String, CaseIterable, Sendable {
case .fender: return "Fender"
case .marshall: return "Marshall"
case .vox: return "Vox"
case .mesa: return "Mesa"
case .soldano: return "Soldano"
}
}
@@ -34,6 +39,8 @@ public enum ToneStackModel: String, CaseIterable, Sendable {
case .fender: return (250_000.0, 1_000_000.0, 25_000.0, 56_000.0)
case .marshall: return (220_000.0, 1_000_000.0, 22_000.0, 33_000.0)
case .vox: return (1_000_000.0, 1_000_000.0, 10_000.0, 100_000.0)
case .mesa: return (250_000.0, 250_000.0, 25_000.0, 100_000.0)
case .soldano: return (250_000.0, 1_000_000.0, 25_000.0, 47_000.0)
}
}
@@ -43,6 +50,8 @@ public enum ToneStackModel: String, CaseIterable, Sendable {
case .fender: return (250.0e-12, 20.0e-9, 20.0e-9)
case .marshall: return (470.0e-12, 22.0e-9, 22.0e-9)
case .vox: return (50.0e-12, 22.0e-9, 22.0e-9)
case .mesa: return (250.0e-12, 100.0e-9, 47.0e-9)
case .soldano: return (470.0e-12, 20.0e-9, 20.0e-9)
}
}
}