SPB Git

Repository Public

MIDIParser.swift 3.1 KB 87 lines swift

1// ORIX DJ2// File: Packages/ORIXDJCore/Sources/ORIXMIDI/MIDIParser.swift3// Description: Streaming MIDI 1.0 byte parser: running status, SysEx and real-time bytes skipped safely.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai78import Foundation910/// A streaming MIDI 1.0 byte parser.11///12/// - Running status is honoured, across `parse` calls too (keep one parser per input stream).13/// - A note-on with velocity 0 is reported as a note-off.14/// - Real-time bytes (`0xF8...0xFF`) are ignored wherever they occur, including between the data bytes of15///   another message, without disturbing it.16/// - SysEx (`0xF0 ... 0xF7`) is skipped; an unterminated SysEx ends at the next status byte.17/// - System common messages cancel running status; polyphonic and channel pressure are consumed but not reported.18/// - Any byte sequence is accepted: the parser has no failing path and cannot trap.19///20/// The engine's live input does not go through this parser (CoreMIDI delivers UMP words, see `MIDIManager`);21/// it exists for byte-oriented sources: files, tests, and any future packet-list or network input.22public struct MIDIParser: Sendable {23    private var status: UInt8 = 024    private var data1: UInt8 = 025    private var haveData1 = false26    private var needed: UInt8 = 027    private var inSysEx = false2829    public init() {}3031    /// Forgets running status and any partially received message.32    public mutating func reset() {33        self = MIDIParser()34    }3536    /// Feeds one byte; returns a message when the byte completes one.37    public mutating func feed(_ byte: UInt8) -> MIDIMessage? {38        if byte >= 0xF8 { return nil } // real-time: transparent39        if byte & 0x80 != 0 {40            inSysEx = false41            haveData1 = false42            switch byte {43            case 0xF0:44                inSysEx = true45                status = 046                needed = 047            case 0xF1, 0xF3:48                status = byte49                needed = 150            case 0xF2:51                status = byte52                needed = 253            case 0xF4, 0xF5, 0xF6, 0xF7:54                status = 055                needed = 056            default:57                status = byte58                let high = byte & 0xF059                needed = (high == 0xC0 || high == 0xD0) ? 1 : 260            }61            return nil62        }63        // Data byte.64        if inSysEx || status == 0 { return nil }65        if needed == 2 && !haveData1 {66            data1 = byte67            haveData1 = true68            return nil69        }70        let d1 = needed == 2 ? data1 : byte71        let d2 = needed == 2 ? byte : 072        haveData1 = false73        if status >= 0xF0 {74            status = 0 // system common: no running status75            return nil76        }77        return MIDIMessage(status: status, data1: d1, data2: d2)78    }7980    /// Feeds a buffer and calls `body` for each complete message, in order.81    public mutating func parse(_ bytes: some Sequence<UInt8>, _ body: (MIDIMessage) -> Void) {82        for byte in bytes {83            if let message = feed(byte) { body(message) }84        }85    }86}87
Files main
Menu

Theme