/* * Created on Jan 3, 2005 * */ package jmslexamples.simple; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JLabel; import com.jsyn.swing.JAppletFrame; import com.softsynth.jmsl.Composable; import com.softsynth.jmsl.EventScheduler; import com.softsynth.jmsl.Instrument; import com.softsynth.jmsl.JMSL; import com.softsynth.jmsl.JMSLMixerContainer; import com.softsynth.jmsl.MusicJob; import com.softsynth.jmsl.MusicShape; import com.softsynth.jmsl.PlayLurker; import com.softsynth.jmsl.Playable; import com.softsynth.jmsl.jsyn2.JSynMusicDevice; import com.softsynth.jmsl.jsyn2.JSynUnitVoiceInstrument; /** * Play a MusicShape repeatedly with a JSyn instrument, each repeat scramble the * order of its elements * * Also demonstrates how to add a PlayLurker which is notified of elements being * played by MusicShape. Don't get the idea that adding the PlayLurker is * functionally necessary in this example. It's just here to show you how to do * it. You could do whatever you wanted with the data the PlayLurker is being * notified with. * * Upgraded to JSyn2 API Dec 6, 2016 by ND * * @author Nick Didkovsky, nick@didkovsky.com * */ public class MusicShapeScrambling extends JFrame { JMSLMixerContainer mixer; Instrument instrument; MusicShape myMusicShape; public void start() { initJMSL(); initMusicDevices(); buildMixer(); buildInstrument(); buildMusicShape(); launchMusicShape(); } private void initJMSL() { JMSL.scheduler = new EventScheduler(); JMSL.scheduler.start(); JMSL.clock.setAdvance(0.1); } private void initMusicDevices() { JSynMusicDevice.instance().open(); } private void buildMixer() { mixer = new JMSLMixerContainer(); mixer.start(); } private void buildInstrument() { String unitVoiceClassName = com.softsynth.jmsl.jsyn2.unitvoices.FMVoice.class.getName(); int unitVoicePreset = com.softsynth.jmsl.jsyn2.unitvoices.FMVoice.PRESET_BRASS; int polyphony = 8; instrument = new JSynUnitVoiceInstrument(polyphony, unitVoiceClassName, unitVoicePreset); mixer.addInstrument(instrument); } void buildMusicShape() { myMusicShape = new MusicShape(4); myMusicShape.addPlayLurker(new SillyLurker()); // non functional, just for demo myMusicShape.setInstrument(instrument); myMusicShape.add(1.0, 60, 0.6, 0.95); myMusicShape.add(0.25, 60.5, 0.4, 0.5); myMusicShape.add(0.75, 65, 0.2, 1.2); myMusicShape.setRepeats(400); myMusicShape.addRepeatPlayable(new Playable() { public double play(double time, Composable parent) throws InterruptedException { MusicShape s = (MusicShape) parent; // -1 means scramble whole elements instead of along a single dimension s.scramble(0, s.size() - 1, -1); return time; } }); } private void launchMusicShape() { myMusicShape.launch(JMSL.now()); } public void stop() { myMusicShape.finishAll(); try { myMusicShape.waitForDone(); } catch (InterruptedException e) { e.printStackTrace(); } JMSL.scheduler.stop(); JMSL.closeMusicDevices(); } public static void main(String[] args) { MusicShapeScrambling demo = new MusicShapeScrambling(); demo.add(new JLabel("There is no GUI for this example", JLabel.CENTER)); demo.setSize(800, 200); demo.setVisible(true); demo.start(); demo.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { demo.stop(); System.exit(0); } }); } } class SillyLurker implements PlayLurker { public void notifyPlayLurker(double playTime, MusicJob list, int index) { JMSL.out.println("SillyLurker knows that the following data is being played by MusicShape:"); MusicShape s = (MusicShape) list; double[] data = s.get(index); JMSL.printDoubleArray(data); } }