JMSL Tutorial: Advanced topics: a well designed JMSL Instrument

We can write quick and dirty Instruments that make a sound, draw, or print text in its play() method, or we can be more rigorous and follow some good design principles that will extend the life and utility of your instruments.

 Good JMSL Instrument design  follows the following principles:

  1. Keep the instantiation of device-specific objects out of the constructor
  2. Specify the Instrument's MusicDevice in constructor
  3. Specify the Instrument's Mixer classname in constructor
  4. Define the Instrument's DimensionNameSpace in constructor
  5. implement AttributeBuildable and do all device-specific  building in the buildFromAttributes() method
  6. Make your sound play(). For even more flexibility, turn your sound on with on() and turn it off with off()
Some of the support classes that reward these design principles include JMSL Score, SimpleXMLLoader, and SimpleXMLSaver

Now for the nuts and bolts:  Instrument's most important method is play().  This is called by MusicShape for example, as it iterates through all its elements, handing each to its instrument's play() method, and waiting for the time returned by play() before proceeding to the next element. 
 
The example here is purely to show design principles. You would never write a JSyn instrument like this from scratch when you could just use JSynUnitVoiceInstrument.
The example here shows a no-arg constructor which defines the instrument's mixer class, its music device, and its dimension name space. Note that the music device does not have to be running for this constructor to execute! All the device dependent building is done in a method called buildFromAttributes(), which satisfies the AttributeBuildable method.
note, too, that on(), update(), and off() are defined. This would allow you to turn the sound on (with a keyboard press for example), update the parameters (changin index of modulation for example), and then turning the sound off (by releasing the key for example). It also defines play() which is a complete musical event initiated by a single method call. play() is used by MusicShape to play its Instrument. It is also used in JMSL Score.

Check the source for SineInstrument here.
 
Previous Tutorial Index Tutorial Contents Next

  (C) 2003 Nick Didkovsky, All Rights Reserved