Files
Lyrebird/Sources/LyrebirdCLI/Resources/XcodeTemplates/LyrebirdNoiseGenerator.xctemplate/___FILEBASENAME___.swift
T
josh f8878324fd v2.1.1 release: delay-click fix + internal consolidation
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>
2026-10-07 21:20:49 -07:00

130 lines
4.9 KiB
Swift

//___FILEHEADER___
//
// In-tree template — drop this file under Sources/Lyrebird/Generators/Noise/.
// Extends `NoiseBase`, whose designated init is module-internal, so this
// template is not usable from a project that depends on Lyrebird as a package.
// External users should start from the basic `LyrebirdGenerator` template and
// declare rngState + xorshift64 inline.
// MARK: - ___FILEBASENAMEASIDENTIFIER___
/// One-line description of ___FILEBASENAMEASIDENTIFIER___.
///
/// Extends `NoiseBase` so `rngState: UInt64` and `_mulAdd` are inherited.
/// Use `xorshift64Next(&rngState)` to draw a `[-1, 1]` sample per call.
///
/// Each instance is independently seeded — concurrent instances produce
/// uncorrelated output.
///
/// ## Usage
/// ```swift
/// let n = ___FILEBASENAMEASIDENTIFIER___.Audio()
/// let n = ___FILEBASENAMEASIDENTIFIER___.Audio(seed: 42) // deterministic for tests
/// ```
public enum ___FILEBASENAMEASIDENTIFIER___ {
/// Shared PRNG state and output buffer for both rate variants.
public class _Base: NoiseBase {
override init(
initialRngState: UInt64,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
super.init(initialRngState: initialRngState, mul: mul, add: add)
}
/// Draw one sample in `[-1, 1]`. Replace this body with any
/// distribution-shaping logic — clip, fold, threshold, etc.
func nextSample() -> Double { xorshift64Next(&rngState) }
}
/// Audio-rate ___FILEBASENAMEASIDENTIFIER___.
///
/// - Parameters:
/// - seed: PRNG seed. Defaults to a time-derived value for unique instances.
/// Pass an explicit seed for reproducible test output.
/// - mul: Output multiplier applied after synthesis. Defaults to `1.0`.
/// - add: Offset added after multiplier. Defaults to `0.0`.
public final class Audio: _Base, AudioGenerator {
/// - Parameters:
/// - seed: PRNG seed.
/// - mul: Output multiplier.
/// - add: Output offset.
public init(
seed: UInt64? = nil,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
// Pick a unique mix constant so this Audio variant doesn't correlate
// with sibling Control / sibling generators when both default-seed
// within the same nanosecond.
let audioMixConstant: UInt64 = 0xA110_AAAA_5A5A_3C3C
let initialRngState = lyrebirdSeedXorshift64(from: seed, mixConstant: audioMixConstant)
super.init(initialRngState: initialRngState, mul: mul, add: add)
}
public func _computeSamples(
graph: SynthGraph,
count: Int
) -> [[Double]] {
_checkCount(count)
for i: Int in 0..<count {
_result[0][i] = nextSample()
}
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
return _result
}
}
/// Control-rate ___FILEBASENAMEASIDENTIFIER___ — draws one new value per
/// audio block, emits a linear ramp to the new target.
/// Conforms to `BlockRateControlGenerator` so the draw rate stays at one per
/// audio block regardless of `controlBlockSize`.
///
/// - Parameters:
/// - seed: PRNG seed. Defaults to a time-derived value.
/// - mul: Output multiplier. Defaults to `1.0`.
/// - add: Output offset. Defaults to `0.0`.
public final class Control: _Base, BlockRateControlGenerator {
public var previousOutputValue: Double = 0.0
private var _rampTarget: Double = 0.0
/// - Parameters:
/// - seed: PRNG seed.
/// - mul: Output multiplier.
/// - add: Output offset.
public init(
seed: UInt64? = nil,
mul: any ValidGeneratorInput = 1.0,
add: any ValidGeneratorInput = 0.0
) {
let controlMixConstant: UInt64 = 0xB220_BBBB_A5A5_C3C3
let initialRngState = lyrebirdSeedXorshift64(from: seed, mixConstant: controlMixConstant)
super.init(initialRngState: initialRngState, mul: mul, add: add)
}
public func _computeSamples(
graph: SynthGraph,
count: Int
) -> [[Double]] {
_checkCount(count)
let target: Double = nextSample()
let rampStart: Double = _rampTarget
_rampTarget = target
guard count > 0 else { return _result }
previousOutputValue = fillRampedResult(
into: &_result, from: rampStart, to: target, mulAdd: _mulAdd, graph: graph, count: count)
return _result
}
}
}
extension ___FILEBASENAMEASIDENTIFIER___: GeneratorEnum {
public typealias AudioType = ___FILEBASENAMEASIDENTIFIER___.Audio
public typealias ControlType = ___FILEBASENAMEASIDENTIFIER___.Control
}