- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用以下代码在 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, ¶m)
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/
我正在尝试从我的 iOS 应用程序向 Logic Pro 发送下一首轨道消息。这是“播放”的十六进制值 {0xF0, 0x7F, 0x7F, 0x06, 0x02, 0xF7}。有谁知道“下一轨道”的
我已经开始开发一个基于 RtMidi 的命令行应用程序,它可以发送任意 MIDI 数据。我发现,当我从控制台运行该工具时,我可以与 MIDI 设备通信,但如果我从另一台机器 ssh 进入我的机器,我根
我在 OSX 上使用 Swift 中的 CoreMIDI 进行了一些尝试,并且大部分功能都可以正常工作。 我现在遇到的唯一问题是其他软件和应用程序看不到我的应用程序以及可用的输入/输出。 Garage
我使用 CoreMIDI 构建了一个虚拟 MIDI Controller ,并希望导入 ControlChange 的制造商预设(即 cc 值、带有 cc 编号的关联效果名称、预设名称等)。有没有一种
我在将 MIDI 实现到我的 iOS 应用程序时遇到问题,因为接收器回调似乎正在跳过 MIDI 消息和数据包。我正在使用 Midi Monitor检查我遗漏、跳过的 MIDI 消息等。 那么百万美元的
给定以下代码,如果我使用 if 分支中的第一个方法来获取 MIDIDestination,代码工作正常,并且 MIDI 数据被发送。如果我使用 else 分支中的第二种方法,则不会发送任何数据。 va
我正在开发一个使用 MIDI 的 MacOS 应用程序。我想做的是将 SysEx 消息发送到 MIDI 设备。现在,我在 Java 中做过一次,但从未在 Swift 中做过。 因为 coremidi
我正在研究 coremidi 并尝试一些实验我想知道如何在 midifile 的 miditrack 的半音中进行移调。有人可以告诉我该怎么做吗? 非常感谢 最佳答案 CoreMIDI 对 MIDI
我有一个可以通过 MIDI 控制的 Marshall CODE 放大器。我可以通过向放大器发送简单的 MIDI 消息来成功更改放大器上的某些控件: var midiClient: MIDIClient
我在使用 CoreMIDI 的 iPad 应用程序崩溃后收到以下错误(* 是为了清空应用程序名称): Dyld Error Message: Symbol not found: _MIDINetwo
我用的coremidi还行,但我还想支持外接USB功能。 我试过一个名为 Midi Monitor 的应用程序,它确实在连接时找到了我的 USB 接口(interface)。问题是如何通过我自己的应用
我可以使用 MusicPlayer 的 MusicTrackSetProperty(t, kSequenceTrackProperty_MuteStatus...) 方法轻松静音/取消静音 midi
我正在使用以下代码在 Swift Playground 中接收 MIDI 事件: import Cocoa import CoreMIDI import XCPlayground XCPSetExec
我无法找到关于 CoreMIDI for iOS 的很多信息。甚至可以通过向设备本身发送消息来播放 MIDI 声音。 iPhone 或 iPad 是否安装了 MIDI 设备,或者您是否必须连接设备才能
我正在创建一个钢琴应用程序(适用于 OSX),它有一个屏幕键盘,可以显示用户在合成器键盘上弹奏的内容。一切都使用 CoreMIDI 框架连接。我通过在名为 PianoKey 的类中子类化 NSButt
背景:我正在尝试编写一个程序,该程序将从我的合成器中获取 MIDI 信息,并用它来做各种音乐上的事情。我已经连接了键盘以便接收 MIDI 数据。在我的 midiInputCallback() 方法(生
这是一个非常简单的 CoreMIDI OS X 应用程序,用于发送 MIDI 数据。问题是它不起作用。它编译良好,并且运行。它不会报告任何错误,也不会崩溃。创建的源在 MIDI 监视器中可见。但是,没
我需要手工组装 14bit MIDI 弯音 iOS 中原始 UInt16 值的值。我想知道是否有人有机会提出一个优雅的解决方案?这就是我所在的位置 - 我可能会在今天晚些时候有机会对此进行测试,但如果
我在 iOS 上的 CoreMIDI 连接显然足够快,可以处理任何碰到它的东西……如果我只是做一些简单的对象创建和 NSLog。在 UI 中,我没有时间处理所有进来的东西。UI 会崩溃,或者完成处理太
我正在开发一个需要通过 wifi 发送 Midi 的应用程序。我查看了 CoreMIDI 和 MIDINetworkSession 类。我已将默认 session 的策略设置为 .anyone 并启用
我是一名优秀的程序员,十分优秀!