0

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


}
Linus
  • 69
  • 4

2 Answers2

1

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

Mark Jeschke
  • 274
  • 1
  • 6
  • I think the main problem is that Linus' command line app doesn't stay on long enough for midi messages to be seen. It just runs and quits as far as I can tell. – Aurelius Prochazka Oct 22 '20 at 09:54
0

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

Aurelius Prochazka
  • 4,510
  • 2
  • 11
  • 34