Skip to content

Instantly share code, notes, and snippets.

@jverkoey
Last active November 18, 2020 14:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jverkoey/defb7f9f3578d5cb3ff3 to your computer and use it in GitHub Desktop.
Save jverkoey/defb7f9f3578d5cb3ff3 to your computer and use it in GitHub Desktop.
Enumerating a MIDIPacketList in Swift 2.
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