> MIDI > Musical Data > Lets make a noise | |||||||||||
Lets make a noiseEnough already I hear you say, lets make some noise. To hear the result play the MIDI file below
The first import statement gets a class called the jMusic Constants (hence JMC). The second line imports the data structure classes including Score, Part, Phrase, and Note. The third import statement gives us access to jMusic utilities, and we will use the Write utility to save the composition to disk as a MIDI file.
When the java file is compiled all the comments are disregarded - they have no impact on the .class file that is generated . But they are very useful for humans like us, and it is good programming practice to include comments in your code.
The public statement enables this class to potentially be used by all other classes, and to be run. The final keyword means that the class may never be subclassed. . . and sublclassed means . . . (later folks). In this line the class implements the interface JMC Constants are variables that are set once and never change. All constants in jMusic are in upper case.
All Java programs start in a main() method like the one defined above, but if you didn't already know that you probably need to take a quick look at a short java tutorial before going on. (There are several online java tutorials that will show more than enough to understand the basics of jMusic. Try looking here for a good place to bookmark.) A brief description of the keywords follows: The public keyword makes
this method 'visible' (able to be used) anywhere the class is visible.
The main() method in Java must take one argument, a string array - in this case called args (short for arguments). The [] brackets after String means that args is an array (a list) of String objects. (Strings are text objects i.e., words). F Finally the curly bracket at the end of the line { announces the start of a block of code, which in this case will contain the rest of the method. At the end of the method, as well see below, a matching bracket marks the end of the method. Anyway, all that may have been either trivial or confusing to you. In any case it was all standard Java, let's get onto more specific jMusic stuff.
Scores, Parts, and Phrases can take a string as a constructor argument which sets a title for them, this is rarely used in composing but would be useful in notational extensions to jMusic which might use the title to display at the top of the score or beside each part in the margin. The Score created in this program is declared as (named) 's'. So, whenever
we want to refer to that score we will use its name. The word new is a special Java command. It is regularly used, so get a feel for it. In this line it means than we are creating a new object that is an instance of the Score class; generating a new score. Classes can be like templates from which you can make new versions, called instances; the Score, Part and Phrase Classes are like this. So we create a new Score object, called s that has a title JMDemo1 - Scale. The Part object also takes two more arguments after the title, an instrument (in this case using one of th JMC constants for General MIDI sounds), and the channel. Notice that we use channel zero, which corresponds to MIDI channel one, because computer programming languages always count from zero. The Phrase object takes a title and a start time which indicates how far into the piece this Phrase should start playing. 0.0 indicates that it should start playing right from the start.
This is a simple loop that increments the pitch by one semitone on each iteration. On each iteration a new note, named n, is created. The new note object has two values set; its pitch and rhythm value. The pitch of the note is C4 plus the number of times we've been around the loop. C4 is a constant value set in JMC class which represents the MIDI pitch 60 (don't you think that C4 is easier to remember than 60!). You could just have easily used 60 instead of C4. Because the pitch changes each cycle around the for-loop we end up with a chromatic scale starting from middle C and growing up to the B below C5 in semitone steps. After the comma the word CROTCHET represents the note's rhythmic value. The constant CROTCHET (equals a double value 1.0) represents a crotchet [Americans may like to use the QUARTER_NOTE constant instead]. Every note in this program will be a CROTCHET in length. After we
create each note Note n = new Note(...) we place it into the phrase object phr.addNote(n)
that we created earlier. For now it might be useful to see the loop expressed a different way - this code has the same result but its structure is more English-like. int i = 0; Note n = new Note(C4+i, CROTCHET); }You can read this as: " i starts as zero. While i is less than 12 execute the loop. Make a new note with a pitch of middle C+i of a crotchet (quarter note) value. Add the note to the phr phrase. Increase the value of i by adding 1 to it. Continue."
This line uses the Write class. The Write class has methods for saving jMusic compositions as files to disk. In this case we are using the midi() method of the Write class. This is what the 'dot' indicates; Write.midi() which we saw also in s.addPart() The Write.midi() method takes two arguments, firstly, the score for exploring exporting to an SMF and, secondly, the name to save the file as (the file name). The last two elements of the class (not shown) are two curly brackets. One to close the method, the second to close the class. Whew. . . . There was quite a lot to take in for those new to Java. Following the instructions below to compile and run the application and if you need to re-read the tutorial a few times to take it all in. Then move on.Compiling and Running Once you have saved this code into a file called Scale.java
you can compile it using the Javac application and produce a Scale.class file. as complicated as you can imagine in fact, and we shall explore some of these options in further tutorials. If you are don't understand how everything in this program worked don't worry at all, there are plenty more easy examples to follow and a great way to learn to program is to learn by example. Do what you can and copy the rest, that's the way we like to program! The important thing is to use the examples as templates for your own explorations. Try using the above example to make a descending scale for example. If you are inexperienced at programming, you should have an introductory
text on programming, ideally one that focuses on the Java language. |
|||||||||||