Skip to content

Commit

Permalink
Merge pull request #37 from orchetect/dev
Browse files Browse the repository at this point in the history
Refactored MIDI packet read
  • Loading branch information
orchetect authored Sep 3, 2021
2 parents 1eb158b + 9414dc7 commit 8d9cc7b
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 96 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// MIDIEventList PacketPointers.swift
// MIDIKit • https://github.com/orchetect/MIDIKit
//

import Foundation
import CoreMIDI

@available(macOS 11, iOS 14, macCatalyst 14, tvOS 14, watchOS 7, *)
extension UnsafePointer where Pointee == MIDIEventList {

@inline(__always)
func packetsPointers() -> [UnsafePointer<MIDIEventPacket>] {

Array(unsafeSequence())

}

@inline(__always)
func packets() -> [MIDI.Packet.UniversalPacketData] {

unsafeSequence().map { MIDI.Packet.UniversalPacketData($0) }

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import CoreMIDI
extension UnsafePointer where Pointee == MIDIPacketList {

/// MIDIKit backwards-compatible implementation of Core MIDI's `MIDIPacketList.UnsafeSequence`
public func mkUnsafeSequence() -> MIDIPacketList.MKUnsafeSequence {
internal func mkUnsafeSequence() -> MIDIPacketList.MKUnsafeSequence {

MIDIPacketList.MKUnsafeSequence(self)

Expand All @@ -20,7 +20,7 @@ extension UnsafePointer where Pointee == MIDIPacketList {
extension MIDIPacketList {

/// MIDIKit backwards-compatible implementation of Core MIDI's `MIDIPacketList.UnsafeSequence`
public struct MKUnsafeSequence: Sequence {
internal struct MKUnsafeSequence: Sequence {

public typealias Element = UnsafePointer<MIDIPacket>

Expand All @@ -33,6 +33,9 @@ extension MIDIPacketList {
pointers.append(unwrappedPtr)
}

assert(pointers.count == midiPacketListPtr.pointee.numPackets,
"Packet count does not match iterated count.")

}

public func makeIterator() -> Iterator {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// MIDIPacketList PacketPointers.swift
// MIDIKit • https://github.com/orchetect/MIDIKit
//

import Foundation
import CoreMIDI

extension UnsafePointer where Pointee == MIDIPacketList {

func packetsPointers() -> [UnsafePointer<MIDIPacket>] {

// prefer newer Core MIDI API if platform supports it

if #available(macOS 10.15, iOS 13.0, macCatalyst 13.0, *) {
return Array(unsafeSequence())
} else {
return mkUnsafeSequence().pointers
}

}

func packets() -> [MIDI.Packet.PacketData] {

packetsPointers().map { MIDI.Packet.PacketData($0) }

}

}
10 changes: 4 additions & 6 deletions Sources/MIDIKit/IO/ReceiveHandler/Handlers/Events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ extension MIDI.IO.ReceiveHandler {
_ srcConnRefCon: UnsafeMutableRawPointer?
) {

for midiPacketPacketPtr in packetListPtr.mkUnsafeSequence() {
let packetData = MIDI.Packet.PacketData(midiPacketPacketPtr)
let events = midi1Parser.parsedEvents(in: packetData)
for midiPacket in packetListPtr.packets() {
let events = midi1Parser.parsedEvents(in: midiPacket)
handler(events)
}

Expand All @@ -36,9 +35,8 @@ extension MIDI.IO.ReceiveHandler {
_ srcConnRefCon: UnsafeMutableRawPointer?
) {

for universalMIDIPacketPtr in eventListPtr.mkSequence() {
let universalPacketData = MIDI.Packet.UniversalPacketData(universalMIDIPacketPtr)
let events = midi2Parser.parsedEvents(in: universalPacketData)
for midiPacket in eventListPtr.packets() {
let events = midi2Parser.parsedEvents(in: midiPacket)
handler(events)
}

Expand Down
10 changes: 4 additions & 6 deletions Sources/MIDIKit/IO/ReceiveHandler/Handlers/EventsLogging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ extension MIDI.IO.ReceiveHandler {
_ srcConnRefCon: UnsafeMutableRawPointer?
) {

for midiPacketPacketPtr in packetListPtr.mkUnsafeSequence() {
let packetData = MIDI.Packet.PacketData(midiPacketPacketPtr)
let events = midi1Parser.parsedEvents(in: packetData)
for midiPacket in packetListPtr.packets() {
let events = midi1Parser.parsedEvents(in: midiPacket)
logEvents(events)
}

Expand All @@ -41,9 +40,8 @@ extension MIDI.IO.ReceiveHandler {
_ srcConnRefCon: UnsafeMutableRawPointer?
) {

for universalMIDIPacketPtr in eventListPtr.mkSequence() {
let universalPacketData = MIDI.Packet.UniversalPacketData(universalMIDIPacketPtr)
let events = midi2Parser.parsedEvents(in: universalPacketData)
for midiPacket in eventListPtr.packets() {
let events = midi2Parser.parsedEvents(in: midiPacket)
logEvents(events)
}

Expand Down
10 changes: 4 additions & 6 deletions Sources/MIDIKit/IO/ReceiveHandler/Handlers/RawData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ extension MIDI.IO.ReceiveHandler {
_ srcConnRefCon: UnsafeMutableRawPointer?
) {

for midiPacketPacketPtr in packetListPtr.mkUnsafeSequence() {
let packetData = MIDI.Packet.PacketData(midiPacketPacketPtr)
let typeErasedPacket = MIDI.Packet.packet(packetData)
for midiPacket in packetListPtr.packets() {
let typeErasedPacket = MIDI.Packet.packet(midiPacket)
handler(typeErasedPacket)
}

Expand All @@ -33,9 +32,8 @@ extension MIDI.IO.ReceiveHandler {
_ srcConnRefCon: UnsafeMutableRawPointer?
) {

for universalMIDIPacketPtr in eventListPtr.mkSequence() {
let universalPacketData = MIDI.Packet.UniversalPacketData(universalMIDIPacketPtr)
let typeErasedPacket = MIDI.Packet.universalPacket(universalPacketData)
for midiPacket in eventListPtr.packets() {
let typeErasedPacket = MIDI.Packet.universalPacket(midiPacket)
handler(typeErasedPacket)
}

Expand Down
10 changes: 4 additions & 6 deletions Sources/MIDIKit/IO/ReceiveHandler/Handlers/RawDataLogging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ extension MIDI.IO.ReceiveHandler {
_ srcConnRefCon: UnsafeMutableRawPointer?
) {

for midiPacketPacketPtr in packetListPtr.mkUnsafeSequence() {
let packetData = MIDI.Packet.PacketData(midiPacketPacketPtr)
handleBytes(packetData.bytes)
for midiPacket in packetListPtr.packets() {
handleBytes(midiPacket.bytes)
}

}
Expand All @@ -39,9 +38,8 @@ extension MIDI.IO.ReceiveHandler {
_ srcConnRefCon: UnsafeMutableRawPointer?
) {

for universalMIDIPacketPtr in eventListPtr.mkSequence() {
let universalPacketData = MIDI.Packet.UniversalPacketData(universalMIDIPacketPtr)
handleBytes(universalPacketData.bytes)
for midiPacket in eventListPtr.packets() {
handleBytes(midiPacket.bytes)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ extension MIDI.Packet {

}

/// Universal MIDI Packet
@available(macOS 11, iOS 14, macCatalyst 14, tvOS 14, watchOS 7, *)
@inline(__always) public init(_ eventPacketPtr: UnsafePointer<MIDIEventPacket>) {

self = Self.packetUnwrapper(eventPacketPtr)

}

/// Universal MIDI Packet
@available(macOS 11, iOS 14, macCatalyst 14, tvOS 14, watchOS 7, *)
@inline(__always) public init(_ eventPacket: MIDIEventPacket) {
Expand All @@ -48,6 +56,42 @@ extension MIDI.Packet {
@available(macOS 11, iOS 14, macCatalyst 14, tvOS 14, watchOS 7, *)
extension MIDI.Packet.UniversalPacketData {

@inline(__always) fileprivate
static func packetUnwrapper(
_ eventPacketPtr: UnsafePointer<MIDIEventPacket>
) -> MIDI.Packet.UniversalPacketData {

let wordCollection = MIDIEventPacket.WordCollection(eventPacketPtr)

guard wordCollection.count > 0 else {
return MIDI.Packet.UniversalPacketData(
words: [],
timeStamp: eventPacketPtr.pointee.timeStamp
)
}

guard wordCollection.count <= 64 else {
assertionFailure("Received MIDIEventPacket reporting \(wordCollection.count) words.")
return MIDI.Packet.UniversalPacketData(
words: [],
timeStamp: eventPacketPtr.pointee.timeStamp
)
}

var words: [UInt32] = []
words.reserveCapacity(wordCollection.count)

for word in wordCollection {
words.append(word)
}

return MIDI.Packet.UniversalPacketData(
words: words,
timeStamp: eventPacketPtr.pointee.timeStamp
)

}

@inline(__always) fileprivate
static func packetUnwrapper(
_ eventPacket: MIDIEventPacket
Expand Down

0 comments on commit 8d9cc7b

Please sign in to comment.