Repository Public
1// ORIX DJ2// File: Packages/ORIXDJCore/Sources/ORIXDSP/Effects/Distortion.swift3// Description: Distortion effects: level-compensated soft-clip overdrive and bitcrusher.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation89// MARK: - Distortion1011/// y = tanh(g x) * 0.5 / tanh(0.5 g), then a one-pole tone low-pass (no overshoot).12/// The compensation keeps a -6 dBFS peak at -6 dBFS for every drive, so the output peak of any signal at or above13/// that level never grows with drive; quieter material is pulled up towards that ceiling (which is what an overdrive does).14public final class DistortionEffect: EffectUnit, @unchecked Sendable {15 private var drive: OnePoleSmoother // linear gain16 private var tone: OnePoleSmoother // one-pole coefficient17 private var lpL: Float = 0, lpR: Float = 01819 public init(sampleRate: Double) {20 drive = OnePoleSmoother(timeConstantMS: 20, sampleRate: Float(sampleRate))21 tone = OnePoleSmoother(timeConstantMS: 20, sampleRate: Float(sampleRate))22 super.init(kind: .distortion, sampleRate: sampleRate)23 parameterChanged(0); parameterChanged(1)24 drive.snap(drive.target); tone.snap(tone.target)25 }2627 public override func parameterChanged(_ index: Int) {28 if index == 0 { drive.target = powf(10, values[0] / 20) } else { tone.target = 1 - expf(-2 * .pi * min(values[1], 0.45 * sampleRate) / sampleRate) }29 }3031 public override func reset() { lpL = 0; lpR = 0 }3233 public override func processWet(_ l: UnsafeMutablePointer<Float>, _ r: UnsafeMutablePointer<Float>, _ n: Int) {34 var dr = drive, tn = tone, sl = lpL, sr = lpR35 for i in 0 ..< n {36 let g = dr.next(), a = tn.next(), comp = 0.5 / tanhf(0.5 * g)37 sl += a * (tanhf(g * sane(l[i])) * comp - sl)38 sr += a * (tanhf(g * sane(r[i])) * comp - sr)39 l[i] = sl; r[i] = sr40 }41 drive = dr; tone = tn; lpL = flush(sl); lpR = flush(sr)42 }43}4445// MARK: - Bitcrusher4647/// Mid-tread quantiser with exactly 2^bits codes (two's-complement style: -1 ... 1 - step, silence stays silent)48/// after an integer sample-and-hold. Both parameters are deliberately unsmoothed: the effect is a staircase by nature.49public final class BitcrusherEffect: EffectUnit, @unchecked Sendable {50 private var counter = 051 private var heldL: Float = 0, heldR: Float = 05253 public init(sampleRate: Double) { super.init(kind: .bitcrusher, sampleRate: sampleRate) }5455 public override func reset() { counter = 0; heldL = 0; heldR = 0 }5657 public override func processWet(_ l: UnsafeMutablePointer<Float>, _ r: UnsafeMutablePointer<Float>, _ n: Int) {58 let half = powf(2, values[0] - 1), inv = 1 / half59 let lo = (-half).rounded(.up), hi = (half - 1).rounded(.down)60 let factor = max(1, Int(values[1].rounded()))61 var c = counter, hl = heldL, hr = heldR62 for i in 0 ..< n {63 if c <= 0 {64 hl = clampf((sane(l[i]) * half).rounded(), lo, hi) * inv65 hr = clampf((sane(r[i]) * half).rounded(), lo, hi) * inv66 c = factor67 }68 c -= 169 l[i] = hl; r[i] = hr70 }71 counter = c; heldL = hl; heldR = hr72 }73}74