Previous  Next          Package

Introduction to MIDICompose

The very basics


In this lesson we will learn the basic steps of using 'MIDICompose'. We will create a short well known melody. First of all, we have to create a JAVA file. we will call it 'MyCompose.java'. Then we implement the package 'MIDICompose' and define a class called MyCompose. To have access to the most common MIDI-values we also implement the interface 'MIDIVariables'. To have access to the most common General-MIDI-values we implement the interface 'GMVariables'.

MyCompose.java (Version 1)

To create a MIDI-file, you need some classes and methods from the package 'java.io'. All MIDI-events write their data to a 'FileOutputStream'. So you have to create a valid 'FileOutputStream' before you can print the MIDI-data to a MIDI-file. The method 'print(FileOutputStream fpp)' in the class 'MIDIFile' will use this Stream. After including exceptionhandling, we can start making music :-)

MyCompose.java (Version 2)

In the last example we created an instance of 'MIDIFile'. We called this instance 'midi' and used it to write the MIDI-file to disc (midi.print(fpp);). But this MIDI-file was empty. Now we will insert some notes to 'midi'. This can be done with the method 'append(MIDIObject object)'. In this case, we will use a 'Note' as object. A note can be created by the constructor 'new Note(int length, int pitch)'. We will use a QUARTER (defined in 'MIDIVariables') for length. For pitch we will have to do some arithmetics. We want to have a middle C. This can be written as C + OCTAVE * 5 (C at the 5th octave). We also append D, E and F.

MyCompose.java (Version 3)

Now we append more notes and create a well known melody. We set the tempo of midi to a higher value (FAST is defined in 'MIDIVariables'). In General-MIDI there are a lot of instruments defined. We want to hear this melody played by a OBOE (defined in 'GMVariables'). To do so, we have to enable General-MIDI, by appending a 'GMSystem' object to midi. And we have to set the instrument by appending a 'ProgramChange'. To ensure that the GM-module can change to the wanted program before the notes start playing, we append a short 'Rest' in front of the notes.

MyCompose.java (Version 4)

This is the end of the first lesson. In the next lesson you will learn how to copy and change melodies. You will also learn to use ghostcopies of melodies.


 Previous  Next          Package