Long notes after stop AKSequencer

梦想与她 提交于 2019-12-08 01:04:16

问题


Sometimes I need some long notes to keep playing after the sequencer stops.

akSequencer.stop() // Need to put some code to ask the question

Is there any way to keep the sound of a long note when AKSequencer stops?


回答1:


Instead of connecting your AKMusicTrack's MIDI output directly to your sampler (or oscillator bank or whatever), send it to an AKCallbackInstrument. In the callback function, you can check the status of the sequencer's MIDI messages, and send your the noteOn and noteOff messages to your sampler from there. In the callback you can add conditional logic, for example, you could use some flag to ignore the noteOff messages under certain conditions.

For the record, this is how I always set up my sequencers, since you can control not only to your sampler, but also external MIDI, Audiobus MIDI and so on, as well as UI updates, from the same AKMusicTrack using a callback.

var seq = AKSequencer()
var sampler = AKAppleSampler()
var callbackInst: AKCallbackInstrument!
var track: AKMusicTrack!
var allowNoteOff: Bool = true

func setupSequencerCallback() {
    track = seq.newTrack()
    callbackInst = AKCallbackInstrument()
    track.setMIDIOutput(callbackInst.midiIn)
    callbackInst.callback = { status, note, vel in
        switch status {
        case .noteOn:
            try? self.sampler.play(noteNumber: note, velocity: vel, channel: 0)
        case .noteOff:
            if self.allowNoteOff {
                try? self.sampler.stop(noteNumber: note, channel: 0)
            }
        default:
            return
        }
    }
}


来源:https://stackoverflow.com/questions/50682689/long-notes-after-stop-aksequencer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!