133 lines
4.6 KiB
Swift
133 lines
4.6 KiB
Swift
//___FILEHEADER___
|
|
|
|
import Foundation
|
|
@_spi(LyrebirdExtensions) import LyrebirdSynth
|
|
|
|
// MARK: - ___FILEBASENAMEASIDENTIFIER___
|
|
|
|
/// One-line description of ___FILEBASENAMEASIDENTIFIER___.
|
|
///
|
|
/// Longer explanation: what the generator does, formula, edge cases.
|
|
///
|
|
/// ## Usage
|
|
/// ```swift
|
|
/// let g = ___FILEBASENAMEASIDENTIFIER___.Audio(signal)
|
|
/// let g = ___FILEBASENAMEASIDENTIFIER___.Control(signal)
|
|
/// ```
|
|
public enum ___FILEBASENAMEASIDENTIFIER___ {
|
|
|
|
/// Shared state between Audio and Control variants.
|
|
///
|
|
/// Holds input references + MulAddSpec. Stored properties live here so the
|
|
/// two final classes share the same field layout (and the same designated init).
|
|
public class _Base: GeneratorBase {
|
|
let inputSource: any ValidGeneratorInput
|
|
let _mulAdd: MulAddSpec
|
|
|
|
public init(
|
|
_ signal: any ValidGeneratorInput,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
inputSource = signal
|
|
_mulAdd = MulAddSpec(mul: mul, add: add)
|
|
// GeneratorBase.init() allocates _result with one mono row of
|
|
// lyrebirdDefaultResultBufferSize. Multi-channel generators call
|
|
// super.init(channels: N) instead.
|
|
}
|
|
}
|
|
|
|
/// Per-sample transform shared by Audio + Control. Replace this with the
|
|
/// generator's actual formula.
|
|
@inline(__always)
|
|
static func _transform(_ value: Double) -> Double {
|
|
// TODO: implement.
|
|
return value
|
|
}
|
|
|
|
/// Audio-rate ___FILEBASENAMEASIDENTIFIER___.
|
|
///
|
|
/// - Parameters:
|
|
/// - signal: Input signal.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public final class Audio: _Base, AudioGenerator {
|
|
/// - Parameters:
|
|
/// - signal: Input signal.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public override init(
|
|
_ signal: any ValidGeneratorInput,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
super.init(signal, mul: mul, add: add)
|
|
}
|
|
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
let block = inputSource.calculatedSamples(graph: graph, count: count).first ?? []
|
|
let n = block.count
|
|
for i: Int in 0..<count {
|
|
_result[0][i] = ___FILEBASENAMEASIDENTIFIER___._transform(i < n ? block[i] : 0.0)
|
|
}
|
|
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
|
|
return _result
|
|
}
|
|
}
|
|
|
|
/// Control-rate ___FILEBASENAMEASIDENTIFIER___. Output is linearly interpolated
|
|
/// toward the block target.
|
|
///
|
|
/// - Parameters:
|
|
/// - signal: 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:
|
|
/// - signal: Input signal.
|
|
/// - mul: Output multiplier. Defaults to `1.0`.
|
|
/// - add: Output offset. Defaults to `0.0`.
|
|
public override init(
|
|
_ signal: any ValidGeneratorInput,
|
|
mul: any ValidGeneratorInput = 1.0,
|
|
add: any ValidGeneratorInput = 0.0
|
|
) {
|
|
super.init(signal, mul: mul, add: add)
|
|
_setupSubBlockBuffer()
|
|
}
|
|
|
|
public func _computeSamples(
|
|
graph: SynthGraph,
|
|
count: Int
|
|
) -> [[Double]] {
|
|
_checkCount(count)
|
|
var target = ___FILEBASENAMEASIDENTIFIER___._transform(
|
|
inputSource.floatValue(graph: graph)
|
|
)
|
|
if !_mulAdd.isIdentity {
|
|
target = target * _mulAdd.mulInput.floatValue(graph: graph)
|
|
+ _mulAdd.addInput.floatValue(graph: graph)
|
|
}
|
|
guard count > 0 else { return _result }
|
|
let rampStart: Double = previousOutputValue
|
|
previousOutputValue = target
|
|
let step = (target - rampStart) / Double(count)
|
|
for i: Int in 0..<count {
|
|
_result[0][i] = rampStart + step * Double(i + 1)
|
|
}
|
|
return _result
|
|
}
|
|
}
|
|
}
|
|
|
|
extension ___FILEBASENAMEASIDENTIFIER___: GeneratorEnum {
|
|
public typealias AudioType = ___FILEBASENAMEASIDENTIFIER___.Audio
|
|
public typealias ControlType = ___FILEBASENAMEASIDENTIFIER___.Control
|
|
}
|