/* * 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 SequentialCollection to ABAB "song form" with Midi piano * * If the Java installation that is running this applet does not have a JavaSound MIDI soundbank, your classes folder * (ie your applet's codebase) must contain a folder called javasound containing a soundbank called soundbank-min.gm * (or call subfolder and the soundbank whatever you want and change the name in the initJavaSoundMidi() method below * * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: didkovn@mail.rockefeller.edu * */ public class MIDISong extends java.applet.Applet { JMSLMixerContainer mixer; Instrument instrument; SequentialCollection mySong; public void init() { JMSL.setIsApplet(true); } public void start() { synchronized (JMSL.class) { initJMSL(); initMusicDevices(); buildMixer(); buildInstrument(); buildSong(); launchSong(); } } 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.edit(new java.awt.Frame()); JMSL.midi.open(); } private void buildMixer() { mixer = new JMSLMixerContainer(); mixer.start(); } private void buildInstrument() { instrument = new MidiInstrument(1); mixer.addInstrument(instrument); } void buildSong() { MusicShape verse = new MusicShape(4); verse.setInstrument(instrument); verse.add(0.25, 60, 80, 0.5); verse.add(0.25, 62, 80, 0.25); verse.add(0.75, 65, 80, 0.2); verse.setRepeats(4); MusicShape chorus = new MusicShape(4); chorus.setInstrument(instrument); chorus.add(0, 50, 90, 0.75); chorus.add(1.0, 59, 90, 0.75); chorus.add(0, 53, 90, 0.75); chorus.add(1.0, 57, 90, 0.75); chorus.setRepeats(2); mySong = new SequentialCollection(); mySong.add(verse); mySong.add(chorus); mySong.add(verse); mySong.add(chorus); } private void launchSong() { mySong.launch(JMSL.now()); } public void stop() { synchronized (JMSL.class) { mySong.finishAll(); try { mySong.waitForDone(); } catch (InterruptedException e) { e.printStackTrace(); } JMSL.scheduler.stop(); JMSL.closeMusicDevices(); } } }