Reading MIDI Files

我与影子孤独终老i 提交于 2019-11-29 09:06:57

问题


What is the best way to read a MIDI file (chronologically) with multiple tracks? (Java)

Note: I don't want to play the MIDI file, just read the messages.

Couple ideas:

Is it safe to assume there are no note events shorter than the 1/64th note? Or should I visit every track and only move to the next tick after all other ticks tracks

  • Assume there is no midi event shorter than a 1/64th note, and move the current position tick count by that fix delta.
  • Visit every track and progress to the next earliest tick

回答1:


JFugue can read a MIDI file and sort the messages in chronological order.

The results can be read as JFugue MusicStrings (for example, C-sharp, 5th octave, whole note = "C#5w"), or you can write your own ParserListener and attach it to the MidiParser so you can output your own text.




回答2:


In Java, you can read a midi file with :

try {
        Sequencer sequencer = MidiSystem.getSequencer();
        sequencer.setSequence(MidiSystem.getSequence(yourMidiFile));
        sequencer.open();
        sequencer.start();
        while(true) {
            if(sequencer.isRunning()) {
                try {
                    Thread.sleep(1000); // Check every second
                } catch(InterruptedException ignore) {
                    break;
                }
            } else {
                break;
            }
        }

} catch(Exception e) {
        System.out.println(e.toString());
} finally {
    // Close resources
    sequencer.stop();
    sequencer.close();
}

This code should read your midi files (even if there are multiple tracks)



来源:https://stackoverflow.com/questions/1182877/reading-midi-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!