Previous Next Package
Introduction to MIDICompose
Usage of MIDIContainer
In the last lesson we learned to create a melody. If we want to use the same melody
again, we would have to append the whole notes again. To avoid this work there is a
class called ' MIDIContainer'. As a
'MIDIFile' a 'MIDIContainer' can store
'MIDIObject's (in fact 'MIDIFile' is a
'MIDIContainer' that can write its data to a file). We now will create such a
'MIDIContainer' and call it 'melody'. Then we will append all the notes to melody
instead of midi. At last we append melody to midi.
MyCompose.java (Version 5)
If we want to use melody at another position in midi, we simply append it again to midi.
To simply append it after the last melody we write 'midi.append(melody)'. If we want to
set it at a specific position (for example after the end of the 8th bar = beginning of
the 9th bar) we write 'midi.appendAt(melody, 8*WHOLE)'. Before we do so, we turn the
melody around by writing 'melody.turnRound()'.
MyCompose.java (Version 6)
In the last example, we didn't really append melody to midi but a copy of melody. If we
would change melody afterwards, these changes would not effect the copies of melody that
have been appended to midi before. To append not a copy, but the original melody to midi,
we need a 'Ghost'-copy of melody. A ghostcopy can be
created by writing 'new Ghost(melody)'. If we keep the last example but append only
ghostcopies to midi, the melody will be played from behind all three times. But we will
only make the second copy of melody a ghostcopy.
MyCompose.java (Version 7)
We can also create real copies of melody by writing 'melody.copy()'. In the next example
we will create a copy of melody, shorten its lenght to the half and append it before the
real melody. The real melody will be accompanied by the turned round melody. To do this
we turn around a copy of melody an append it at the same position as the correctly
played melody. To find and set the right positions, we use 'midi.position()'
and 'midi.setPosition(int length)'.
MyCompose.java (Version 8)
It sounds wrong! There are some notes deleted. This is because the two melodies played at
the same time send their notes at the same MIDI-channel (NoteOff!). To avoid such poor
notes, we will send the two melodies on different MIDI-channles. Without knowing we have
send all notes on MIDI-channel one (this is the default channel). Now we will force the
turned round melody to play its notes on channel two. There are more possibilities to do
so.
First we could create new 'Note's that send on
channel two. But this is not the best solution in this case.
Another way would be, to
create two different 'MIDIContainer's - one
sending only at channel one, the other only sending at channel two - but this is also not
the best way in this case.
We will simply make melody only send on channel two before appending the turned round
melody. This will have no influence to the other - already appended - melodies (we didn't
use ghostcopies). To force a 'MIDIContainer' to send only at one specific channel, the
'MIDIContainer' must be channelable. This can be done by writing 'setChannelable(int
channel)'. In this case we use CHAN_2 as channel (defined in
'MIDIVariables').
MyCompose.java (Version 9)
Only channel one is set to OBOE. We have to set channel two, too. So we insert a new
'ProgramChange' and set it to CLARINET for
channel two. Additionally we insert a 'Rest' between
the solo and the duet.
MyCompose.java (Version 10)
You have successfully done the second lesson. In the next
lesson, you will learn more about MIDIObjects.
Previous Next Package