JMSL Tutorial: Instruments and Interpreters

Just to demonstrate that arbitrary interpretation of data is a hallmark of JMSL, let's whip together an Interpreter that sets the background color of the applet by interpreting MusicShape data as (R,G,B) colors. The zeroth dimension of the MusicShape shall be interpreted as duration (this will rapidly become a standard interpretation), while dimensions 1..3 are respectively, intensity values of red, green, and blue.

The Interpreter class, shown below, simply extracts RGB data from its double[] parameter, and communicates the color change to the Applet parent.
/** A simple Interpreter subclass that interprets data as rgb color, interprets element[0] as duration */
class AppletColorInterpreter extends Interpreter {

java.applet.Applet parent;

/* Constructor initializes the parent Applet */
AppletColorInterpreter(java.applet.Applet a) {
parent = a;
}

/** Overridden for custom interpretation */
public double interpret(double playTime, double timeStretch, double dar[], Instrument ins) {
int r = (int)dar[1];
int g = (int)dar[2];
int b = (int)dar[3];
parent.setBackground(new Color (r,g,b));
return playTime + (dar[0]*timeStretch);
}
}


The applet is shown below. This MusicShape is loaded with random data, and completely prints itself out in the TextArea before it starts performing.

IMPORTANT: This did not have to be an Interpreter.  We could have done this just as easily in Instrument.play() !

You need a Java-enabled browser to view this applet.

View the complete source for this applet here.

View the complete source for AppletColorInstrument here.

TextAreaSTDOut source

Previous Tutorial Index Tutorial Contents Next
  (C) 1997 Phil Burk and Nick Didkovsky, All Rights Reserved
JMSL is based upon HMSL (C) Phil Burk, Larry Polansky and David Rosenboom.