NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed. (sponsored)
In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.
Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.
Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
JFugue
is an open-source Java API for programming music without the complexities of MIDI. Read that again.
WITHOUT
the complexities of MIDI. It's all pretty simple, as you'll see. Let's get our feet wet by opening some MIDI file into a Swing application and then... we will
change
the music file
AND
play it!
So, just create some kind of Java application and attach
jfugue.jar
. Next, let's add a
JFrame
, we'll call it "MusicFixer". We'll declare the magical
Player
object, which will do all the heavy lifting (i.e., loading, playing, and saving music) for us:
Player player = new Player();
Next, add a
JTextArea
to display music files, a
JButton
for opening them, and a
JButton
that will do the playing. For example, in your GUI designer, all that could look as follows:
Now we'll add some code to the Open button's
actionPerformed
event:
File midiFile = null;
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
midiFile = fc.getSelectedFile();
}
Next, we'll add some code to the end of the same event, this time for setting the content of the
JTextArea
with the content of the Midi file, which is a segment of music, represented by the
Pattern
class:
Ok. Run that puppy, click that button, and open some MIDI file. Tada... this is what you see (or something like it):
Good for you! You've just opened a MIDI file, and can see its internals, using JFugue notation to represent the music embedded in the MIDI file. Let's now... play that music!
In the
actionPerformed
event of the Play button, shove these two lines:
Pattern pattern = new Pattern(jTextArea1.getText());
player.play(pattern);
Hurray! When you run the application, those two lines will do what 20 or more lines would have done for you in the Midi API. But, that's all well and good. Let's now
change
the music. Here's a handy class, called
PatternTransformer
, for playing our music an octave lower:
PatternTransformer octaveTransformer = new PatternTransformer() {
public void noteEvent(Note note) {
byte currentValue = note.getValue();
if (currentValue > 12) {
note.setValue((byte) (currentValue - 12));
returnPattern.addElement(note);
}
}
};
Cool. Now, how to play it? Couldn't be simpler. Get the content from our
JTextArea
, pass it to our transformer, and then play the pattern that the transformer comes up with. Thus, our Play button now has this content:
Pattern pattern = new Pattern(jTextArea1.getText());
Pattern octaveLowerSong = octaveTransformer.transform(pattern);
player.play(octaveLowerSong);
And that's it. Click your Play button again and your music is an octave lower. For all this and much (much) more, see
JFugue: Java API for Music Programming
. At the very least, you'll have fun! At most, it'll be educational and you'll be productive at the same time. The
javadoc
will give you most of what you need to get started, together with some
basic samples
.
Re: JFugue... Java Music Programming For Total Beginners!
I don't think embedding music in code is a good way to go.
You'll want to be able to completely control all musical elements outside of the program. It makes much more sense to use a music editor, and just load it in the code.
Things like play("C D E F#") etc isn't useful I think.
Also, think of editors like SoundTracker, ScreamTracker, those tracker type music editors. It started in the demo scene in the 80's, and is used for game music even today.
Mod music: modarchive.com
Discover an astronomical amount of music created by all kinds of people all over the world. I bet most people have never even heard of this.
It makes sense that you "hire" one of those music buffs, have them produce an xm or mod file, and load it at runtime using a mod player. I've seen an xm and mod player written in Java, and it works very well.
Re: JFugue... Java Music Programming For Total Beginners!
Oh, definitely. You need a user interface on top of the code above. See, for example, https://nbjfuguesupport.dev.java.net/. That generates the music strings and so on. The API only provides the MIDI layer.
Re: JFugue... Java Music Programming For Total Beginners!
"Yes" to both of your questions. It plays the same music instruments as the Midi API. Look at the screenshot at https://nbjfuguesupport.dev.java.net/ and you will see a long list. I believe there are over 200. Keyboard is also possible. I've asked Dave Koelle, the author of the API, to stop by here and give some answers to these questions, so watch this space!
Re: JFugue... Java Music Programming For Total Beginners!
You are right, there are java mod players around. If you would like to use one with open source code, try JavaMod: http://www.quippy.de
But nevertheless using a play-command with fixed notes and samples is nearly the same as to load an external midi or mod file - e.g. for short hymns or sounds I think this can be very usefull.
The playback of a whole score via the direct integration of musical information into the sourcecode does not seem right to me eihter.
JFugue... Java Music Programming For Total Beginners!
URL: JFugue website
At 11:31 PM on Sep 27, 2007, Geertjan wrote:
Fresh Jobs for Developers Post a job opportunity
So, just create some kind of Java application and attach jfugue.jar . Next, let's add a JFrame , we'll call it "MusicFixer". We'll declare the magical Player object, which will do all the heavy lifting (i.e., loading, playing, and saving music) for us:
Next, add a JTextArea to display music files, a JButton for opening them, and a JButton that will do the playing. For example, in your GUI designer, all that could look as follows:
Now we'll add some code to the Open button's actionPerformed event:
File midiFile = null; JFileChooser fc = new JFileChooser(); int returnVal = fc.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { midiFile = fc.getSelectedFile(); }Next, we'll add some code to the end of the same event, this time for setting the content of the JTextArea with the content of the Midi file, which is a segment of music, represented by the Pattern class:
try { Pattern pattern = Player.loadMidi(midiFile); jTextArea1.setText(pattern.toString()); } catch (InvalidMidiDataException ex) { ex.printStackTrace(); } catch (IOException ioEx) { ioEx.printStackTrace(); }Ok. Run that puppy, click that button, and open some MIDI file. Tada... this is what you see (or something like it):
Good for you! You've just opened a MIDI file, and can see its internals, using JFugue notation to represent the music embedded in the MIDI file. Let's now... play that music!
In the actionPerformed event of the Play button, shove these two lines:
Hurray! When you run the application, those two lines will do what 20 or more lines would have done for you in the Midi API. But, that's all well and good. Let's now change the music. Here's a handy class, called PatternTransformer , for playing our music an octave lower:
PatternTransformer octaveTransformer = new PatternTransformer() { public void noteEvent(Note note) { byte currentValue = note.getValue(); if (currentValue > 12) { note.setValue((byte) (currentValue - 12)); returnPattern.addElement(note); } } };Cool. Now, how to play it? Couldn't be simpler. Get the content from our JTextArea , pass it to our transformer, and then play the pattern that the transformer comes up with. Thus, our Play button now has this content:
And that's it. Click your Play button again and your music is an octave lower. For all this and much (much) more, see JFugue: Java API for Music Programming . At the very least, you'll have fun! At most, it'll be educational and you'll be productive at the same time. The javadoc will give you most of what you need to get started, together with some basic samples .
9 replies so far (
Post your own)
Re: JFugue... Java Music Programming For Total Beginners!
I don't think embedding music in code is a good way to go.You'll want to be able to completely control all musical elements outside of the program. It makes much more sense to use a music editor, and just load it in the code.
Things like play("C D E F#") etc isn't useful I think.
Also, think of editors like SoundTracker, ScreamTracker, those tracker type music editors. It started in the demo scene in the 80's, and is used for game music even today.
Mod music: modarchive.com
Discover an astronomical amount of music created by all kinds of people all over the world. I bet most people have never even heard of this.
It makes sense that you "hire" one of those music buffs, have them produce an xm or mod file, and load it at runtime using a mod player. I've seen an xm and mod player written in Java, and it works very well.
~Mike
Re: JFugue... Java Music Programming For Total Beginners!
Oh, definitely. You need a user interface on top of the code above. See, for example, https://nbjfuguesupport.dev.java.net/. That generates the music strings and so on. The API only provides the MIDI layer.Re: JFugue... Java Music Programming For Total Beginners!
Could this api surpport use keyboard to play music.Could it simulate the tone of Musical Instrument
Re: JFugue... Java Music Programming For Total Beginners!
"Yes" to both of your questions. It plays the same music instruments as the Midi API. Look at the screenshot at https://nbjfuguesupport.dev.java.net/ and you will see a long list. I believe there are over 200. Keyboard is also possible. I've asked Dave Koelle, the author of the API, to stop by here and give some answers to these questions, so watch this space!Re: JFugue... Java Music Programming For Total Beginners!
Hi I tried this example but I'm getting errorsnon-static method play(org.jfugue.Pattern) cannot be referenced from a static context
I know this is probably a very basic error but can anyone offer some help???
Re: JFugue... Java Music Programming For Total Beginners!
Maybe you can solve it by making your play method static.Re: JFugue... Java Music Programming For Total Beginners!
I have tried that but obviously I must have done it wrong. Could you please explain.Thanks
Re: JFugue... Java Music Programming For Total Beginners!
Feel free to send me your code, to geertjan dot wielenga at sun dot com, and I will look at it.Re: JFugue... Java Music Programming For Total Beginners!
You are right, there are java mod players around. If you would like to use one with open source code, try JavaMod: http://www.quippy.deBut nevertheless using a play-command with fixed notes and samples is nearly the same as to load an external midi or mod file - e.g. for short hymns or sounds I think this can be very usefull.
The playback of a whole score via the direct integration of musical information into the sourcecode does not seem right to me eihter.