SPB Git

Repository Public License

SignalGenerator.swift 3.8 KB 90 lines swift

1//2//  ORIX Audio3//  File: Core/Sources/ORIXAudioCore/DSP/SignalGenerator.swift4//  Description: Deterministic reference signals for DSP validation (sine, multitone, noise, chirp, impulse, clipped).5//6//  Author: Simon-Pierre Boucher7//  Contact: contact@spboucher.ai8//9import Foundation1011/// SplitMix64: small, deterministic, good enough for test noise.12public struct SeededGenerator: RandomNumberGenerator, Sendable {13    private var state: UInt6414    public init(seed: UInt64) { state = seed }15    public mutating func next() -> UInt64 {16        state &+= 0x9E3779B97F4A7C1517        var z = state18        z = (z ^ (z >> 30)) &* 0xBF58476D1CE4E5B919        z = (z ^ (z >> 27)) &* 0x94D049BB133111EB20        return z ^ (z >> 31)21    }22}2324public enum SignalGenerator {25    public static func sine(frequency: Double, amplitude: Double = 1, phase: Double = 0, sampleRate: Double, count: Int) -> [Float] {26        let w = 2 * Double.pi * frequency / sampleRate27        return (0..<count).map { Float(amplitude * sin(w * Double($0) + phase)) }28    }2930    public static func multitone(_ tones: [(frequency: Double, amplitude: Double)], sampleRate: Double, count: Int) -> [Float] {31        var out = [Float](repeating: 0, count: count)32        for t in tones {33            let w = 2 * Double.pi * t.frequency / sampleRate34            for i in 0..<count { out[i] += Float(t.amplitude * sin(w * Double(i))) }35        }36        return out37    }3839    /// Gaussian white noise with the given RMS (Box–Muller).40    public static func whiteNoise(rms: Double, count: Int, seed: UInt64 = 1) -> [Float] {41        var g = SeededGenerator(seed: seed)42        return (0..<count).map { _ in43            let u1 = Double.random(in: Double.leastNonzeroMagnitude..<1, using: &g), u2 = Double.random(in: 0..<1, using: &g)44            return Float(rms * sqrt(-2 * log(u1)) * cos(2 * Double.pi * u2))45        }46    }4748    /// Pink (1/f) noise, Paul Kellett's refined filter (±0.05 dB above 9.2 Hz at 44.1 kHz), scaled to the requested RMS.49    public static func pinkNoise(rms: Double, count: Int, seed: UInt64 = 1) -> [Float] {50        let white = whiteNoise(rms: 1, count: count, seed: seed)51        var b0 = 0.0, b1 = 0.0, b2 = 0.0, b3 = 0.0, b4 = 0.0, b5 = 0.0, b6 = 0.052        var out = [Double](repeating: 0, count: count)53        for i in 0..<count {54            let w = Double(white[i])55            b0 = 0.99886 * b0 + w * 0.055517956            b1 = 0.99332 * b1 + w * 0.075075957            b2 = 0.96900 * b2 + w * 0.153852058            b3 = 0.86650 * b3 + w * 0.310485659            b4 = 0.55000 * b4 + w * 0.532952260            b5 = -0.7616 * b5 - w * 0.016898061            out[i] = b0 + b1 + b2 + b3 + b4 + b5 + b6 + w * 0.536262            b6 = w * 0.11592663        }64        let actual = sqrt(out.reduce(0) { $0 + $1 * $1 } / Double(max(1, count)))65        let g = actual > 0 ? rms / actual : 066        return out.map { Float($0 * g) }67    }6869    /// Linear chirp from f0 to f1 over the whole duration.70    public static func chirp(from f0: Double, to f1: Double, amplitude: Double = 1, sampleRate: Double, count: Int) -> [Float] {71        let duration = Double(count) / sampleRate72        let k = (f1 - f0) / duration73        return (0..<count).map {74            let t = Double($0) / sampleRate75            return Float(amplitude * sin(2 * Double.pi * (f0 * t + 0.5 * k * t * t)))76        }77    }7879    public static func impulse(at index: Int, amplitude: Float = 1, count: Int) -> [Float] {80        var out = [Float](repeating: 0, count: count)81        if index < count { out[index] = amplitude }82        return out83    }8485    public static func silence(count: Int) -> [Float] { [Float](repeating: 0, count: count) }8687    /// Hard-clips a signal at ±limit.88    public static func clipped(_ signal: [Float], limit: Float = 1) -> [Float] { signal.map { max(-limit, min(limit, $0)) } }89}90
Files main
Menu

Theme