Download NetBeans!

20070315 Thursday March 15, 2007

Transforming Midi Files

I received an interesting code snippet from JFugue API's Dave Koelle yesterday. It takes any JFugue Pattern and changes all notes to be played an octave lower:

        // Lower the octaves of the pattern
        PatternTransformer octaveTransformer = new PatternTransformer() {
            public void noteEvent(Note note)
            {
                byte currentValue = note.getValue();
                if (currentValue > 12) {
                    note.setValue((byte)(currentValue - 12));
                    returnPattern.addElement(note);
                }  
            }
        };

And here's how to use it:

       Pattern octaveLowerSong = octaveTransformer.transform(song);

So I built a Java desktop client around this code, using a slider to change the octave (and instrument icons to insert instruments into the opened Midi file). Therefore, note that the syntax you see below comes from an actual Midi file, converted to JFugue music strings (thanks to Player.load(file)), that I've opened in the client:

A next step is to transform the music while it is being played. That seems to be possible too. This JFugue API is pretty fun. You only need to focus on the user interface, and on the connection points between the user interface and the JFugue API, not on the underlying Midi magic. And that's the way it should be.

Mar 15 2007, 03:54:39 AM PDT Permalink