Linus
Linus

Reputation: 77

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)
}


}

Upvotes: 0

Views: 73

Answers (2)

Aurelius Prochazka
Aurelius Prochazka

Reputation: 4573

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

Upvotes: 0

Mark Jeschke
Mark Jeschke

Reputation: 274

Linus,

Please check out the AudioKit v5 Cookbook in SwiftUI. It includes a MIDI monitor example that prints out all of the the MIDI notes, program changes, and continuous control numbers that your app receives:

https://github.com/AudioKit/Cookbook/blob/main/Cookbook/Cookbook/Recipes/MIDIMonitor.swift

Take care,
Mark

Upvotes: 1

Related Questions