/* * Created by Nick on Jan 3, 2005 * */ package jmslexamples.simple; import java.net.MalformedURLException; import java.net.URL; import com.softsynth.jmsl.*; import com.softsynth.jmsl.midi.MidiIO_JavaSound; import com.softsynth.jmsl.midi.MidiInstrument; /** * Use a MusicJob to play ten MIDI notes with random pitch, duration, vel, and * hold time * * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: * didkovn@mail.rockefeller.edu * */ public class TenRandomMidiNotes extends java.applet.Applet { JMSLMixerContainer mixer; Instrument midiInstrument; MusicJob randomMusicJob; public void init() { JMSL.setIsApplet(true); } public void start() { synchronized (JMSL.class) { initJMSL(); initMusicDevices(); buildMixer(); buildInstrument(); buildRandomMusicJob(); launchRandomMusicJob(); } } private void initJMSL() { JMSL.scheduler = new EventScheduler(); JMSL.scheduler.start(); JMSL.clock.setAdvance(0.1); } private void initMusicDevices() { JMSL.clock = new DefaultMusicClock(); initJavaSoundMidi(); } private void initJavaSoundMidi() { JMSL.midi = MidiIO_JavaSound.instance(); try { ((MidiIO_JavaSound) JMSL.midi).setSoundbankURL(new URL(getCodeBase(), "javasound/soundbank-min.gm")); } catch (MalformedURLException e) { e.printStackTrace(); } JMSL.midi.open(); } private void buildMixer() { mixer = new JMSLMixerContainer(); mixer.start(); } private void buildInstrument() { midiInstrument = new MidiInstrument(1); mixer.addInstrument(midiInstrument); } void buildRandomMusicJob() { randomMusicJob = new MusicJob() { public double repeat(double playTime) { double randomDuration = JMSLRandom.choose(0.1, 5.0); double randomPitch = JMSLRandom.choose(127) + 1; double randomVel = JMSLRandom.choose(30, 90); double randomHold = JMSLRandom.choose(0.2, 10.0); double[] data = { randomDuration, randomPitch, randomVel, randomHold }; // FYI JMSL.printDoubleArray(data); return getInstrument().play(playTime, 1, data); } }; randomMusicJob.setRepeats(10); randomMusicJob.setInstrument(midiInstrument); } private void launchRandomMusicJob() { randomMusicJob.launch(JMSL.now()); } public void stop() { synchronized (JMSL.class) { randomMusicJob.finishAll(); try { randomMusicJob.waitForDone(); } catch (InterruptedException e) { e.printStackTrace(); } JMSL.scheduler.stop(); JMSL.closeMusicDevices(); } } }