ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Jfugue编程概要

Jfugue编程概要

原创 Linux操作系统 作者:grazier 时间:2011-02-19 15:27:57 0 删除 编辑

转自:http://www.sudu.cn/info/html/edu/java/20060912/304274.html

JFugue是个用于音乐作曲的Java API。和其他的音乐API不同,他能够让你用数据字符串来指定音符、乐器、和弦,及其他音乐数据,而不必进行底层的MIDI操控。他和其他快速应用程式 研发环境类似,只不过是专门用于音乐编程的。在JFugue环境里,音乐编程目前相对简单,你不一定需要了解音乐理论知识;你能在Java应用程式正在运 行的时候用JFugue来演奏音乐,或把音乐保存为MIDI文件。

列表A向你显示了一个演奏音阶中乐符的简单JFugue应用程式(example1.java):

列表A
importorg.JFugue.*;
public class Example1
{
        public static void main(String[] args)
        {
                Player player = new Player();
                Pattern pattern = new Pattern("C D E F G A B");
                player.play(pattern);
                System.exit(0);
        }
}

音乐字符串

JFugue使用了“音乐字符串(Music String)”的概念??他用来指定音符、乐器变化,及演奏音乐所需要的其他数据。这个音乐字符串是个由字符组成的文字串,其中的每一组字符都代表着一条音乐命令。音乐命令能是下面几种类型中的一种:

  • Notes和chords??用来指定将要演奏的音符或和弦,及音值;
  • Tempo??用来指定乐曲的速度;
  • Voice??用来表示音符演奏的声部;
  • Instrument change??用来更换演奏音符的乐器或音色;
  • Controller messages??用来设置MIDI控制器事件,例如平衡等;
  • Variables??用来定义其他命令所使用的值。

目前就让我们来看看这些命令吧。

Notes你能用#和b字符分别表示一个音符是升半音还是降半音。除了用音符字母来指定音符之外,更有两种其他的方式来指定音符。一个是使用“音符值(note value)”,他是个表示音符的数值。MIDI有128个音符,编号是从0到127。第60号音符是Middle-C。要指定音符值,就要在一个方括号里加上数值,比如[60]。第二种方式是使用“打击乐器名(percussion name)”。在MIDI里,第10个声部(Voice 9,声部的编号从0开始)被保留给打击乐器。在这个声部,你能演奏多种不同的打击乐器声音,而且你能通过在方括号里指定打击乐器名来表示他们,例如[Hi_Bongo]。

OctaveMIDI能够演奏超过10个八度音阶的音符。要表示八度音阶,就要使用数字0到9。例如,有一个位于第6个八度音阶的A音符,他就要被表示为A6。如果你没有指定八度音阶,那么默认的音阶是Octave 5。

DurationDuration用来表示音符演奏的音值。如果你不输入 音值,那么默认的就是1/4个音符。下面这些值都是允许的:全音值(“w”)、半音值(“h”)、1/4音值(“q”)、1/8(“i”)、 1/16(“s”)、1/32(“t”)、1/64(“x”)和1/128音值(“n”)。你能通过在音值字符后面加上一个点(“.”)来表示一个点音 值。

英文原文:http://www.sudu.cn/info/html/edu/java/20060912/304274.html

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
importorg.jfugue.*;

public class Example1
{
public static void main(String[] args)
{
Player player = new Player();
Pattern pattern = new Pattern("C D E F G A B");
player.play(pattern);
System.exit(0);
}
}

Music strings
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:

  • Notes and chords -- to specify a note or chord to play, and its duration;
  • Tempo -- specify the speed of the song;
  • Voice -- indicate the voice in which notes are to be played;
  • Instrument change -- Change the instrument, or patch, that is being used to play the notes;
  • Controller messages -- Set any of the MIDI controller events such as balance etc.;
  • Variables -- define a value for use by the other commands.

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, [60]. 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]".

Patterns
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
Pattern intro = new Pattern("music string");
Pattern verse = new Pattern("music string");
Pattern chorus = new Pattern("music string");
Pattern breakdown = new Pattern("music string");
Pattern utro = new Pattern("music string");

Then, you can create the song using those sections (Listing C):

Listing C
Pattern song = new Pattern();
song.add(intro);
song.add(outro);

You can play the entire song by method:

player.play(song);

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.

Example
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):

Listing D
import com.innix.jfugue.Player;
import com.innix.jfugue.Pattern;

public class Example2
{
public static void main(String[] args)
{
Player player = new Player();
Pattern pattern = new Pattern("T160 I[Cello] "+
"G3q G3qG3q Eb3q Bb3i G3q Eb3q Bb3i G3h");
player.play(song);
}

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/118838/viewspace-687551/,如需转载,请注明出处,否则将追究法律责任。

上一篇: Freemarker中文指南
下一篇: Audiveris 设计指南
请登录后发表评论 登录
全部评论

注册时间:2008-10-20

  • 博文量
    53
  • 访问量
    222627