Skip to content

Instantly share code, notes, and snippets.

@analogcode
Last active October 1, 2024 22:41
Show Gist options
  • Save analogcode/fa097afb59ee57ccd29e59dfb2526977 to your computer and use it in GitHub Desktop.
Save analogcode/fa097afb59ee57ccd29e59dfb2526977 to your computer and use it in GitHub Desktop.
Handling AudioBus MIDI Input & State Saving
// **********************************************************
// MARK: - AudioBus MIDI Input
// **********************************************************
extension ParentViewController {
func setupAudioBusInput() {
midiInput = ABMIDIReceiverPort(name: "FM Player MIDI In", title: "FM Player MIDI In") { (port, midiPacketListPointer) in
let events = AKMIDIEvent.midiEventsFrom(packetListPointer: midiPacketListPointer)
for event in events {
guard event.channel == self.midiChannelIn || self.omniMode else { return }
if event.status == AKMIDIStatus.noteOn {
if event.internalData[2] == 0 {
self.conductor.stopNote(note: event.noteNumber!, channel: event.channel!)
} else {
self.conductor.playNote(note: event.noteNumber!, velocity: event.internalData[2], channel: event.channel!)
}
}
if event.status == AKMIDIStatus.noteOff {
guard event.channel == self.midiChannelIn || self.omniMode else { return }
self.conductor.stopNote(note: event.noteNumber!, channel: event.channel!)
}
if event.status == AKMIDIStatus.pitchWheel {
guard event.channel == self.midiChannelIn || self.omniMode else { return }
let x = MIDIWord(event.internalData[1])
let y = MIDIWord(event.internalData[2]) << 7
self.receivedMIDIPitchWheel(y+x, channel: event.channel!)
}
if event.status == AKMIDIStatus.programChange {
guard event.channel == self.midiChannelIn || self.omniMode else { return }
self.receivedMIDIProgramChange(event.data1, channel: event.channel!)
}
if event.status == AKMIDIStatus.controllerChange {
guard event.channel == self.midiChannelIn || self.omniMode else { return }
self.receivedMIDIController(event.data1, value: event.data2, channel: event.channel!)
}
}
}
Audiobus.client?.controller.addMIDIReceiverPort(midiInput)
Audiobus.client?.controller.stateIODelegate = self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment