gpt4 book ai didi

swift - 从 C 样式指针访问 self

转载 作者:行者123 更新时间:2023-11-28 08:11:39 26 4
gpt4 key购买 nike

<分区>

我正在开发一个使用 MIDI 设备的应用程序。在 CoreMIDI 的 Playground 上玩了一会儿之后,我发现了如何获取 MIDI 输入信号,所以我实现了这个:

func makeInputSource() {
var midiClient : MIDIClientRef = 0
var inPort : MIDIPortRef = 0

MIDIClientCreate("WobClient" as CFString, nil, nil, &midiClient)
MIDIInputPortCreate(midiClient, "WobClient_InPort" as CFString, {
(pktList: UnsafePointer<MIDIPacketList>, readProcRefCon: UnsafeMutableRawPointer?, srcConnRefCon: UnsafeMutableRawPointer?) in
let packetList : MIDIPacketList = pktList.pointee
var packet : MIDIPacket = packetList.packet

for _ in 1...packetList.numPackets {
let bytes = Mirror(reflecting: packet.data).children
var params : [UInt64] = []

var i = packet.length
for (_, attr) in bytes.enumerated() {
let string = String(format: "%02X ", attr.value as! UInt8)
params.append(UInt64(strtoul(string, nil, 16)))
i -= 1

if (i <= 0) {
break
}
}

packet = MIDIPacketNext(&packet).pointee
}
}, nil, &inPort)
MIDIPortConnectSource(inPort, self.source, &self.source)
}

这就像使用信号的魅力一样。现在,我想使用信号来编辑 NSSlider 的值,所以,很自然地,我想到的是:

self.slider_one?.integerValue = params[2]

但是,当我尝试这样做时,出现以下错误:

A C function pointer cannot be formed from a closure that captures context

所以我想知道的是,是否有一种方法可以从该闭包内部访问 self,或者是否有其他方法可以快速使用 MIDI 输入?

谢谢。

--- 编辑:根据要求,我修改后的代码:

func makeInputSource() {
var midiClient : MIDIClientRef = 0
var inPort : MIDIPortRef = 0
var observer = UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque())

MIDIClientCreate("WobClient" as CFString, nil, nil, &midiClient)
MIDIInputPortCreate(midiClient, "WobClient_InPort" as CFString, {
(pktList: UnsafePointer<MIDIPacketList>, readProcRefCon: UnsafeMutableRawPointer?, srcConnRefCon: UnsafeMutableRawPointer?) in
let packetList : MIDIPacketList = pktList.pointee
var packet : MIDIPacket = packetList.packet

for _ in 1...packetList.numPackets {
let bytes = Mirror(reflecting: packet.data).children
var params : [UInt64] = []

var i = packet.length
for (_, attr) in bytes.enumerated() {
let string = String(format: "%02X ", attr.value as! UInt8)
params.append(UInt64(strtoul(string, nil, 16)))
i -= 1

if (i <= 0) {
break
}
}

let mySelf = Unmanaged<Wob>.fromOpaque(observer).takeUnretainedValue()
mySelf.slider_one?.integerValue = 25 // 25 is a test value
packet = MIDIPacketNext(&packet).pointee
}

}, &observer, &inPort)
MIDIPortConnectSource(inPort, self.source, &self.source)

}

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