Search code examples
swiftmidiaudiokit

How do you use AKMIDIStatus in a AKMIDICallbackInstrument callback with AudioKit 4.6.1


Using AKSequencer for midi and a Control track. I'm aware AKMIDIStatus has undergone some changes in recent revisions. My exploration leads me to believe this simple 'note on' 'note off' callback should work:

        func playThroughCallback(_ statusByte: UInt8,
                             _ noteNumber: MIDINoteNumber,
                             _ velocity: MIDIVelocity) {
        //print("MIDI Sequence Event \(status)")
        guard let status = AKMIDIStatus(byte: statusByte) else { return }

        switch status {
        case .noteOn: midi?.sendNoteOnMessage(noteNumber: noteNumber, velocity: velocity)
        case .noteOff: midi?.sendNoteOffMessage(noteNumber: noteNumber, velocity: velocity)
        default: return

        }
}

But the enum cases seem to have vanished.

Edit: Reverting to 4.5.5 enabled me to use the solution here: AKMIDICallbackInstrument Implementation Issue


Solution

  • Try creating an AKMIDIStatus using the incoming byte, then reading the AKMIDIStatusType

    let callbackReceiver = AKMIDICallbackInstrument(midiInputName: "myCoolInput", 
        callback: { status, noteNumber, velocity in
            let statusType = AKMIDIStatus(byte: status)?.type //can be noteOn, noteOff, etc
            if(statusType == AKMIDIStatusType.noteOn){
                print("Note on.")
            }
    })
    
    myTrack.setMIDIOutput(callbackReceiver.midiIn)
    

    Just tested as working in AudioKit 4.6.1