Last active
November 18, 2020 14:46
-
-
Save jverkoey/defb7f9f3578d5cb3ff3 to your computer and use it in GitHub Desktop.
Enumerating a MIDIPacketList in Swift 2.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension MIDIPacketList: SequenceType { | |
public typealias Generator = MIDIPacketListGenerator | |
public func generate() -> Generator { | |
return Generator(packetList: self) | |
} | |
} | |
/** | |
Generator for MIDIPacketList allowing iteration over its list of MIDIPacket objects. | |
*/ | |
public struct MIDIPacketListGenerator : GeneratorType { | |
public typealias Element = MIDIPacket | |
init(packetList: MIDIPacketList) { | |
let ptr = UnsafeMutablePointer<MIDIPacket>.alloc(1) | |
ptr.initialize(packetList.packet) | |
self.packet = ptr | |
self.count = packetList.numPackets | |
} | |
public mutating func next() -> Element? { | |
guard self.packet != nil && self.index < self.count else { return nil } | |
let lastPacket = self.packet! | |
self.packet = MIDIPacketNext(self.packet!) | |
self.index++ | |
return lastPacket.memory | |
} | |
// Extracted packet list info | |
var count: UInt32 | |
var index: UInt32 = 0 | |
// Iteration state | |
var packet: UnsafeMutablePointer<MIDIPacket>? | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment