Lucas
Lucas

Reputation: 6729

How do a play a midi note with a determined duration

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

Answers (1)

Gene De Lisa
Gene De Lisa

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

Related Questions