开发者

MIDI File Parsing

How can we do midi file parsing using objective-C ?

In Java for MIDI file parsing there is packa开发者_开发百科ge called javax.sound.midi; Is there something in Objective-c ?

Would be of great help if anybody of you send a reply..

Thanks in advance.


You can parse a MIDI file using CoreMidi. The general idea is that you get the MusicSequence from the file:

MusicSequence s;
NewMusicSequence(&s);

NSString *midiFilePath = [[NSBundle mainBundle]
                          pathForResource:path
                          ofType:@"mid"];

NSURL * midiFileURL = [NSURL fileURLWithPath:midiFilePath];

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

Then get the tracks:

MusicTrack track = NULL;

UInt32 tracks;
MusicSequenceGetTrackCount(s, &tracks);

for (NSInteger i=0; i<tracks; i++) {
    MusicSequenceGetIndTrack(s, i, &track);

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

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

    Boolean hasNext = YES;

    // A variable to store note messages
    MIDINoteMessage * midiNoteMessage;

    // 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_MIDINoteMessage) {
            // Cast the midi event data as a midi note message
            midiNoteMessage = (MIDINoteMessage*) eventData;

        }

        // Load the next event
        MusicEventIteratorNextEvent(iterator);
    }
}


There are plenty of sites describing the MIDI format if you're up to rolling your own loader, or consider MusicXML which should be easier to load with NSXMLParser. I don't know much about this format, or what exactly it is you're trying to achieve so thats all I can recommend. CoreMidi only deals with comms with midi devices, not playback or file parsing (as far as I know). You'll also need to write your own player if thats what you're thinking of doing and thats going to be the real challenge.

Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜