package jmslexamples.jsyn; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import com.softsynth.jmsl.*; import com.softsynth.jmsl.jsyn.JSynInsFromClassName; import com.softsynth.jmsl.jsyn.JSynMusicDevice; import com.softsynth.jmsl.util.EventDistributions; import com.softsynth.jmsl.view.JMSLScrollbar; import com.softsynth.jmsl.view.JMSLScrollbarProcessor; import com.softsynth.jsyn.AppletFrame; /** * Launch a MusicJob whose performance density is generated by a Myhill Distribution. Provide GUI * sliders for user to change density parameters. * * @author Nick Didkovsky, copyright Nick Didkovsky 4/4/01 10:32AM */ public class EventDistributionsJSynDemo extends java.applet.Applet implements JMSLScrollbarProcessor, ActionListener { JMSLMixerContainer mixer; JMSLScrollbar meanScrollbar; JMSLScrollbar ratioScrollbar; Label meanLabel; Label ratioLabel; double mean = 4.0; double ratio = 1.01; DensityJob job; Button startButton; Button stopButton; public void init() { JMSLRandom.randomize(); setLayout(new GridLayout(2, 1)); } public void start() { JSynMusicDevice.instance().edit(new Frame()); JSynMusicDevice.instance().open(); JSynMusicDevice.instance().setEditEnabled(false); JMSL.clock.setAdvance(0.1); JSynInsFromClassName ins = new JSynInsFromClassName(8, com.softsynth.jmsl.jsyn.circuits.BrassSynthNote.class .getName()); mixer = new JMSLMixerContainer(); mixer.start(); mixer.addInstrument(ins); job = new DensityJob(); job.setRepeats(Integer.MAX_VALUE); job.setInstrument(ins); job.setDensity(mean); job.setRatio(ratio); meanScrollbar = new JMSLScrollbar(this, (int) mean * 100, 10, 2000); meanScrollbar.setSize(200, 20); meanLabel = new Label(" Mean= " + meanScrollbar.getValue() / 100.0); Panel p1 = new Panel(); p1.setLayout(new GridLayout(1, 2)); p1.add(meanScrollbar); p1.add(meanLabel); ratioScrollbar = new JMSLScrollbar(this, (int) (ratio * 100), 101, 12800); // 100ths ratioScrollbar.setSize(200, 20); ratioLabel = new Label(" Ratio= " + (ratioScrollbar.getValue() / 100.0)); Panel p2 = new Panel(); p2.setLayout(new GridLayout(1, 2)); p2.add(ratioScrollbar); p2.add(ratioLabel); Panel p = new Panel(); p.setLayout(new GridLayout(2, 1)); p.add(p1); p.add(p2); add("North", p); p = new Panel(); p.setLayout(new GridLayout(1, 2)); p.add(startButton = new Button("START")); p.add(stopButton = new Button("STOP")); add("South", p); startButton.addActionListener(this); stopButton.addActionListener(this); /* Synchronize Java display. */ getParent().validate(); getToolkit().sync(); } public void stop() { job.finish(); removeAll(); JMSL.closeMusicDevices(); } // JMSLScrollbar style of handling scrollbar events public void JMSLScrollbarValueChanged(JMSLScrollbar source) { if (source == meanScrollbar) { handleMean(); } if (source == ratioScrollbar) { handleRatio(); } } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == startButton) job.launch(JMSL.now()); if (source == stopButton) job.finish(); } public double getMean() { return mean; } void handleMean() { mean = (double) meanScrollbar.getValue() / 100.0; meanLabel.setText(" Mean= " + mean); job.setDensity(mean); } public double getRatio() { return ratio; } private String prettyString(double val) { return val + ""; // what's so pretty about that? } void handleRatio() { ratio = ((double) ratioScrollbar.getValue()) / 100.0; ratioLabel.setText(" Ratio= " + prettyString(ratio)); job.setRatio(ratio); } public static void main(String args[]) { EventDistributionsJSynDemo applet = new EventDistributionsJSynDemo(); AppletFrame frame = new AppletFrame("Event Density Example", applet); frame.setSize(400, 200); frame.setVisible(true); frame.setLayout(new FlowLayout()); frame.test(); } } class DensityJob extends MusicJob { private double eventDensity; private double ratio; private MusicShape musicShapeLog; public double start(double playTime) { musicShapeLog = new MusicShape(4); musicShapeLog.useStandardDimensionNameSpace(); return playTime; } public double repeat(double playTime) { double dur = EventDistributions.genEntryDelayMyhill(eventDensity, ratio); double pitch = 60 + JMSLRandom.choose(12); double amplitude = JMSLRandom.choose(0.1, 0.6); double[] dar = { dur, pitch, amplitude, dur * 1.25 }; musicShapeLog.add(dar); return getInstrument().play(playTime, 1.0, dar); } public void setRatio(double r) { ratio = r; } public void setDensity(double d) { eventDensity = d; } /** * Get MusicShape loaded with algorithmically generated elements (used by subclass * EventDistributionsJSynDemoWithScore ) * * @return Returns the musicShapeLog. */ public MusicShape getMusicShapeLog() { return musicShapeLog; } }