从midi文件IOS读取时间签名(如4/4)

时间:2014-09-16 23:53:16

标签: ios objective-c time midi

花了差不多半天时间仍然无法理解如何使用iOS(目标c,CoreMIDI)从midi文件中获取4和4(4/4)的值

我有一个midi文件4/4 19 bar 120 bmp

我尝试了以下代码:

MusicTimeStamp inBeats;
UInt32 inSubbeatDivisor;
CABarBeatTime outBarBeatTime;
MusicSequenceBeatsToBarBeatTime(aSequence, inBeats, inSubbeatDivisor, &outBarBeatTime);

NSLog(@"%i, %i, %i, %i, %i, %f", outBarBeatTime.bar, outBarBeatTime.beat, outBarBeatTime.reserved, outBarBeatTime.subbeat, outBarBeatTime.subbeatDivisor, inBeats);

NSLOG结果:5,3,755,0,50617,0.000000

不确定如何处理来自NSLog的此信息。

如果midi文件是6/2或4/2或5/8。然后我想简单地得到这个值(第二个/第一个)

有人可以帮我计算一下吗?谢谢!

2 个答案:

答案 0 :(得分:2)

MusicSequenceBeatsToBarBeatTime只是在两种不同格式之间转换时间戳,但它不会告诉您它用于转换的时间签名。

时间签名是使用速度轨道中的时间签名元事件指定的。 因此,您必须使用MusicSequenceGetTempoTrack获取速度跟踪,使用MusicEventIterator搜索类型为kMusicEventType_Meta的事件,并检查这些事件是否为时间签名元事件(可能有多个时间签名变更时的时间签名事件。

答案 1 :(得分:0)

谢谢你的CL。用户经过小规模的实验后,我做了以下事情:

这里我们加载midi文件:

+(void) openMidiFile: (NSString *) path{

    MusicSequence s;
    NewMusicSequence(&s);


    NSURL * midiFileURL = [NSURL fileURLWithPath:path];

    MusicSequenceFileLoad(s, (CFURLRef)midiFileURL, 0, 0);

    //Get tempo and time signature
    [self getSequenceTempo:s];
}

在这里,我们从节奏轨道获取所需信息。

+(MusicTrack) getSequenceTempo: (MusicSequence) aSequence{
    OSStatus result = noErr;

    MusicTrack tempoTrack;

    result = MusicSequenceGetTempoTrack(aSequence, &tempoTrack);
    if (noErr != result) {
        NSLog(@"MusicSequenceGetTempoTrack, %d", (int)result);
    }

    // Create an interator
    MusicEventIterator iterator = NULL;
    NewMusicEventIterator(tempoTrack, &iterator);
    MusicTimeStamp timestamp = 0;
    MusicEventType eventType = 0;

    const void *eventData = NULL;
    UInt32 eventDataSize = 0;

    Boolean hasNext = YES;

    // A variable to store note messages
    ExtendedTempoEvent * message;
    MIDIMetaEvent *metaEvent;

    // Iterate over events
    while (hasNext) {

        // See if there are any more events
        MusicEventIteratorHasNextEvent(iterator, &hasNext);

        // Copy the event data into the variables we prepaired earlier
        MusicEventIteratorGetEventInfo(iterator, &timestamp, &eventType, &eventData, &eventDataSize);

        // Process Midi Note messages
        if(eventType==kMusicEventType_ExtendedTempo) {
            // Cast the midi event data as a midi note message
            message = (ExtendedTempoEvent*) eventData;
            // NSLog(@"%f", message->bpm); //TEMPO VALUE

        }
        if (eventType == kMusicEventType_Meta){
            metaEvent = (MIDIMetaEvent *) eventData;

            for (int i = 0; i < metaEvent->dataLength;i++){
                NSLog(@"%i, %i, %i, %i, %i", metaEvent->metaEventType, metaEvent->data[i], metaEvent->unused1, metaEvent->unused2, metaEvent->unused3);
            }
            NSLog(@"...");
        }

        MusicEventIteratorNextEvent(iterator);
    }


    return tempoTrack;
}

结果是:

**For 7/8:**
2014-09-17 14:03:14.073 MidiSequencer[1959:907] 88, **7**, 224, 215, 47
2014-09-17 14:03:14.075 MidiSequencer[1959:907] 88, **3**, 224, 215, 47
2014-09-17 14:03:14.077 MidiSequencer[1959:907] 88, 36, 224, 215, 47
2014-09-17 14:03:14.079 MidiSequencer[1959:907] 88, 8, 224, 215, 47
2014-09-17 14:03:14.080 MidiSequencer[1959:907] ...
2014-09-17 14:03:14.081 MidiSequencer[1959:907] 88, **7**, 224, 215, 47
2014-09-17 14:03:14.083 MidiSequencer[1959:907] 88, **3**, 224, 215, 47
2014-09-17 14:03:14.084 MidiSequencer[1959:907] 88, 36, 224, 215, 47
2014-09-17 14:03:14.086 MidiSequencer[1959:907] 88, 8, 224, 215, 47 


**For 7/4**
2014-09-17 13:59:45.455 MidiSequencer[1922:907] 88, **7**, 32, 220, 47
2014-09-17 13:59:45.457 MidiSequencer[1922:907] 88, **2**, 32, 220, 47
2014-09-17 13:59:45.459 MidiSequencer[1922:907] 88, 36, 32, 220, 47
2014-09-17 13:59:45.461 MidiSequencer[1922:907] 88, 8, 32, 220, 47
2014-09-17 13:59:45.462 MidiSequencer[1922:907] 88, **7**, 32, 220, 47
2014-09-17 13:59:45.464 MidiSequencer[1922:907] 88, **2**, 32, 220, 47
2014-09-17 13:59:45.465 MidiSequencer[1922:907] 88, 36, 32, 220, 47
2014-09-17 13:59:45.466 MidiSequencer[1922:907] 88, 8, 32, 220, 47

因此,如果我们看一下7/4示例,这里我们有值:7和值:2。第二个值意味着:0是1; 1是2; 2是4; 3是8而4是16;