Two audio instruments at once
This class shows how to render two parts
with different instruments.
To hear the result download the MP3 file
below.
WaveformExample6.mp3
[148K]
View
/ Download source
Lets have a closer look.
import jm.music.data.*;
import jm.JMC;
import jm.audio.*;
import jm.util.*;
public class WaveformExample6 implements JMC {
public static void main(String[] args) {
new WaveformExample6();
}
public WaveformExample6() {
Phrase melody = new Phrase();
for(int i = 0; i < 24; i++) {
Note n = new Note(
(int)(Math.random() * 12) * 2 + 60,
(int)(Math.random()* 5) * 0.25 + 0.25,
(int)(Math.random() * 60 + 60));
melody.addNote(n);
}
Score score = new Score(new Part(melody));
Part part2 = new Part("Unison", 1);
Phrase phrase2 = new Phrase();
phrase2 = melody.copy();
part2.addPhrase(phrase2);
score.addPart(part2);
Instrument chiff = new ChiffInst(44100);
Instrument tri = new TriangleInst(44100);
Instrument[] ensemble = {chiff, tri};
Write.au(score, "WaveformExample6.au", ensemble);
}
} |
The score in this program is slighty more elaborate than previous.
The melody has random dynamic settings and there is a second part
that has a copy of the melodic phrase in it. These two parts play in
unison but on different instruments.
Part part2 = new Part("Unison", 1);
When the second part is decalared (above) it uses the constructor that
takes two elements, a name and instrument number.
In this case "Unison"
is the name and 1 is the instrument number. By default, parts use instrument
number 0 (zero) and so this is the instrument number
used by the initial part.
Instrument chiff = new ChiffInst(44100);
Instrument tri = new TriangleInst(44100);
The two instruments are declared (as above) in the normal way, but
the Write.au() method needs to know about both
of them so they are packed into an instrument array.
Instrument[] ensemble = {chiff, tri};
The array in this case is called "ensemble"
which reflects the metaphor of many instruments being in a band or group
which is availible to "play" the score.
Write.au(score, "WaveformExample6.au", ensemble);
This array is then passed to the Write.au()
method for rendering. The part assigned to instrument ) (zero) will
render with the chiff instrument
and part2 assigned to instrument number 1 will render with the tri instrument.
Following this process any number of instruments can be decalared and
packed into an instrument array.
Parts (or phrases) in the score can use any of the instruments in the
array.
Another interesting compositional issue pointed to by this example
is that because the parts are in unison the resulting music sounds like
one "instrument".
There will be, in jMusic, two directions from which to solve this multilayered
sound effect.
One is to have parallel simple instruments play the same notes,as done
here, or you could create a more complex instrument
that internally combines both the noise and triangle wave elements.
As you develop skills in instrument making and score writing with jMusic
you will find that there are many compositional situations which are
open
to score and/or instrument solutions.
We see this choice of working at the score or instrument level as positive
because it allows composers to choose the work method they prefer.