X

Geertjan's Blog

  • March 14, 2007

Reading and Changing Midi Files

Geertjan Wielenga
Product Manager
Dave Koelle recently (very recently) released JFugue 3.0, which includes the long-awaited functionality for OPENING Midi files, for purposes of CHANGING existing Midi files. Previous versions of JFugue API provided functionality for CREATING Midi files. But the point is not to comment on the world, but to change it, as a famous person once said. So, now it should be possible to select a Midi file, open it somewhere, and then change it and save it again. How cool is that? David Hasselhoff (and many others) better watch out. The end is nigh for mediocre music. Time to try it out.

I obviously used the Matisse GUI Builder, because throwing together a client that way is a "fluitje van een cent". (That's a Dutch expression which, directly translated, means a "whistle of a cent", which is nonsense, but means the same as "as easy as pie", which is also nonsense.) Here's the result after opening a Midi file:

Under the Open button, the actionPerformed() does exactly the following (the interesting two lines are in bold):

private void OpenButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
File midiFile = null;
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
midiFile = fc.getSelectedFile();
}
try {Pattern pattern = player.load(midiFile);
jTextArea1.setText(pattern.toString());

} catch (InvalidMidiDataException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}

And this is the Player object declared at the top of the class:

Player player = new Player();

The Play and Save buttons are the same as in previous releases of JFugue API:

private void PlayButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
player.play(jTextArea1.getText());
}
private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt) {
Pattern pattern = new Pattern(jTextArea1.getText());
try {
player.save(pattern, new File("myfile.midi"));
} catch (IOException ex) {
ex.printStackTrace();
}
}

Obviously, now that the file is open, I need to know what the syntax means. It isn't exactly the same syntax as for creating Midi files. So now I'm going to look around on the JFugue API site and try and work out (or find out from Dave Koelle) what the syntax is all about. But, really, that is a secondary question. The primary point is that in 5 minutes, I have a simple yet effective means of opening existing Midi files and tweaking them. Hurray for Dave Koelle. And come to our technical session at JavaOne where all this and more will be celebrated! And, note that ultimately, this functionality should be integrated into the JFugue Music NotePad.

Join the discussion

Comments ( 1 )
  • Toni Epple Wednesday, March 14, 2007
    How about a NetBeans based Karaoke machine? :)(something like this : http://www.vanbasco.com/)
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha