首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从midi文件IOS读取时间签名(如4/4)

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

Stack Overflow用户
提问于 2014-09-16 23:53:15
回答 2查看 567关注 0票数 0

花了将近半天的时间,我仍然无法理解如何使用iOS (目标c,CoreMIDI)从midi文件中获得4和4 (4/4)的值​​。

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

我尝试了以下代码:

代码语言:javascript
复制
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);

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

不知道如何处理这些来自NSLog的信息。

如果midi文件是6/2、4/2或5/8,那么我只想得到这个值(第二个/第一个)

有人能帮我算一下吗?谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-17 06:53:39

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

时间签名是使用节奏轨道中的时间签名元事件指定的。因此,您必须使用MusicSequenceGetTempoTrack获得节奏跟踪,使用MusicEventIterator搜索具有kMusicEventType_Meta类型的事件,并检查这些事件是否为时间签名元事件(如果时间签名更改,可能会出现多个时间签名事件)。

票数 2
EN

Stack Overflow用户

发布于 2014-09-17 11:20:45

谢谢你的CL。用户经过小规模的实验后,我做了并得到了以下结果:

在这里,我们加载midi文件:

代码语言:javascript
复制
+(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];
}

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

代码语言:javascript
复制
+(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;
}

结果是:

代码语言:javascript
复制
**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;

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25880178

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档