Reputation: 77
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
Reputation: 4573
Just make the program keep running until a keypress happens, say pressing the "q" or "esc" key for example.
Upvotes: 0
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