Pau Senabre
Pau Senabre

Reputation: 4209

How to change MIDI TEMPO on the fly? [CoreMIDI] iOS

I've got a MusicTrack with MIDI Notes set up into a MusicSequence, that ¡s being played with a MusicPlayer. The problem comes when I tried to adjust the tempo by using;

MusicTrackNewExtendedTempoEvent(musicTrack, 0.0, newBPM);

Apparently this should change the TempoEvent in the MusicTrack that is being played, but it doesn't. Any idea why this could be happening?

Upvotes: 4

Views: 829

Answers (1)

dave234
dave234

Reputation: 4945

You first have 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);
}

Upvotes: 6

Related Questions