Search code examples
swiftaudiokit

How to use AudioKit to write a Mac command line app that print all the midi notes from keyboard?


I want to write a simple program that will print the notes input from the keyboard. I have something already shown below, but the program will finish and won't wait there for midi signal. Any ideas? Thanks

The main file:

import Foundation
import AudioKit

let midi = MIDI()
midi.openInput()
let receiver = MIDIReceiver()
midi.addListener(receiver)

print("Done")

The MIDIReceiver class:

import Foundation
import AudioKit
import CoreMIDI

class MIDIReceiver: MIDIListener {
func receivedMIDINoteOn(noteNumber: MIDINoteNumber, velocity: MIDIVelocity, channel: MIDIChannel, portID: MIDIUniqueID?, offset: MIDITimeStamp) {
    print(noteNumber)
}

func receivedMIDINoteOff(noteNumber: MIDINoteNumber, velocity: MIDIVelocity, channel: MIDIChannel, portID: MIDIUniqueID?, offset: MIDITimeStamp) {
    print(noteNumber)
}

func receivedMIDIController(_ controller: MIDIByte, value: MIDIByte, channel: MIDIChannel, portID: MIDIUniqueID?, offset: MIDITimeStamp) {
    print(controller)
}

func receivedMIDIAftertouch(noteNumber: MIDINoteNumber, pressure: MIDIByte, channel: MIDIChannel, portID: MIDIUniqueID?, offset: MIDITimeStamp) {
    print(noteNumber)
}

func receivedMIDIAftertouch(_ pressure: MIDIByte, channel: MIDIChannel, portID: MIDIUniqueID?, offset: MIDITimeStamp) {
    print(pressure)
}

func receivedMIDIPitchWheel(_ pitchWheelValue: MIDIWord, channel: MIDIChannel, portID: MIDIUniqueID?, offset: MIDITimeStamp) {
    print(pitchWheelValue)
}

func receivedMIDIProgramChange(_ program: MIDIByte, channel: MIDIChannel, portID: MIDIUniqueID?, offset: MIDITimeStamp) {
    print(program)
}

func receivedMIDISystemCommand(_ data: [MIDIByte], portID: MIDIUniqueID?, offset: MIDITimeStamp) {
    print(data)
}

func receivedMIDISetupChange() {
    print("SetupChange")
}

func receivedMIDIPropertyChange(propertyChangeInfo: MIDIObjectPropertyChangeNotification) {
    print(propertyChangeInfo)
}

func receivedMIDINotification(notification: MIDINotification) {
    print(notification)
}


}

Solution

  • Just make the program keep running until a keypress happens, say pressing the "q" or "esc" key for example.