JFugue - Java API for Music Programming

Examples

One of the best ways to learn something new is to see it in action. The following samples are complete programs that show how to use JFugue. More examples are available in the JFugue Music Exchange.

List of examples below:

  • Example 1: Exploring the MusicString
  • Example 2: Sample Transcription of Music to JFugue
  • Example 3: Using Patterns to Build a Song
  • Example 4: Using Patterns to Create a Round or Fugue
  • Example 5: Create a Rhythm (This is cool!)




Example 1: Exploring the MusicString

The features of the JFugue MusicString are discussed in detail in Chapter 2 of The Complete Guide to JFugue. Chapter 2 is available for free on the Getting Started page.

 Player player = new Player();
Play the notes of an octaveplayer.play("C D E F G A B");
Play notes and rests in different octaves (0-10),
with different durations (w, h, q, etc)
player.play("C3w D6h E3q F#5i Rs Ab7q Bb2i");
Play notes using different instrumentsplayer.play("I[Piano] C5q D5q I[Flute] G5q F5q");
Play notes in harmony using different voicesplayer.play("V0 A3q B3q C3q B3q V1 A2h C2h");
Play some chords, and chord inversionsplayer.play("Cmaj5q F#min2h Bbmin13^^^");
For more examples, see The Complete Guide to JFugue or the Getting Started page



Example 2: Sample Transcription of Music to JFugue

Excerpt from Inventio 13 (J. S. Bach)Player player = new Player();
player.play("E5s A5s C6s B5s E5s B5s D6s C6i E6i G#5i E6i | A5s E5s A5s C6s B5s E5s B5s D6s C6i A5i Ri");



Example 3: Using Patterns to Build a Song

Sheet music for "Frere Jacques"
// "Frere Jacques"
Pattern pattern1 = new Pattern("C5q D5q E5q C5q");

// "Dormez-vous?"
Pattern pattern2 = new Pattern("E5q F5q G5h");

// "Sonnez les matines"
Pattern pattern3 = new Pattern("G5i A5i G5i F5i E5q C5q");

// "Ding ding dong"
Pattern pattern4 = new Pattern("C5q G4q C5h");

// Put all of the patters together to form the song
Pattern song = new Pattern();
song.add(pattern1, 2); // Adds 'pattern1' to 'song' twice
song.add(pattern2, 2); // Adds 'pattern2' to 'song' twice
song.add(pattern3, 2); // Adds 'pattern3' to 'song' twice
song.add(pattern4, 2); // Adds 'pattern4' to 'song' twice

// Play the song!
Player player = new Player(); player.play(song);



Example 4: Using Patterns to Create a Round or Fugue

Building on the "Frere Jacques" example above.
Listen to the song this program generates: frerejacques.mid
Pattern doubleMeasureRest = new Pattern("Rw Rw");

// Create the first voice
Pattern round1 = new Pattern("V0");
round1.add(song);

// Create the second voice
Pattern round2 = new Pattern("V1");
round2.add(doubleMeasureRest);
round2.add(song);

// Create the third voice
Pattern round3 = new Pattern("V2");
round3.add(doubleMeasureRest, 2);
round3.add(song);

// Put the voices together
Pattern roundSong = new Pattern();
roundSong.add(round1);
roundSong.add(round2);
roundSong.add(round3);

// Play the song!
player.play(roundSong);



Example 5: Create a Rhythm

Listen to the beat this program generates: beat16.mid
The Rhythm feature is explained in detail in The Complete Guide to JFugue.
 Rhythm rhythm = new Rhythm();
Bang out your drum beat rhythm.setLayer(1, "O..oO...O..oOO..");
rhythm.setLayer(2, "..*...*...*...*.");
rhythm.setLayer(3, "^^^^^^^^^^^^^^^^");
rhythm.setLayer(4, "...............!");
Associate percussion notes with your beat rhythm.addSubstitution('O', "[BASS_DRUM]i");
rhythm.addSubstitution('o', "Rs [BASS_DRUM]s");
rhythm.addSubstitution('*', "[ACOUSTIC_SNARE]i");
rhythm.addSubstitution('^', "[PEDAL_HI_HAT]s Rs");
rhythm.addSubstitution('!', "[CRASH_CYMBAL_1]s Rs");
rhythm.addSubstitution('.', "Ri");
Play the rhythm! Pattern pattern = rhythm.getPattern();
pattern.repeat(4);
Player player = new Player();
player.play(pattern);