Get Midi audio file properties

With this tutorial we are going to see how to get MIDI audio file properties. This is a useful operation when you want to inform the user about several important properties of the audio files.

Basically in order to get MIDI audio file properties one should follow these steps:

  • Obtain the default Sequencer connected to a default device using MidiSystem.getSequencer().
  • Create a stream from a file to the MIDI file.
  • Use MidiSystem.getMidiFileFormat to obtain the MIDI file format of the data in the specified input stream. This will return you an MidiFileFormat object.

Let’s see the code snippet that follows:

package com.javacodegeeks.snippets.desktop;


import javax.sound.midi.MidiFileFormat;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequencer;

public class GetMidiAudioFileProperties {

	public static void main(String[] args) throws Exception {

		// Obtains the default Sequencer connected to a default device.
		Sequencer sequencer = MidiSystem.getSequencer();

	    // create a stream from a file
	    InputStream is = new BufferedInputStream(new FileInputStream(new File("midifile.mid")));

	    // Obtain the MIDI file format of the data in the specified input stream.
	    MidiFileFormat fformat = MidiSystem.getMidiFileFormat(is);

	    // Obtain the MIDI file type.
		switch (fformat.getType()) {
			case 0:
				System.out.println("mid file format");
			case 1:
				System.out.println("rmf file format");

	    // Sets the current sequence on which the sequencer operates.
	    // The stream must point to MIDI file data.

	    // Obtains the length of the current sequence, expressed in microseconds
	    long durationMicroSecs = sequencer.getMicrosecondLength();
	    long durationSecs = durationMicroSecs/1000000;

	    System.out.println("durationInSecs " + durationSecs);




rmf file format
durationInSecs 122

This was an example on how to get Midi audio file properties.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button