繁体   English   中英

Swift - AudioKit AKMidi到AKSequencer

[英]Swift - AudioKit AKMidi to AKSequencer

我目前有一个应用程序使用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)")
}

您实际上不需要直接从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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM