Pitch in hertz
This example will show how you can specify any frequency
for jMusic notes.
When Notes are constructed they can take an int
or a double value.
When taking an int value it is assumed that MIDI note numbers
(i.e., 60 = C4) are desired.
When taking a double value for pitch, it is assumed the
value is a frequency in hertz (i.e., 440.0 = A4).
Because the pitch is a double value
(64 bit fractional) the pitch accuracy available in jMusic is ridiculously
fine (e.g., 1026.08940567 Hz).
All you micro-tonal composers go wild at this point! When specifying the
frequency value, division can be used as a convenient way of
accurately calculating harmonic intervals from a fundamental. For example,
Note n = new Note(440.0 * 3 / 2, CROTCHET); will produce a
tone that is a perfect fifth above the fundamental.
In this example, random frequency values are generated to
provide a texture of unpredictable interplay and beating.
The final Audio file will sound more or less like
this
Click here to view source.
Lets have a closer look.
import jm.JMC; import jm.music.data.*; import jm.util.View; import jm.util.Write; import jm.audio.Instrument; |
As well as the jm.music
classes, the jm.audio.Instrumentclass is imported. The jm.util.Write
class has the method for outputting an audio file to disk.
public final class SineTest implements JMC{ public static void main(String[] args){ Score s = new Score(); s.setTempo(60); Part p = new Part("Sine", 0);
|
The class is declared and variables set as required.
for(int i=0; i<20;i++) { Note n = new Note(Math.random() * 500 + 1000 , 4.0); n.setPan(Math.random()); Phrase phrase2 = new Phrase(n, Math.random() * 10); p.addPhrase(phrase2); }
s.addPart(p); |
In this code fragment a part is filled with twenty notes.
Each note has a random pitch between 1000 and 1500 Hertz (cycles per second).
Each note is wrapped in a phrase so that it can have a unique (random)
start time, in this way the notes can overlap causing the timbral
and beating patterns.
The part is added to the score, then instruments are set up.
Instrument inst = new SineInst(22000); Write.au(s,"SineTest.au", inst);
View.print(s); } }
|
This example uses the SineInst
instrument that produces a simple sine wave tone.
There are a number of instruments (including this one) that ship with jMusic.
You can create your own of any complexity you wish. The argument, 22000, specifies the sample rate.
This will determine the quality and highest frequency of the instrument
- for sine waves the nyquist frequency is more of a consideration than
quality
when choosing this value. Also, the higher the sample rate, the longer
it will take to render, and the larger the resulting file.
The Write.au() method renders the score to a file on disk with the provided name,
using the provided instrument(s);
Finally, the score is printed to the standard output so you
can see the pitch values that have been used. |