/* * Created on Apr 7, 2005 * */ package didkovsky2604.applets; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import com.softsynth.jmsl.*; import com.softsynth.jmsl.jsyn.JSynMusicDevice; import com.softsynth.jmsl.jsyn.SimpleSamplePlayingInstrument; /** * Load a 1 sec. stereo sample and a 2 sec. stereo sample. Map them to two different pitches. Use a * MusicShape and a SimpleSamplePlayingInstrument to play them rhythmically. * * @author Nick Didkovsky, didkovn@mail.rockefeller.edu * */ public class StereoLoopingMusicApplet extends java.applet.Applet { SimpleSamplePlayingInstrument ins; JMSLMixerContainer mixer; Button startButton; Button stopButton; MusicShape musicShape; Label msgLabel; public void start() { synchronized (JMSL.class) { JMSL.setIsApplet(true); JMSL.setCodeBase(this.getCodeBase()); JSynMusicDevice.instance().open(); JMSL.clock.setAdvance(0.1); setLayout(new BorderLayout()); buildFileLoaderListener(); buildInstrument(); buildMixer(); add(mixer.getPanAmpControlPanel()); buildMusicShape(); buildControlPanel(); } } public void stop() { synchronized (JMSL.class) { musicShape.finishAll(); JMSL.closeMusicDevices(); } } /** * notify users of file load progress */ private void buildFileLoaderListener() { add(BorderLayout.NORTH, msgLabel = new Label("Message label")); JMSL.addFileLoaderListener(new FileLoaderListener() { public void fileLoading(String msg) { msgLabel.setText("Loading: " + msg); } public void fileDoneLoading(String msg) { msgLabel.setText(msg); } }); } private void buildControlPanel() { Panel p = new Panel(); p.setLayout(new FlowLayout()); p.add(startButton = new Button("start")); p.add(stopButton = new Button("stop")); add(BorderLayout.SOUTH, p); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { startButton.setEnabled(false); stopButton.setEnabled(true); musicShape.launch(JMSL.now()); } }); stopButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { startButton.setEnabled(true); stopButton.setEnabled(false); musicShape.finishAll(); } }); } private void buildMusicShape() { musicShape = new MusicShape(4); musicShape.add(2, 60, 0.5, 2.0); // play 2 second sample mapped to pitch 60 musicShape.add(1, 61, 0.4, 1.0); // play 1 second sample mapped to pitch 61 musicShape.add(1, 61, 0.8, 1.0); // play it again louder musicShape.setInstrument(ins); musicShape.setRepeats(Integer.MAX_VALUE); } private void buildMixer() { mixer = new JMSLMixerContainer(); mixer.addInstrument(ins); mixer.start(); // we know ins is stereo so set panning by hand: mixer.panAmpChange(0, 0, 0.5); mixer.panAmpChange(1, 1, 0.5); } private void buildInstrument() { ins = new SimpleSamplePlayingInstrument("E:/jwork/eclipse_projects/didkovsky2604/"); // top // level // sample // directory ins.setNumChannels(2); ins.addSamplePitch("stereo_aif/TwoSeconds.aif", 60); ins.addSamplePitch("stereo_aif/OneSecond.aif", 61); ins.buildFromAttributes(); } }