首页 > 解决方案 > 在 IOS 上使用 AudioKit 将声音文件作为 MIDI 音符发送

问题描述

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

标签: iosswiftmidiaudiokit

解决方案


发送 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)

推荐阅读