/* * Created by Nick on Jan 3, 2005 * */ package jmslexamples.simple; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JLabel; import com.jsyn.swing.JAppletFrame; import com.softsynth.jmsl.*; import com.softsynth.jmsl.jsyn2.JSynUnitVoiceInstrument; /** * Use a MusicJob to turn an Instrument on() for two seconds, update its timbral dimensions numerous times, * then shut it off(). Turn it on in MusicJob.start(). Turn it off in MusicJob.stop(). Cause the * initial delay with musicJob's startPause. Call the updates in MusicJob.repeat() * * * Updated to JSyn2 API Dec 6, 2016 * * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: nick@didkovsky.com * */ public class InstrumentOnOffUpdate extends InstrumentOnOff { protected void buildMusicJob() { myMusicJob = new MusicJob() { Object voiceToken; public double start(double playTime) { JMSL.out.println("MusicJob.start() is calling instrument.on() "); if (getInstrument() != null) { double[] data = MusicShape.getDefaultArray(getInstrument().getDimensionNameSpace()); voiceToken = getInstrument().on(playTime, 1.0, data); } return playTime; } public double repeat(double playTime) { JMSL.out.println("MusicJob.repeat() is calling instrument.update() with random values for its dimensions"); if (getInstrument() != null) { double[] data = MusicShape.getDefaultArray(getInstrument().getDimensionNameSpace()); // let's generate random values for all the higher dimensions of this // instrument. for (int dimension = 4; dimension < getInstrument().getDimensionNameSpace().dimension(); dimension++) { // String dimensionName = getInstrument().getDimensionNameSpace().getDimensionName(dimension); // System.out.println("Updating " + dimensionName); double lowLimit = getInstrument().getDimensionNameSpace().getLowLimit(dimension); double highLimit = getInstrument().getDimensionNameSpace().getHighLimit(dimension); double newValue = JMSLRandom.choose(lowLimit, highLimit); data[dimension] = newValue; } // retrieve voice token, defined in superclass and stored in on() method int vt = ((Integer) voiceToken).intValue(); ((JSynUnitVoiceInstrument) getInstrument()).update(playTime, 1.0, data, vt); } return playTime; } public double stop(double playTime) { JMSL.out.println("MusicJob.stop() is calling instrument.off() "); if (getInstrument() != null) { // retrieve voiceToken int vt = ((Integer) voiceToken).intValue(); ((JSynUnitVoiceInstrument) getInstrument()).off(playTime, vt); } return playTime; } }; myMusicJob.setStartPause(2); myMusicJob.setInstrument(instrument); myMusicJob.setRepeats(30); myMusicJob.setRepeatPause(0.3); myMusicJob.setStopDelay(2); } public static void main(String[] args) { InstrumentOnOffUpdate myFrame = new InstrumentOnOffUpdate(); myFrame.add(new JLabel("There is no GUI for this example", JLabel.CENTER)); myFrame.setSize(600, 200); myFrame.setVisible(true); myFrame.start(); myFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { myFrame.stop(); System.exit(0); } }); } }