gpt4 book ai didi

swift - Swift 中的 CoreMIDI 回调

转载 作者:搜寻专家 更新时间:2023-10-31 08:07:11 25 4
gpt4 key购买 nike

我正在使用以下代码在 Swift Playground 中接收 MIDI 事件:

import Cocoa
import CoreMIDI
import XCPlayground

XCPSetExecutionShouldContinueIndefinitely(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])), nil, &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 是否像网络上的一些博客文章那样还没有准备好。我忽略了 Apple 的任何官方信息,明确指出 Swift 尚未准备好 CoreMIDI 回调?

2015-03-10 更新:相应的 Objective-C 代码可以在 http://www.digital-aud.io/blog/2015/03/10/on-coremidi-callbacks/ 找到

更新 2021-06-25:我写了一个关于如何在 Apple 设备上接收 MIDI 消息的编程教程 https://twissmueller.medium.com/midi-listener-in-swift-b6e5fb277406 .教程中有一个链接,您可以在其中购买带有示例应用程序的完整 Xcode 项目。

最佳答案

Swift3 似乎比早期版本更好地支持 CoreMIDI。下面显示的示例 Playground 演示展示了一些工作中的基本 CoreMIDI 调用。

import Cocoa
import CoreMIDI
import PlaygroundSupport

// helper method to extract the display name from a MIDIObjectRef
func midiObjectDisplayName(_ obj: MIDIObjectRef) -> String {

var param: Unmanaged<CFString>?
var capturedName = "Error"
let err = MIDIObjectGetStringProperty(obj, kMIDIPropertyDisplayName, &param)
if err == OSStatus(noErr) {
capturedName = param!.takeRetainedValue() as String
}
return capturedName
}

// method to collect display names of available MIDI destinations
func 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, nil, nil, &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, dest, &packetList)
print("Note off sent to \(destName)")
}

关于swift - Swift 中的 CoreMIDI 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28924831/

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