My Library of reusable methodsThis tutorial outlines two classes. One which has a bunch of useful methods, and the other which creates a jMusic score by calling those library methods as required and structuring the results into a completed piece. Click here to view MyLibrary source .. Click here to view UseMyLibrary source .. Let's have a closer look.
The above class consists of three methods: basicDrums(), majorChord(), andbassPhrase(). These methods contain algorithms for constructing Phrases. The theory is that when building your piece you can simply call upon these methods to do the work for you, minimising code and maximising reusability. In the class below (UseMyLibrary) we are able to call upon these mthods to quickly and easily retrieve the parts. basicDrums() simply returns a basic rock drum beat. When we call majorChord() or bassPhrase(),we pass the methods a note which these methods can use to base their construction upon (i.e. so that a chord or bass line is built that suits the current key). By declaring each method as 'static', we are able to use these methods without having to instantiate a MyLibrary object. Infact, we havn't even bothered writing a constructor for the class as doing so would be pointless! Now we will look at using the methods. Below is the class 'UseMyLibrary'. In this class we want to create a simple 8-bar Blues progression (the Blues progression has the advantage of using only major chords). Firstly, we must define the chord progression. This is done by creating an array of integers. Within this array we store the values of the root note of each chord we wish to use, in order (i.e. if a chord repeats, we must repeat the entry). Since we have implemented JMC, we could use actual names here instead of numbers, such as 'C4' etc. Next we create a Score object to store our Parts in, and in turn our Phrases. Next we will address each Part. Firstly, the drums. After making the Part object, we need to add a Phrase of musical data to the object. This is easy now, since we have a library to do all this for us. Therefore we can simply say phr = MyLibrary.basicDrums();. Since the drums dont have a key we don't need to pass it anything. Now, using the Mod.repeat() method, we can extend this Phrase to last for the entire 8 bars. Then we simply add this Phrase to the Part, and add the Part to the Score. The next two parts are essentially the same, the only difference being that one creates a piano chord part and the other a bass part. For both, a for loop is started, which loops once for every chord in the chordProgression. Each time the loop is run, we simply call the appropriate MyLibrary method and send as a parameter the current chord (the chord is stored in the current position within the chordProgression array). Once all three Parts are added to the Score, we can simply create a midi file of the data. Try it and see what you get!
As you can probably see, using a library can make the
code a lot easier to read. In addition, you can use these methods
in any new class you write, without having to copy/paste code.
Reusing existing code and methods is one of the great advantages
of Java and Object-Orientated (OO) programming!
|