         }
     }
 }
-
+import AVFoundation
+
+public typealias AUMIDIPacketBlock = (AUEventSampleTime, UInt8, UnsafePointer<MIDIPacket>) -> Void
+public typealias AUMIDIPacketListBuilderBlock = (AUEventSampleTime, UInt8, MIDIPacketListBuilder) -> Void
+
+extension AUAudioUnit {
+    public var scheduleMIDIPacketBlock: AUMIDIPacketBlock? {
+        guard let block = self.scheduleMIDIEventBlock else { return nil }
+        return {(ts, channel, ptr) in
+            ptr.withUnsafeBytes {
+                block(ts, channel, ptr.count, $0)
+            }
+        }
+    }
+
+    public var scheduleMIDIPacketListBuilderBlock: AUMIDIPacketListBuilderBlock? {
+        guard let block = self.scheduleMIDIPacketBlock else { return nil }
+        return {(ts, channel, list) in
+            for e in list {
+                block(ts, channel, e)
+            }
+        }
+
+    }
+}

        Int(self.pointee.length)
    }

    @inline(__always)
    public func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) -> R) -> R {
        body(withUnsafePointer(to: pointee.data) {
            UnsafeRawBufferPointer(start: $0, count: count)
        })
    }

    @inline(__always)
    public subscript(index: Int) -> UInt8 {
        assert(index < count)
        return withUnsafeBytes { $0[index] }
    }
}

extension UnsafeMutablePointer: Sequence where Pointee == MIDIPacketList {
    public typealias Element = UnsafeMutablePointer<MIDIPacket>

    public func makeIterator() -> AnyIterator<Element> {
        var idx = 0

                    p = MIDIPacketNext(p)
                    idx += 1
                }
                return UnsafePointer(p)
//                return p
            }
        }
    }
