Files
Lyrebird/Sources/Lyrebird/Generators/Routing/CrossFade.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

222 lines
9.0 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// CrossFade.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdDSP
/// Half-pi constant for CrossFade equal-power gain derivation.
private let crossFadeHalfPi: Double = .pi / 2
// MARK: - CrossFade
/// Equal-power crossfade between two inputs. Equivalent to SuperCollider `XFade2`.
///
/// Crossfades between `inA` and `inB` using an equal-power curve derived from a quarter-
/// sine/cosine pair. `mul` plays the role of SC's `level` argument (overall output gain).
///
/// **Mono only.** Operates on channel 0 of each input; multichannel support is deferred.
///
/// Equal-power gain derivation:
/// - Map `p = clamp(pan + 1, 0, 2) * 0.5` so `p ∈ [0, 1]`.
/// - `gainA = cos(p × π/2)`, `gainB = sin(p × π/2)`.
/// - At `pan = -1`: A full, B silent. At `pan = +1`: A silent, B full.
/// - At `pan = 0`: both at `cos(π/4) = √2/2 ≈ 0.7071` — equal-power invariant
/// `gainA² + gainB² = 1` holds at every `pan` value.
///
/// ```swift
/// // Crossfade between two oscillators — generator pan input for smooth sweep.
/// let xfade = CrossFade.Audio(inA: osc1, inB: osc2, pan: modLFO)
/// ```
///
/// - Parameters:
/// - inA: First input signal.
/// - inB: Second input signal.
/// - pan: Crossfade position in `[-1, 1]`. `-1` = full `inA`; `+1` = full `inB`.
/// Values outside `[-1, 1]` are clamped. Defaults to `0.0`.
/// - mul: Output multiplier (SC `level`). Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public enum CrossFade {
// MARK: - Shared base
/// Shared stored properties for `CrossFade.Audio` and `CrossFade.Control`.
public class _Base: GeneratorBase {
let _inA: any ValidGeneratorInput
let _inB: any ValidGeneratorInput
let _panInput: any ValidGeneratorInput
let _inAIsGenerator: Bool
let _inBIsGenerator: Bool
let _panIsGenerator: Bool
let _mulAdd: MulAddSpec
init(
inA: any ValidGeneratorInput,
inB: any ValidGeneratorInput,
pan: any ValidGeneratorInput,
mul: any ValidGeneratorInput,
add: any ValidGeneratorInput
) {
let resolvedA: any ValidGeneratorInput = inA._resolvedInput()
let resolvedB: any ValidGeneratorInput = inB._resolvedInput()
let resolvedPan: any ValidGeneratorInput = pan._resolvedInput()
_inA = resolvedA
_inB = resolvedB
_panInput = resolvedPan
_inAIsGenerator = resolvedA is any Generator
_inBIsGenerator = resolvedB is any Generator
_panIsGenerator = resolvedPan is any Generator
_mulAdd = MulAddSpec(mul: mul, add: add)
}
}
// MARK: - Audio rate
/// Audio-rate equal-power crossfade between two inputs.
///
/// When `pan` is a scalar, gains are computed once per block. When `pan` is a generator,
/// gains are computed per sample via `sampleOrFallback`.
///
/// - Parameters:
/// - inA: First input signal.
/// - inB: Second input signal.
/// - pan: Crossfade position in `[-1, 1]`. `-1` = full `inA`; `+1` = full `inB`.
/// Values outside `[-1, 1]` are clamped. Defaults to `0.0`.
/// - mul: Output multiplier (SC `level`). Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public final class Audio: _Base, AudioGenerator {
/// - Parameters:
/// - inA: First input signal.
/// - inB: Second input signal.
/// - pan: Crossfade position in `[-1, 1]`. `-1` = full `inA`; `+1` = full `inB`.
/// Values outside `[-1, 1]` are clamped. Defaults to `0.0`.
/// - mul: Output multiplier (SC `level`). Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public override init(
inA: any ValidGeneratorInput,
inB: any ValidGeneratorInput,
pan: any ValidGeneratorInput = 0.0,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
super.init(inA: inA, inB: inB, pan: pan, mul: mul, add: add)
}
public func _computeSamples(
graph: SynthGraph,
count: Int
) -> [[Double]] {
_checkCount(count)
let (aSamples, aScalar) = resolveGeneratorOrScalar(
_inA, isGenerator: _inAIsGenerator, graph: graph, count: count)
let (bSamples, bScalar) = resolveGeneratorOrScalar(
_inB, isGenerator: _inBIsGenerator, graph: graph, count: count)
let (panSamples, panScalar) = resolveGeneratorOrScalar(
_panInput, isGenerator: _panIsGenerator, graph: graph, count: count)
if panSamples == nil {
// Scalar pan: compute gains once for the whole block.
let clampedPan: Double = Swift.max(-1.0, Swift.min(1.0, panScalar))
let p: Double = (clampedPan + 1.0) * 0.5
let gainA: Double = cos(p * crossFadeHalfPi)
let gainB: Double = sin(p * crossFadeHalfPi)
for i: Int in 0..<count {
let a: Double = sampleOrFallback(aSamples, scalar: aScalar, at: i)
let b: Double = sampleOrFallback(bSamples, scalar: bScalar, at: i)
_result[0][i] = a * gainA + b * gainB
}
} else {
// Generator pan: compute gains per sample.
for i: Int in 0..<count {
let rawPan: Double = sampleOrFallback(panSamples, scalar: panScalar, at: i)
let clampedPan: Double = Swift.max(-1.0, Swift.min(1.0, rawPan))
let p: Double = (clampedPan + 1.0) * 0.5
let gainA: Double = cos(p * crossFadeHalfPi)
let gainB: Double = sin(p * crossFadeHalfPi)
let a: Double = sampleOrFallback(aSamples, scalar: aScalar, at: i)
let b: Double = sampleOrFallback(bSamples, scalar: bScalar, at: i)
_result[0][i] = a * gainA + b * gainB
}
}
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
return _result
}
}
// MARK: - Control rate
/// Control-rate equal-power crossfade between two inputs.
///
/// Reads `inA`, `inB`, and `pan` once per block via `floatValue`. Applies mul/add to
/// the scalar target before ramping. Index changes glide over the sub-block ramp.
///
/// - Parameters:
/// - inA: First input signal.
/// - inB: Second input signal.
/// - pan: Crossfade position in `[-1, 1]`. `-1` = full `inA`; `+1` = full `inB`.
/// Values outside `[-1, 1]` are clamped. Defaults to `0.0`.
/// - mul: Output multiplier (SC `level`). Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public final class Control: _Base, ControlGenerator {
public var previousOutputValue: Double = 0.0
/// - Parameters:
/// - inA: First input signal.
/// - inB: Second input signal.
/// - pan: Crossfade position in `[-1, 1]`. `-1` = full `inA`; `+1` = full `inB`.
/// Values outside `[-1, 1]` are clamped. Defaults to `0.0`.
/// - mul: Output multiplier (SC `level`). Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public override init(
inA: any ValidGeneratorInput,
inB: any ValidGeneratorInput,
pan: any ValidGeneratorInput = 0.0,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
super.init(inA: inA, inB: inB, pan: pan, mul: mul, add: add)
_setupSubBlockBuffer()
}
public func _computeSamples(
graph: SynthGraph,
count: Int
) -> [[Double]] {
_checkCount(count)
let a: Double = _inA.floatValue(graph: graph)
let b: Double = _inB.floatValue(graph: graph)
let rawPan: Double = _panInput.floatValue(graph: graph)
let clampedPan: Double = Swift.max(-1.0, Swift.min(1.0, rawPan))
let p: Double = (clampedPan + 1.0) * 0.5
let gainA: Double = cos(p * crossFadeHalfPi)
let gainB: Double = sin(p * crossFadeHalfPi)
var target: Double = a * gainA + b * gainB
if !_mulAdd.isIdentity {
target = target * _mulAdd.mulInput.floatValue(graph: graph)
+ _mulAdd.addInput.floatValue(graph: graph)
}
fillScalarRampedResult(
into: &_result,
previousOutputValue: &previousOutputValue,
target: target,
count: count
)
return _result
}
public func floatValue(graph: SynthGraph) -> Double { previousOutputValue }
}
}
// MARK: - GeneratorEnum
extension CrossFade: GeneratorEnum {
public typealias AudioType = CrossFade.Audio
public typealias ControlType = CrossFade.Control
}