How to change MIDI TEMPO on the fly? [CoreMIDI] iOS
I have a MusicTrack with MIDI Notes set to MusicSequence playing with MusicPlayer. The problem occurs when I tried to adjust the tempo using
MusicTrackNewExtendedTempoEvent (musicTrack, 0.0, newBPM);
Apparently this should change the TempoEvent in the MusicTrack that's playing, but it isn't. Any idea why this might be happening?
+3
source to share
1 answer
First you need to remove all tempo events from the tempo track.
static void removeTempoEvents(MusicTrack tempoTrack){
MusicEventIterator tempIter;
NewMusicEventIterator(tempoTrack, &tempIter);
Boolean hasEvent;
MusicEventIteratorHasCurrentEvent(tempIter, &hasEvent);
while (hasEvent) {
MusicTimeStamp stamp;
MusicEventType type;
const void *data = NULL;
UInt32 sizeData;
MusicEventIteratorGetEventInfo(tempIter, &stamp, &type, &data, &sizeData);
if (type == kMusicEventType_ExtendedTempo){
MusicEventIteratorDeleteEvent(tempIter);
MusicEventIteratorHasCurrentEvent(tempIter, &hasEvent);
}
else{
MusicEventIteratorNextEvent(tempIter);
MusicEventIteratorHasCurrentEvent(tempIter, &hasEvent);
}
}
DisposeMusicEventIterator(tempIter);
}
static void setTempo(MusicSequence sequence,Float64 tempo){
MusicTrack tempoTrack;
MusicSequenceGetTempoTrack(sequence ,&tempoTrack);
removeTempoEvents(tempoTrack);
MusicTrackNewExtendedTempoEvent(tempoTrack,0, tempo);
}
+5
source to share