 
 }
 
+// Like MIDISendSysex, but specify a size for each buffer to send, and a delay in seconds between sending each buffer.
 private func customMIDISendSysex(_ request: UnsafeMutablePointer<MIDISysexSendRequest>, _ bufferSize: Int, _ perBufferDelay: Double) -> OSStatus {
     guard let client = SMClient.sharedClient,
           bufferSize >= 3 && bufferSize <= 32767 else { return OSStatus(paramErr) }

        }
        else {
            request.pointee.completionProc(request)

            MIDIPortDispose(port)
            rawPacketListPtr.deallocate()
        }
    }


    }

    let packetListSize = MemoryLayout.offset(of: \MIDIPacketList.packet.data)! + bufferSize
    var packetListData = Data(count: packetListSize)

    let queue = DispatchQueue(label: "com.snoize.SnoizeMIDI.CustomMIDISendSysex")
    queue.setTarget(queue: DispatchQueue.global(priority: .high))

    func sendNextBuffer() {
        let packetDataSize = min(Int(request.pointee.bytesToSend), bufferSize)

        packetListData.withUnsafeMutableBytes { (packetListRawBufferPtr: UnsafeMutableRawBufferPointer) in
            let packetListPtr = packetListRawBufferPtr.bindMemory(to: MIDIPacketList.self).baseAddress!

            let curPacket = MIDIPacketListInit(packetListPtr)
            _ = MIDIPacketListAdd(packetListPtr, packetListSize, curPacket, 0, packetDataSize, request.pointee.data)

            MIDISend(port, request.pointee.destination, packetListPtr)
        }

        request.pointee.data += packetDataSize
        request.pointee.bytesToSend -= UInt32(packetDataSize)
        if request.pointee.bytesToSend == 0 {
            request.pointee.complete = true
        }
