Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3222676
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:01:07+00:00 2026-05-17T16:01:07+00:00

I’m trying to read in .MID files to a Java program, and would like

  • 0

I’m trying to read in .MID files to a Java program, and would like to separate each note/chord so as to display them on a UI of some sort. I didn’t have much luck using the Sequencer API in Java, and trying to use MidiFileReader directly didn’t work for me either. I’ll attach the code I used here, if anyone wants to see it:

package miditest;
import java.io.File;
import java.io.IOException;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;

public class Main {
    public static void main(String[] args) throws InvalidMidiDataException, IOException, MidiUnavailableException{
    Sequence sequence = MidiSystem.getSequence(new File("test.mid"));

    // Create a sequencer for the sequence
    Sequencer sequencer = MidiSystem.getSequencer();
    sequencer.open();
    sequencer.setSequence(sequence);

    // Start playing
    sequencer.start();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:01:07+00:00Added an answer on May 17, 2026 at 4:01 pm

    I’ve never looked deeply into the MIDI support in Java, and the last time I played seriously with MIDI programming was when Commodore Amiga was king.

    It looks like you may have to do quite a bit of manual work. Here’s a crude example that interprets all NOTE_ON and NOTE_OFF events, for the rest of the events it just prints the command number.

    The reason it might seem trickier than one might have thought at first is because MIDI focuses on capturing the instrument events (for example, when a keyboard key was pressed, when it was released, etc), and not on sheet music notation.

    This code prints out one line per event, stating with the tick (which is the timing information for the event), channel, event type, note name, key, velocity

    import java.io.File;
    
    import javax.sound.midi.MidiEvent;
    import javax.sound.midi.MidiMessage;
    import javax.sound.midi.MidiSystem;
    import javax.sound.midi.Sequence;
    import javax.sound.midi.ShortMessage;
    import javax.sound.midi.Track;
    
    public class Test {
        public static final int NOTE_ON = 0x90;
        public static final int NOTE_OFF = 0x80;
        public static final String[] NOTE_NAMES = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
    
        public static void main(String[] args) throws Exception {
            Sequence sequence = MidiSystem.getSequence(new File("test.mid"));
    
            int trackNumber = 0;
            for (Track track :  sequence.getTracks()) {
                trackNumber++;
                System.out.println("Track " + trackNumber + ": size = " + track.size());
                System.out.println();
                for (int i=0; i < track.size(); i++) { 
                    MidiEvent event = track.get(i);
                    System.out.print("@" + event.getTick() + " ");
                    MidiMessage message = event.getMessage();
                    if (message instanceof ShortMessage) {
                        ShortMessage sm = (ShortMessage) message;
                        System.out.print("Channel: " + sm.getChannel() + " ");
                        if (sm.getCommand() == NOTE_ON) {
                            int key = sm.getData1();
                            int octave = (key / 12)-1;
                            int note = key % 12;
                            String noteName = NOTE_NAMES[note];
                            int velocity = sm.getData2();
                            System.out.println("Note on, " + noteName + octave + " key=" + key + " velocity: " + velocity);
                        } else if (sm.getCommand() == NOTE_OFF) {
                            int key = sm.getData1();
                            int octave = (key / 12)-1;
                            int note = key % 12;
                            String noteName = NOTE_NAMES[note];
                            int velocity = sm.getData2();
                            System.out.println("Note off, " + noteName + octave + " key=" + key + " velocity: " + velocity);
                        } else {
                            System.out.println("Command:" + sm.getCommand());
                        }
                    } else {
                        System.out.println("Other message: " + message.getClass());
                    }
                }
    
                System.out.println();
            }
    
        }
    }
    

    For example the fur elise.mid I had lying around here produces something like this at the beginning:

    @0 Channel: 1 Note on, E5 key=76 velocity: 127
    @192 Channel: 1 Note off, E5 key=76 velocity: 64
    @192 Channel: 1 Note on, D#5 key=75 velocity: 127
    @384 Channel: 1 Note off, D#5 key=75 velocity: 64
    @384 Channel: 1 Note on, E5 key=76 velocity: 127
    @576 Channel: 1 Note off, E5 key=76 velocity: 64
    @576 Channel: 1 Note on, D#5 key=75 velocity: 127
    @768 Channel: 1 Note off, D#5 key=75 velocity: 64
    @768 Channel: 1 Note on, E5 key=76 velocity: 127
    @960 Channel: 1 Note off, E5 key=76 velocity: 64
    @960 Channel: 1 Note on, B4 key=71 velocity: 127
    @1152 Channel: 1 Note off, B4 key=71 velocity: 64
    @1152 Channel: 1 Note on, D5 key=74 velocity: 127
    @1344 Channel: 1 Note off, D5 key=74 velocity: 64
    @1344 Channel: 1 Note on, C5 key=72 velocity: 127
    @1536 Channel: 1 Note off, C5 key=72 velocity: 64
    @1536 Channel: 1 Note on, A4 key=69 velocity: 127
    @1920 Channel: 1 Note off, A4 key=69 velocity: 64
    

    UPDATE:
    The channels are the 16 channels of the MIDI specification.

    http://www.midi.org/techspecs/gm.php

    Channels: All 16 MIDI Channels are
    supported. Each Channel can play a
    variable number of voices (polyphony).
    Each Channel can play a different
    instrument (sound/patch/timbre).
    Key-based percussion is always on MIDI
    Channel 10.

    And velocity is one of the attributes used to control the sounds. For example, capturing MIDI data on a keyboard it’d be the force with which you press a key. Normally it controls the volume of the sound. See here for more details: http://audio.tutsplus.com/tutorials/production/7-ways-to-use-and-edit-midi-velocity/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.