Reading a MIDI file

In the previous tutorial we wrote a short program that wrote a one octave chromatic scale to a MIDI file called scale.mid. Now we are going to learn how to read that MIDI file do a little manipulation to it and then write it to a new MIDI file. Our new program is called Transpose and here is the source code. Again you can copy this code into a file called Transpose.java and compile it as we learnt in the previous tutorial. Oh . . . and make sure that you have a copy of scale.mid in your current directory or Transpose.java will not know where to find it.

This program reads in the scale.mid file, transposes the scale up a fifth and then writes a new MIDI file called transposed.mid, lets have a closer look at how it works.

Note: At the time of writing this tutorial the Read.midi() method will only handle format 1 MIDI files with monophonic parts!

We're going to skip the initial stuff and get straight to the working part of the code this time . . . on with the show!

import jm.JMC;
import jm.music.data.*;
import jm.util.*;

The JMC is requred to use music-like language such as CROTCHET and C4, and STACCATO. The music.data classes are Note , Phrase, Score and so on. The jm.util package contains the Read and Write classes required to import and export MIDI files.

public final class Transpose implements JMC{
	/**
	 * Main method we're all good Java programs start
	 */
	public static void main(String[] args){
		Score score = new Score();
		//read the MIDI file scale.mid into our score object
		Read.midi(score, "scale.mid");
This is where we read in our scale.mid file. Looks pretty much the same as writing huh : ) Just remember that you need to create a new Score to read into whenever you read from a MIDI file.

		//now that we have read it we need to get to our Note data
		//first we get the part that it is in.
		Part part1 = instList.getPart(0); 
		//then we get the phrase
		Phrase scalePhrase = part1.getPhrase(0);
		//then we get our Note list
Once we have read our MIDI file into our new score we need to access the many Parts, Phrases and Notes that may be contained within. We know that our scale.mid file is simple and only has one part and one phrase so looking for the right one is simple (as there is only one to get we know that it must be the first), just grab the first object in the list (jmusic data index's are Vectors and, like arrays, start from 0). First we get our Part from the score, then we get the phrase from the part, and finally we get Notes from the phrase.

		//now we can transpose the notes in the note list
		int oldPitch, newPitch;
		for(int i=0;i<scalePhrase.size();i++){
			Note note = scalePhrase.getNote(i);
			oldPitch = note.getPitch();
			newPitch = (int) (oldPitch + 7);
			note.setPitch(newPitch);
		}
This is were we do the transposition. The basic idea is this. Cycle through the phrase, getting each note from in turn. As we get each note, grab its pitch and add 7 semitones to it. Then set the note's pitch value to reflect the change.

	    Write.midi(score, "transposed.mid");
	 
And finally we write to a new MIDI file called transposed.mid.

Well now isn't that easy. You can read and write MIDI files before you can even program in Java :) Hopefully you have bought a good Java book to help you understand what's going on.


jMusic Tutorial Index

Lets make a noise

Tea for two, or three, or.....