Thing which seemed very Thingish inside you is quite different when it gets out into the open and has other people looking at it

Tuesday, September 27, 2011

Kick Start on Music Programming – Create your first music program in java

In this tutorial I am going to talk about basics of music programming and music technology. Music programming is an interesting but a very vast area of learning and applying however, jMusic project makes music programmer’s life much easier and makes music programming more effective. jMusic library is an API for Java music programming and provides tools to for music compositional and audio processing.

Today I am going to talk about the fundamentals of music programming using jMusisc library and compose, and process and monitor simple music.

Today's TODO list ...
  1. Create a Simple Song
  2. Add Instrumentals
  3. Save and notate
Create Your First Song 

Creating your first song is crucial :) If you know the music notation to a particular song you can create almost any song using this technique (make sure you select your favorite song for this task) . Since most people know twinkle twinkle little star and its quite catchy I am going to select that song.

Before we start we need to make sure we have jMusic library, you need to download and import this library to your java class path.
First look at the manuscript notation of our songs.

For those who are not fluent with music notations and western music theory this is how it looks likes in abc format. (More like c,d,e,f,g format :) )


Now let's create this song in java.
package mymusicapp; 
import jm.JMC; 
import jm.music.data.Note;  
import jm.music.data.Part; import jm.music.data.Phrase; import jm.music.data.Score; import jm.util.Play;
import jm.util.Write;
public class Main implements JMC {
public static void main(String[] args) { 
Phrase phr = new Phrase("Twinkle twinkle", 0.0); int[] pitchArray = {C4,C4,G4,G4,A4,A4,G4,F4,F4,E4,E4,D4,D4,C4,G4,G4,F4,F4,E4,E4,D4,G4,G4,F4,F4,E4,E4,D4};
double[] rhythmArray = {C, C, C, C, C, C, M, C, C, C, C, C, C, M, C, C, C, C, C, C, M, C, C, C, C, C, C, M}; phr.addNoteList(pitchArray, rhythmArray);
Play.midi(phr);}
}
}
As you can see it is very easy to create music using jMusic library. All you need to do is add the notes and the pitch classes and put them together. If you look at it closely, Notes are given in “pitchArray” int array of pitch classes in C,D,E,F,G manner number 4 represents the fourth octave. And the duration of each pitch classes are given Crochet ( C ), Quaver (Q), Semi Quaver (SQ), Minim (M) ect.
Following table list duration of each note.


Musical Notation Name Duration jMusic Notation
Semibreve

Whole Note
4 Crotchets
4.0 SB
Minim

Half Note
2 Crotchets
2.0 M


Crotchet

Quarter Note
1 Crotchets
1.0 C
Quaver

Eight Note
½ a Crotchet
0.5 Q
Semi-Quaver

16th note
¼ a Crotchet
0.25 SQ


Add Instrumentals

Changing the instrument is easier than creating the song all you need to do is map your phrase to a part and play the part as shown below.

phr = new Phrase("Twinkle twinkle", 0.0);
Part p = new Part("FLUTE", FLUTE, 0);  
phr.addNoteList(pitchArray, rhythmArray);
p.add(phr);
Play.midi(p);

You can try and experiment with different musical instruments such as guitar violin ect. :)


Save and notate

You can save your creation as a midi file by simply adding the following code.
Write.midi(phr,"twinkle.mid");


Click here to listen to our creation

To view the manuscript notation you can use View.notate method and you can view the manuscript notation of your music creation.

View.notate(phr);

No comments:

Post a Comment