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.
224 lines
8.8 KiB
Swift
224 lines
8.8 KiB
Swift
// Schmidt.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.
|
|
private let schmidtZeroBlock: [Double] = [Double](
|
|
repeating: 0,
|
|
count: lyrebirdDefaultResultBufferSize
|
|
)
|
|
|
|
/// Schmitt trigger with hysteresis.
|
|
///
|
|
/// Maintains a binary `0.0` / `1.0` state with separate switch-on and switch-off
|
|
/// thresholds, preventing rapid toggling near the threshold (chatter rejection).
|
|
///
|
|
/// - When state is `0.0` and `input > hi`, state switches to `1.0`.
|
|
/// - When state is `1.0` and `input < lo`, state switches to `0.0`.
|
|
/// - When `input` is between `lo` and `hi` ("hysteresis band"), state is unchanged.
|
|
///
|
|
/// SC equivalent: `Schmidt`.
|
|
///
|
|
/// ```swift
|
|
/// // Gate signal on amplitude with 0.1/0.3 hysteresis band.
|
|
/// let amp = AmplitudeFollower.Audio(in: In.Audio(bus: 0))
|
|
/// let gate = Schmidt.Audio(input: amp, lo: 0.1, hi: 0.3)
|
|
/// ```
|
|
///
|
|
/// - Parameters:
|
|
/// - input: Signal whose level drives the state machine.
|
|
/// - lo: Switch-off threshold. When state is `1.0` and `input < lo`, switches to `0.0`.
|
|
/// Any `ValidGeneratorInput` — may be audio-rate, control-rate, or a scalar literal.
|
|
/// Defaults to `0.0`.
|
|
/// - hi: Switch-on threshold. When state is `0.0` and `input > hi`, switches to `1.0`.
|
|
/// Any `ValidGeneratorInput` — may be audio-rate, control-rate, or a scalar literal.
|
|
/// Defaults to `0.0`.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public enum Schmidt {
|
|
|
|
// MARK: - Shared base
|
|
|
|
public class _Base: GeneratorBase {
|
|
let _input: any ValidGeneratorInput
|
|
let _loInput: any ValidGeneratorInput
|
|
let _hiInput: any ValidGeneratorInput
|
|
let _loIsGenerator: Bool
|
|
let _hiIsGenerator: Bool
|
|
let _mulAdd: MulAddSpec
|
|
|
|
var _state: Double = 0.0
|
|
|
|
init(
|
|
input: any ValidGeneratorInput,
|
|
lo: any ValidGeneratorInput,
|
|
hi: any ValidGeneratorInput,
|
|
mul: any ValidGeneratorInput,
|
|
add: any ValidGeneratorInput
|
|
) {
|
|
let resolvedInput: any ValidGeneratorInput = input._resolvedInput()
|
|
let resolvedLo: any ValidGeneratorInput = lo._resolvedInput()
|
|
let resolvedHi: any ValidGeneratorInput = hi._resolvedInput()
|
|
_input = resolvedInput
|
|
_loInput = resolvedLo
|
|
_hiInput = resolvedHi
|
|
_loIsGenerator = resolvedLo is any Generator
|
|
_hiIsGenerator = resolvedHi is any Generator
|
|
_mulAdd = MulAddSpec(mul: mul, add: add)
|
|
}
|
|
}
|
|
|
|
// MARK: - Audio rate
|
|
|
|
/// Sample-accurate Schmitt trigger. Both `lo` and `hi` may be audio-rate generators.
|
|
///
|
|
/// - Parameters:
|
|
/// - input: Signal whose level drives the state machine.
|
|
/// - lo: Switch-off threshold (any `ValidGeneratorInput`). Defaults to `0.0`.
|
|
/// - hi: Switch-on threshold (any `ValidGeneratorInput`). 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 whose level drives the state machine.
|
|
/// - lo: Switch-off threshold. When state is `1.0` and `input < lo`, switches to `0.0`.
|
|
/// Defaults to `0.0`.
|
|
/// - hi: Switch-on threshold. When state is `0.0` and `input > hi`, switches to `1.0`.
|
|
/// Defaults to `0.0`.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public override init(
|
|
input: any ValidGeneratorInput,
|
|
lo: any ValidGeneratorInput = 0.0,
|
|
hi: any ValidGeneratorInput = 0.0,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
super.init(input: input, lo: lo, hi: hi, mul: mul, add: add)
|
|
}
|
|
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
|
|
let inputSamples: [Double] = _input.calculatedSamples(graph: graph, count: count).first
|
|
?? schmidtZeroBlock
|
|
let (loSamples, loScalar) = resolveGeneratorOrScalar(
|
|
_loInput, isGenerator: _loIsGenerator, graph: graph, count: count)
|
|
let (hiSamples, hiScalar) = resolveGeneratorOrScalar(
|
|
_hiInput, isGenerator: _hiIsGenerator, graph: graph, count: count)
|
|
|
|
var state: Double = _state
|
|
for i: Int in 0..<count {
|
|
let x: Double = inputSamples[i]
|
|
let lo: Double = loSamples?[i] ?? loScalar
|
|
let hi: Double = hiSamples?[i] ?? hiScalar
|
|
|
|
if state <= 0 && x > hi {
|
|
state = 1.0
|
|
} else if state > 0 && x < lo {
|
|
state = 0.0
|
|
}
|
|
_result[0][i] = state
|
|
}
|
|
_state = state
|
|
|
|
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
|
|
return _result
|
|
}
|
|
}
|
|
|
|
// MARK: - Control rate
|
|
|
|
/// Block-rate Schmitt trigger. Scans the full audio-rate input for state transitions;
|
|
/// outputs the block-final binary state as a flat block — no ramp.
|
|
///
|
|
/// > Note: Schmitt trigger output is discrete (0.0 or 1.0) and must not be
|
|
/// > interpolated/smoothed. This variant writes a flat constant block equal to the
|
|
/// > block-final state rather than using `fillRampedResult`.
|
|
///
|
|
/// - Parameters:
|
|
/// - input: Signal whose level drives the state machine.
|
|
/// - lo: Switch-off threshold (any `ValidGeneratorInput`). Defaults to `0.0`.
|
|
/// - hi: Switch-on threshold (any `ValidGeneratorInput`). 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 whose level drives the state machine.
|
|
/// - lo: Switch-off threshold. When state is `1.0` and `input < lo`, switches to `0.0`.
|
|
/// Defaults to `0.0`.
|
|
/// - hi: Switch-on threshold. When state is `0.0` and `input > hi`, switches to `1.0`.
|
|
/// Defaults to `0.0`.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public override init(
|
|
input: any ValidGeneratorInput,
|
|
lo: any ValidGeneratorInput = 0.0,
|
|
hi: any ValidGeneratorInput = 0.0,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
super.init(input: input, lo: lo, hi: hi, mul: mul, add: add)
|
|
_setupSubBlockBuffer()
|
|
}
|
|
|
|
/// Bypasses `ControlGenerator` sub-block loop — state-machine scanning requires
|
|
/// 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
|
|
?? schmidtZeroBlock
|
|
let (loSamples, loScalar) = resolveGeneratorOrScalar(
|
|
_loInput, isGenerator: _loIsGenerator, graph: graph, count: count)
|
|
let (hiSamples, hiScalar) = resolveGeneratorOrScalar(
|
|
_hiInput, isGenerator: _hiIsGenerator, graph: graph, count: count)
|
|
|
|
var state: Double = _state
|
|
for i: Int in 0..<count {
|
|
let x: Double = inputSamples[i]
|
|
let lo: Double = loSamples?[i] ?? loScalar
|
|
let hi: Double = hiSamples?[i] ?? hiScalar
|
|
|
|
if state <= 0 && x > hi {
|
|
state = 1.0
|
|
} else if state > 0 && x < lo {
|
|
state = 0.0
|
|
}
|
|
}
|
|
_state = state
|
|
|
|
for i: Int in 0..<count {
|
|
_result[0][i] = state
|
|
}
|
|
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
|
|
if count > 0 { previousOutputValue = _result[0][count - 1] }
|
|
return _result
|
|
}
|
|
}
|
|
}
|
|
|
|
extension Schmidt: GeneratorEnum {
|
|
public typealias AudioType = Schmidt.Audio
|
|
public typealias ControlType = Schmidt.Control
|
|
}
|