import Cocoaimport CoreMIDIimport XCPlaygroundXcpsetExecutionShouldContinueIndefinitely(continueIndefinitely: true)func notifyCallback(message:UnsafePointer<MIDINotification>,refCon:UnsafeMutablePointer<VoID>){ println("MIDI Notify")}func eventCallback(pktList:UnsafePointer<MIDIPacketList>,refCon:UnsafeMutablePointer<VoID>,connRefCon:UnsafeMutablePointer<VoID>){ println("MIDI Read")}var clIEnt = MIDIClIEntRef()MIDIClIEntCreate("Core MIDI Callback Demo" as Nsstring,MIDINotifyProc(copaquePointer([notifyCallback])),nil,&clIEnt)var inPort = MIDIPortRef()MIDIinputPortCreate(clIEnt,"input port",MIDIReadProc(copaquePointer([eventCallback])),&inPort)let sourceCount = MIDIGetNumberOfSources()for var count:UInt = 0; count < sourceCount; ++count{ let src:MIDIEndpointRef = MIDIGetSource(count) MIDIPortConnectSource(inPort,src,nil)}
我通过将工作的Objective-C代码转换成我认为正确的Swift版本来实现这一点.
它编译并运行正常,直到其中一个回调触发,例如当我拔下MIDI设备或按下其中一个键时.我总是得到一个BAD_EXEC.
任何想法如何使这项工作或Swift只是没有准备好作为网络上的一些博客文章.苹果官方的任何我忽视的事情都清楚地表明Swift尚未准备好进行CoreMIDI回调?
更新2015-03-10:相应的Objective-C代码可在http://www.digital-aud.io/blog/2015/03/10/on-coremidi-callbacks/找到
解决方法 与早期版本相比,Swift3似乎对CoreMIDI有更好的支持.下面显示的示例游乐场演示显示了一些有效的基本CoreMIDI调用.import Cocoaimport CoreMIDIimport PlaygroundSupport// helper method to extract the display name from a MIdiobjectReffunc midiobjectdisplayname(_ obj: MIdiobjectRef) -> String { var param: Unmanaged<CFString>? var capturedname = "Error" let err = MIdiobjectGetStringProperty(obj,kMIDIPropertydisplayname,¶m) if err == Osstatus(noErr) { capturedname = param!.takeRetainedValue() as String } return capturedname}// method to collect display names of available MIDI destinationsfunc mIDIDestinationnames() -> [String] { var names:[String] = [] let count:Int = MIDIGetNumberOfDestinations() for i in 0..<count { let endpoint:MIDIEndpointRef = MIDIGetDestination(i) if endpoint != 0 { names.append(midiobjectdisplayname(endpoint)) } } return names}let destinationnames = mIDIDestinationnames()// check if we have any available MIDI destinations.if destinationnames.count > 0 { // establish a MIDI clIEnt and output port,and send a note on/off pair. var mIDiClIEnt:MIDIClIEntRef = 0 var outPort:MIDIPortRef = 0 MIDIClIEntCreate("Swift3 Test ClIEnt" as CFString,&mIDiClIEnt) MIdioutputPortCreate(mIDiClIEnt,"Swift3 Test OutPort" as CFString,&outPort) let destNum = 0 let destname = destinationnames[destNum] var dest:MIDIEndpointRef = MIDIGetDestination(destNum) var mIDiPacket:MIDIPacket = MIDIPacket() mIDiPacket.timeStamp = 0 mIDiPacket.length = 3 mIDiPacket.data.0 = 0x90 + 0 // Note On event channel 1 mIDiPacket.data.1 = 0x3D // Note Db mIDiPacket.data.2 = 100 // VeLocity var packetList:MIDIPacketList = MIDIPacketList(numPackets: 1,packet: mIDiPacket) print("Sending note on to \(destname)") MIdisend(outPort,dest,&packetList) mIDiPacket.data.0 = 0x80 + 0 // Note off event channel 1 mIDiPacket.data.2 = 0 // VeLocity sleep(1) packetList = MIDIPacketList(numPackets: 1,packet: mIDiPacket) MIdisend(outPort,&packetList) print("Note off sent to \(destname)")}总结
以上是内存溢出为你收集整理的Swift中的CoreMIDI回调全部内容,希望文章能够帮你解决Swift中的CoreMIDI回调所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)