Repository Public License
1//2// ORIX Audio3// File: Core/Sources/ORIXAudioCore/DSP/Biquad.swift4// Description: Biquad sections (RBJ cookbook designs) in double precision, with streaming state.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8//9import Foundation1011/// Normalised biquad coefficients (a0 = 1): y[n] = b0 x[n] + b1 x[n-1] + b2 x[n-2] − a1 y[n-1] − a2 y[n-2].12public struct BiquadCoefficients: Sendable, Equatable {13 public var b0, b1, b2, a1, a2: Double14 public init(b0: Double, b1: Double, b2: Double, a1: Double, a2: Double) { self.b0 = b0; self.b1 = b1; self.b2 = b2; self.a1 = a1; self.a2 = a2 }1516 public static let identity = BiquadCoefficients(b0: 1, b1: 0, b2: 0, a1: 0, a2: 0)1718 private static func normalised(_ b0: Double, _ b1: Double, _ b2: Double, _ a0: Double, _ a1: Double, _ a2: Double) -> Self {19 .init(b0: b0 / a0, b1: b1 / a0, b2: b2 / a0, a1: a1 / a0, a2: a2 / a0)20 }2122 public static func lowPass(frequency f: Double, q: Double = 0.7071, sampleRate fs: Double) -> Self {23 let w = 2 * Double.pi * min(f, fs * 0.49) / fs, c = cos(w), alpha = sin(w) / (2 * q)24 return normalised((1 - c) / 2, 1 - c, (1 - c) / 2, 1 + alpha, -2 * c, 1 - alpha)25 }26 public static func highPass(frequency f: Double, q: Double = 0.7071, sampleRate fs: Double) -> Self {27 let w = 2 * Double.pi * min(f, fs * 0.49) / fs, c = cos(w), alpha = sin(w) / (2 * q)28 return normalised((1 + c) / 2, -(1 + c), (1 + c) / 2, 1 + alpha, -2 * c, 1 - alpha)29 }30 public static func peaking(frequency f: Double, gainDB: Double, q: Double = 1, sampleRate fs: Double) -> Self {31 let a = pow(10, gainDB / 40), w = 2 * Double.pi * min(f, fs * 0.49) / fs, c = cos(w), alpha = sin(w) / (2 * q)32 return normalised(1 + alpha * a, -2 * c, 1 - alpha * a, 1 + alpha / a, -2 * c, 1 - alpha / a)33 }34 public static func lowShelf(frequency f: Double, gainDB: Double, sampleRate fs: Double) -> Self {35 let a = pow(10, gainDB / 40), w = 2 * Double.pi * min(f, fs * 0.49) / fs, c = cos(w), s = sin(w)36 let alpha = s / 2 * 2.0.squareRoot(), k = 2 * a.squareRoot() * alpha37 return normalised(a * ((a + 1) - (a - 1) * c + k), 2 * a * ((a - 1) - (a + 1) * c), a * ((a + 1) - (a - 1) * c - k),38 (a + 1) + (a - 1) * c + k, -2 * ((a - 1) + (a + 1) * c), (a + 1) + (a - 1) * c - k)39 }40 public static func highShelf(frequency f: Double, gainDB: Double, sampleRate fs: Double) -> Self {41 let a = pow(10, gainDB / 40), w = 2 * Double.pi * min(f, fs * 0.49) / fs, c = cos(w), s = sin(w)42 let alpha = s / 2 * 2.0.squareRoot(), k = 2 * a.squareRoot() * alpha43 return normalised(a * ((a + 1) + (a - 1) * c + k), -2 * a * ((a - 1) + (a + 1) * c), a * ((a + 1) + (a - 1) * c - k),44 (a + 1) - (a - 1) * c + k, 2 * ((a - 1) - (a + 1) * c), (a + 1) - (a - 1) * c - k)45 }4647 /// Magnitude response in dB at frequency `f`.48 public func responseDB(at f: Double, sampleRate fs: Double) -> Double {49 let w = 2 * Double.pi * f / fs50 let nr = b0 + b1 * cos(w) + b2 * cos(2 * w), ni = -(b1 * sin(w) + b2 * sin(2 * w))51 let dr = 1 + a1 * cos(w) + a2 * cos(2 * w), di = -(a1 * sin(w) + a2 * sin(2 * w))52 return 10 * log10(max(1e-30, (nr * nr + ni * ni) / (dr * dr + di * di)))53 }54}5556/// A cascade of biquads with per-section state (transposed direct form II). One instance per channel.57public struct BiquadCascade: Sendable {58 public var sections: [BiquadCoefficients]59 private var z1: [Double], z2: [Double]6061 public init(_ sections: [BiquadCoefficients]) {62 self.sections = sections63 z1 = [Double](repeating: 0, count: sections.count); z2 = z164 }6566 public mutating func reset() { for i in z1.indices { z1[i] = 0; z2[i] = 0 } }6768 @inlinable public mutating func process(_ x: Double) -> Double { processSample(x) }6970 public mutating func processSample(_ input: Double) -> Double {71 var x = input72 for i in sections.indices {73 let c = sections[i]74 let y = c.b0 * x + z1[i]75 z1[i] = c.b1 * x - c.a1 * y + z2[i]76 z2[i] = c.b2 * x - c.a2 * y77 x = y78 }79 return x80 }8182 public mutating func process(_ buffer: UnsafeMutableBufferPointer<Float>) {83 guard !sections.isEmpty else { return }84 for n in buffer.indices { buffer[n] = Float(processSample(Double(buffer[n]))) }85 }8687 public mutating func process(_ samples: inout [Float]) { samples.withUnsafeMutableBufferPointer { process($0) } }88}89