package jmsltutorial; import java.awt.*; 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; /** * Set JMSL midi output to Java Sound MIDI device. For this to work in an * applet, Java Sound midi needs a soundbank Ine of two way to acheive this: 1) * the path to the soundbank should be on the local machine in * (jre/)lib/audio/soundbank.gm. You may have to make the "audio" directory * manually and install the soundbank manually. 2) Provide MidiIO_JavaSound with * a URL to a soundbank on the same server as the applet (see the call to * setSoundbankURL() in start() ) * *
 * 
 *  
 *   
 *    
 *     
 *      NOTE TO WINDOWS USERS!
 *      JavaSound Midi applet notes, Windows XP
 *     
 *      Applets using JavaSound MIDI ( javax.sound.midi ) will not work using the standard Sun Java plugin distribution.
 *      You must manually install a soundbank for the Java plugin to use. (or provide MidiIO_JavaSound with a URL to a soundbank)
 *     
 *      To install a soundbank locally, find out where your Java plugin is installed:
 *      Control Panel -> Java Plug-in -> Advanced tab (read location under Java Runtime Environment listing)
 *      My Java plugin is in C:\Program Files\Java\j2re1.4.1_07
 *     
 *      In Windows Explorer, I browsed to C:\Program Files\Java\j2re1.4.1_07 and opened the "lib" folder
 *      In "lib" I made New Folder named "audio"
 *     
 *      From various jsdk installations, I found and copied soundbank.gm into the new audio folder [ C:\Program Files\Java\j2re1.4.1_07\lib\audio ]
 *     
 *      
 *     
 *    
 *   
 *  
 * 
* * @author Nick Didkovsky, email: didkovn@mail.rockefeller.edu, (c) 2003 Nick * Didkovsky, all rights reserved. * */ public class JavaSoundMidiApplet extends java.applet.Applet { MusicShape m; // Frame mixerFrame; public void init() { JMSL.setIsApplet(true); setLayout(new BorderLayout()); add(BorderLayout.NORTH, new Label("JMSL JavaSound MIDI test")); TextArea textArea = new TextArea(10, 80); add(BorderLayout.SOUTH, textArea); String msg = "JMSL is playing a MIDI melody with JavaSound MIDI right now\n"; msg += " If you hear nothing, you probably need to install a soundbank.\n"; msg += "The path of a soundbank should be (jre/)lib/audio/soundbank.gm\n"; msg += "You may have to install the soundbank manually. You can download a soundbank of your choice\n"; msg += "at http://java.sun.com/products/java-media/sound/soundbanks.html\n"; msg += "More instructions are there.\n\n"; msg += " Or call ((MidiIO_JavaSound) JMSL.midi).setSoundbankURL(new URL(\"URLtoMyOnlineSoundBank.gm\"))\n"; textArea.append(msg); } public void start() { synchronized (JMSL.class) { JMSL.clock = new DefaultMusicClock(); JMSL.scheduler = new EventScheduler(); JMSL.scheduler.start(); JMSL.midi = MidiIO_JavaSound.instance(); // Use Sun's MIDI Library try { ((MidiIO_JavaSound) JMSL.midi).setSoundbankURL(new URL(getCodeBase(), "javasound/soundbank.gm")); } catch (MalformedURLException e) { e.printStackTrace(); } JMSL.midi.open(); MidiInstrument ins = new MidiInstrument(); buildMixer(ins); // optional, but nice - give user pan/amp control panel buildMusicShape(ins); m.launch(JMSL.now()); } } private void buildMusicShape(MidiInstrument ins) { m = new MusicShape(4); m.setInstrument(ins); m.setRepeats(100000); m.add(1.0, 65, JMSLRandom.choose(80, 127), 2.5); m.add(0.5, 66, JMSLRandom.choose(80, 127), 1.5); m.add(0.75, 58, JMSLRandom.choose(80, 127), 1.5); m.add(0.75, 60, JMSLRandom.choose(80, 127), 1.5); m.add(0.5, 69, JMSLRandom.choose(80, 127), 1.0); m.addRepeatPlayable(new Playable() { private int fromIndex; private int toIndex; public double play(double time, Composable parent) throws InterruptedException { MusicShape s = (MusicShape) parent; chooseRange(s); s.scramble(fromIndex, toIndex, 0); chooseRange(s); s.scramble(fromIndex, toIndex, 1); return time; } private void chooseRange(MusicShape s) { fromIndex = JMSLRandom.choose(s.size()); toIndex = JMSLRandom.choose(s.size()); if (fromIndex > toIndex) { int temp = fromIndex; fromIndex = toIndex; toIndex = temp; } } }); } private void buildMixer(MidiInstrument ins) { JMSLMixerContainer mixer = new JMSLMixerContainer(); mixer.addInstrument(ins, 0.5, 1.0); // ins, pan (0..1), amp (0..1) mixer.start(); add(mixer.getPanAmpControlPanel()); } public void stop() { synchronized (JMSL.class) { m.finishAll(); try { m.waitForDone(); } catch (InterruptedException e) { e.printStackTrace(); } JMSL.scheduler.stop(); JMSL.closeMusicDevices(); } } }