4

I need to delete all tempo events from an AKSequencer instance but I can't find a way to do it.

I tried to use the clearRange() method as shown below but I'm not sure it is the right way because it won't work:

var sequencer = AKSequencer()

// successfully adding a few tempo events with addTempoEventAt(tempo bpm: Double, position: AKDuration)

...

// trying to remove them

let sequenceLength = sequencer.length
sequencer.clearRange(start: AKDuration(beats: 0.0), duration: sequenceLength)

Is there something I am missing?

MicheleDG
  • 93
  • 6
  • What behavior did you get, and what behavior were you actually expecting? – Robert Harvey Aug 25 '18 at 17:46
  • I need to rearrange bars with different tempos and time signatures in a sequence, so what I am trying to do is deleting all the tempo events and re-adding them in the new order. What happens, though, is that using clearRange() won't actually delete the tempo events, as @c_booth said, so I end up with both the old tempo events and the new. – MicheleDG Aug 26 '18 at 07:41

1 Answers1

3

Unfortunately clearRange() removes note events and meta events but, as you observed, it does not remove tempo events (the next version of the docs will make this explicit - thank you for catching this). AKSequencer has a private clearTempoEvents method called internally by the setTempo() method, so you can use setTempo() to clear all the existing tempo events. Of course, after removing the existing tempo events, this will also add a new tempo event at the start of the sequence with the tempo you include as an argument.

So it won't exactly give you a sequence with no tempo events - but a sequence needs a tempo and with no tempo events it will default to 120 - so forcing you to be explicit about the starting tempo isn't such a bad thing. Anyway, unless you really need for there to be absolutely no tempo events, setTempo() should do the trick for clearing all existing tempo events in the sequence.

c_booth
  • 2,185
  • 1
  • 13
  • 22
  • Thank you this is exactly what I was looking for, though now I am thinking: what if you need to delete only one tempo event in the middle of a sequence? The only way is to delete all tempo events and re-add them without the unwanted one? – MicheleDG Aug 26 '18 at 07:03
  • 1
    That's basically right, unless you want to start working directly with the CoreMIDI's C API. You could make a feature request about deleting single events if you needed it - but the cleanest way to implement it would still probably involve 1) iterating through the tempo track to get a record of the current tempo events 2) clearing the existing events, 3) re-inserting the old events with the unwanted ones filtered out. – c_booth Aug 26 '18 at 12:08