JMSL Tutorial: Instruments and Interpreters

We will now achieve the same effect by creating our own Interpreter, and plugging it into a MusicShape's stock Instrument with setInterpreter(). As before, the Interpreter will simply identify itself and print its data.

You may skip this tutorial if you intend to only use InstrumentAdapter subclasses.  The difference is really quite subtle so you won't miss core functionality by moving ahead.

Our Interpreter subclass, let's call it PrintingInterpreter, has to define interpret(), like so
	
/** A simple Interpreter subclass that just prints out the data it is handed, interprets element[0] as duration */
class PrintingInterpreter extends Interpreter {

/** Overridden for custom interpretation */
public double interpret(double playTime, double timeStretch, double dar[], Instrument ins) {
JMSL.out.print(getName() + " called by " + ins + " with ");
JMSL.printDoubleArray(dar);
return playTime + (dar[0]*timeStretch);
}
}


Plugging a custom Interpreter into an Instrument is done like this:
myMusicShape.getInstrument().setInterpreter(new PrintingInterpreter());


The results are shown in the applet below. Note that the duration of each is the first element in the array.

Note that the Interpreter has access to the Instrument when it calls interpret(). You may use this to grab the Instrument's Midi channel, for example, or other data that varies from Instrument to Instrument.

You need a Java-enabled browser to view this applet.
View the complete source for this applet here.

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.