/* * Created by Nick 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.softsynth.jmsl.EventScheduler; import com.softsynth.jmsl.Instrument; import com.softsynth.jmsl.JMSL; import com.softsynth.jmsl.JMSLMixerContainer; import com.softsynth.jmsl.JMSLRandom; import com.softsynth.jmsl.MusicJob; import com.softsynth.jmsl.jsyn2.JSynMusicDevice; import com.softsynth.jmsl.jsyn2.JSynUnitVoiceInstrument; /** * 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: * nick@didkovsky.com * */ public class RandomJSynMusicJobMelody extends JFrame { JMSLMixerContainer mixer; Instrument instrument; MusicJob randomMelodyMaker; public void start() { JMSLRandom.randomize(); initJMSL(); initMusicDevices(); buildMixer(); buildInstrument(); buildRandomMelodyMaker(); launchRandomMelodyMaker(); } private void initJMSL() { JMSL.scheduler = new EventScheduler(); JMSL.scheduler.start(); JMSL.clock.setAdvance(0.2); } 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_WOODDRUM; int polyphony = 8; instrument = new JSynUnitVoiceInstrument(polyphony, unitVoiceClassName, unitVoicePreset); 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() { randomMelodyMaker.finishAll(); try { randomMelodyMaker.waitForDone(); } catch (InterruptedException e) { e.printStackTrace(); } JMSL.scheduler.stop(); JMSL.closeMusicDevices(); } public static void main(String[] args) { RandomJSynMusicJobMelody demo = new RandomJSynMusicJobMelody(); 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); } }); } }