Files
Lyrebird/Sources/Lyrebird/Generators/Panning/Pan4.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

105 lines
4.4 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.
// Pan4.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import LyrebirdDSP
/// Open base class for 4-speaker equal-power square panners.
///
/// `Pan4` owns the shared infrastructure used by every 4-corner panner in
/// Lyrebird — output buffer allocation, `mul` / `add` post-processing, named
/// channel aliases, and the protocol conformance plumbing. Concrete panners
/// subclass `Pan4`, declare their own input parameters, and override
/// `_computeSamples(graph:count:)` to fill the four output rows per sample
/// using the helpers in `Pan4Helpers.swift`.
///
/// Speakers are arranged at the corners of a unit square `[-1, 1]²`:
/// - Channel 0 — L-front (x = −1, y = +1)
/// - Channel 1 — R-front (x = +1, y = +1)
/// - Channel 2 — R-rear (x = +1, y = −1)
/// - Channel 3 — L-rear (x = −1, y = −1)
///
/// Pan4 itself is not directly instantiable as a useful generator — its
/// `_computeSamples` default returns a silent (zero-filled) buffer. Use one of
/// the four concrete subclasses depending on the parameter style you want:
///
/// | Subclass | Coordinate system | Distance modelling |
/// |------------------|-------------------|--------------------|
/// | `PanAz` | polar (azimuth radians, position fixed to unit-square boundary) | none |
/// | `PanAzDistance` | polar (azimuth + radial distance) | 1/d amp, air-absorption LP, Doppler past `distance = 1` |
/// | `PanXY` | cartesian (x, y in `[-1, 1]`) | none |
/// | `PanXYDistance` | cartesian (x, y) with Chebyshev distance metric | 1/d amp, air-absorption LP, Doppler past `max(|x|, |y|) = 1` |
///
/// All four subclasses share the same equal-power gain law (see
/// `writeEqualPowerSquarePanSample` in `Pan4Helpers.swift`) and the same
/// power invariant `LF² + RF² + RR² + LR² = 1` along the boundary.
///
/// ## Output
///
/// Four-channel `[[Double]]`. Wire through `output(buses:source:)` to write
/// four consecutive buses, or connect individual channels via the named
/// aliases (`frontLeft` / `frontRight` / `rearRight` / `rearLeft`) or
/// subscript (`pan[0]` … `pan[3]`).
///
/// - Parameters:
/// - mul: Output multiplier applied after synthesis. Defaults to `1.0`.
/// - add: Offset added after multiplier. Defaults to `0.0`.
open class Pan4: GeneratorBase, AudioGenerator {
// MARK: - Output buffer (shared)
/// 4-row output buffer, pre-allocated at `lyrebirdDefaultResultBufferSize`
/// in `init` and never resized. Subclasses fill this in `_computeSamples`.
/// Mul/add post-processing applied to every row after the per-sample loop.
internal let _mulAdd: MulAddSpec
public override var outputChannelCount: Int { 4 }
// MARK: - Init
/// - Parameters:
/// - mul: Output multiplier applied after synthesis. Defaults to `1.0`.
/// - add: Offset added after multiplier. Defaults to `0.0`.
public init(
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
_mulAdd = MulAddSpec(mul: mul, add: add)
super.init(channels: 4)
}
// MARK: - Compute
/// Default implementation returns the (zero-filled) result buffer unchanged.
/// Subclasses override and write per-sample gains via
/// `writeEqualPowerSquarePanSample(into:sampleIndex:source:x:y:)`, then
/// call `_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)`
/// before returning `_result`.
open func _computeSamples(
graph: SynthGraph,
count: Int
) -> [[Double]] {
_checkCount(count)
return _result
}
}
// MARK: - Named channel aliases (inherited by every Pan4 subclass)
extension Pan4 {
/// Left-front output channel (channel 0). Equivalent to `self[0]`.
public var frontLeft: ChannelSelect.Audio { self[0] }
/// Right-front output channel (channel 1). Equivalent to `self[1]`.
public var frontRight: ChannelSelect.Audio { self[1] }
/// Right-rear output channel (channel 2). Equivalent to `self[2]`.
public var rearRight: ChannelSelect.Audio { self[2] }
/// Left-rear output channel (channel 3). Equivalent to `self[3]`.
public var rearLeft: ChannelSelect.Audio { self[3] }
}
// `GeneratorEnum` conformance intentionally omitted — Pan4 is an open base
// class with multiple subclasses, each Audio-only.