Repository Public
1// ORIX DJ2// File: Packages/ORIXDJCore/Tests/ORIXMIDITests/MIDIMappingEngineTests.swift3// Description: Mapping engine tests: every mode, relative encodings, invert, range, soft takeover, device scope.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai78import Foundation9import Testing10@testable import ORIXMIDI1112private func engine(_ mappings: MIDIMapping...) -> MIDIMappingEngine {13 MIDIMappingEngine(profile: MIDIMappingProfile(name: "test", mappings: mappings))14}1516private func value(_ events: [MIDITargetEvent]) -> Double? {17 guard events.count == 1, case .value(_, let v) = events[0] else { return nil }18 return v19}2021private func cc(_ controller: UInt8, _ value: UInt8, channel: UInt8 = 0) -> MIDIMessage {22 .controlChange(channel: channel, controller: controller, value: value)23}2425private func near(_ a: Double?, _ b: Double, _ eps: Double = 1e-9) -> Bool {26 guard let a else { return false }27 return abs(a - b) <= eps28}2930@Suite struct MIDIMappingEngineTests {31 @Test func noProfileOrNoMatchProducesNothing() {32 var empty = MIDIMappingEngine()33 #expect(empty.process(cc(1, 2), deviceUniqueID: 1).isEmpty)34 var e = engine(MIDIMapping(control: .cc(channel: 0, controller: 7), target: "t", mode: .absolute))35 #expect(e.process(cc(8, 2), deviceUniqueID: 1).isEmpty)36 #expect(e.process(cc(7, 2, channel: 1), deviceUniqueID: 1).isEmpty)37 #expect(e.process(.noteOn(channel: 0, note: 7, velocity: 2), deviceUniqueID: 1).isEmpty)38 #expect(e.process(.programChange(channel: 0, program: 7), deviceUniqueID: 1).isEmpty)39 }4041 @Test func absoluteSevenBit() {42 var e = engine(MIDIMapping(control: .cc(channel: 0, controller: 7), target: "vol", mode: .absolute))43 expectEqual(e.process(cc(7, 0), deviceUniqueID: nil), [.value(target: "vol", normalized: 0)])44 expectEqual(e.process(cc(7, 64), deviceUniqueID: nil), [.value(target: "vol", normalized: 0.5)])45 expectEqual(e.process(cc(7, 127), deviceUniqueID: nil), [.value(target: "vol", normalized: 1)])46 // Monotonic over the whole range.47 var previous = -1.048 for raw: UInt8 in 0...127 {49 let v = value(e.process(cc(7, raw), deviceUniqueID: nil))!50 #expect(v > previous && v >= 0 && v <= 1)51 previous = v52 }53 }5455 @Test func absoluteFourteenBitPitchBend() {56 var e = engine(MIDIMapping(control: .pitchBend(channel: 1), target: "pitch", mode: .absolute))57 #expect(value(e.process(.pitchBend(channel: 1, value: 0), deviceUniqueID: nil)) == 0)58 #expect(value(e.process(.pitchBend(channel: 1, value: 8192), deviceUniqueID: nil)) == 0.5)59 #expect(value(e.process(.pitchBend(channel: 1, value: 16383), deviceUniqueID: nil)) == 1)60 let fine = value(e.process(.pitchBend(channel: 1, value: 8193), deviceUniqueID: nil))!61 #expect(fine > 0.5 && fine < 0.5001) // 14-bit resolution is kept62 #expect(e.process(.pitchBend(channel: 0, value: 0), deviceUniqueID: nil).isEmpty)63 }6465 @Test func absoluteRangeAndInvert() {66 var e = engine(67 MIDIMapping(control: .cc(channel: 0, controller: 1), target: "a", mode: .absolute, minValue: 0.2, maxValue: 0.8),68 MIDIMapping(control: .cc(channel: 0, controller: 2), target: "b", mode: .absolute, invert: true),69 MIDIMapping(control: .cc(channel: 0, controller: 3), target: "c", mode: .absolute, minValue: 0.25, maxValue: 0.75, invert: true),70 MIDIMapping(control: .cc(channel: 0, controller: 4), target: "d", mode: .absolute, minValue: 1, maxValue: 0)71 )72 #expect(near(value(e.process(cc(1, 0), deviceUniqueID: nil)), 0.2))73 #expect(near(value(e.process(cc(1, 64), deviceUniqueID: nil)), 0.5))74 #expect(near(value(e.process(cc(1, 127), deviceUniqueID: nil)), 0.8))75 #expect(near(value(e.process(cc(2, 0), deviceUniqueID: nil)), 1))76 #expect(near(value(e.process(cc(2, 127), deviceUniqueID: nil)), 0))77 #expect(near(value(e.process(cc(3, 0), deviceUniqueID: nil)), 0.75))78 #expect(near(value(e.process(cc(3, 127), deviceUniqueID: nil)), 0.25))79 #expect(near(value(e.process(cc(4, 0), deviceUniqueID: nil)), 1)) // reversed range80 #expect(near(value(e.process(cc(4, 127), deviceUniqueID: nil)), 0))81 }8283 @Test func absoluteOnNoteUsesVelocity() {84 var e = engine(MIDIMapping(control: .note(channel: 0, note: 36), target: "pad", mode: .absolute))85 #expect(near(value(e.process(.noteOn(channel: 0, note: 36, velocity: 127), deviceUniqueID: nil)), 1))86 #expect(near(value(e.process(.noteOff(channel: 0, note: 36, velocity: 50), deviceUniqueID: nil)), 0))87 }8889 @Test func relativeEncodingsDecodeSteps() {90 #expect(MIDIRelativeEncoding.twosComplement.delta(for: 1) == 1)91 #expect(MIDIRelativeEncoding.twosComplement.delta(for: 63) == 63)92 #expect(MIDIRelativeEncoding.twosComplement.delta(for: 127) == -1)93 #expect(MIDIRelativeEncoding.twosComplement.delta(for: 65) == -63)94 #expect(MIDIRelativeEncoding.twosComplement.delta(for: 64) == -64)95 #expect(MIDIRelativeEncoding.twosComplement.delta(for: 0) == 0)96 #expect(MIDIRelativeEncoding.signedBit.delta(for: 1) == 1)97 #expect(MIDIRelativeEncoding.signedBit.delta(for: 0x3F) == 63)98 #expect(MIDIRelativeEncoding.signedBit.delta(for: 0x41) == -1)99 #expect(MIDIRelativeEncoding.signedBit.delta(for: 0x7F) == -63)100 #expect(MIDIRelativeEncoding.signedBit.delta(for: 0x40) == 0)101 #expect(MIDIRelativeEncoding.signedBit.delta(for: 0) == 0)102 #expect(MIDIRelativeEncoding.binaryOffset.delta(for: 64) == 0)103 #expect(MIDIRelativeEncoding.binaryOffset.delta(for: 65) == 1)104 #expect(MIDIRelativeEncoding.binaryOffset.delta(for: 63) == -1)105 #expect(MIDIRelativeEncoding.binaryOffset.delta(for: 0) == -64)106 }107108 @Test func relativeTwosComplementWithProvider() {109 var e = engine(MIDIMapping(control: .cc(channel: 0, controller: 10), target: "jog", mode: .relative))110 let step = 1.0 / 127111 #expect(near(value(e.process(cc(10, 1), deviceUniqueID: nil) { _ in 0.5 }), 0.5 + step))112 #expect(near(value(e.process(cc(10, 127), deviceUniqueID: nil) { _ in 0.5 }), 0.5 - step))113 #expect(near(value(e.process(cc(10, 3), deviceUniqueID: nil) { _ in 0.5 }), 0.5 + 3 * step))114 #expect(near(value(e.process(cc(10, 125), deviceUniqueID: nil) { _ in 0.5 }), 0.5 - 3 * step))115 #expect(e.process(cc(10, 0), deviceUniqueID: nil) { _ in 0.5 }.isEmpty) // no movement116 // Clamped to the range.117 #expect(near(value(e.process(cc(10, 10), deviceUniqueID: nil) { _ in 0.99 }), 1))118 #expect(near(value(e.process(cc(10, 118), deviceUniqueID: nil) { _ in 0.01 }), 0))119 // Notes and pitch bend never drive a relative mapping.120 var note = engine(MIDIMapping(control: .note(channel: 0, note: 1), target: "x", mode: .relative))121 #expect(note.process(.noteOn(channel: 0, note: 1, velocity: 1), deviceUniqueID: nil).isEmpty)122 }123124 @Test func relativeSignedBitSensitivityInvertAndRange() {125 var e = engine(126 MIDIMapping(control: .cc(channel: 0, controller: 11), target: "s", mode: .relative, relativeEncoding: .signedBit, sensitivity: 4),127 MIDIMapping(control: .cc(channel: 0, controller: 12), target: "i", mode: .relative, relativeEncoding: .signedBit, invert: true),128 MIDIMapping(control: .cc(channel: 0, controller: 13), target: "r", mode: .relative, relativeEncoding: .binaryOffset, sensitivity: 127, minValue: 0.25, maxValue: 0.75)129 )130 let step = 1.0 / 127131 #expect(near(value(e.process(cc(11, 0x01), deviceUniqueID: nil) { _ in 0.5 }), 0.5 + 4 * step))132 #expect(near(value(e.process(cc(11, 0x42), deviceUniqueID: nil) { _ in 0.5 }), 0.5 - 8 * step))133 #expect(near(value(e.process(cc(12, 0x01), deviceUniqueID: nil) { _ in 0.5 }), 0.5 - step))134 #expect(near(value(e.process(cc(12, 0x41), deviceUniqueID: nil) { _ in 0.5 }), 0.5 + step))135 #expect(near(value(e.process(cc(13, 65), deviceUniqueID: nil) { _ in 0.5 }), 0.75)) // +1.0 clamped to max136 #expect(near(value(e.process(cc(13, 63), deviceUniqueID: nil) { _ in 0.5 }), 0.25)) // clamped to min137 }138139 @Test func relativeWithoutProviderAccumulates() {140 var e = engine(MIDIMapping(control: .cc(channel: 0, controller: 10), target: "browse", mode: .relative, sensitivity: 12.7))141 #expect(near(value(e.process(cc(10, 1), deviceUniqueID: nil)), 0.1))142 #expect(near(value(e.process(cc(10, 1), deviceUniqueID: nil)), 0.2))143 #expect(near(value(e.process(cc(10, 127), deviceUniqueID: nil)), 0.1))144 #expect(near(value(e.process(cc(10, 126), deviceUniqueID: nil)), 0)) // clamped145 }146147 @Test func triggerFiresOnPressOnly() {148 var e = engine(149 MIDIMapping(control: .note(channel: 0, note: 1), target: "cue", mode: .trigger),150 MIDIMapping(control: .cc(channel: 0, controller: 1), target: "sync", mode: .trigger)151 )152 expectEqual(e.process(.noteOn(channel: 0, note: 1, velocity: 127), deviceUniqueID: nil), [.trigger(target: "cue")])153 #expect(e.process(.noteOff(channel: 0, note: 1, velocity: 0), deviceUniqueID: nil).isEmpty)154 expectEqual(e.process(.noteOn(channel: 0, note: 1, velocity: 1), deviceUniqueID: nil), [.trigger(target: "cue")])155 // A controller that never sends note-off still triggers every time.156 expectEqual(e.process(.noteOn(channel: 0, note: 1, velocity: 1), deviceUniqueID: nil), [.trigger(target: "cue")])157 // CC button: edge-detected.158 expectEqual(e.process(cc(1, 127), deviceUniqueID: nil), [.trigger(target: "sync")])159 #expect(e.process(cc(1, 100), deviceUniqueID: nil).isEmpty) // still held (pressure)160 #expect(e.process(cc(1, 0), deviceUniqueID: nil).isEmpty)161 expectEqual(e.process(cc(1, 1), deviceUniqueID: nil), [.trigger(target: "sync")])162 }163164 @Test func toggleFlipsOnPress() {165 var e = engine(MIDIMapping(control: .note(channel: 2, note: 5), target: "deck.A.play", mode: .toggle))166 expectEqual(e.process(.noteOn(channel: 2, note: 5, velocity: 127), deviceUniqueID: nil), [.toggle(target: "deck.A.play")])167 #expect(e.process(.noteOff(channel: 2, note: 5, velocity: 0), deviceUniqueID: nil).isEmpty)168 expectEqual(e.process(.noteOn(channel: 2, note: 5, velocity: 127), deviceUniqueID: nil), [.toggle(target: "deck.A.play")])169 #expect(e.process(.pitchBend(channel: 2, value: 1), deviceUniqueID: nil).isEmpty)170 }171172 @Test func momentaryTracksHold() {173 var e = engine(174 MIDIMapping(control: .note(channel: 0, note: 9), target: "fx", mode: .momentary),175 MIDIMapping(control: .cc(channel: 0, controller: 9), target: "shift", mode: .momentary)176 )177 expectEqual(e.process(.noteOn(channel: 0, note: 9, velocity: 1), deviceUniqueID: nil), [.momentary(target: "fx", pressed: true)])178 expectEqual(e.process(.noteOff(channel: 0, note: 9, velocity: 0), deviceUniqueID: nil), [.momentary(target: "fx", pressed: false)])179 expectEqual(e.process(cc(9, 127), deviceUniqueID: nil), [.momentary(target: "shift", pressed: true)])180 #expect(e.process(cc(9, 127), deviceUniqueID: nil).isEmpty)181 expectEqual(e.process(cc(9, 0), deviceUniqueID: nil), [.momentary(target: "shift", pressed: false)])182 #expect(e.process(cc(9, 0), deviceUniqueID: nil).isEmpty)183 }184185 @Test func invertedButtonsSwapPressAndRelease() {186 var e = engine(187 MIDIMapping(control: .note(channel: 0, note: 9), target: "m", mode: .momentary, invert: true),188 MIDIMapping(control: .note(channel: 0, note: 10), target: "t", mode: .trigger, invert: true)189 )190 expectEqual(e.process(.noteOn(channel: 0, note: 9, velocity: 1), deviceUniqueID: nil), [.momentary(target: "m", pressed: false)])191 expectEqual(e.process(.noteOff(channel: 0, note: 9, velocity: 0), deviceUniqueID: nil), [.momentary(target: "m", pressed: true)])192 #expect(e.process(.noteOn(channel: 0, note: 10, velocity: 1), deviceUniqueID: nil).isEmpty)193 expectEqual(e.process(.noteOff(channel: 0, note: 10, velocity: 0), deviceUniqueID: nil), [.trigger(target: "t")])194 }195196 @Test func deviceScope() {197 var e = engine(198 MIDIMapping(control: .cc(channel: 0, controller: 1), deviceUniqueID: 42, target: "only42", mode: .absolute),199 MIDIMapping(control: .cc(channel: 0, controller: 1), target: "any", mode: .absolute)200 )201 expectEqual(e.process(cc(1, 127), deviceUniqueID: 42), [202 .value(target: "only42", normalized: 1), .value(target: "any", normalized: 1),203 ])204 expectEqual(e.process(cc(1, 127), deviceUniqueID: 7), [.value(target: "any", normalized: 1)])205 expectEqual(e.process(cc(1, 127), deviceUniqueID: nil), [.value(target: "any", normalized: 1)])206 }207208 @Test func softTakeoverWaitsForCrossingFromBelow() {209 var e = engine(MIDIMapping(control: .cc(channel: 0, controller: 7), target: "vol", mode: .absolute, softTakeover: true))210 let software: (String) -> Double? = { _ in 0.8 }211 #expect(e.process(cc(7, 10), deviceUniqueID: nil, currentValue: software).isEmpty)212 #expect(e.process(cc(7, 50), deviceUniqueID: nil, currentValue: software).isEmpty)213 #expect(e.process(cc(7, 90), deviceUniqueID: nil, currentValue: software).isEmpty) // 0.706, still below214 let picked = value(e.process(cc(7, 110), deviceUniqueID: nil, currentValue: software)) // 0.865, crossed215 #expect(near(picked, MIDIMappingEngine.normalize7(110)))216 // Once picked up, everything passes, even far from the software value.217 #expect(near(value(e.process(cc(7, 0), deviceUniqueID: nil, currentValue: software)), 0))218 }219220 @Test func softTakeoverFromAboveAndWhenAlreadyClose() {221 var e = engine(MIDIMapping(control: .cc(channel: 0, controller: 7), target: "vol", mode: .absolute, softTakeover: true))222 #expect(e.process(cc(7, 127), deviceUniqueID: nil) { _ in 0.25 }.isEmpty)223 #expect(e.process(cc(7, 60), deviceUniqueID: nil) { _ in 0.25 }.isEmpty)224 #expect(value(e.process(cc(7, 20), deviceUniqueID: nil) { _ in 0.25 }) != nil) // crossed downwards225226 // First message already within tolerance: immediate pickup.227 var close = engine(MIDIMapping(control: .cc(channel: 0, controller: 7), target: "vol", mode: .absolute, softTakeover: true))228 #expect(value(close.process(cc(7, 65), deviceUniqueID: nil) { _ in 0.5 }) != nil)229230 // Unknown software value: nothing to protect, pass through.231 var unknown = engine(MIDIMapping(control: .cc(channel: 0, controller: 7), target: "vol", mode: .absolute, softTakeover: true))232 #expect(value(unknown.process(cc(7, 3), deviceUniqueID: nil) { _ in nil }) != nil)233234 // Without the flag there is no gating at all.235 var plain = engine(MIDIMapping(control: .cc(channel: 0, controller: 7), target: "vol", mode: .absolute))236 #expect(value(plain.process(cc(7, 3), deviceUniqueID: nil) { _ in 0.9 }) != nil)237 }238239 @Test func softTakeoverResetAndMovingSoftwareValue() {240 var e = engine(241 MIDIMapping(control: .cc(channel: 0, controller: 7), target: "vol", mode: .absolute, softTakeover: true),242 MIDIMapping(control: .cc(channel: 0, controller: 8), target: "eq", mode: .absolute, softTakeover: true)243 )244 #expect(value(e.process(cc(7, 64), deviceUniqueID: nil) { _ in 0.5 }) != nil)245 #expect(value(e.process(cc(8, 64), deviceUniqueID: nil) { _ in 0.5 }) != nil)246 e.resetSoftTakeover(target: "vol")247 #expect(e.process(cc(7, 100), deviceUniqueID: nil) { _ in 0.1 }.isEmpty) // gated again248 #expect(value(e.process(cc(8, 100), deviceUniqueID: nil) { _ in 0.1 }) != nil) // other target untouched249 // The software value moves between messages: crossing is judged against the latest one.250 #expect(e.process(cc(7, 90), deviceUniqueID: nil) { _ in 0.1 }.isEmpty)251 #expect(value(e.process(cc(7, 89), deviceUniqueID: nil) { _ in 0.9 }) == nil) // 0.71 -> 0.70, both below 0.9252 #expect(value(e.process(cc(7, 120), deviceUniqueID: nil) { _ in 0.9 }) != nil)253 e.resetSoftTakeover()254 #expect(e.process(cc(8, 0), deviceUniqueID: nil) { _ in 0.9 }.isEmpty)255 }256257 @Test func softTakeoverRespectsRangeAndInvertAndPitchBend() {258 // Range 0.5...1 inverted: hardware 0 -> 1.0, hardware 127 -> 0.5. Software at 0.75.259 var e = engine(MIDIMapping(control: .cc(channel: 0, controller: 7), target: "x", mode: .absolute, minValue: 0.5, maxValue: 1, invert: true, softTakeover: true))260 #expect(e.process(cc(7, 0), deviceUniqueID: nil) { _ in 0.75 }.isEmpty) // 1.0261 #expect(e.process(cc(7, 30), deviceUniqueID: nil) { _ in 0.75 }.isEmpty) // 0.883262 #expect(value(e.process(cc(7, 80), deviceUniqueID: nil) { _ in 0.75 }) != nil) // 0.686: crossed263 // Software value outside the mapped range is clamped, so the end stop picks it up.264 var clamped = engine(MIDIMapping(control: .cc(channel: 0, controller: 7), target: "x", mode: .absolute, minValue: 0, maxValue: 0.5, softTakeover: true))265 #expect(clamped.process(cc(7, 10), deviceUniqueID: nil) { _ in 0.9 }.isEmpty)266 #expect(value(clamped.process(cc(7, 127), deviceUniqueID: nil) { _ in 0.9 }) != nil)267 // 14-bit.268 var bend = engine(MIDIMapping(control: .pitchBend(channel: 0), target: "tempo", mode: .absolute, softTakeover: true))269 #expect(bend.process(.pitchBend(channel: 0, value: 0), deviceUniqueID: nil) { _ in 0.5 }.isEmpty)270 #expect(bend.process(.pitchBend(channel: 0, value: 4000), deviceUniqueID: nil) { _ in 0.5 }.isEmpty)271 #expect(value(bend.process(.pitchBend(channel: 0, value: 9000), deviceUniqueID: nil) { _ in 0.5 }) != nil)272 }273274 @Test func degenerateValuesNeverProduceNonFiniteOutput() {275 var e = engine(276 MIDIMapping(control: .cc(channel: 0, controller: 1), target: "a", mode: .absolute, minValue: .nan, maxValue: 1),277 MIDIMapping(control: .cc(channel: 0, controller: 1), target: "b", mode: .relative, sensitivity: .infinity),278 MIDIMapping(control: .cc(channel: 0, controller: 1), target: "c", mode: .relative, minValue: -.infinity, maxValue: 1)279 )280 for raw: UInt8 in 0...127 {281 for event in e.process(cc(1, raw), deviceUniqueID: nil, currentValue: { _ in .nan }) {282 if case .value(_, let v) = event { #expect(v.isFinite) }283 }284 }285 }286}287