/* * Created by Nick on Jan 3, 2005 * */ package jmslexamples.simple; import com.softsynth.jmsl.*; import com.softsynth.jmsl.jsyn.JSynInsFromClassName; import com.softsynth.jmsl.jsyn.JSynMusicDevice; /** * Use a MusicJob to play a melody made up of uniformly random pitches, durations, amplitudes, & hold times * * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: didkovn@mail.rockefeller.edu * */ public class RandomJSynMusicJobMelody extends java.applet.Applet { JMSLMixerContainer mixer; Instrument instrument; MusicJob randomMelodyMaker; public void init() { JMSL.setIsApplet(true); } public void start() { synchronized (JMSL.class) { JMSLRandom.randomize(); initJMSL(); initMusicDevices(); buildMixer(); buildInstrument(); buildRandomMelodyMaker(); launchRandomMelodyMaker(); } } 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() { instrument = new JSynInsFromClassName(8, com.softsynth.jsyn.circuits.FilteredSawtoothBL.class.getName()); mixer.addInstrument(instrument); } void buildRandomMelodyMaker() { randomMelodyMaker = new MusicJob() { public double repeat(double playTime) { double duration = JMSLRandom.choose(4) * 0.125; double pitch = JMSLRandom.choose(60, 72); double amplitude = JMSLRandom.choose(0.2, 0.5); double hold = JMSLRandom.choose(0.1, 0.5); double[] data = { duration, pitch, amplitude, hold }; return getInstrument().play(playTime, 1.0, data); } }; randomMelodyMaker.setInstrument(instrument); randomMelodyMaker.setRepeats(10000); } private void launchRandomMelodyMaker() { randomMelodyMaker.launch(JMSL.now()); } public void stop() { synchronized (JMSL.class) { randomMelodyMaker.finishAll(); try { randomMelodyMaker.waitForDone(); } catch (InterruptedException e) { e.printStackTrace(); } JMSL.scheduler.stop(); JMSL.closeMusicDevices(); } } }