Jump to content

JFugue

From Alfawiki
Adapted from the English Wikipedia article JFugue, available under CC BY-SA 4.0Updated 9 August 2025
JFugue
Company typeOpen source software
IndustryMusic software, Audio programming
Founded2002
FounderDavid Koelle
ProductsMusic programming toolkit for Java
Websitewww.jfugue.org

JFugue is an open source programming toolkit designed to facilitate music programming in the Java programming language by abstracting away the complexities associated with MIDI. Created by David Koelle in 2002, JFugue initially featured a proprietary license in version 2, which later transitioned to LGPL-2.1-or-later for versions 3 and 4. The latest iteration, JFugue 5.0, released in March 2015, is distributed under the Apache-2.0. Brian Eubanks has emphasized its effectiveness for swiftly playing music or producing MIDI files.

Example

The following Java snippet demonstrates how to play the C-major scale using JFugue:

<syntaxhighlight lang="java"> import org.jfugue.player.Player;

public class HelloWorld {

 public static void main(String[] args) {
   Player player = new Player();
   player.play("C D E F G A B");
 }

} </syntaxhighlight>

This string input employs "Staccato," a compact notation system crafted for clarity that can express the full range of MIDI capabilities. While its primary function is converting Staccato notation to MIDI, JFugue also supports other formats, including MIDI to MusicXML and Staccato to LilyPond. Below is an example illustrating how to translate a MIDI file into Staccato:

<syntaxhighlight lang="java"> MidiParser parser = new MidiParser(); StaccatoParserListener listener = new StaccatoParserListener(); parser.addParserListener(listener); parser.parse(MidiSystem.getSequence(new File("YourMidiFile.mid"))); Pattern staccatoPattern = listener.getPattern(); System.out.println(staccatoPattern); </syntaxhighlight>

Version 5.0 of JFugue incorporates classes that embody fundamental music theory elements such as intervals, scales, chords, and chord progressions.

At the heart of JFugue are Patterns—musical sequences that can be merged, repeated, and manipulated through music-aware methods:

<syntaxhighlight lang="java"> Pattern pattern1 = new Pattern("A B C"); Pattern pattern2 = new Pattern("G F E"); pattern1.add(pattern2).repeat(3); Player player = new Player(); player.play(pattern1); </syntaxhighlight>

JFugue makes use of fluent interfaces (method chaining), enabling succinct and expressive code examples like this:

<syntaxhighlight lang="java"> Chord[] chords = new ChordProgression("I IV V").setRoot("C").getChords(); </syntaxhighlight>

Advanced features

The library supports microtonal music through a Staccato command denoted by 'm' followed by a frequency value in Hertz. This microtone notation can be combined with note duration (such as 'w' for whole note) and velocity settings (e.g., "note on" intensity). Internally, these microtones are translated into MIDI Pitch Wheel and Note events to achieve the intended sound.

<syntaxhighlight lang="java"> Player player = new Player(); player.play("m440.0q A4q"); // Equivalent sounds player.play("m568.7w"); // Whole note at 568.7 Hz </syntaxhighlight>

JFugue provides an easy-to-use interface for constructing beats and rhythms on the percussive MIDI Channel (channel 10). While it includes default mappings for standard percussion instruments (for instance, "O" representing the bass drum), users can define custom mappings using the Rhythm constructor.

<syntaxhighlight lang="java"> Rhythm rhythm = new Rhythm()

 .addLayer("O..oO...O..oOO..")
 .addLayer("..S...S...S...S.")
 .addLayer("````````````````")
 .addLayer("...............+");

new Player().play(rhythm.getPattern().repeat(2)); </syntaxhighlight>

The architecture of JFugue supports the development of programmatic utilities capable of analyzing and transforming music across various formats. For example, one can create a tool that parses a MIDI file and enumerates all instruments contained within:

<syntaxhighlight lang="java"> public class InstrumentToolDemo {

   public static void main(String[] args) throws Exception {
       MidiParser parser = new MidiParser();
       InstrumentTool instrumentTool = new InstrumentTool();
       parser.addParserListener(instrumentTool);
       parser.parse(MidiSystem.getSequence(new File("YourMidiFile.mid")));
   
       for (String name : instrumentTool.getInstrumentNames()) {
           System.out.println(name);
       }
   }

}

class InstrumentTool extends ParserListenerAdapter {

   private List<String> instrumentNames = new ArrayList<>();
   
   @Override
   public void onInstrumentParsed(byte instrument) {
       String name = MidiDictionary.INSTRUMENT_BYTE_TO_STRING.get(instrument);
       if (!instrumentNames.contains(name)) instrumentNames.add(name);
   }
   
   public List<String> getInstrumentNames() { return instrumentNames; }

} </syntaxhighlight>

Moreover, JFugue extends Java's built-in MIDI Transmitter and Receiver classes, streamlining the code necessary to interface with external MIDI hardware.

Uses in other applications

JFugue has been incorporated into a range of software solutions and creative projects, demonstrating its adaptability in music programming. Key examples include:

  • TransProse, which converts literary classics into musical compositions utilizing JFugue.
  • The JFugue Music NotePad, providing an interface designed for composing music.
  • JFrets, an instructional resource aimed at teaching guitar tablature.
  • Log4JFugue, a tool that transforms log4j log files into musical sequences, allowing auditory tracking of system events.
  • Audovia, a database-driven application that generates music through JFugue MusicStrings.

Moreover, JFugue is used to audibly indicate the success or failure of software builds.

Praised for its ease of use and robust capabilities, JFugue enables significant musical programming with minimal Java code, and it is referenced in the publication Groovy in Action.

External links