我目前有一个使用AKKeyboard使用振荡器创建声音的应用程序。每当弹奏键盘时,我也会获取MIDI数据。我想做的是根据收到的MIDI数据创建AKSequence。
任何建议或指示都将不胜感激,谢谢。
这是我的部分代码:
var bank = AKOscillatorBank()
var midi: AKMIDI!
let sequencer = AKSequencer()
let sequenceLength = AKDuration(beats: 8.0)
func configureBank() {
AudioKit.output = bank
do {
try AudioKit.start()
} catch {
AKLog("AudioKit couldn't be started")
}
midi = AudioKit.midi
midi.addListener(self)
midi.openInput()
}
// AKKeyboard Protocol methods
func noteOn(note: MIDINoteNumber) {
let event = AKMIDIEvent(noteOn: note, velocity: 80, channel: 5)
midi.sendEvent(event)
bank.play(noteNumber: note, velocity: 100)
}
func noteOff(note: MIDINoteNumber) {
let event = AKMIDIEvent(noteOff: note, velocity: 0, channel: 5)
midi.sendEvent(event)
bank.stop(noteNumber: note)
}
// AKMIDIListener Protocol methods..
func receivedMIDINoteOff(noteNumber: MIDINoteNumber, velocity: MIDIVelocity, channel: MIDIChannel) {
print("ReceivedMIDINoteOff: \(noteNumber), velocity: \(velocity), channel: \(channel)")
}
答案 0 :(得分:3)
您实际上并不需要直接从AKMIDIEvents构建序列。只需在调用AKKeyboardView的noteOn和noteOff方法时查询序列的currentPosition,然后根据此方法将事件以编程方式添加到音序器轨道中即可。
此过程基本上与此相同(当然要减去最后一步):https://stackoverflow.com/a/50071028/2717159
编辑-获取noteOn和noteOff时间以及持续时间:
// store notes and times in a dictionary:
var noteDict = [MIDINoteNumber: MIDITimeStamp]()
// when you get a noteOn, note the time
noteDict[currentMIDINote] = seq.currentPosition.beats
// when you get a noteOff
let endTime = seq.currentPosition.beats
if let startTime = noteDict[currentMIDINote] {
let durationInBeats = endTime - startTime
// use the startTime, duration and currentMIDINote to add event to track
noteDict[currentMIDINote] = nil
}