Friday, June 1, 2012

Fun with music in Java

Music programming in Java is now easy with jfugue.

Step 1. Download

Go to jfugue.org → Download → jfugue-4.0.3.jar

Step 2. Place it in the classpath

Eclipse: Project > Java Build Path > Libraries > Add External JARs…
Netbeans: File > “Project” Properties > Libraries > Add JAR/Folder
JDeveloper: Tools > Project Properties… > Libraries and Classpath > Add JAR/Directory…

Step 3. Have fun

Play a note

new Player().play("C");

Play a note on a different octave

// 7th octave of the piano
new Player().play("C7");

Play a note with different duration

// w means whole = 4/4
new Player().play("Cw");

Play two notes

new Player().play("C D");

Use a rest

new Player().play("C R D");

Play some notes together

// h means half = 2/4
new Player().play("C5h+E5h+G5h+C6h");

Pick a different instrument

new Player().play("I[HARMONICA] C D E");

Play a simple song

new Player().play("I[BLOWN_BOTTLE] Gh Eq Cq | Gw | Gh Eq Cq | Gw");

Play a simple song with different tempo

new Player().play("T[220] I[KALIMBA] Gh Eq Cq | Gw | Gh Eq Cq | Gw");

Create a rythm!

Rhythm rhythm = new Rhythm();
rhythm.setLayer(1, "...*...*...*...*");
rhythm.setLayer(2, "----------------");
rhythm.addSubstitution('.', "Ri");
rhythm.addSubstitution('*', "[SKAKUHACHI]i");
rhythm.addSubstitution('-', "[PEDAL_HI_HAT]s Rs");
new Player().play(rhythm.getPattern());

Conclusion

At last, it’s easy to program some music for our games and create sounds for our applications.
The sound API of JDK really needed this comfortable boost. Congratulations to David Koelle for creating this tool!




No comments: