audio MIDI Music Algorithms Interfaces Programming Acoustics Context
> MIDI > Input / Output > Reading and Reusing MIDI file data    
 
   

Reading and Reusing MIDI file data

It can be useful to create musical material in other programs (such as sequencers) and bring it into jMusic.
This class shows how parts from various MIDI files can be brought together in a jMusic score.
This score can then itself be saved as a MIDI file - perhaps to find it's way back to a sequencer of other program.

Here is an example of the output from the class

Click here to view the source file

Let's have a closer look.

import jm.JMC;
import jm.util.*;
import jm.music.data.*;
import jm.music.tools.Mod;
import jm.midi.MidiSynth;
 
public final class MidiSynthTest implements JMC{
/**
* Main method where all good Java programs start
*/

public static void main(String[] args){
Score s = new Score();
s.setTempo(130.0);
for(int i=0; i<3;i++) {
Part p = new Part("part",i*5,i);
p.setTempo(120.0 + (double)i * 0.5);
for(int j=0; j<1;j++) {
Phrase phrase = new Phrase();
phrase = makePhrase(0.0, 50);
p.addPhrase(phrase);
}
s.addPart(p);
}
Play.midi(s);
}

Above is the whole file, apart from the composition method (shown below).
Starting from the bottom the Play.midi(s); line does all the work of interest to this tutorial.
It plays the score "s" via the JavaSound General MIDI sound set - as simple as that.

The rest of the code is dedicated to building a score of 3 parts each with one phrase.

private static Phrase makePhrase(double startTime, int length) {
Phrase phr = new Phrase(startTime);
int pitch = (int)(Math.random()*60+30);
for(int i=0; i < length; i++) {
pitch += (int)(Math.random()*10-5);
Note n = new Note(pitch, CROTCHET,
(int)(Math.random()*70 + 30));
phr.addNote(n);
}
return phr;
}
}
The score to be played is made up of phrases created by the method above.

 

jMusic Australia Council Queensland University of Technology Sitemap Contact Home Home http://www.qut.com http://explodingart.com/jmusic http://www.ozco.gov.au

Digital Instrument making Home