229 lines
8.4 KiB
Swift
229 lines
8.4 KiB
Swift
// RunningMin.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Accelerate
|
|
|
|
/// File-scope zero-fill block for RunningMin — fallback when upstream returns no channel.
|
|
private let runningMinZeroBlock: [Double] = [Double](
|
|
repeating: 0,
|
|
count: lyrebirdDefaultResultBufferSize
|
|
)
|
|
|
|
/// Running minimum with trigger reset. Equivalent to SuperCollider `RunningMin`.
|
|
///
|
|
/// Tracks the running minimum of `input` since the last trigger. On a ≤0 → >0 rising
|
|
/// edge of `trig`, the extreme is reset to the current `input` value at that frame.
|
|
/// On the very first sample/block, the extreme is initialised to the first input value.
|
|
///
|
|
/// ```swift
|
|
/// let peak = RunningMin.Audio(input: signal)
|
|
/// RunningMin.Audio(input: sig, trig: resetClock) // reset every clock pulse
|
|
/// RunningMin.Control(input: $cv, trig: gate) // block-rate minimum tracking
|
|
/// ```
|
|
///
|
|
/// - Parameters:
|
|
/// - input: Signal to track. Any `ValidGeneratorInput`.
|
|
/// - trig: Reset trigger. ≤0 → >0 rising edges reset the running minimum. Defaults to `0.0`.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public enum RunningMin {
|
|
|
|
// MARK: - Shared base
|
|
|
|
/// Shared stored properties for `RunningMin.Audio` and `RunningMin.Control`.
|
|
public class _Base: GeneratorBase {
|
|
let _input: any ValidGeneratorInput
|
|
let _trigInput: any ValidGeneratorInput
|
|
let _mulAdd: MulAddSpec
|
|
|
|
/// Running minimum. Initialised to the first input sample (lazy init via `_initialized`).
|
|
var _extreme: Double = 0.0
|
|
/// Previous trigger value for edge detection.
|
|
var _prevTrig: Double = 0.0
|
|
/// `false` until the first sample is processed; prevents a false start at `0.0`.
|
|
var _initialized: Bool = false
|
|
|
|
init(
|
|
input: any ValidGeneratorInput,
|
|
trig: any ValidGeneratorInput,
|
|
mul: any ValidGeneratorInput,
|
|
add: any ValidGeneratorInput
|
|
) {
|
|
let resolvedInput: any ValidGeneratorInput = input._resolvedInput()
|
|
let resolvedTrig: any ValidGeneratorInput = trig._resolvedInput()
|
|
_input = resolvedInput
|
|
_trigInput = resolvedTrig
|
|
_mulAdd = MulAddSpec(mul: mul, add: add)
|
|
super.init()
|
|
}
|
|
}
|
|
|
|
// MARK: - Audio rate
|
|
|
|
/// Audio-rate running minimum with trigger reset.
|
|
///
|
|
/// Evaluates per sample: lazy-initialises on the first sample; resets on rising trig
|
|
/// edge; otherwise tracks `min(_extreme, x)`.
|
|
///
|
|
/// - Parameters:
|
|
/// - input: Signal to track.
|
|
/// - trig: Reset trigger. ≤0 → >0 rising edges reset to the current `input` value.
|
|
/// Defaults to `0.0` (no reset).
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public final class Audio: _Base, AudioGenerator {
|
|
|
|
/// - Parameters:
|
|
/// - input: Signal to track.
|
|
/// - trig: Reset trigger. ≤0 → >0 edges reset the running minimum. Defaults to `0.0`.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public override init(
|
|
input: any ValidGeneratorInput,
|
|
trig: any ValidGeneratorInput = 0.0,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
super.init(input: input, trig: trig, mul: mul, add: add)
|
|
}
|
|
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
|
|
let inputSamples: [Double] = _input.calculatedSamples(
|
|
graph: graph, count: count).first ?? runningMinZeroBlock
|
|
let trigSamples: [Double] = _trigInput.calculatedSamples(
|
|
graph: graph, count: count).first ?? runningMinZeroBlock
|
|
|
|
var extreme: Double = _extreme
|
|
var prevTrig: Double = _prevTrig
|
|
var initialized: Bool = _initialized
|
|
|
|
for i: Int in 0..<count {
|
|
let x: Double = inputSamples[i]
|
|
let t: Double = trigSamples[i]
|
|
if !initialized {
|
|
extreme = x
|
|
initialized = true
|
|
} else if t > 0 && prevTrig <= 0 {
|
|
extreme = x
|
|
} else {
|
|
extreme = Swift.min(extreme, x)
|
|
}
|
|
prevTrig = t
|
|
_result[0][i] = extreme
|
|
}
|
|
|
|
_extreme = extreme
|
|
_prevTrig = prevTrig
|
|
_initialized = initialized
|
|
|
|
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
|
|
return _result
|
|
}
|
|
}
|
|
|
|
// MARK: - Control rate
|
|
|
|
/// Block-rate running minimum with trigger reset.
|
|
///
|
|
/// Scans the full audio-rate input block, applying the same state machine as `Audio`
|
|
/// (lazy init; trig-edge reset; `min` tracking). Outputs the block-final extreme as
|
|
/// a flat block (all samples equal). Bypasses the sub-block loop — the state machine
|
|
/// must scan the full audio block exactly once per audio cycle.
|
|
///
|
|
/// - Parameters:
|
|
/// - input: Signal to track.
|
|
/// - trig: Reset trigger. ≤0 → >0 rising edges reset the running minimum. 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 to track.
|
|
/// - trig: Reset trigger. ≤0 → >0 edges reset the running minimum. Defaults to `0.0`.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public override init(
|
|
input: any ValidGeneratorInput,
|
|
trig: any ValidGeneratorInput = 0.0,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
super.init(input: input, trig: trig, mul: mul, add: add)
|
|
_setupSubBlockBuffer()
|
|
}
|
|
|
|
/// Bypass sub-block loop: the running-minimum state machine must scan the full
|
|
/// audio-rate input block exactly once per audio block.
|
|
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 ?? runningMinZeroBlock
|
|
let trigSamples: [Double] = _trigInput.calculatedSamples(
|
|
graph: graph, count: count).first ?? runningMinZeroBlock
|
|
|
|
var extreme: Double = _extreme
|
|
var prevTrig: Double = _prevTrig
|
|
var initialized: Bool = _initialized
|
|
|
|
for i: Int in 0..<count {
|
|
let x: Double = inputSamples[i]
|
|
let t: Double = trigSamples[i]
|
|
if !initialized {
|
|
extreme = x
|
|
initialized = true
|
|
} else if t > 0 && prevTrig <= 0 {
|
|
extreme = x
|
|
} else {
|
|
extreme = Swift.min(extreme, x)
|
|
}
|
|
prevTrig = t
|
|
}
|
|
|
|
_extreme = extreme
|
|
_prevTrig = prevTrig
|
|
_initialized = initialized
|
|
|
|
var fillValue: Double = extreme
|
|
_result[0].withUnsafeMutableBufferPointer { buf in
|
|
guard let base: UnsafeMutablePointer<Double> = buf.baseAddress else { return }
|
|
vDSP_vfillD(&fillValue, base, 1, vDSP_Length(count))
|
|
}
|
|
|
|
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
|
|
|
|
guard count > 0 else { return _result }
|
|
previousOutputValue = _result[0][count - 1]
|
|
return _result
|
|
}
|
|
|
|
public func floatValue(graph: SynthGraph) -> Double { previousOutputValue }
|
|
}
|
|
}
|
|
|
|
// MARK: - GeneratorEnum
|
|
|
|
extension RunningMin: GeneratorEnum {
|
|
public typealias AudioType = RunningMin.Audio
|
|
public typealias ControlType = RunningMin.Control
|
|
}
|