f8878324fd
Mirror of Lyrebird dev HEAD (9f791c7c) minus maintainer-only paths. Patch release: fixes the allpass fractional-delay click (DelayLine/CombFilter/AllpassFilter/ DelayLineFeedback under a modulated delay) and lands the internal code-quality consolidation pass (~160 deduped sites + shared base classes/helpers, bit-identical output). No public API removed or changed. See RELEASES.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
176 lines
7.6 KiB
Swift
176 lines
7.6 KiB
Swift
// UnipolarRangeMapper.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// MARK: - UnipolarRangeMapper
|
|
|
|
/// Maps a unipolar input signal in `[0, 1]` to an output range `[low, high]`
|
|
/// using a `RangeCurve` shape.
|
|
///
|
|
/// Use for `Line`, `EnvGen`, unipolar LFOs, and any source whose natural output
|
|
/// is `[0, 1]`. For bipolar `[-1, 1]` sources (oscillators, noise), use
|
|
/// `RangeMapper` instead.
|
|
///
|
|
/// ## Usage
|
|
/// ```swift
|
|
/// // Fluent — infers source rate.
|
|
/// let cutoff = env.unipolarRange(low: 80.0, high: 8000.0, curve: .exponential)
|
|
///
|
|
/// // Explicit form.
|
|
/// let mapped = UnipolarRangeMapper.Audio(env, low: 0.0, high: 1.0, curve: .linear)
|
|
/// ```
|
|
///
|
|
/// ## Mono only (v1)
|
|
/// Like `RangeMapper`, this consumes only the first channel of a multi-row source.
|
|
///
|
|
/// - Parameters:
|
|
/// - signal: Input signal, expected range `[0, 1]`.
|
|
/// - low: Output range lower bound. Accepts any `ValidGeneratorInput` (modulateable).
|
|
/// When `curve == .exponential`, must be > 0 (mismatched-sign or zero values
|
|
/// produce `NaN` per IEEE 754).
|
|
/// - high: Output range upper bound. Accepts any `ValidGeneratorInput` (modulateable).
|
|
/// Same `.exponential` constraint as `low`.
|
|
/// - curve: Curve shape. Defaults to `.linear`.
|
|
/// - mul: Output multiplier applied after curve mapping. Defaults to `1.0`.
|
|
/// - add: Output offset applied after curve mapping. Defaults to `0.0`.
|
|
public enum UnipolarRangeMapper {
|
|
|
|
public class _Base: GeneratorBase {
|
|
let inputSource: any ValidGeneratorInput
|
|
let lowInput: any ValidGeneratorInput
|
|
let highInput: any ValidGeneratorInput
|
|
let lowIsGenerator: Bool
|
|
let highIsGenerator: Bool
|
|
let curve: RangeCurve
|
|
let _mulAdd: MulAddSpec
|
|
public init(
|
|
_ signal: any ValidGeneratorInput,
|
|
low: any ValidGeneratorInput,
|
|
high: any ValidGeneratorInput,
|
|
curve: RangeCurve = .linear,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
|
|
validateRangeCurveAtInit(curve)
|
|
let resolvedSignal: any ValidGeneratorInput = signal._resolvedInput()
|
|
let resolvedLow: any ValidGeneratorInput = low._resolvedInput()
|
|
let resolvedHigh: any ValidGeneratorInput = high._resolvedInput()
|
|
inputSource = resolvedSignal
|
|
lowInput = resolvedLow
|
|
highInput = resolvedHigh
|
|
lowIsGenerator = resolvedLow is any Generator
|
|
highIsGenerator = resolvedHigh is any Generator
|
|
self.curve = curve
|
|
_mulAdd = MulAddSpec(mul: mul, add: add)
|
|
}
|
|
}
|
|
|
|
/// Audio-rate unipolar range mapper.
|
|
///
|
|
/// - Parameters:
|
|
/// - signal: Input signal, expected range `[0, 1]`.
|
|
/// - low: Output range lower bound. When `curve == .exponential`, must be > 0
|
|
/// (mismatched-sign or zero values produce `NaN` per IEEE 754).
|
|
/// - high: Output range upper bound. Same `.exponential` constraint as `low`.
|
|
/// - curve: Curve shape. Defaults to `.linear`.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public final class Audio: _Base, AudioGenerator {
|
|
/// - Parameters:
|
|
/// - signal: Input signal, expected range `[0, 1]`.
|
|
/// - low: Output range lower bound.
|
|
/// - high: Output range upper bound.
|
|
/// - curve: Curve shape. Defaults to `.linear`.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public override init(
|
|
_ signal: any ValidGeneratorInput,
|
|
low: any ValidGeneratorInput,
|
|
high: any ValidGeneratorInput,
|
|
curve: RangeCurve = .linear,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
|
|
super.init(signal, low: low, high: high, curve: curve, mul: mul, add: add)
|
|
}
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
// `?? []` is defensive — see the comment on `RangeMapper.Audio._computeSamples`.
|
|
let block: [Double] = inputSource.calculatedSamples(graph: graph, count: count).first ?? []
|
|
let low: Double = lastGeneratorSampleOrFloat(lowInput, isGenerator: lowIsGenerator, graph: graph, count: count)
|
|
let high: Double = lastGeneratorSampleOrFloat(highInput, isGenerator: highIsGenerator, graph: graph, count: count)
|
|
writeRangeMapperBlock(
|
|
into: &_result[0],
|
|
block: block,
|
|
count: count,
|
|
low: low,
|
|
high: high,
|
|
curve: curve,
|
|
kind: .unipolar
|
|
)
|
|
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
|
|
return _result
|
|
}
|
|
}
|
|
|
|
/// Control-rate unipolar range mapper. Output is linearly interpolated toward the block target.
|
|
///
|
|
/// - Parameters:
|
|
/// - signal: Input signal, expected range `[0, 1]`.
|
|
/// - low: Output range lower bound. When `curve == .exponential`, must be > 0
|
|
/// (mismatched-sign or zero values produce `NaN` per IEEE 754).
|
|
/// - high: Output range upper bound. Same `.exponential` constraint as `low`.
|
|
/// - curve: Curve shape. Defaults to `.linear`.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public final class Control: _Base, ControlGenerator {
|
|
public var previousOutputValue: Double = 0.0
|
|
/// - Parameters:
|
|
/// - signal: Input signal, expected range `[0, 1]`.
|
|
/// - low: Output range lower bound.
|
|
/// - high: Output range upper bound.
|
|
/// - curve: Curve shape. Defaults to `.linear`.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public override init(
|
|
_ signal: any ValidGeneratorInput,
|
|
low: any ValidGeneratorInput,
|
|
high: any ValidGeneratorInput,
|
|
curve: RangeCurve = .linear,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
|
|
super.init(signal, low: low, high: high, curve: curve, mul: mul, add: add)
|
|
_setupSubBlockBuffer()
|
|
}
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
// Sub-block low/high re-pull is `_segmentSerial`-cached after the first call —
|
|
// see `RangeMapper.Control._computeSamples`.
|
|
let low: Double = lastGeneratorSampleOrFloat(lowInput, isGenerator: lowIsGenerator, graph: graph, count: count)
|
|
let high: Double = lastGeneratorSampleOrFloat(highInput, isGenerator: highIsGenerator, graph: graph, count: count)
|
|
let t: Double = inputSource.floatValue(graph: graph)
|
|
var target: Double = mapScalarRangeCurve(t: t, low: low, high: high, curve: curve)
|
|
target = _mulAdd.applyScalar(target, graph: graph)
|
|
fillScalarRampedResult(into: &_result, previousOutputValue: &previousOutputValue, target: target, count: count)
|
|
return _result
|
|
}
|
|
}
|
|
}
|
|
|
|
extension UnipolarRangeMapper: GeneratorEnum {
|
|
public typealias AudioType = UnipolarRangeMapper.Audio
|
|
public typealias ControlType = UnipolarRangeMapper.Control
|
|
}
|