Lets make a noiseEnough already I hear you say, lets make some noise. What follows is a simple program that plays a scale and saves it to a Standard MIDI File (SMF). To try this example download the code from the link below, it results in a file named 'Scale.java'. Don't worry if you don't understand the code, we haven't explained it to you yet! Just take a look... To hear the result play the MIDI file below. Lets have a closer look at what just happened.
Take another look at the package documentation to work out what gets imported from where if you're interested -> PACKAGES).
In this line the class implements the interface JMC. All you need to understand about interfaces for the moment is that whenever you want to be able to use CROTCHET (or just C) to represent crotchet (instead of 1.0) or C4 to represent the pitch middle C (instead of 60) then you need your class to implement the JMC interface. Have another look at JMC.java to see what constants are defined in there. Constants are variables that are set once and never change. All constants in jMusic are in upper case.
A brief description of the keywords follows: The public keyword makes this method 'visible' (able to be used) anywhere the class is visible. The static keyword indicates that there can only be one copy of this method, and this being the 'main' method where all Java applications start running we (of course) only want one starting place. The void keyword indicates that this method does not return any results to a calling method (again of course in this case considering that the main() method is the first method of all to be called!). 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.
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. For example, View.show(s); 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.
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. Phrases hold notes (and rests) in the order you enter them. The for-loop syntax can look a little odd at first. In time you will see this used so often that it will become second nature. 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."
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 RunningOnce 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. Once the program is compiled you can run the program using the Java application (on a Macintosh drop the .class file on the JBindery application icon) and a SMF called ChromaticScale.mid should appear in your current directory. You can then load the SMF into a sequencer and play the file. If you don't have a MIDI sequencer try using QuickTime's Movieplayer or the Microsoft media player, or on a UNIX machine try typing playmidi scale.mid on the command line. Congratulations, you have just made your first jMusic piece. Of course jMusic is capable of generating much more complicated music than this, 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. If you have a copy of the Computer Music Tutorial (Roads 1996) then read Chapter 2 starting on page 50 for an introduction to computer programming for musicians. Welcome to jMusic.
|