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>
107 lines
4.0 KiB
Swift
107 lines
4.0 KiB
Swift
// Ring4.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// MARK: - Ring4
|
|
|
|
/// Ring modulation variant 4: `a * a * b - a * b`.
|
|
///
|
|
/// Formula: `a * a * b - a * b`. Combines squared and linear ring modulation for a richer timbre.
|
|
///
|
|
/// - Parameters:
|
|
/// - a: First input signal.
|
|
/// - b: Second input signal.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public enum Ring4 {
|
|
public class _Base: GeneratorBase {
|
|
let inputA: any ValidGeneratorInput
|
|
let inputB: any ValidGeneratorInput
|
|
let _mulAdd: MulAddSpec
|
|
public init(
|
|
_ a: any ValidGeneratorInput,
|
|
_ b: any ValidGeneratorInput,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
|
|
let resolvedA: any ValidGeneratorInput = a._resolvedInput()
|
|
let resolvedB: any ValidGeneratorInput = b._resolvedInput()
|
|
inputA = resolvedA; inputB = resolvedB
|
|
_mulAdd = MulAddSpec(mul: mul, add: add)
|
|
}
|
|
}
|
|
/// - Parameters:
|
|
/// - a: First input signal.
|
|
/// - b: Second input signal.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public final class Audio: _Base, AudioGenerator {
|
|
/// - Parameters:
|
|
/// - a: First input signal.
|
|
/// - b: Second input signal.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public override init(
|
|
_ a: any ValidGeneratorInput,
|
|
_ b: any ValidGeneratorInput,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) { super.init(a, b, mul: mul, add: add) }
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
let blockA: [Double] = inputA.calculatedSamples(graph: graph, count: count).first ?? []
|
|
let blockB: [Double] = inputB.calculatedSamples(graph: graph, count: count).first ?? []
|
|
let na: Int = blockA.count; let nb: Int = blockB.count
|
|
for i: Int in 0..<count {
|
|
let a: Double = i < na ? blockA[i] : 0.0
|
|
let b: Double = i < nb ? blockB[i] : 0.0
|
|
_result[0][i] = a * a * b - a * b
|
|
}
|
|
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
|
|
return _result
|
|
}
|
|
}
|
|
/// - Parameters:
|
|
/// - a: First input signal.
|
|
/// - b: Second input signal.
|
|
/// - 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:
|
|
/// - a: First input signal.
|
|
/// - b: Second input signal.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public override init(
|
|
_ a: any ValidGeneratorInput,
|
|
_ b: any ValidGeneratorInput,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) { super.init(a, b, mul: mul, add: add); _setupSubBlockBuffer() }
|
|
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
let a: Double = inputA.floatValue(graph: graph); let b: Double = inputB.floatValue(graph: graph)
|
|
var target: Double = a * a * b - a * b
|
|
target = _mulAdd.applyScalar(target, graph: graph)
|
|
fillScalarRampedResult(into: &_result, previousOutputValue: &previousOutputValue, target: target, count: count)
|
|
return _result
|
|
}
|
|
}
|
|
}
|
|
|
|
extension Ring4: GeneratorEnum {
|
|
public typealias AudioType = Ring4.Audio
|
|
public typealias ControlType = Ring4.Control
|
|
}
|