JFugue


JFugue is an open source programming library that allows one to program music in the Java programming language without the complexities of MIDI. It was first released in 2002 by David Koelle. The current version, JFugue 5.0, was released in March 2015. Brian Eubanks has described JFugue as "useful for applications that need a quick and easy way to play music or to generate MIDI files." JFugue is free software released under the Apache License.

Example

Here's an example Java program that will play the C-major scale in JFugue.

import org.jfugue.player.Player;
public class HelloWorld

The string passed to JFugue contains a series of musical instructions that JFugue parses and turns into musical events, which by default are rendered in MIDI. This format, called "Staccato," can represent all of the musical features of MIDI and is specifically designed to be easy for people to read and write. While the default use case for JFugue is to convert Staccato to MIDI, the architecture allows it to read and write musical information from and to a variety of formats. Below is an example converting a MIDI file to the Staccato format.

MidiParser parser = new MidiParser;
StaccatoParserListener listener = new StaccatoParserListener;
parser.addParserListener;
parser.parse; // Change to the name of a MIDI file that you own the rights to
Pattern staccatoPattern = listener.getPattern;
System.out.println;

JFugue 5.0 contains a set of classes that represent ideas in music theory, including intervals, scales, chords, and chord progressions.
The notion of Patterns is integral to JFugue. Patterns are used to represent phrases of music that can be combined, repeated, and altered using tools that are aware of the musical content of the pattern.

Pattern pattern1 = new Pattern;
Pattern pattern2 = new Pattern;
pattern1.add.repeat;
Player player = new Player;
player.play;

JFugue 5.0 makes extensive use of fluent interfaces, also known as method chaining, which lets developers write short, expressive pieces of code like the following:

Chord chords = new ChordProgression.setRoot.getChords;

Advanced Features

JFugue is capable of producing microtonal music by using a Staccato element consisting of the letter 'm' followed by the frequency in Hertz of the desired tone. Like other notes in JFugue, this tone may be followed by a duration and note dynamics. JFugue converts the microtone frequency to a sequence of MIDI Pitch Wheel and Note events to achieve the desired tone.

Player player = new Player;
player.play; // These sound the same
player.play; // Whole duration note at 568.7 Hertz

JFugue provides an intuitive programming interface for creating beats and rhythms. The characters in the strings below each correspond to a percussion note that is played on the percussive MIDI Channel ; default settings are provided for common sounds, although any Java Map or Character to String may be passed to the Rhythm constructor.

Rhythm rhythm = new Rhythm
.addLayer
.addLayer
.addLayer
.addLayer;
new Player.play.repeat);

In addition to allowing music to be converted from one music format to another, the architecture of JFugue can be used to create programmatic tools that are capable of both performing computations on incoming music and changing incoming music. The example below is a simple tool that keeps track of all instruments used in a musical piece.

public class InstrumentToolDemo
class InstrumentTool extends ParserListenerAdapter

JFugue provides functionality on top of Java's MIDI Transmitter and Receiver classes to reduce the amount of code that a developer would need to write to connect to external MIDI devices.

Uses in Other Applications

JFugue has been used in a number of applications, including software projects and artistic installations.
JFugue has been used to play music when a software build fails or succeeds.
JFugue is one of the few Java libraries that lets one do something interesting in as little as one or two lines of code. This distinction earned JFugue a place in the book "Groovy in Action"