问题
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