Files
Lyrebird/Sources/Lyrebird/Generators/Panning/AmbiDecoderUHJ.swift
T
2026-06-01 18:27:40 -07:00

123 lines
4.3 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.
// AmbiDecoderUHJ.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Foundation
/// UHJ stereo → B-format decoder.
///
/// Decodes a 2-row UHJ-compatible stereo Generator back into 4-row 1st-order
/// B-format `[W, X, Y, Z]`. The Z row is written as silent (UHJ carries no
/// height information) so the output drops directly into ``AmbiDecoder1`` or
/// any other 4-row B-format consumer.
///
/// ## Algorithm
///
/// Per Gerzon 1985:
/// ```
/// W = 0.5 · ( 0.982·L + 0.982·R + j·0.164·L − j·0.164·R )
/// X = 0.5 · ( 0.419·L + 0.419·R − j·0.828·L + j·0.828·R )
/// Y = 0.5 · ( 0.763·L − 0.763·R + j·0.385·L + j·0.385·R )
/// Z = 0
/// ```
/// `j` denotes a 90° (Hilbert quadrature) component. Implementation builds two
/// private ``Hilbert/Audio`` instances — one per L, R channel — and feeds the
/// `.real` and `.imag` outputs of each into the matrix.
///
/// ## Use
///
/// ```swift
/// let bformat = AmbiDecoderUHJ(input: uhjStereo)
/// let dec = AmbiDecoder1(input: bformat, speakers: AmbiSpeakerLayout.quad())
/// output(buses: Array(0..<4), source: dec)
/// ```
///
/// - Parameters:
/// - input: 2-row UHJ stereo source. Any Generator with `outputChannelCount == 2`.
/// - mul: Output multiplier applied after synthesis. Defaults to `1.0`.
/// - add: Offset added after multiplier. Defaults to `0.0`.
public final class AmbiDecoderUHJ: GeneratorBase, AudioGenerator {
// MARK: - Per-channel Hilbert filters
private let _hilbertL: Hilbert.Audio
private let _hilbertR: Hilbert.Audio
// MARK: - Output buffer
public override var outputChannelCount: Int { 4 }
private let _mulAdd: MulAddSpec
// MARK: - Init
/// - Parameters:
/// - input: 2-row UHJ stereo source. Any Generator with `outputChannelCount == 2`.
/// - mul: Output multiplier applied after synthesis. Defaults to `1.0`.
/// - add: Offset added after multiplier. Defaults to `0.0`.
public init(
input: any Generator,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
precondition(
input.outputChannelCount == 2,
"AmbiDecoderUHJ: input must have outputChannelCount == 2 (L, R UHJ stereo), "
+ "got \(input.outputChannelCount)"
)
// Extract L / R rows via ChannelSelect.Audio (input may be `any Generator` —
// e.g. GeneratorArray — which doesn't carry the AudioGenerator subscript).
// Segment-serial caching ensures the shared `input` is computed once per block.
_hilbertL = Hilbert.Audio(input: ChannelSelect.Audio(source: input, channel: 0))
_hilbertR = Hilbert.Audio(input: ChannelSelect.Audio(source: input, channel: 1))
_mulAdd = MulAddSpec(mul: mul, add: add)
super.init(channels: 4)
}
// MARK: - Compute
public func _computeSamples(
graph: SynthGraph,
count: Int
) -> [[Double]] {
_checkCount(count)
let lRows: [[Double]] = _hilbertL.calculatedSamples(graph: graph, count: count)
let rRows: [[Double]] = _hilbertR.calculatedSamples(graph: graph, count: count)
guard lRows.count == 2, rRows.count == 2 else { return _result }
let lRe: [Double] = lRows[0]
let lIm: [Double] = lRows[1]
let rRe: [Double] = rRows[0]
let rIm: [Double] = rRows[1]
let kWRe: Double = uhjDecodeWRe
let kWIm: Double = uhjDecodeWIm
let kXRe: Double = uhjDecodeXRe
let kXIm: Double = uhjDecodeXIm
let kYRe: Double = uhjDecodeYRe
let kYIm: Double = uhjDecodeYIm
let scale: Double = uhjMatrixScale
for i: Int in 0..<count {
let l: Double = lRe[i]
let r: Double = rRe[i]
let lj: Double = lIm[i]
let rj: Double = rIm[i]
_result[0][i] = scale * ( kWRe * l + kWRe * r + kWIm * lj - kWIm * rj )
_result[1][i] = scale * ( kXRe * l + kXRe * r - kXIm * lj + kXIm * rj )
_result[2][i] = scale * ( kYRe * l - kYRe * r + kYIm * lj + kYIm * rj )
_result[3][i] = 0
}
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
return _result
}
}
// `GeneratorEnum` conformance intentionally omitted — Audio-only.