Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

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.

Introduction to Groovy Part 3

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.

Easier Custom Components with Swing Fuse

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.

Benchmark Analysis: Guice vs Spring

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.
Replies: 9 - Pages: 1  
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

JFugue... Java Music Programming For Total Beginners!

URL: JFugue website

At 11:31 PM on Sep 27, 2007, Geertjan wrote:

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:

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:

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 .

1 . At 5:09 PM on Sep 28, 2007, Mike P wrote:
  Click to reply to this thread Reply

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
2 . At 5:17 PM on Sep 28, 2007, Geertjan wrote:
  Click to reply to this thread Reply

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.
3 . At 4:29 AM on Sep 29, 2007, javacorner wrote:
  Click to reply to this thread Reply

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
java persistance
4 . At 4:34 AM on Sep 29, 2007, Geertjan wrote:
  Click to reply to this thread Reply

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!
5 . At 12:04 PM on Nov 16, 2007, jennk wrote:
  Click to reply to this thread Reply

Re: JFugue... Java Music Programming For Total Beginners!

Hi I tried this example but I'm getting errors

non-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???
6 . At 1:01 PM on Nov 16, 2007, Geertjan wrote:
  Click to reply to this thread Reply

Re: JFugue... Java Music Programming For Total Beginners!

Maybe you can solve it by making your play method static.
7 . At 1:05 PM on Nov 16, 2007, jennk wrote:
  Click to reply to this thread Reply

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
8 . At 1:41 PM on Nov 16, 2007, Geertjan wrote:
  Click to reply to this thread Reply

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.
9 . At 9:52 AM on Dec 28, 2007, quippy wrote:
  Click to reply to this thread Reply

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.

thread.rss_message