SPB Git

Repository Public

Benchmarks.swift 16.8 KB 309 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX DJ9// File             : Packages/ORIXDJCore/Sources/orix-dj-bench/Benchmarks.swift10// Description      : Offline benchmarks of the engine. Run from a release11//                    build: swift run -c release orix-dj-bench bench12// Module           : orix-dj-bench13// Repository       : https://git.spboucher.ai/orix-dj14// Created          : 2026-11-2115// Last Modified    : 2026-11-2116//17// =============================================================================18// AUTHOR & PROFESSIONAL AFFILIATION19// =============================================================================20//21// Author           : Simon-Pierre Boucher22// Position         : Professor of Finance23// Institution      : Université du Québec en Outaouais (UQO)24// Department       : Département des sciences administratives25// Organization     : SP Boucher26//27// Website          : https://spboucher.ai28// Git              : https://git.spboucher.ai29//30// =============================================================================31// OFFICIAL CONTACT DIRECTORY32// =============================================================================33//34// General          : info@spboucher.ai35// Professional     : contact@spboucher.ai36// Direct           : simon@spboucher.ai37// Research         : research@spboucher.ai38// Development      : dev@spboucher.ai39// Support          : support@spboucher.ai40// Security/Admin   : admin@spboucher.ai41// Notifications    : notifications@spboucher.ai42//43// Preferred Contact: support@spboucher.ai44//45// =============================================================================46// COPYRIGHT & INTELLECTUAL PROPERTY47// =============================================================================48//49// Copyright (c) 202650// Simon-Pierre Boucher51//52// All rights reserved where applicable.53//54// This file is governed by the applicable project license,55// copyright notices, institutional agreements and relevant law.56//57// Unless expressly authorized by the applicable license or58// rights holder, copying, redistribution, sublicensing,59// publication and commercial exploitation are not permitted.60//61// Existing third-party rights and notices remain unaffected.62//63// =============================================================================64// CONFIDENTIALITY & SECURITY65// =============================================================================66//67// Classification   : Public68// Distribution     : Public repository; use is governed by the project license69// Security Contact : admin@spboucher.ai70//71// Access to this source does not independently grant permission72// to redistribute or disclose confidential materials.73//74// Do not embed credentials, private keys, access tokens,75// certificates or confidential personal information.76//77// =============================================================================78// LICENSE & MAINTENANCE79// =============================================================================80//81// License          : None specified (no license file in this repository)82// Maintainer       : Simon-Pierre Boucher83// Technical Contact: dev@spboucher.ai84//85// =============================================================================86// END OF SP BOUCHER SOURCE HEADER87// =============================================================================8889import Foundation90import ORIXAnalysis91import ORIXDSP92import ORIXDJEngine93import ORIXScript9495func percentile(_ sorted: [Double], _ p: Double) -> Double { sorted.isEmpty ? 0 : sorted[min(sorted.count - 1, Int(Double(sorted.count) * p))] }9697func measureRender(label: String, buffer: Int, decks: Int, keylock: Bool, effects: Bool, seconds: Double) {98    var c = EngineConfiguration(); c.sampleRate = 48000; c.bufferFrames = buffer99    let e = DJEngine(configuration: c)100    for d in 0 ..< decks {101        let t = SignalSynth.track(.init(bpm: 122 + Double(d) * 2, seconds: seconds + 12, seed: UInt64(d + 1)), sampleRate: 44100)102        let b = TrackBuffer(left: t.left, right: t.right, sampleRate: 44100)103        _ = e.load(deck: d, buffer: b, info: LoadedTrack(title: "t\(d)", duration: b.duration, sampleRate: 44100, grid: BeatGrid(bpm: 122 + Double(d) * 2, firstBeatSeconds: 0)))104        e.set(.deck(d, "assign"), .number(2))105        e.set(.deck(d, "volume"), .gain(-9))106        if keylock { e.set(.deck(d, "keylock"), .bool(true)); e.set(.deck(d, "rate"), .number(1.04)) }107        e.set(.deck(d, "eq.low"), .gain(-4)); e.set(.deck(d, "filter"), .number(0.15))108        if effects { for k in [EffectKind.echo, .flanger, .compressor] { let s = e.insertEffect(k, rack: d); e.setEffectEnabled(rack: d, slot: s, true) } }109        e.play(deck: d)110    }111    if effects { for k in [EffectKind.reverb, .delay] { let s = e.insertEffect(k, rack: nil); e.setEffectEnabled(rack: nil, slot: s, true) } }112    _ = e.renderOffline(frames: 48000)113    // Time each block individually to get percentiles.114    var times: [Double] = []115    let blocks = Int(seconds * 48000) / buffer116    times.reserveCapacity(blocks)117    for _ in 0 ..< blocks {118        _ = e.renderOffline(frames: buffer)119        times.append(Double(e.renderState.lastRenderNanos) / 1000)120    }121    times.sort()122    let budget = Double(buffer) / 48000 * 1e6123    let mean = times.reduce(0, +) / Double(times.count)124    print(String(format: "%-46@ buffer %4d  mean %7.1f µs  p99 %7.1f µs  max %7.1f µs  budget %6.0f µs  load %5.2f %%  clipped %d",125                 label as NSString, buffer, mean, percentile(times, 0.99), times.last ?? 0, budget, mean / budget * 100, e.renderState.clippedBlocks))126}127128func measureStretch() {129    print("\nTime stretching (key lock), 440 Hz sine, 48 kHz")130    for rate in [0.84, 0.92, 0.96, 1.0, 1.04, 1.08, 1.16] {131        var c = EngineConfiguration(); c.sampleRate = 48000132        let e = DJEngine(configuration: c)133        e.set(.mixer("master.limiter"), .bool(false)); e.set(.mixer("curve"), .string("linear"))134        _ = e.renderOffline(frames: 4096)135        let sine = SignalSynth.sine(frequency: 440, seconds: 12, sampleRate: 48000, amplitude: 0.25)136        _ = e.load(deck: 0, buffer: TrackBuffer(left: sine, right: sine, sampleRate: 48000), info: LoadedTrack(title: "sine", duration: 12, sampleRate: 48000))137        e.set(.deck(0, "keylock"), .bool(true)); e.set(.deck(0, "rate"), .number(rate))138        e.play(deck: 0)139        let out = e.renderOffline(frames: 48000 * 8)[0]140        // Pitch from zero crossings; residual = everything that is not the 440 Hz fundamental.141        var first = -1.0, last = -1.0, count = 0142        for i in 48000 ..< out.count - 1 where out[i] < 0 && out[i + 1] >= 0 {143            let t = Double(i) + Double(-out[i]) / Double(out[i + 1] - out[i])144            if first < 0 { first = t } else { count += 1 }145            last = t146        }147        let f = Double(count) * 48000 / (last - first)148        var re = 0.0, im = 0.0, total = 0.0149        let n = out.count - 48000150        for i in 0 ..< n { let x = Double(out[48000 + i]); let w = 2 * Double.pi * f * Double(i) / 48000; re += x * cos(w); im += x * sin(w); total += x * x }151        let fundamental = 2 * (re * re + im * im) / Double(n)152        let residual = max(1e-12, total - fundamental)153        let cents = 1200 * log2(f / 440)154        print(String(format: "  rate %.2f  pitch %8.3f Hz (%+.2f cents)  residual %6.1f dB below the tone  playhead speed %.4f",155                     rate, f, cents, 10 * log10(fundamental / residual), e.deckStatus(0).position / 8))156    }157}158159func measureAnalysis() {160    print("\nAnalysis of a 5-minute 44.1 kHz stereo track")161    let t = SignalSynth.track(.init(bpm: 126, tonic: 7, mode: .minor, seconds: 300, seed: 5, offsetSeconds: 0.08), sampleRate: 44100)162    let start = Date()163    let r = try! TrackAnalyzer.analyze(left: t.left, right: t.right, sampleRate: 44100)164    print(String(format: "  %.3f s  bpm %.3f (truth 126)  first beat %.4f s (truth 0.0800)  key %@ (truth G minor)  %.1f LUFS",165                 Date().timeIntervalSince(start), r.grid?.bpm ?? 0, r.grid?.firstBeatSeconds ?? 0, (r.key?.name ?? "none") as NSString, r.integratedLUFS))166}167168func measureAutomation(seconds: Double) {169    170    var c = EngineConfiguration(); c.sampleRate = 48000; c.bufferFrames = 256171    let e = DJEngine(configuration: c)172    let host = AutomationHost(engine: e)173    print("\nAutomation scheduling (offline, 256-frame buffers, \(Int(host.lookaheadSeconds * 1000)) ms look-ahead)")174    let clicks = SignalSynth.clickTrack(bpm: 128, seconds: seconds + 20, sampleRate: 48000)175    _ = e.load(deck: 0, buffer: TrackBuffer(left: clicks, right: clicks, sampleRate: 48000), info: LoadedTrack(title: "clicks", duration: seconds + 20, sampleRate: 48000, grid: BeatGrid(bpm: 128, firstBeatSeconds: 0)))176    e.play(deck: 0)177    _ = host.renderOffline(frames: 48000)178    // Sixteen commands on every beat plus continuous ramps, for the whole run.179    let source = """180    automation "Stress" quantize bar {181        fx.delay.enable()182        every 1beat for \(Int(seconds * 128 / 60))beats {183            repeat 4 {184                deck.A.eq.high = -3dB185                deck.A.eq.high = 0dB186                fx.delay.feedback = 0.3187                fx.delay.feedback = 0.4188            }189        }190    }191    automation "Ramps" {192        repeat \(Int(seconds * 128 / 60 / 8)) {193            over 4beats { deck.A.filter.highpass -> 4kHz194                mixer.crossfader -> 0.2195                deck.A.volume -> -12dB }196            over 4beats { deck.A.filter.highpass -> 20Hz197                mixer.crossfader -> 0.5198                deck.A.volume -> 0dB }199        }200    }201    """202    let program = ScriptCompiler.compile(source).program!203    host.start(program, automation: "Stress"); host.start(program, automation: "Ramps")204    host.resetStats()205    _ = host.renderOffline(frames: Int(seconds * 48000))206    let s = host.stats207    print(String(format: "  ticks %d  tick mean %.1f µs  max %.1f µs", s.ticks, s.averageTickMicros, s.maxTickMicros))208    print("  timed commands \(s.trackedEvents)  late \(s.lateEvents)  worst lateness \(s.maxLateSamples) samples  dropped control events \(s.droppedControlEvents)  runtime errors \(s.runtimeErrors)")209}210211func runBenchmarks(seconds: Double) {212    #if DEBUG213    print("WARNING: debug build. Use: swift run -c release orix-dj-bench bench")214    #endif215    var info = utsname(); uname(&info)216    var size = 0; sysctlbyname("machdep.cpu.brand_string", nil, &size, nil, 0)217    var brand = [CChar](repeating: 0, count: size); sysctlbyname("machdep.cpu.brand_string", &brand, &size, nil, 0)218    print("ORIX DJ engine benchmark — \(String(cString: brand)), macOS \(ProcessInfo.processInfo.operatingSystemVersionString), \(seconds) s of audio per case, single render thread, 48 kHz\n")219    for buffer in [64, 128, 256, 512] { measureRender(label: "2 decks, EQ + filter", buffer: buffer, decks: 2, keylock: false, effects: false, seconds: seconds) }220    measureRender(label: "2 decks, key lock", buffer: 256, decks: 2, keylock: true, effects: false, seconds: seconds)221    measureRender(label: "4 decks, EQ + filter", buffer: 256, decks: 4, keylock: false, effects: false, seconds: seconds)222    measureRender(label: "4 decks, key lock, 14 effects, limiter", buffer: 256, decks: 4, keylock: true, effects: true, seconds: seconds)223    measureRender(label: "4 decks, key lock, 14 effects, limiter", buffer: 64, decks: 4, keylock: true, effects: true, seconds: seconds)224    print("\nEffects, one unit, 512-frame blocks")225    for kind in EffectKind.allCases {226        let u = EffectFactory.make(kind, sampleRate: 48000)227        u.setEnabled(true)228        let l = UnsafeMutablePointer<Float>.allocate(capacity: 512), r = UnsafeMutablePointer<Float>.allocate(capacity: 512)229        var noise = NoiseGenerator(seed: 7)230        let start = DispatchTime.now().uptimeNanoseconds231        let blocks = 4000232        for b in 0 ..< blocks {233            for i in 0 ..< 512 { l[i] = noise.next() * 0.3; r[i] = l[i] }234            u.setTransport(bpm: 124, beatPosition: Double(b) * 512 / 48000 * 124 / 60)235            u.render(l, r, 512)236        }237        let perBlock = Double(DispatchTime.now().uptimeNanoseconds - start) / Double(blocks) / 1000238        print(String(format: "  %-12@ %6.2f µs per block  %.3f %% of real time", kind.rawValue as NSString, perBlock, perBlock / (512.0 / 48000 * 1e6) * 100))239        l.deallocate(); r.deallocate()240    }241    measureStretch()242    measureAnalysis()243    measureAutomation(seconds: seconds)244}245246/// Live measurement on the real output device. The master level is at -60 dB (silence) so nothing is heard, but the247/// whole graph runs on Core Audio's real-time thread and the automation scheduler runs on its own thread.248func runLive(seconds: Double, buffer: Int) {249    var c = EngineConfiguration(); c.bufferFrames = buffer250    let e = DJEngine(configuration: c)251    let host = AutomationHost(engine: e)252    e.set(.mixer("master.volume"), .gain(-60))253    do { try e.start() } catch { print("could not open the audio device: \(error.localizedDescription)"); exit(1) }254    guard let d = e.currentDevice else { print("no device"); exit(1) }255    let sr = e.core.sampleRate256    print("device: \(d.name) [\(d.transport)], \(Int(sr)) Hz, buffer \(d.bufferFrames) frames (\(String(format: "%.2f", Double(d.bufferFrames) / sr * 1000)) ms), reported device latency \(d.latencyFrames) frames (\(String(format: "%.2f", Double(d.latencyFrames) / sr * 1000)) ms), output latency incl. buffer \(String(format: "%.2f", Double(d.latencyFrames + d.bufferFrames) / sr * 1000)) ms")257    for deck in 0 ..< 4 {258        let t = SignalSynth.track(.init(bpm: 124, seconds: seconds + 20, seed: UInt64(deck + 1)), sampleRate: 44100)259        let b = TrackBuffer(left: t.left, right: t.right, sampleRate: 44100)260        _ = e.load(deck: deck, buffer: b, info: LoadedTrack(title: "t\(deck)", duration: b.duration, sampleRate: 44100, grid: BeatGrid(bpm: 124, firstBeatSeconds: 0)))261        e.set(.deck(deck, "assign"), .number(2)); e.set(.deck(deck, "volume"), .gain(-9))262        e.set(.deck(deck, "keylock"), .bool(true)); e.set(.deck(deck, "rate"), .number(1.03))263        for k in [EffectKind.echo, .flanger, .compressor] { let s = e.insertEffect(k, rack: deck); e.setEffectEnabled(rack: deck, slot: s, true) }264        e.play(deck: deck)265    }266    for k in [EffectKind.reverb, .delay] { let s = e.insertEffect(k, rack: nil); e.setEffectEnabled(rack: nil, slot: s, true) }267    host.startScheduler()268    Thread.sleep(forTimeInterval: 1.5)269    let beats = Int(seconds * 124 * 1.03 / 60) - 8270    let source = """271    automation "Live stress" quantize bar {272        every 1beat for \(beats)beats {273            repeat 4 {274                deck.A.eq.high = -3dB275                deck.A.eq.high = 0dB276                deck.B.eq.low = -3dB277                deck.B.eq.low = 0dB278            }279        }280    }281    automation "Live ramps" {282        repeat \(beats / 8) {283            over 4beats { deck.A.filter.highpass -> 4kHz284                mixer.crossfader -> 0.2 }285            over 4beats { deck.A.filter.highpass -> 20Hz286                mixer.crossfader -> 0.5 }287        }288    }289    """290    let program = ScriptCompiler.compile(source).program!291    e.core.resetCountersForBenchmark()292    host.resetStats()293    host.start(program, automation: "Live stress"); host.start(program, automation: "Live ramps")294    // Sample the last-render time ten times a second for a distribution.295    var samples: [Double] = []296    let end = Date().addingTimeInterval(seconds)297    while Date() < end { samples.append(Double(e.renderState.lastRenderNanos) / 1000); Thread.sleep(forTimeInterval: 0.1) }298    let r = e.renderState, s = host.stats299    samples.sort()300    let budget = Double(d.bufferFrames) / sr * 1e6301    let mean = Double(r.totalRenderNanos) / Double(max(1, r.renderCalls)) / 1000302    print(String(format: "render (real-time thread, 4 decks with key lock, 14 effects, limiter): %d callbacks, mean %.1f µs, sampled p99 %.1f µs, max %.1f µs, budget %.0f µs, mean load %.2f %%, overloads %d",303                 r.renderCalls, mean, percentile(samples, 0.99), Double(r.maxRenderNanos) / 1000, budget, mean / budget * 100, r.overloads))304    print(String(format: "scheduler: %d ticks, tick mean %.1f µs, max %.1f µs, largest gap between ticks %.2f ms (nominal 4 ms)", s.ticks, s.averageTickMicros, s.maxTickMicros, s.maxTickGapMillis))305    print(String(format: "automation: %d timed commands, %d late, worst lateness %d samples (%.3f ms), mean lateness %.3f samples, dropped control events %d, runtime errors %d, automations still running %d",306                 s.trackedEvents, s.lateEvents, s.maxLateSamples, Double(s.maxLateSamples) / sr * 1000, s.meanLateSamples, s.droppedControlEvents, s.runtimeErrors, s.activeAutomations))307    host.stopScheduler(); e.stop()308}309
Files main
Menu

Theme