JFugue是个用于音乐作曲的Java API。和其他的音乐API不同，他能够让你用数据字符串来指定音符、乐器、和弦，及其他音乐数据，而不必进行底层的MIDI操控。他和其他快速应用程式 研发环境类似，只不过是专门用于音乐编程的。在JFugue环境里，音乐编程目前相对简单，你不一定需要了解音乐理论知识；你能在Java应用程式正在运 行的时候用JFugue来演奏音乐，或把音乐保存为MIDI文件。
public class Example1
public static void main(String args)
Player player = new Player();
Pattern pattern = new Pattern("C D E F G A B");
Notes：你能用#和b字符分别表示一个音符是升半音还是降半音。除了用音符字母来指定音符之外，更有两种其他的方式来指定音符。一个是使用“音符值（note value）”，他是个表示音符的数值。MIDI有128个音符，编号是从0到127。第60号音符是Middle-C。要指定音符值，就要在一个方括号里加上数值，比如。第二种方式是使用“打击乐器名（percussion name）”。在MIDI里，第10个声部（Voice 9，声部的编号从0开始）被保留给打击乐器。在这个声部，你能演奏多种不同的打击乐器声音，而且你能通过在方括号里指定打击乐器名来表示他们，例如[Hi_Bongo]。
Duration：Duration用来表示音符演奏的音值。如果你不输入 音值，那么默认的就是1／4个音符。下面这些值都是允许的：全音值（“w”）、半音值（“h”）、1／4音值（“q”）、1／8（“i”）、 1／16（“s”）、1／32（“t”）、1／64（“x”）和1／128音值（“n”）。你能通过在音值字符后面加上一个点（“.”）来表示一个点音 值。
JFugue is a Java API for music programming. Unlike other music APIs, it allows you to play data strings specifying notes, instruments, chords, and other musical data, without low-level MIDI manipulation.
It is similar to other rapid application development environments, only for music programming. In the JFugue environment, music programming is now relatively simple and a familiarity with music theory essentials is not required; you can use JFugue in your Java applications to play music while your application is running, or you can save music into MIDI files.
Listing A shows you a JFugue example of a simple application playing notes of a scale (example1.java):Listing A
JFugue employs the concept of a Music String – it is used to specify the notes, instrument changes, and other data required to play music. The Music String is a string of characters, where each group of characters represents a musical command. A musical command can be of one of the following types:
Let's have a closer look at these commands.
Notes: You can indicate that a note is sharp or flat by using the # and b characters, respectively. In addition to specifying a note using the note letter, there are two other ways to specify a note. One is to use the note value, which is a numerical value that indicates the note. MIDI has 128 notes, numbered 0 through 127. Note 60 is Middle-C. To specify a note value, enclose the value in square brackets; for example, . The second way is to use a percussion name. In MIDI, the tenth voice (Voice 9, the voices begin with 0) is reserved for percussion sounds. There are a bunch of different percussion sounds you can play in that voice, and you can indicate them by giving the percussion name in brackets. For example,[Hi_Bongo].
Octave: MIDI is capable of playing notes than span 10 octaves. To indicate an octave, use the numbers 0 through 9. For example, here's an A note in the 6th octave: A6. If you don't specify the octave, the default is Octave 5.
Duration: The Duration tells for how long to play the note. If you don't enter a duration, the default is a quarter note. The following values are allowed: whole duration ("w"), half duration ("h"), quarter duration ("q"), eight ("i"), sixteenth ("s"), thirty-second ("t"), sixty-fourth ("x") and 1/128th duration ("n"). You can specify a dotted duration by following the duration character with a period ('.').
The duration of a dotted note is the original duration, plus half the duration. You can append durations to get a larger duration. For example, qh.would be a quarter note plus a dotted half note; wwww is four whole notes. In addition to specifying duration with a letter, you may also specify the duration as a decimal fraction. A quarter note would be 0.25, an eighth note would be 0.125, and so on. To represent a number as a decimal fraction use the / character followed by the fraction. For example, the following is an A note, 4th octave, half duration: A4/0.5.
Chords: Once you've specified the root (and, optionally, octave) of a chord, you can give its structure. All notes in a chord are played using the same instrument, in the same voice. There are many common chords structures recognized by JFugue, here are some of them: Major ("maj"), Minor ("min"), Major 7th ("maj7"), Minor 7th ("min7"), Diminished ("dim"), Augmented ("aug"). The chord indicator goes directly after the root and octave, and before the duration. For example, a C-major, 5th octave, quarter note would be C5majq.
There are two special characters that you may use to combine notes. The plus (+) character can be used to play multiple notes in at the same time (in harmony). The underscore (_) character can be used to play notes in order (in melody) when the melody is being played with a harmony. For example, "C5q+E5q+G5q" will play the same as "C5majq" chord.
Velocity: You can indicate how hard a note is struck, and how quickly the note is released. To indicate how hard a note is struck, set the attack velocity using the "a" indicator, followed by a value from 0 to 127. To indicate how quickly a note is released, set the decay velocity using the "d" indicator, followed by a value from 0 to 127. The default attack velocity and decay velocity for each note is 64. Incidentally, this is a standard default value for MIDI devices that do not support velocity. For example, "C5qa120d30" represents a Middle C, quarter duration, which is struck very strongly (120), and is released softly (30).
Tempo: You should specify the tempo of your song - how fast or slow the song should be played. The tempo value represents "Pulses Per Quarter", or PPQ, which is how many "pulses", or clock cycles, to give a quarter note. A smaller value will result in a faster song, and a larger number will result in a slower song. A good value to start with is 120; this is also the default if you do not specify a tempo. The command is a T, followed by a number from 0 to infinity. For example: T120.
Voice: The Voice command tells JFugue to play the following notes in the given MIDI voice (also known as a channel). You can think of a voice as a musical track. Voices give you the ability to play multiple melodies at the same time. If you're programming piano music, you can use one voice for the treble clef, and a second voice for the bass clef. If you're creating a symphonic piece, you could give each instrument its own track. The command is "V", followed by a number from 0 to 15. There are 16 voices, numbered 0 through 15. The tenth voice (or channel) is special -- it is the channel that plays the rhythm instruments. Notes played in this voice sound like drums, cymbals, woodblocks, and other percussion instruments. Since we're counting from 0, the tenth voice is Voice 9. When playing notes on Voice 9, you can use percussion names such as [Bass_Drum] or [Open_Hi_Hat] to represent the percussion instruments.
Instrument change: The Instrument Change command tells JFugue to play the following notes with the given MIDI instrument number or name. The command is an "I", followed by either a number from 0 to 127, or the name of an instrument enclosed in brackets, like "I9" or "I[Guitar]".
A pattern is a collection of notes that comprise a musical statement. A pattern can represent an entire song, individual parts of a song, or snippets of frequently used music. Patterns may be created from other patterns, and the music in a pattern may be transformed in interesting and creative ways.
Patterns may be used to represent parts of a song. Suppose you're writing a standard rock or pop song. You might break your song into the following parts: Intro, Verse 1, Chorus, verse 2, Chorus, Verse 3, Chorus, Outro. To program this in JFugue, you can create the sections of music as patterns (Listing B, patterns.txt):Listing B
Then, you can create the song using those sections (Listing C):Listing C
You can play the entire song by method:
There are several additional features in JFugue which you can apply to a pattern.
Pattern Factories: A pattern factory gives you a pattern that you can use in your own songs.
Pattern Transformers: You can apply a transformation on any pattern. You can, for example, increase the interval between notes, add an echo to the notes, and many other transformations. The JFugue package comes with a few pattern transformers. They're all named XxxxPatternTransformer, where Xxxx indicates what they do -- for example, IntervalPatternTransformer changes the intervals between notes, DurationPatternTransformer changes the duration of notes, and ReversePatternTransformer reverses a pattern.
Pattern Tools: Patterns are a great way to manage sections of a musical statement. As you're using patterns, you may realize the need to perform. some calculation on a pattern. How long does this pattern play? What is the last voice that was used in this pattern? How many A notes are there in this pattern? Pattern tools are used to perform. calculations across a pattern. Like pattern transformers, they are parser listeners, and they work by parsing the pattern, and performing their calculations when the parser makes callbacks to the event methods.
To give you an idea for further steps, this is one more example, ready to work. For further reading the JFugue site is the best option. This is the simplest example of a song player (Listing D, example2.java):
来自 “ ITPUB博客 ” ，链接：http://blog.itpub.net/118838/viewspace-687551/，如需转载，请注明出处，否则将追究法律责任。