Files
Lyrebird/Sources/Lyrebird/Generators/Dynamics/DynamicsHelpers.swift
T
josh b6fdee8bfc Lyrebird 2.0.0 — Linux support
Consolidated release: the cross-platform Linux port (engine, generators, CLI,
REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable
WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux
live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive.
See RELEASES.md and LINUX_README.md.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-09-10 21:09:11 -07:00

56 lines
1.8 KiB
Swift

// DynamicsHelpers.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdDSP
// MARK: - Asymmetric attack/release smoother
/// Direction selector for `attackReleaseSmoother`.
///
/// - `.rising`: use attack-coefficient when `target > state` (envelope-follower
/// convention — fast attack when amplitude rises, slow release when it falls).
/// - `.falling`: use attack-coefficient when `target < state` (compressor /
/// expander convention — fast attack when the gain target moves toward more
/// reduction, slow release when it relaxes back).
internal enum AttackReleaseDirection {
case rising
case falling
}
/// Shared per-sample asymmetric attack/release smoother step. Mutates `state`
/// in place to:
///
/// ```
/// state = chosenCoeff · state + chosenComp · target
/// ```
///
/// where the `(coeff, comp)` pair is the attack pair when the target moves in
/// the user-selected direction relative to state, and the release pair
/// otherwise. `comp = 1 - coeff` is precomputed by the caller (typically once
/// per block from scalar `attack` / `release` inputs).
///
/// Consumers: `AmplitudeFollower.Audio` (`.rising`), `Compressor.Audio`
/// (`.falling`), `Expander.Audio` (`.falling`).
@inline(__always)
internal func attackReleaseSmoother(
state: inout Double,
target: Double,
attackCoeff: Double, attackComp: Double,
releaseCoeff: Double, releaseComp: Double,
direction: AttackReleaseDirection
) {
let useAttack: Bool
switch direction {
case .rising: useAttack = target > state
case .falling: useAttack = target < state
}
if useAttack {
state = attackCoeff * state + attackComp * target
} else {
state = releaseCoeff * state + releaseComp * target
}
}