如何动态更改MIDI TEMPO? [CoreMIDI] iOS

时间:2015-06-04 16:50:07

标签: ios objective-c midi coremidi

我有一个带有MIDI音符的MusicTrack设置为MusicSequence,用MusicPlayer播放。当我尝试使用;

来调整速度时出现问题
  

MusicTrackNewExtendedTempoEvent(musicTrack,0.0,newBPM);

显然这应该改变正在播放的MusicTrack中的TempoEvent,但事实并非如此。知道为什么会这样吗?

1 个答案:

答案 0 :(得分:5)

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

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