Repository Public
1// ORIX DJ2// File: App/Sources/Features/MIDI/MIDIDemo.swift3// Description: DEBUG-only launch option `-ORIXMIDIDemo YES` for screenshots and UI tests without hardware: creates a4// real CoreMIDI virtual source in this process, sends a few messages through the system (they come back5// through the normal input path) and stores a small example profile. Not compiled in release builds.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9#if DEBUG10import CoreMIDI11import ORIXDJCore12import SwiftUI1314@MainActor15enum MIDIDemo {16 private static var client = MIDIClientRef()17 private static var source = MIDIEndpointRef()18 private static var started = false1920 static func runIfRequested(model: AppModel, controller: MIDIController) {21 guard UserDefaults.standard.bool(forKey: "ORIXMIDIDemo"), !started else { return }22 started = true23 guard MIDIClientCreateWithBlock("ORIX DJ Demo" as CFString, &client, nil) == noErr,24 MIDISourceCreateWithProtocol(client, "ORIX Virtual Controller" as CFString, ._1_0, &source) == noErr else { return }2526 // Example profile. The values sent below leave the mix as it is: the fader ends fully open, the crossfader27 // stays centred, the encoder steps cancel out and the toggle is pressed twice.28 let profile = MIDIMappingProfile(name: "Virtual Controller (example)", controllerNameHint: "ORIX Virtual Controller", mappings: [29 MIDIMapping(control: .cc(channel: 0, controller: 7), target: "deck.A.volume", mode: .absolute, softTakeover: true),30 MIDIMapping(control: .cc(channel: 0, controller: 8), target: "deck.B.volume", mode: .absolute, softTakeover: true),31 MIDIMapping(control: .cc(channel: 0, controller: 16), target: "mixer.crossfader", mode: .absolute),32 MIDIMapping(control: .cc(channel: 0, controller: 22), target: "deck.A.filter", mode: .relative, sensitivity: 2),33 MIDIMapping(control: .note(channel: 9, note: 36), target: "deck.A.play", mode: .trigger),34 MIDIMapping(control: .note(channel: 9, note: 37), target: "deck.A.keylock", mode: .toggle),35 MIDIMapping(control: .note(channel: 9, note: 38), target: "deck.B.cueMonitor", mode: .momentary),36 ])37 try? model.session.saveMIDIProfile(profile, activate: true)38 controller.reloadProfiles(model: model)39 if let stored = controller.profiles.first(where: { $0.name == profile.name }) { controller.select(stored) }4041 let messages: [MIDIMessage] = [42 .controlChange(channel: 0, controller: 7, value: 118), .controlChange(channel: 0, controller: 7, value: 123),43 .controlChange(channel: 0, controller: 7, value: 127), .controlChange(channel: 0, controller: 16, value: 64),44 .controlChange(channel: 0, controller: 22, value: 1), .controlChange(channel: 0, controller: 22, value: 127),45 .noteOn(channel: 9, note: 37, velocity: 100), .noteOff(channel: 9, note: 37, velocity: 0),46 .noteOn(channel: 9, note: 37, velocity: 100), .noteOff(channel: 9, note: 37, velocity: 0),47 .pitchBend(channel: 0, value: 8192 + 1200), .pitchBend(channel: 0, value: 8192), .programChange(channel: 0, program: 5),48 ]49 Task {50 // Give CoreMIDI time to announce the new source and the manager time to connect to it.51 try? await Task.sleep(nanoseconds: 1_200_000_000)52 model.session.midi.rescan()53 model.refreshNow()54 try? await Task.sleep(nanoseconds: 400_000_000)55 for message in messages {56 send(message)57 try? await Task.sleep(nanoseconds: 90_000_000)58 }59 }60 }6162 private static func send(_ message: MIDIMessage) {63 var list = MIDIEventList()64 let packet = MIDIEventListInit(&list, ._1_0)65 var word = message.umpWord66 _ = MIDIEventListAdd(&list, MemoryLayout<MIDIEventList>.size, packet, 0, 1, &word)67 MIDIReceivedEventList(source, &list)68 }69}70#endif71