使用IOS上的AudioKit发送声音文件作为MIDI音符

时间:2018-10-28 16:08:44

标签: ios swift midi audiokit

我有一个IOS应用程序,该应用程序使用AudioKit播放与Pad相关的声音的AudioFiles。因此,我希望该应用程序支持MIDI文件。我想知道如何使用MIDI导出这些声音文件,以便在Garage band这类应用程序中播放

1 个答案:

答案 0 :(得分:1)

发送MIDI:

// to send between app, create a virtual port:
AudioKit.midi.createVirtualOutputPort()
// you can specify which outputs you want to open, or open all by default:
AudioKit.midi.openOutput()

// to send a noteOn message:
AudioKit.midi.sendNoteOnMessage(noteNumber: aNoteNumber, velocity: aVelocity)

// to send a noteOff message:
AudioKit.midi.sendNoteOffMessage(noteNumber: aNoteNumber, velocity: 0)

要接收MIDI,您需要具有一个实现AKMIDIListener协议的类(它甚至可以是您的ViewController,但可能不应该是)。此类可让您实现receivedMIDINoteOn之类的方法来处理传入事件。

class ClassThatImplementsMIDIListener: AKMIDIListener {
    func receivedMIDINoteOn(noteNumber: MIDINoteNumber,
                            velocity: MIDIVelocity,
                            channel: MIDIChannel) {
        // handle the MIDI event in your app, e.g., trigger you sound file
    }
}

设置起来很容易:

// if you want to receive midi from other apps, create a virtual in
AudioKit.midi.createVirtualInputPort()

// you can specify which inputs you want to open, or open them all by default
AudioKit.midi.openInput()

// add your listener
AudioKit.midi.addListener(classImplementingMIDIListener)