对于使用AudioKit的iOS应用程序,在启用MIDI之前,需要在info.plist中添加以下键/值对:
Key: "NSMidiUsageDescription" Value: "We need to use your MIDI devices to get input"
这样就能够请求用户授权访问MIDI输入设备。 然后,需要使用以下代码段来设置MIDI输入:
// Create a MIDI Input Port var midiIn = AudioKit.MIDI.createInputPort("My MIDI Input")
// Open all available MIDI input devices midiIn.openInput()
现在,您可以在应用程序中使用MIDI数据了,示例代码如下:
// MIDI Note On event midiIn.onNoteOn = { note, velocity, channel in print("MIDI Note On Note: (note) Velocity: (velocity) Channel: (channel)") }
// MIDI Note Off event midiIn.onNoteOff = { note, velocity, channel in print("MIDI Note Off Note: (note) Velocity: (velocity) Channel: (channel)") }
// MIDI Controller event midiIn.onController = { controller, value, channel in print("MIDI Controller Controller: (controller) Value: (value) Channel: (channel)") }
// MIDI Pitch Bend event midiIn.onPitchBend = { pitchbend, channel in print("MIDI Pitch Bend Value: (pitchbend) Channel: (channel)") }
以上就是要使用AudioKit使MIDI输入正常工作的解决方法,以及一些常见的MIDI事件的示例代码。