1.0.0 release
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
// Pow.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Foundation
|
||||
|
||||
// MARK: - Pow
|
||||
|
||||
/// Raises signal to `exp` power: `pow(signal, exp)`.
|
||||
///
|
||||
/// ## Usage
|
||||
/// ```swift
|
||||
/// let curved = Pow.Audio(osc, exp: 2.0)
|
||||
/// let curved = Pow.Control(lfo, exp: 0.5)
|
||||
/// ```
|
||||
public enum Pow {
|
||||
|
||||
public class _Base: GeneratorBase {
|
||||
let inputSource: any ValidGeneratorInput
|
||||
let expInput: any ValidGeneratorInput
|
||||
let _mulAdd: MulAddSpec
|
||||
public init(
|
||||
_ signal: any ValidGeneratorInput,
|
||||
exp: any ValidGeneratorInput,
|
||||
mul: any ValidGeneratorInput = 1.0,
|
||||
add: any ValidGeneratorInput = 0.0
|
||||
) {
|
||||
|
||||
let resolvedSignal: any ValidGeneratorInput = signal._resolvedInput()
|
||||
let resolvedExp: any ValidGeneratorInput = exp._resolvedInput()
|
||||
inputSource = resolvedSignal
|
||||
expInput = resolvedExp
|
||||
_mulAdd = MulAddSpec(mul: mul, add: add)
|
||||
}
|
||||
}
|
||||
|
||||
/// Audio-rate power function.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - signal: Input signal (base). Negative base with non-integer exponent produces NaN.
|
||||
/// - exp: Exponent.
|
||||
/// - mul: Output multiplier. Defaults to `1.0`.
|
||||
/// - add: Output offset. Defaults to `0.0`.
|
||||
public final class Audio: _Base, AudioGenerator {
|
||||
/// - Parameters:
|
||||
/// - signal: Input signal (base). Negative base with non-integer exponent produces NaN.
|
||||
/// - exp: Exponent.
|
||||
/// - mul: Output multiplier. Defaults to `1.0`.
|
||||
/// - add: Output offset. Defaults to `0.0`.
|
||||
public override init(
|
||||
_ signal: any ValidGeneratorInput,
|
||||
exp: any ValidGeneratorInput,
|
||||
mul: any ValidGeneratorInput = 1.0,
|
||||
add: any ValidGeneratorInput = 0.0
|
||||
) {
|
||||
|
||||
super.init(signal, exp: exp, mul: mul, add: add)
|
||||
}
|
||||
public func _computeSamples(
|
||||
graph: SynthGraph,
|
||||
count: Int
|
||||
) -> [[Double]] {
|
||||
_checkCount(count)
|
||||
let block: [Double] = inputSource.calculatedSamples(graph: graph, count: count).first ?? []
|
||||
let e: Double = expInput.floatValue(graph: graph)
|
||||
let n: Int = block.count
|
||||
for i: Int in 0..<count {
|
||||
_result[0][i] = Foundation.pow(i < n ? block[i] : 0.0, e)
|
||||
}
|
||||
_mulAdd.applyIfNeeded(to: &_result, graph: graph, count: count)
|
||||
return _result
|
||||
}
|
||||
}
|
||||
|
||||
/// Control-rate power function. Output is linearly interpolated toward the block target.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - signal: Input signal (base). Negative base with non-integer exponent produces NaN.
|
||||
/// - exp: Exponent.
|
||||
/// - 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 (base). Negative base with non-integer exponent produces NaN.
|
||||
/// - exp: Exponent.
|
||||
/// - mul: Output multiplier. Defaults to `1.0`.
|
||||
/// - add: Output offset. Defaults to `0.0`.
|
||||
public override init(
|
||||
_ signal: any ValidGeneratorInput,
|
||||
exp: any ValidGeneratorInput,
|
||||
mul: any ValidGeneratorInput = 1.0,
|
||||
add: any ValidGeneratorInput = 0.0
|
||||
) {
|
||||
|
||||
super.init(signal, exp: exp, mul: mul, add: add)
|
||||
_setupSubBlockBuffer()
|
||||
}
|
||||
public func _computeSamples(
|
||||
graph: SynthGraph,
|
||||
count: Int
|
||||
) -> [[Double]] {
|
||||
_checkCount(count)
|
||||
var target: Double = Foundation.pow(inputSource.floatValue(graph: graph),
|
||||
expInput.floatValue(graph: graph))
|
||||
if !_mulAdd.isIdentity {
|
||||
target = target * _mulAdd.mulInput.floatValue(graph: graph)
|
||||
+ _mulAdd.addInput.floatValue(graph: graph)
|
||||
}
|
||||
fillScalarRampedResult(into: &_result, previousOutputValue: &previousOutputValue, target: target, count: count)
|
||||
return _result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Pow: GeneratorEnum {
|
||||
public typealias AudioType = Pow.Audio
|
||||
public typealias ControlType = Pow.Control
|
||||
}
|
||||
Reference in New Issue
Block a user