/*
* Created on May 27, 2004
*
*/
package jmslexamples;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.util.Date;
import com.softsynth.jmsl.*;
import com.softsynth.jmsl.midi.*;
/**
*
* Play a self-modifying MusicShape with a JMSL MidiInstrument using
* MidiIO_JavaSound as MusicDevice
*
*
*
* 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 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 ]
*
* Loading this applet plays midi notes ok now.
*
* Nick Didkovsky
*
*
*
*
*
* @author Nick Didkovsky, email: didkovn@mail.rockefeller.edu, (c) 2004 Nick
* Didkovsky, all rights reserved.
*/
public class JavaSoundMidiAppletExample extends java.applet.Applet implements ActionListener {
JMSLMixerContainer mixer;
MusicShape musicShape;
Button startButton;
Button stopButton;
public void init() {
// tell JMSL it is running in an applet so MidiIO_JavaSound will look
// for a sound bank at a URL instead of a File if the client has no
// soundbank installed
JMSL.setIsApplet(true);
JMSLRandom.randomize();
}
public void start() {
synchronized (JMSL.class) {
System.out.println("JavaSoundMidiAppletExample.start() " + new Date());
JMSL.clock = new DefaultMusicClock();
// IMPORTANT! Start with a new scheduler!
JMSL.scheduler = new EventScheduler();
JMSL.scheduler.start();
JMSL.clock.setAdvance(0.1);
initMIDI();
buildMixer();
buildMusicShape();
buildGUI();
System.out.println("JavaSoundMidiAppletExample.start() done");
}
}
public void stop() {
synchronized (JMSL.class) {
System.out.println("JavaSoundMidiAppletExample.stop() " + new Date());
musicShape.finishAll();
try {
musicShape.waitForDone();
} catch (InterruptedException e) {
e.printStackTrace();
}
JMSL.scheduler.stop();
JMSL.closeMusicDevices();
removeAll();
System.out.println("JavaSoundMidiAppletExample.stop() done");
}
}
private void buildMixer() {
mixer = new JMSLMixerContainer();
mixer.start();
}
private void buildGUI() {
setLayout(new BorderLayout());
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.LEFT));
p.add(startButton = new Button("START"));
p.add(stopButton = new Button("STOP"));
add(BorderLayout.SOUTH, p);
add(BorderLayout.NORTH, new Label(JMSL.version));
add(BorderLayout.CENTER, mixer.getPanAmpControlPanel());
stopButton.setEnabled(false);
startButton.addActionListener(this);
stopButton.addActionListener(this);
}
private void buildMusicShape() {
MidiInstrument ins = new MidiInstrument(1);
mixer.addInstrument(ins);
musicShape = new MusicShape(4);
musicShape.add(0.5, 60, 90, 1.0);
musicShape.add(0.75, 63, 110, 1.0);
musicShape.add(0.75, 58, 120, 1.0);
musicShape.add(0.5, 68, 80, 1.0);
musicShape.setRepeats(Integer.MAX_VALUE);
musicShape.setInstrument(ins);
musicShape.addRepeatPlayable(new Playable() {
public double play(double time, Composable parent) throws InterruptedException {
MusicShape ms = (MusicShape) parent;
ms.scramble(0, ms.size() - 1, 1);
if (JMSLRandom.choose() < 0.6) {
ms.scramble(0, ms.size() - 1, 0);
}
ms.transpose(JMSLRandom.choose(-7, 7), 0, ms.size() - 1, 1);
ms.calcStats();
if (ms.getMin(1) < 30) {
ms.transpose(JMSLRandom.choose(1, 4) * 12, 0, ms.size() - 1, 1);
}
if (ms.getMin(1) > 80) {
ms.transpose(JMSLRandom.choose(1, 4) * -12, 0, ms.size() - 1, 1);
}
return time;
}
});
}
private void initMIDI() {
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 Frame());
JMSL.midi.open();
}
public void actionPerformed(ActionEvent ev) {
Object source = ev.getSource();
if (source == startButton) {
musicShape.launch(JMSL.now());
startButton.setEnabled(false);
stopButton.setEnabled(true);
}
if (source == stopButton) {
musicShape.finishAll();
startButton.setEnabled(true);
stopButton.setEnabled(false);
}
}
}