Tuesday, May 02, 2006

jfugue, jython, jjedit, classpath, vpython, music viz

So I'm working on music visualization (with vpython) and would like to play the tunes I am visualizing. There are some very sophisticated sound tools out there, but simple real-time ones are hard to find. (I'd be happy to be able to specify pitch and duration, but no....).

Anyway, JFugue is admirably clean and simple looking, but written in java.
http://www.jfugue.org/

I've had some luck with jython recently, so I thought I'd pursue it. www.jython.org
First the happy ending,
[208-186-48-2:remo/MusicViz/JFugue] apple% jython
Jython 2.2a0 on java1.5.0_06 (JIT: null)
>>> from org.jfugue import *
>>> player=Player()
>>> pattern=Pattern("C D E F G A B C")
>>> player.play(pattern)
(music!)

For this to work, I need only have JFugue's directory tree called "org" in the current directory.

The above four lines are considerably shorter and simpler than than original java

import org.jfugue.*;
public class MyMusicApp
{
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); // If using versions lower than Java 5.0
}
}

and to run the java code you must apparently
1) give the file the name MyMusicApp.java
2) compile the file using the incantation
javac -classpath ./jfugue.jar MyMusicApp.java
3) run the file using the incantation
java -cp ./jfugue.jar:. MyMusicApp
(and the colon is a mac-specific necessity. On windows you use semicolon(??))

Typing the classpath is a nuisance, and getting it into the environment requires the kind of unix geekery that drives me crazy. But I did find a really elegant little java IDE called JJEdit that gives you a compile button and a run button, and a form-field into which you can enter a classpath of ./ and get on with iterative development. http://homepage.mac.com/jmacmullin/

So this is more bearable, if much more cumbersome than working in jython.

On the other hand, I must say that the jython documentation leaves something to be desired. www.jython.org points you to an installer for Mac Classic(!) http://www.jython.org/platform.html.

Google led me to a darwin port installer which installed admirably and works as shown above.
But I just noticed
(a) that somehow I'm running version 2.2a0 and
(b) that it doesn't respond to command line arguments, so I can't run it non-interactively.
Fixing this will require tweaking the shell script that invokes jython I suppose...

but sheesh! Genuinely simple simplicity is apparently not simple....