560724d116
- Fixed: SynthesizerBase subclasses' release/free now dispatch correctly (gated synths were hard-freed instead of released). - New: quickstart project template + LyrebirdQuickstartDemo. - Docs: every example typechecked or provably illustrative (426->542); ~20 wrong-API examples fixed; root docs brought under the compile guard. - Fixed: NRT/Score demo Package.swift build from terminal again. See RELEASES.md for details.
207 lines
8.1 KiB
Swift
207 lines
8.1 KiB
Swift
// SignalChanged.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
/// File-scope zero-fill block — used as the `??` fallback when an upstream
|
||
/// generator returns an empty channel array. Length matches the runtime
|
||
/// precondition `count <= lyrebirdDefaultResultBufferSize`.
|
||
private let signalChangedZeroBlock: [Double] = [Double](
|
||
repeating: 0,
|
||
count: lyrebirdDefaultResultBufferSize
|
||
)
|
||
|
||
/// Per-sample change detector.
|
||
///
|
||
/// Outputs `1.0` for any sample where `|input(i) − input(i-1)| > threshold`,
|
||
/// otherwise `0.0`. Output is a binary signal — it stays high for as many
|
||
/// consecutive samples as the change condition holds, not a one-sample pulse.
|
||
///
|
||
/// The first sample of the very first block compares against `0.0` (no prior
|
||
/// sample yet).
|
||
///
|
||
/// `threshold` is any `ValidGeneratorInput` and may be a scalar literal,
|
||
/// `ParameterNode`, `@LiveParam`, or audio/control-rate generator.
|
||
///
|
||
/// ```swift
|
||
/// // Cheap onset proxy — fires whenever amplitude jumps > 0.05.
|
||
/// let amp = AmplitudeFollower.Audio(in: In.Audio(bus: 0))
|
||
/// let edge = SignalChanged.Audio(input: amp, threshold: 0.05)
|
||
/// addOutputGenerator(SendTrigger.Audio(trig: edge, id: 1))
|
||
/// ```
|
||
///
|
||
/// - Parameters:
|
||
/// - input: Signal under test.
|
||
/// - threshold: Minimum `|Δ input|` (exclusive) required to emit `1.0`.
|
||
/// Defaults to `0.0` — every change fires.
|
||
/// - mul: Output multiplier. Defaults to `1.0`.
|
||
/// - add: Output offset. Defaults to `0.0`.
|
||
public enum SignalChanged {
|
||
|
||
// MARK: - Shared base
|
||
|
||
public class _Base: GeneratorBase {
|
||
let _input: any ValidGeneratorInput
|
||
let _thresholdInput: any ValidGeneratorInput
|
||
let _thresholdIsGenerator: Bool
|
||
let _mulAdd: MulAddSpec
|
||
|
||
var _prevSample: Double = 0.0
|
||
|
||
init(
|
||
input: any ValidGeneratorInput,
|
||
threshold: any ValidGeneratorInput,
|
||
mul: any ValidGeneratorInput,
|
||
add: any ValidGeneratorInput
|
||
) {
|
||
let resolvedInput: any ValidGeneratorInput = input._resolvedInput()
|
||
let resolvedThreshold: any ValidGeneratorInput = threshold._resolvedInput()
|
||
_input = resolvedInput
|
||
_thresholdInput = resolvedThreshold
|
||
_thresholdIsGenerator = resolvedThreshold is any Generator
|
||
_mulAdd = MulAddSpec(mul: mul, add: add)
|
||
}
|
||
}
|
||
|
||
// MARK: - Audio rate
|
||
|
||
/// Sample-accurate change detector.
|
||
///
|
||
/// - Parameters:
|
||
/// - input: Signal under test.
|
||
/// - threshold: Minimum `|Δ input|` to emit `1.0`. Defaults to `0.0`.
|
||
/// - mul: Output multiplier. Defaults to `1.0`.
|
||
/// - add: Output offset. Defaults to `0.0`.
|
||
public final class Audio: _Base, AudioGenerator {
|
||
|
||
/// - Parameters:
|
||
/// - input: Signal under test.
|
||
/// - threshold: Minimum `|Δ input|` (exclusive) required to emit `1.0`.
|
||
/// Defaults to `0.0` — every change fires.
|
||
/// - mul: Output multiplier. Defaults to `1.0`.
|
||
/// - add: Output offset. Defaults to `0.0`.
|
||
public override init(
|
||
input: any ValidGeneratorInput,
|
||
threshold: any ValidGeneratorInput = 0.0,
|
||
mul: any ValidGeneratorInput = 1.0,
|
||
add: any ValidGeneratorInput = 0.0
|
||
) {
|
||
super.init(input: input, threshold: threshold, mul: mul, add: add)
|
||
}
|
||
public func _computeSamples(
|
||
graph: SynthGraph,
|
||
count: Int
|
||
) -> [[Double]] {
|
||
_checkCount(count)
|
||
|
||
let inputSamples: [Double] = _input.calculatedSamples(graph: graph, count: count).first ?? signalChangedZeroBlock
|
||
// Per-sample threshold when generator-driven; per-block scalar otherwise.
|
||
// `threshScalar` is computed from `floatValue` even when the threshold is a
|
||
// generator so it remains a usable fallback if `calculatedSamples` returns
|
||
// an empty channel array.
|
||
let threshSamples: [Double]? = _thresholdIsGenerator
|
||
? _thresholdInput.calculatedSamples(graph: graph, count: count).first
|
||
: nil
|
||
let threshScalar: Double = _thresholdInput.floatValue(graph: graph)
|
||
|
||
var prev: Double = _prevSample
|
||
for i: Int in 0..<count {
|
||
let x: Double = inputSamples[i]
|
||
let t: Double = threshSamples?[i] ?? threshScalar
|
||
_result[0][i] = abs(x - prev) > t ? 1.0 : 0.0
|
||
prev = x
|
||
}
|
||
_prevSample = prev
|
||
|
||
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
|
||
return _result
|
||
}
|
||
}
|
||
|
||
// MARK: - Control rate
|
||
|
||
/// Block-rate change detector. Scans the full audio-rate input for
|
||
/// per-sample change events; collapses to a single block-constant
|
||
/// output sample (the value of the most recent change comparison).
|
||
///
|
||
/// Square output (no ramp) — change detection is binary by nature.
|
||
///
|
||
/// - Parameters:
|
||
/// - input: Signal under test.
|
||
/// - threshold: Minimum `|Δ input|` to emit `1.0`. Defaults to `0.0`.
|
||
/// - 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:
|
||
/// - input: Signal under test.
|
||
/// - threshold: Minimum `|Δ input|` (exclusive) required to emit `1.0`.
|
||
/// Defaults to `0.0` — every change fires.
|
||
/// - mul: Output multiplier. Defaults to `1.0`.
|
||
/// - add: Output offset. Defaults to `0.0`.
|
||
public override init(
|
||
input: any ValidGeneratorInput,
|
||
threshold: any ValidGeneratorInput = 0.0,
|
||
mul: any ValidGeneratorInput = 1.0,
|
||
add: any ValidGeneratorInput = 0.0
|
||
) {
|
||
super.init(input: input, threshold: threshold, mul: mul, add: add)
|
||
_setupSubBlockBuffer()
|
||
}
|
||
|
||
|
||
/// Bypasses `ControlGenerator` sub-block loop — change detection
|
||
/// requires scanning the full audio-rate input once per audio block,
|
||
/// not per sub-block chunk.
|
||
public func calculatedSamples(
|
||
graph: SynthGraph,
|
||
count: Int
|
||
) -> [[Double]] {
|
||
_calculatedSamplesBlockRate(graph: graph, count: count)
|
||
}
|
||
|
||
public func _computeSamples(
|
||
graph: SynthGraph,
|
||
count: Int
|
||
) -> [[Double]] {
|
||
_checkCount(count)
|
||
|
||
let inputSamples: [Double] = _input.calculatedSamples(graph: graph, count: count).first ?? signalChangedZeroBlock
|
||
let threshSamples: [Double]? = _thresholdIsGenerator
|
||
? _thresholdInput.calculatedSamples(graph: graph, count: count).first
|
||
: nil
|
||
let threshScalar: Double = _thresholdInput.floatValue(graph: graph)
|
||
|
||
var prev: Double = _prevSample
|
||
// Did any sample in the block exceed the threshold? Once we've
|
||
// detected a change, the remaining iterations only need to advance
|
||
// `prev` so cross-block detection stays correct.
|
||
var anyChanged: Bool = false
|
||
for i: Int in 0..<count {
|
||
let x: Double = inputSamples[i]
|
||
if !anyChanged {
|
||
let t: Double = threshSamples?[i] ?? threshScalar
|
||
if abs(x - prev) > t { anyChanged = true }
|
||
}
|
||
prev = x
|
||
}
|
||
_prevSample = prev
|
||
|
||
let value: Double = anyChanged ? 1.0 : 0.0
|
||
for i: Int in 0..<count {
|
||
_result[0][i] = value
|
||
}
|
||
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
|
||
if count > 0 { previousOutputValue = _result[0][count - 1] }
|
||
return _result
|
||
}
|
||
}
|
||
}
|
||
|
||
extension SignalChanged: GeneratorEnum {
|
||
public typealias AudioType = SignalChanged.Audio
|
||
public typealias ControlType = SignalChanged.Control
|
||
}
|