Repository Public License
1// ORIX Arcade2// File: AudioHaptics/Synth.swift3// Description: Procedural audio synthesis — every sound effect and music loop is rendered in code, so the app ships no audio files.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation89nonisolated enum Synth {10 static let sampleRate: Double = 44_1001112 enum Wave: Sendable { case sine, triangle, square, saw }1314 static func samples(_ seconds: Double) -> Int { Int(seconds * sampleRate) }1516 @inline(__always)17 private static func osc(_ wave: Wave, _ phase: Double) -> Double {18 let p = phase - phase.rounded(.down)19 switch wave {20 case .sine: return sin(2 * .pi * p)21 case .triangle: return 4 * abs(p - 0.5) - 122 case .square: return tanh(4 * sin(2 * .pi * p))23 case .saw: return 2 * p - 124 }25 }2627 /// Adds an enveloped tone. `f1` glides the pitch exponentially; `wrap` folds the tail around the buffer (seamless loops).28 static func tone(_ b: inout [Float], at start: Double, dur: Double, f0: Double, f1: Double? = nil, wave: Wave = .sine,29 amp: Double, attack: Double = 0.004, curve: Double = 4, wrap: Bool = false) {30 let n = samples(dur), s0 = samples(start), count = b.count31 guard n > 0, count > 0 else { return }32 var phase = 0.033 let ratio = (f1 ?? f0) / f034 let release = min(0.012, dur * 0.25)35 for i in 0..<n {36 let t = Double(i) / sampleRate37 let x = t / dur38 let f = ratio == 1 ? f0 : f0 * pow(ratio, x)39 phase += f / sampleRate40 var env = min(1, t / attack) * exp(-curve * x)41 let left = dur - t42 if left < release { env *= left / release }43 var index = s0 + i44 if index >= count { if wrap { index %= count } else { break } }45 b[index] += Float(osc(wave, phase) * env * amp)46 }47 }4849 /// Adds enveloped filtered noise. `lowpass` in (0, 1]: 1 is white, small values are dark. `highpass` removes the rumble.50 static func noise(_ b: inout [Float], at start: Double, dur: Double, amp: Double, lowpass: Double = 1, highpass: Bool = false,51 attack: Double = 0.002, curve: Double = 5, seed: UInt64 = 7, wrap: Bool = false) {52 let n = samples(dur), s0 = samples(start), count = b.count53 guard n > 0, count > 0 else { return }54 var rng = SeededRNG(seed: seed)55 var lp = 0.056 for i in 0..<n {57 let t = Double(i) / sampleRate58 let white = rng.unit() * 2 - 159 lp += lowpass * (white - lp)60 let v = highpass ? white - lp : lp61 let env = min(1, t / attack) * exp(-curve * t / dur)62 var index = s0 + i63 if index >= count { if wrap { index %= count } else { break } }64 b[index] += Float(v * env * amp)65 }66 }6768 static func finish(_ b: inout [Float], peak: Float = 0.85) {69 var maxValue: Float = 0.000170 for i in b.indices {71 b[i] = tanh(b[i] * 1.2)72 maxValue = max(maxValue, abs(b[i]))73 }74 let gain = min(peak / maxValue, 3)75 for i in b.indices { b[i] *= gain }76 }7778 static func midi(_ note: Double) -> Double { 440 * pow(2, (note - 69) / 12) }7980 // MARK: Sound effects8182 static func effect(_ cue: AudioDirector.Cue) -> [Float] {83 var b: [Float]84 switch cue {85 case .tap:86 b = .init(repeating: 0, count: samples(0.07))87 tone(&b, at: 0, dur: 0.06, f0: 920, f1: 640, amp: 0.5, curve: 5)88 case .confirm:89 b = .init(repeating: 0, count: samples(0.26))90 tone(&b, at: 0, dur: 0.12, f0: midi(76), wave: .triangle, amp: 0.5)91 tone(&b, at: 0.08, dur: 0.18, f0: midi(83), wave: .triangle, amp: 0.5)92 case .back:93 b = .init(repeating: 0, count: samples(0.16))94 tone(&b, at: 0, dur: 0.15, f0: 620, f1: 380, wave: .triangle, amp: 0.45)95 case .coin:96 b = .init(repeating: 0, count: samples(0.34))97 tone(&b, at: 0, dur: 0.08, f0: midi(88), amp: 0.4)98 tone(&b, at: 0.06, dur: 0.28, f0: midi(93), amp: 0.45, curve: 5)99 tone(&b, at: 0.06, dur: 0.2, f0: midi(105), amp: 0.12, curve: 7)100 case .star:101 b = .init(repeating: 0, count: samples(0.6))102 for (i, n) in [79.0, 83, 86, 91].enumerated() {103 tone(&b, at: Double(i) * 0.07, dur: 0.36, f0: midi(n), amp: 0.32, curve: 5)104 tone(&b, at: Double(i) * 0.07, dur: 0.2, f0: midi(n + 12), amp: 0.1, curve: 7)105 }106 case .win:107 b = .init(repeating: 0, count: samples(1.3))108 for (i, n) in [72.0, 76, 79, 84, 88].enumerated() {109 tone(&b, at: Double(i) * 0.11, dur: 0.5, f0: midi(n), wave: .triangle, amp: 0.36, curve: 3.5)110 }111 for n in [72.0, 79, 84, 88] { tone(&b, at: 0.6, dur: 0.7, f0: midi(n), amp: 0.2, curve: 3) }112 case .record:113 b = .init(repeating: 0, count: samples(1.6))114 for (i, n) in [72.0, 76, 79, 84, 79, 84, 88, 91].enumerated() {115 tone(&b, at: Double(i) * 0.09, dur: 0.42, f0: midi(n), wave: .triangle, amp: 0.32, curve: 3.5)116 tone(&b, at: Double(i) * 0.09, dur: 0.2, f0: midi(n + 12), amp: 0.1, curve: 6)117 }118 for n in [60.0, 72, 79, 84, 88, 91] { tone(&b, at: 0.8, dur: 0.8, f0: midi(n), amp: 0.16, curve: 2.6) }119 case .fail:120 b = .init(repeating: 0, count: samples(0.8))121 for (i, n) in [67.0, 63, 60, 55].enumerated() {122 tone(&b, at: Double(i) * 0.13, dur: 0.4, f0: midi(n), wave: .triangle, amp: 0.36, curve: 3)123 }124 tone(&b, at: 0.4, dur: 0.4, f0: midi(43), amp: 0.3, curve: 2.5)125 case .knock:126 b = .init(repeating: 0, count: samples(0.1))127 noise(&b, at: 0, dur: 0.05, amp: 0.7, lowpass: 0.35, curve: 7)128 tone(&b, at: 0, dur: 0.08, f0: 330, f1: 190, wave: .triangle, amp: 0.5, attack: 0.001, curve: 6)129 case .thud:130 b = .init(repeating: 0, count: samples(0.22))131 tone(&b, at: 0, dur: 0.2, f0: 140, f1: 60, amp: 0.8, attack: 0.001, curve: 5)132 noise(&b, at: 0, dur: 0.08, amp: 0.35, lowpass: 0.12, curve: 6)133 case .crash:134 b = .init(repeating: 0, count: samples(0.75))135 noise(&b, at: 0, dur: 0.7, amp: 0.8, lowpass: 0.5, curve: 5, seed: 11)136 noise(&b, at: 0.03, dur: 0.3, amp: 0.4, lowpass: 1, highpass: true, curve: 7, seed: 5)137 tone(&b, at: 0, dur: 0.35, f0: 180, f1: 50, amp: 0.7, attack: 0.001, curve: 4)138 case .boom:139 b = .init(repeating: 0, count: samples(0.95))140 tone(&b, at: 0, dur: 0.9, f0: 120, f1: 34, amp: 0.95, attack: 0.002, curve: 3.2)141 noise(&b, at: 0, dur: 0.6, amp: 0.5, lowpass: 0.08, curve: 4, seed: 3)142 case .whoosh:143 b = .init(repeating: 0, count: samples(0.36))144 noise(&b, at: 0, dur: 0.34, amp: 0.55, lowpass: 0.22, highpass: false, attack: 0.12, curve: 3, seed: 19)145 tone(&b, at: 0, dur: 0.3, f0: 300, f1: 900, amp: 0.1, attack: 0.1, curve: 3)146 case .launch:147 b = .init(repeating: 0, count: samples(0.26))148 tone(&b, at: 0, dur: 0.22, f0: 240, f1: 980, wave: .triangle, amp: 0.45, attack: 0.01, curve: 3)149 noise(&b, at: 0, dur: 0.18, amp: 0.25, lowpass: 0.4, highpass: true, curve: 5)150 case .horn:151 b = .init(repeating: 0, count: samples(0.3))152 tone(&b, at: 0, dur: 0.28, f0: 392, wave: .square, amp: 0.22, attack: 0.01, curve: 1.2)153 tone(&b, at: 0, dur: 0.28, f0: 494, wave: .square, amp: 0.2, attack: 0.01, curve: 1.2)154 case .siren:155 b = .init(repeating: 0, count: samples(0.7))156 tone(&b, at: 0, dur: 0.34, f0: 660, f1: 990, amp: 0.34, attack: 0.02, curve: 0.8)157 tone(&b, at: 0.34, dur: 0.34, f0: 990, f1: 660, amp: 0.34, attack: 0.02, curve: 0.8)158 case .tick:159 b = .init(repeating: 0, count: samples(0.035))160 tone(&b, at: 0, dur: 0.03, f0: 1500, amp: 0.4, attack: 0.001, curve: 6)161 case .pass:162 b = .init(repeating: 0, count: samples(0.2))163 tone(&b, at: 0, dur: 0.18, f0: midi(81), wave: .triangle, amp: 0.4, curve: 5)164 tone(&b, at: 0, dur: 0.1, f0: midi(93), amp: 0.1, curve: 7)165 case .combo:166 b = .init(repeating: 0, count: samples(0.24))167 tone(&b, at: 0, dur: 0.1, f0: midi(84), wave: .triangle, amp: 0.36, curve: 5)168 tone(&b, at: 0.05, dur: 0.18, f0: midi(91), wave: .triangle, amp: 0.36, curve: 5)169 case .upgrade:170 b = .init(repeating: 0, count: samples(0.75))171 tone(&b, at: 0, dur: 0.3, f0: 330, f1: 1320, wave: .triangle, amp: 0.3, attack: 0.02, curve: 1.5)172 for n in [84.0, 88, 91] { tone(&b, at: 0.28, dur: 0.45, f0: midi(n), amp: 0.24, curve: 4) }173 case .warning:174 b = .init(repeating: 0, count: samples(0.34))175 tone(&b, at: 0, dur: 0.12, f0: 880, wave: .square, amp: 0.2, curve: 1.5)176 tone(&b, at: 0.17, dur: 0.12, f0: 880, wave: .square, amp: 0.2, curve: 1.5)177 case .drill:178 b = .init(repeating: 0, count: samples(0.14))179 noise(&b, at: 0, dur: 0.13, amp: 0.5, lowpass: 0.18, attack: 0.01, curve: 2.5, seed: 23)180 tone(&b, at: 0, dur: 0.13, f0: 92, wave: .saw, amp: 0.22, attack: 0.01, curve: 2)181 case .crumble:182 b = .init(repeating: 0, count: samples(0.2))183 noise(&b, at: 0, dur: 0.18, amp: 0.6, lowpass: 0.3, curve: 5, seed: 29)184 tone(&b, at: 0, dur: 0.07, f0: 210, f1: 120, amp: 0.3, attack: 0.001, curve: 6)185 case .hiss:186 b = .init(repeating: 0, count: samples(0.5))187 noise(&b, at: 0, dur: 0.48, amp: 0.5, lowpass: 0.7, highpass: true, attack: 0.02, curve: 4, seed: 31)188 case .gem:189 b = .init(repeating: 0, count: samples(0.5))190 for (i, n) in [91.0, 96, 100].enumerated() {191 tone(&b, at: Double(i) * 0.05, dur: 0.36, f0: midi(n), amp: 0.3, curve: 5)192 }193 case .engine:194 b = .init(repeating: 0, count: samples(0.3))195 tone(&b, at: 0, dur: 0.28, f0: 110, f1: 230, wave: .saw, amp: 0.22, attack: 0.02, curve: 2)196 noise(&b, at: 0, dur: 0.25, amp: 0.2, lowpass: 0.1, attack: 0.02, curve: 3, seed: 37)197 }198 finish(&b, peak: 0.8)199 return b200 }201202 // MARK: Music203204 private struct Song {205 var bpm: Double206 var root: Double207 /// Chord roots in semitones from `root`, one per two bars, with their triad quality.208 var chords: [(offset: Double, minor: Bool)]209 var arpWave: Wave210 var arpEvery: Double211 var arpAmp: Double212 var padAmp: Double213 var bassPattern: [Double]214 var kick: Bool215 var hat: Bool216 var seed: UInt64217 }218219 private static func song(_ music: AudioDirector.Music) -> Song {220 switch music {221 case .lobby:222 Song(bpm: 92, root: 57, chords: [(0, true), (-4, false), (3, false), (-2, false)], arpWave: .sine, arpEvery: 0.5, arpAmp: 0.16,223 padAmp: 0.11, bassPattern: [0, 2.5], kick: false, hat: false, seed: 1)224 case .domino:225 Song(bpm: 112, root: 60, chords: [(0, false), (5, false), (-3, true), (-5, false)], arpWave: .triangle, arpEvery: 0.5, arpAmp: 0.15,226 padAmp: 0.08, bassPattern: [0, 1.5, 2.5], kick: true, hat: true, seed: 2)227 case .dig:228 Song(bpm: 100, root: 50, chords: [(0, true), (5, false), (-2, false), (3, false)], arpWave: .sine, arpEvery: 0.75, arpAmp: 0.17,229 padAmp: 0.12, bassPattern: [0, 1.5, 3], kick: true, hat: false, seed: 3)230 case .traffic:231 Song(bpm: 124, root: 55, chords: [(0, false), (-3, true), (5, false), (7, false)], arpWave: .triangle, arpEvery: 0.5, arpAmp: 0.14,232 padAmp: 0.07, bassPattern: [0, 0.75, 1.5, 2.5, 3.25], kick: true, hat: true, seed: 4)233 }234 }235236 /// Renders a seamless 8-bar loop (mono).237 static func musicLoop(_ music: AudioDirector.Music) -> [Float] {238 let s = song(music)239 let beat = 60 / s.bpm240 let total = beat * 32241 var b = [Float](repeating: 0, count: samples(total))242 var rng = SeededRNG(seed: s.seed)243 for (index, chord) in s.chords.enumerated() {244 let start = Double(index) * beat * 8245 let third = chord.minor ? 3.0 : 4.0246 let tones = [0.0, third, 7.0, 12.0, 12 + third, 19.0]247 let base = s.root + chord.offset248 // Pad: slow swell, two slightly detuned sines per chord tone.249 for interval in [0.0, third, 7.0, 14.0] {250 let f = midi(base + interval)251 tone(&b, at: start, dur: beat * 8.6, f0: f, amp: s.padAmp, attack: 0.9, curve: 1.1, wrap: true)252 tone(&b, at: start, dur: beat * 8.6, f0: f * 1.006, amp: s.padAmp * 0.8, attack: 1.2, curve: 1.1, wrap: true)253 }254 for bar in 0..<2 {255 let barStart = start + Double(bar) * beat * 4256 for hit in s.bassPattern {257 let f = midi(base - 24)258 tone(&b, at: barStart + hit * beat, dur: beat * 0.9, f0: f, amp: 0.3, attack: 0.006, curve: 3, wrap: true)259 tone(&b, at: barStart + hit * beat, dur: beat * 0.5, f0: f * 2, wave: .triangle, amp: 0.07, curve: 4, wrap: true)260 }261 if s.kick {262 for k in [0.0, 2.0] {263 tone(&b, at: barStart + k * beat, dur: 0.16, f0: 115, f1: 44, amp: 0.36, attack: 0.001, curve: 4, wrap: true)264 }265 }266 if s.hat {267 for h in stride(from: 0.5, to: 4.0, by: 1.0) {268 noise(&b, at: barStart + h * beat, dur: 0.045, amp: 0.07, lowpass: 0.9, highpass: true, curve: 6, seed: 41, wrap: true)269 }270 }271 var step = 0.0272 var cursor = rng.int(in: 0...2)273 while step < 4 {274 if !rng.chance(0.22) {275 cursor = (cursor + rng.pick([1, 1, 2, -1]) + tones.count) % tones.count276 let f = midi(base + 12 + tones[cursor])277 tone(&b, at: barStart + step * beat, dur: beat * 1.3, f0: f, wave: s.arpWave, amp: s.arpAmp, attack: 0.005, curve: 5, wrap: true)278 }279 step += s.arpEvery280 }281 }282 }283 finish(&b, peak: 0.7)284 return b285 }286}287