一个易于使用的 CoreMIDI 和 AudioSampler 库

MIDIPiano采样器

一个易于使用的 CoreMIDI 和 AudioSampler 库。

用法

创建一个处理 MIDI 设备并接收 MIDI 消息。MIDIController

// Create the MIDISession.
let controller = MIDIController()

// Retreive the devices connected to the system.
let devices = controller.inputDevices

// Set a selected device for the controller as input.
controller.set(device: selectedDevice)

创建一个接收 MIDI 消息并合成它们。AudioSampler

// soundbankURL can be a URL to an sf2 soundbank file. 
let sampler = try AudioSampler(soundbank: soundbankURL)

// Attach a MIDIService to the sampler.
sampler.attach(controller)

// Connect the sampler.node to an AVAudioEngine.
audioEngine.attach(sampler.node)
audioEngine.connect(sampler.node, to: audioEngine.mainMixerNode, format: nil)
try engine.start()

高深

解码 MIDIEvent

手柄注意注意关闭和持续踏板消息。AudioSampler

enum MIDIEvent {
    case noteOn(note: Note, velocity: Velocity)
    case noteOff(note: Note)
    case sustain(Bool)
}

实现中从 to happanes 的转换。CoreMIDI.MIDIEventPacketMIDIEventMIDICodingStrategy

protocol MIDICodingStrategy {
    var verion: MIDIProtocolID { get }
    func decode(event: MIDIEventPacket) -> MIDIEvent?
}

Confirming to this protocol you can create your own custom MIDIEvent conversion for your own MIDI controller.

let controller = MIDIController(decoder: CustomAKAICoder())

Or you can use the default MIDI 1.0 coder and add a fallback operator for unhandled MIDI events.

let controller = MIDIController(decoder: .default_v1.fallback(PadsDecoder()))

GitHub

点击跳转