Search code examples
iosswiftcoremidi

midi packet list dose not change timestamp


i have this code from here(Using MIDIPacketList in swift) but i can't pm the user or comment on that question, so i will ask my question. @ephemer dude if you see my question, i love your code on midi list and it work so well, but when i change the time stamp, nothing happens, it must create some delay but it will be the same as 0 time stamp. do anyone know how to fix this?

and how i can have the time stamp out of this extension to have that in midi event, i want to be able to change time stamp for every midi event, to have it here:

 var packets = MIDIPacketList(midiEvents: [[0x90, 60, 100]])

 public typealias MidiEvent = [UInt8]

 extension MIDIPacketList {

    init(midiEvents: [MidiEvent]) {

    let timestamp = MIDITimeStamp(0) // do it now
    let totalBytesInAllEvents = midiEvents.reduce(0) { total, event in
        return total + event.count
    }

    // Without this, we'd run out of space for the last few MidiEvents
    let listSize = MemoryLayout<MIDIPacketList>.size + totalBytesInAllEvents

    // CoreMIDI supports up to 65536 bytes, but in practical tests it seems
    // certain devices accept much less than that at a time. Unless you're
    // turning on / off ALL notes at once, 256 bytes should be plenty.
    assert(totalBytesInAllEvents < 256,
           "The packet list was too long! Split your data into multiple lists.")

    // Allocate space for a certain number of bytes
    let byteBuffer = UnsafeMutablePointer<UInt8>.allocate(capacity: listSize)

    // Use that space for our MIDIPacketList
    self = byteBuffer.withMemoryRebound(to: MIDIPacketList.self, capacity: 1) { packetList -> MIDIPacketList in
        var packet = MIDIPacketListInit(packetList)
        midiEvents.forEach { event in
            packet = MIDIPacketListAdd(packetList, listSize, packet, timestamp, event.count, event)
        }

        return packetList.pointee
    }

    byteBuffer.deallocate() // release the manually managed memory
 }
}

 // to send use this

 var packets = MIDIPacketList(midiEvents: [[0x90, 60, 100]])

 MIDISend(clientOutputPort, destination, &packetList)

Solution

  • I found the answer.

    set the timestamp to :

    var delay = 0.2
    
    var timestamp: MIDITimeStamp = mach_absolute_time() + MIDITimeStamp(delay * 1_000_000_000)
    

    the delay is the time you want the midi message age to have delay.