如何即时更改 MIDI TEMPO? [核心 MIDI] iOS

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

我在 MusicSequence 中设置了带有 MIDI 音符的 MusicTrack,正在使用 MusicPlayer 播放。当我尝试使用;

来调整节奏时,问题就来了

MusicTrackNewExtendedTempoEvent(musicTrack, 0.0, newBPM);

显然这应该会更改正在播放的 MusicTrack 中的 TempoEvent,但实际上并没有。知道为什么会这样吗?

您首先必须从速度轨道中删除所有速度事件。

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);
}