>我从这里有这段代码(在 swift 中使用 MIDIPacketList(,但我无法 pm 用户或对这个问题发表评论,所以我会问我的问题。 @ephemer伙计,如果你看到我的问题,我喜欢你在 midi 列表中的代码,它工作得很好,但是当我更改时间戳时,什么也没发生,它必须产生一些延迟,但它将与 0 时间戳相同。 有谁知道如何解决这个问题?
以及我如何从此扩展中获得时间戳以在 midi 事件中拥有时间戳,我希望能够更改每个 midi 事件的时间戳, 在这里有它:
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)
我想通了,它应该如何工作,但不太正确。 根据苹果文档,它必须使用 mach_absolute_time(( 。 所以我使用了它,但我不知道如何使用mach_absolute_time((正常工作.如果有人知道,也请告诉我。 如果我使用 var 时间戳 = mach_absolute_time((+1000000000,它会延迟 1 分钟左右。如果我用任何低于此的数字更改 1000000000,不会延迟。 有谁知道如何使用 mach_absolute_time((? 我在 mach_absolute_time(( 上看到了一些代码,但他们使用它作为计时器,它是一个从拳头启动开始提供时间的计时器,但如何给出一个时间作为 mach_absolute_time(( 来处理 midi 时间戳。
我找到了答案。
将时间戳设置为 :
var delay = 0.2
var timestamp: MIDITimeStamp = mach_absolute_time() + MIDITimeStamp(delay * 1_000_000_000)
延迟是您希望 MIDI 消息时代具有延迟的时间。