gpt4 book ai didi

ios - MIDI 数据包列表不改变时间戳

转载 作者:行者123 更新时间:2023-11-29 05:32:54 24 4
gpt4 key购买 nike

我从这里得到了这段代码(Using MIDIPacketList in swift),但我无法私信用户或评论该问题,所以我会问我的问题。@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 timestamp = mach_absolute_time()+1000000000 ,它将延迟大约 1 分钟左右。如果我将 1000000000 更改为低于此值的任何数字,则不会延迟。有谁知道如何使用 mach_absolute_time() ?我在 mach_absolute_time() 上看到了一些代码,但他们使用它作为计时器,它实际上是一个从第一次启动就给出时间的计时器,但是如何将时间作为 mach_absolute_time() 来与 midi 时间戳一起使用。

关于ios - MIDI 数据包列表不改变时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57355435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com