簡體   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