/** InsToot09.java Fill a MusicShape with frequency and amplitude data, perform it with a JMSL Instrument that bangs a JSyn circuit. This example is very strict about making no references to JSyn at all. it instantiates a new SineInstrument, gets it MusicDevice, opens an editor to test the MusicDevice, opens the MusicDevice, and proceeds. Applet.stop() calls JMSL.closeMusicDevices(); * @author Nick Didkovsky, didkovn@mail.rockefeller.edu, (C) 2003 Nick Didkovsky, All Rights Reserved */ package jmsltutorial; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import com.softsynth.jmsl.*; import com.softsynth.jmsl.util.Oof; import com.softsynth.jsyn.AppletFrame; public class InsToot09 extends java.applet.Applet implements ActionListener { MusicShape sineShape; TextArea myTextArea; Button startButton; Button stopButton; SineInstrument sineInstrument; JMSLMixerContainer mixer; /* Build our data when the applet initializes */ public void init() { JMSLRandom.randomize(); } /** Do it all here */ public void start() { JMSL.setIsApplet(true); initMusicDeviceAndInstrument(); buildMixer(); buildSineShape(); buildGUI(); /* Synchronize Java display. */ getParent().validate(); getToolkit().sync(); } private void buildMixer() { mixer = new JMSLMixerContainer(); mixer.start(); mixer.addInstrument(sineInstrument); } private void buildGUI() { setLayout(new BorderLayout()); myTextArea = new TextArea(10, 50); startButton = new Button("Start"); stopButton = new Button("Stop"); startButton.setBackground(new Color(110, 255, 100)); startButton.addActionListener(this); stopButton.addActionListener(this); stopButton.setBackground(new Color(255, 100, 100)); add("West", startButton); add("East", stopButton); add("Center", mixer.getPanAmpControlPanel()); add(BorderLayout.NORTH, myTextArea); JMSL.setSTDOut(new TextAreaSTDOut(myTextArea)); } private void initMusicDeviceAndInstrument() { try { sineInstrument = new SineInstrument(); MusicDevice dev = sineInstrument.getMusicDevice(); dev.edit(new Frame()); dev.open(); sineInstrument.buildFromAttributes(); } catch (Exception e) { System.out.println("InsToot09.initMusicDeviceAndInstrument() error: " + e); e.printStackTrace(); } } /** Stuff random duration, frequency, amplitude data into a MusicShape */ void buildSineShape() { double centerFreq = JMSLRandom.choose(440, 880); Oof oof = new Oof(8); oof.randomize(); sineShape = new MusicShape(4); sineShape.setRepeats(1000); // Four dimensions: duration, frequncy, amplitude, holdtime for (int el = 0; el < 17; el++) { double dur = 4.0 * (oof.next() / 255.0); double freq = JMSLRandom.gauss(500, centerFreq); double amp = JMSLRandom.choose(0.3, 0.8); double hold = dur * 0.8; sineShape.add(dur, freq, amp, hold); } sineShape.setInstrument(sineInstrument); } /** Some JSyn term's, and shut down the MusicShape */ public void stop() { sineShape.finish(); mixer.stop(); removeAll(); // remove all gui components JMSL.closeMusicDevices(); } public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == startButton) { myTextArea.setText(""); sineShape.finishAll(); buildSineShape(); sineShape.launch(JMSL.now()); } if (source == stopButton) sineShape.finishAll(); } /* Can be run as either an application or as an applet. */ public static void main(String args[]) { InsToot09 applet = new InsToot09(); AppletFrame frame = new AppletFrame("JMSL Tutorial", applet); frame.setSize(600, 500); frame.show(); frame.test(); } }