Reading MIDI Files

前端 未结 2 1569
夕颜
夕颜 2020-12-30 10:01

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.

Co

相关标签:
2条回答
  • 2020-12-30 10:32

    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)

    0 讨论(0)
  • 2020-12-30 10:57

    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.

    0 讨论(0)
提交回复
热议问题