/*
* Created on May 27, 2004
*
*/
package jmslexamples;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.JFrame;
import com.softsynth.jmsl.*;
import com.softsynth.jmsl.midi.MidiIO_JavaSound;
import com.softsynth.jmsl.midi.MidiInstrument;
/**
*
* Play a self-modifying MusicShape with a JMSL MidiInstrument using MidiIO_JavaSound as MusicDevice
*
*
*
* SoundFont support! As of Java 1.7 uses Gervill JavaSound. This demo uses FluidR3 soundfont. If
* you don't have it, JavaSound will use Java's default sounds.
* You can download this SoundFont at http://www.algomusic.com/SoundFonts/
*
* Nick Didkovsky
*
* @author Nick Didkovsky, email: nick@didkovsky.com, (c) 2004 Nick Didkovsky, all rights
* reserved.
*/
public class JavaSoundMidiExample extends JFrame implements ActionListener {
JMSLMixerContainer mixer;
MusicShape musicShape;
Button startButton;
Button stopButton;
// you can download this SoundFont at http://www.algomusic.com/SoundFonts/
String LOCATION_OF_SOUNDFONT = "F:/jwork/SoundFonts/FluidR3/FluidR3 GM2-2.SF2";
private void initMIDI() {
JMSL.midi = MidiIO_JavaSound.instance();
((MidiIO_JavaSound) JMSL.midi).setSoundbankFile(new File(LOCATION_OF_SOUNDFONT));
JMSL.midi.setOutDevice("Gervill");
JMSL.midi.edit(this);
JMSL.midi.open();
}
public void start() {
JMSLRandom.randomize();
JMSL.clock = new DefaultMusicClock();
JMSL.scheduler = new EventScheduler();
JMSL.scheduler.start();
JMSL.clock.setAdvance(0.1);
initMIDI();
buildMixer();
buildMusicShape();
buildGUI();
}
public void stop() {
musicShape.finishAll();
try {
musicShape.waitForDone();
} catch (InterruptedException e) {
e.printStackTrace();
}
JMSL.scheduler.stop();
JMSL.closeMusicDevices();
removeAll();
}
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;
}
});
}
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);
}
}
public static void main(String[] args) {
final JavaSoundMidiExample example = new JavaSoundMidiExample();
example.start();
example.pack();
example.setVisible(true);
example.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
example.stop();
System.exit(0);
}
});
}
}