SPB Git

Repository Public

Modulation.swift 8.0 KB 161 lines swift

1// ORIX DJ2// File: Packages/ORIXDJCore/Sources/ORIXDSP/Effects/Modulation.swift3// Description: Modulation effects: flanger, six-stage phaser, three-voice stereo chorus.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation89// MARK: - Flanger1011/// Modulated short delay (0.8 to 6.8 ms) with saturated feedback; the right channel sweeps a quarter cycle ahead.12public final class FlangerEffect: EffectUnit, @unchecked Sendable {13    private let lineL: DelayLine, lineR: DelayLine14    private var phase: Float = 015    private var depth: OnePoleSmoother16    private var feedback: OnePoleSmoother1718    public init(sampleRate: Double) {19        lineL = DelayLine(minimumSamples: Int(0.02 * sampleRate)); lineR = DelayLine(minimumSamples: Int(0.02 * sampleRate))20        depth = OnePoleSmoother(timeConstantMS: 30, sampleRate: Float(sampleRate))21        feedback = OnePoleSmoother(timeConstantMS: 20, sampleRate: Float(sampleRate))22        super.init(kind: .flanger, sampleRate: sampleRate)23        depth.snap(values[1]); feedback.snap(values[2])24    }2526    public override func parameterChanged(_ index: Int) {27        if index == 1 { depth.target = values[1] } else if index == 2 { feedback.target = values[2] }28    }2930    /// The short lines keep recording while bypassed, so the taps already hold the live signal when the unit fades in.31    public override func observeBypassed(_ l: UnsafeMutablePointer<Float>, _ r: UnsafeMutablePointer<Float>, _ n: Int) {32        for i in 0 ..< n { lineL.write(sane(l[i])); lineR.write(sane(r[i])) }33    }3435    public override func reset() { lineL.reset(); lineR.reset(); phase = 0 }3637    public override func processWet(_ l: UnsafeMutablePointer<Float>, _ r: UnsafeMutablePointer<Float>, _ n: Int) {38        let inc = values[0] / sampleRate, base = 0.0008 * sampleRate, sweep = 0.006 * sampleRate39        var ph = phase, dp = depth, fb = feedback40        for i in 0 ..< n {41            let d = dp.next() * sweep, f = fb.next()42            let dl = lineL.read(delaySamples: base + d * (0.5 - 0.5 * cosf(2 * .pi * ph)))43            let dr = lineR.read(delaySamples: base + d * (0.5 - 0.5 * cosf(2 * .pi * (ph + 0.25))))44            lineL.write(flush(sane(l[i]) + f * 2 * softClip(dl * 0.5)))45            lineR.write(flush(sane(r[i]) + f * 2 * softClip(dr * 0.5)))46            l[i] = dl; r[i] = dr47            ph += inc; if ph >= 1 { ph -= 1 }48        }49        phase = ph; depth = dp; feedback = fb50    }51}5253// MARK: - Phaser5455/// Six first-order all-pass stages per channel, swept two octaves either side of 800 Hz, with saturated feedback.56/// The wet signal is the all-pass chain alone: the notches appear once the base mixes it with the dry signal.57public final class PhaserEffect: EffectUnit, @unchecked Sendable {58    private static let stages = 659    private let state: UnsafeMutablePointer<Float> // 6 left then 6 right60    private var phase: Float = 061    private var coefL: Float = 0, coefR: Float = 062    private var lastL: Float = 0, lastR: Float = 063    private var depth: OnePoleSmoother64    private var feedback: OnePoleSmoother6566    public init(sampleRate: Double) {67        state = .allocate(capacity: 2 * Self.stages); state.initialize(repeating: 0, count: 2 * Self.stages)68        depth = OnePoleSmoother(timeConstantMS: 30, sampleRate: Float(sampleRate))69        feedback = OnePoleSmoother(timeConstantMS: 20, sampleRate: Float(sampleRate))70        super.init(kind: .phaser, sampleRate: sampleRate)71        depth.snap(values[1]); feedback.snap(values[2])72        coefL = coefficient(0, values[1]); coefR = coefficient(0.25, values[1])73    }7475    deinit { state.deallocate() }7677    /// All-pass coefficient for the LFO phase `ph`: a = (tan(pi f / sr) - 1) / (tan(pi f / sr) + 1).78    @inline(__always) private func coefficient(_ ph: Float, _ depth: Float) -> Float {79        let f = min(800 * powf(2, 2 * depth * sinf(2 * .pi * ph)), 0.45 * sampleRate)80        let t = tanf(.pi * f / sampleRate)81        return (t - 1) / (t + 1)82    }8384    public override func parameterChanged(_ index: Int) {85        if index == 1 { depth.target = values[1] } else if index == 2 { feedback.target = values[2] }86    }8788    public override func reset() { state.update(repeating: 0, count: 2 * Self.stages); lastL = 0; lastR = 0; phase = 0 }8990    public override func processWet(_ l: UnsafeMutablePointer<Float>, _ r: UnsafeMutablePointer<Float>, _ n: Int) {91        let inc = values[0] / sampleRate, zl = state, zr = state + Self.stages92        var fb = feedback, aL = coefL, aR = coefR, yl = lastL, yr = lastR, i = 093        while i < n { // coefficients are evaluated every 16 samples and interpolated linearly in between94            let c = min(16, n - i)95            phase += inc * Float(c); if phase >= 1 { phase -= floorf(phase) }96            let d = depth.advance(c)97            let stepL = (coefficient(phase, d) - aL) / Float(c), stepR = (coefficient(phase + 0.25, d) - aR) / Float(c)98            for k in i ..< i + c {99                aL += stepL; aR += stepR100                let f = fb.next()101                var xl = sane(l[k]) + f * 2 * softClip(yl * 0.5), xr = sane(r[k]) + f * 2 * softClip(yr * 0.5)102                for s in 0 ..< Self.stages {103                    let ol = aL * xl + zl[s]; zl[s] = xl - aL * ol; xl = ol104                    let or = aR * xr + zr[s]; zr[s] = xr - aR * or; xr = or105                }106                yl = flush(xl); yr = flush(xr)107                l[k] = yl; r[k] = yr108            }109            i += c110        }111        for s in 0 ..< 2 * Self.stages { state[s] = flush(state[s]) }112        feedback = fb; coefL = aL; coefR = aR; lastL = yl; lastR = yr113    }114}115116// MARK: - Chorus117118/// Three modulated taps (9, 14 and 20 ms) a third of a cycle apart; the right channel reads a quarter cycle later.119/// Above 1 Hz the excursion shrinks with the rate so the pitch deviation stays musical instead of turning into vibrato.120public final class ChorusEffect: EffectUnit, @unchecked Sendable {121    private let lineL: DelayLine, lineR: DelayLine122    private var phase: Float = 0123    private var excursion: OnePoleSmoother // samples124125    public init(sampleRate: Double) {126        lineL = DelayLine(minimumSamples: Int(0.05 * sampleRate)); lineR = DelayLine(minimumSamples: Int(0.05 * sampleRate))127        excursion = OnePoleSmoother(timeConstantMS: 40, sampleRate: Float(sampleRate))128        super.init(kind: .chorus, sampleRate: sampleRate)129        parameterChanged(0); excursion.snap(excursion.target)130    }131132    public override func parameterChanged(_: Int) {133        excursion.target = values[1] * 0.0035 * sampleRate * min(1, 1 / max(values[0], 0.01))134    }135136    /// The short lines keep recording while bypassed, so the taps already hold the live signal when the unit fades in.137    public override func observeBypassed(_ l: UnsafeMutablePointer<Float>, _ r: UnsafeMutablePointer<Float>, _ n: Int) {138        for i in 0 ..< n { lineL.write(sane(l[i])); lineR.write(sane(r[i])) }139    }140141    public override func reset() { lineL.reset(); lineR.reset(); phase = 0 }142143    public override func processWet(_ l: UnsafeMutablePointer<Float>, _ r: UnsafeMutablePointer<Float>, _ n: Int) {144        let inc = values[0] / sampleRate, twoPi = 2 * Float.pi, third: Float = 1 / 3145        let b0 = 0.009 * sampleRate, b1 = 0.014 * sampleRate, b2 = 0.020 * sampleRate, gain: Float = 0.57735146        var ph = phase, ex = excursion147        for i in 0 ..< n {148            let e = ex.next(), a = twoPi * ph149            lineL.write(sane(l[i])); lineR.write(sane(r[i]))150            let sl = lineL.read(delaySamples: b0 + e * sinf(a)) + lineL.read(delaySamples: b1 + e * sinf(a + twoPi * third))151                + lineL.read(delaySamples: b2 + e * sinf(a + 2 * twoPi * third))152            let q = a + 0.5 * .pi153            let sr = lineR.read(delaySamples: b0 + e * sinf(q)) + lineR.read(delaySamples: b1 + e * sinf(q + twoPi * third))154                + lineR.read(delaySamples: b2 + e * sinf(q + 2 * twoPi * third))155            l[i] = sl * gain; r[i] = sr * gain156            ph += inc; if ph >= 1 { ph -= 1 }157        }158        phase = ph; excursion = ex159    }160}161
Files main
Menu

Theme