/** InsToot03.java Fill a MusicShape with random numbers, play it with a custom Instrument. * @author Phil Burk and Nick Didkovsky */ /* * (C) 1997 Phil Burk and Nick Didkovsky, All Rights Reserved * JMSL is based upon HMSL (C) Phil Burk, Larry Polansky and David Rosenboom. */ package jmsltutorial; import java.awt.TextArea; import com.softsynth.jmsl.*; public class InsToot03 extends java.applet.Applet { MusicShape myMusicShape; TextArea myTextArea; /* Build data when the applet initializes, and send JMSL's STDOut to a TextArea */ public void init() { // initialize the TextArea with 15 rows, 80 columns myTextArea = new TextArea(15, 80); // Add the TextArea to the applet's layout add(myTextArea); // Hand this TextArea to a new TextAreaSTDOut, and use it for JMSL.out JMSL.setSTDOut(new TextAreaSTDOut(myTextArea)); myMusicShape = new MusicShape(4); for (int i = 0; i < 20; i++) { // choose a duration between 0.25 and 3.0 seconds, in qtr second intervals double duration = JMSLRandom.choose(12) * 0.25 + 0.25; // choose a chord root between Midi pitch 40 and 64 double root = (double) JMSLRandom.choose(40, 64); // choose a number of intervals in chord double intervals = (double) JMSLRandom.choose(1, 6); // choose a harmonic complexity value 0..11 double harmonicComplexity = JMSLRandom.choose(12); myMusicShape.add(duration, root, intervals, harmonicComplexity); } // Now let's name the dimensions with human-friendly monikers myMusicShape.setDimensionName(0, "Duration"); myMusicShape.setDimensionName(1, "Chord Root"); myMusicShape.setDimensionName(2, "Number of Intervals"); myMusicShape.setDimensionName(3, "Harmonic Complexity"); // Finally, let's set up a custom Instrument myMusicShape.setInstrument(new PrintingInstrument()); } /* When applet starts up, launch the MusicShape */ public void start() { JMSL.clock = new DefaultMusicClock(); myMusicShape.launch(JMSL.now()); } public void stop() { myMusicShape.finish(); } } /** A simple Instrument subclass that just prints out the data it is handed, interprets element[0] as duration */ class PrintingInstrument extends InstrumentAdapter { /** Overridden for custom interpretation */ public double play(double playTime, double timeStretch, double dar[]) { JMSL.out.println("Instrument.play() is handed the following array of doubles:"); JMSL.printDoubleArray(dar); JMSL.out.println(); return playTime + (dar[0] * timeStretch); // interpret dar[0] as duration } }