如何使用 AudioKit 编写一个 Mac 命令行应用程序来打印键盘上的所有 midi 音符?

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

我想写一个简单的程序来打印从键盘输入的音符。我已经在下面显示了一些内容,但是程序将完成并且不会在那里等待 MIDI 信号。有任何想法吗?谢谢

主文件:

import Foundation
import AudioKit

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

print("Done")

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


}

莱纳斯,

请查看 SwiftUI 中的 AudioKit v5 Cookbook。它包括一个 MIDI 监视器示例,打印出您的应用程序接收到的所有 MIDI 音符、程序更改和连续控制编号:

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

保重,
马克

只需让程序保持 运行 直到按键发生,例如按下“q”或“esc”键。