Reputation: 6729
To play I'm using a AVAudioUnitSampler:
sampler.stopNote(note, onChannel: channel)
But to stop I don't wanna use:
DispatchQueue.main.asyncAfter(
deadline: DispatchTime.now() + Double(Int64(0.7 * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC))
{
sampler.stopNote(note, onChannel: channel)
}
What's a better solution? Thanks
Upvotes: 0
Views: 685
Reputation: 3838
You can create a MusicSequence and each event in it will be scheduled by a MusicPlayer.
You can use AVMIDIPlayer, but you need to convert the MusicSequence into Data since it doesn't grok MusicSequence directly.
If you want to emulate note on via a button press and note off via button release, then use MusicDeviceMIDIEvent.
let status = MusicDeviceMIDIEvent(midisynthUnit, noteCommand, noteNum, velocity, 0)
You might want to read this for more info on using AVAudioUnitSampler.
Upvotes: 3