Repository Public
1// ORIX DJ2// File: Packages/ORIXDJCore/Tests/ORIXDSPTests/EffectTestSupport.swift3// Description: Test rig for the effect units: block rendering with a running transport, signal generators and measurements.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import ORIXDSP9import Testing1011let testRate: Double = 480001213/// Renders arrays through an effect in blocks, moving the transport like the engine does.14final class Rig {15 let fx: EffectUnit16 var bpm: Double17 var beat: Double18 var block: Int1920 init(_ kind: EffectKind, bpm: Double = 120, beat: Double = 0, block: Int = 512, mix: Float? = 1, enabled: Bool = true) {21 fx = EffectFactory.make(kind, sampleRate: testRate)22 self.bpm = bpm; self.beat = beat; self.block = block23 fx.setTransport(bpm: bpm, beatPosition: beat)24 if let mix { fx.setMix(mix) }25 if enabled { fx.setEnabled(true) }26 }2728 func set(_ id: String, _ value: Float) { fx.setParameter(fx.descriptor.index(of: id)!, value) }2930 /// Processes the stereo signal and returns the output.31 func run(_ l: [Float], _ r: [Float]) -> (l: [Float], r: [Float]) {32 var outL = l, outR = r33 let n = l.count34 outL.withUnsafeMutableBufferPointer { pl in35 outR.withUnsafeMutableBufferPointer { pr in36 var i = 037 while i < n {38 let c = min(block, n - i)39 fx.setTransport(bpm: bpm, beatPosition: beat)40 fx.render(pl.baseAddress! + i, pr.baseAddress! + i, c)41 beat += Double(c) * bpm / 60 / testRate42 i += c43 }44 }45 }46 return (outL, outR)47 }4849 func run(_ mono: [Float]) -> (l: [Float], r: [Float]) { run(mono, mono) }50 func runSilence(seconds: Double) { _ = run(silence(seconds)) }51}5253func frames(_ seconds: Double) -> Int { Int(seconds * testRate) }54func silence(_ seconds: Double) -> [Float] { [Float](repeating: 0, count: frames(seconds)) }55func constant(_ value: Float, _ seconds: Double) -> [Float] { [Float](repeating: value, count: frames(seconds)) }5657func sine(_ frequency: Double, amplitude: Float, seconds: Double, phase: Double = 0) -> [Float] {58 (0 ..< frames(seconds)).map { amplitude * Float(sin(2 * Double.pi * frequency * Double($0) / testRate + phase)) }59}6061func whiteNoise(amplitude: Float, seconds: Double, seed: UInt32 = 7) -> [Float] {62 var g = NoiseGenerator(seed: seed)63 return (0 ..< frames(seconds)).map { _ in amplitude * g.next() }64}6566/// 440 Hz sine plus white noise, peak <= 0.6.67func testSignal(seconds: Double, seed: UInt32 = 7) -> [Float] {68 let s = sine(440, amplitude: 0.3, seconds: seconds), w = whiteNoise(amplitude: 0.3, seconds: seconds, seed: seed)69 return zip(s, w).map { $0 + $1 }70}7172func peak<S: Sequence>(_ x: S) -> Float where S.Element == Float { x.reduce(0) { max($0, abs($1)) } }73func rms<C: Collection>(_ x: C) -> Float where C.Element == Float { sqrtf(x.reduce(0) { $0 + $1 * $1 } / Float(max(1, x.count))) }74func energy<C: Collection>(_ x: C) -> Double where C.Element == Float { x.reduce(0.0) { $0 + Double($1) * Double($1) } }75func db(_ x: Float) -> Float { 20 * log10f(max(x, 1e-12)) }76func maxDelta(_ x: ArraySlice<Float>) -> Float {77 var m: Float = 0, previous = x.first ?? 078 for v in x { m = max(m, abs(v - previous)); previous = v }79 return m80}8182/// Amplitude of the component at `frequency` (single-bin Fourier correlation).83func amplitude(_ x: ArraySlice<Float>, at frequency: Double) -> Float {84 var re = 0.0, im = 0.085 for (k, v) in x.enumerated() {86 let w = 2 * Double.pi * frequency * Double(k) / testRate87 re += Double(v) * cos(w); im += Double(v) * sin(w)88 }89 return Float(2 * (re * re + im * im).squareRoot() / Double(x.count))90}9192/// Energy of `x` inside a band, measured with the package band-pass (Q = 4).93func bandEnergy(_ x: [Float], centre: Float) -> Double {94 var f = StereoBiquad()95 f.c = .bandpass(frequency: centre, q: 4, sampleRate: Float(testRate))96 return x.reduce(0.0) { let y = f.processLeft($1); return $0 + Double(y * y) }97}98