/** 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, nick@didkovsky.com, (C) 2003 Nick Didkovsky, All Rights Reserved */ package jmsltutorial; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Frame; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import com.softsynth.jmsl.JMSL; import com.softsynth.jmsl.JMSLMixerContainer; import com.softsynth.jmsl.JMSLRandom; import com.softsynth.jmsl.MusicDevice; import com.softsynth.jmsl.MusicShape; import com.softsynth.jmsl.util.Oof; import com.softsynth.jmsl.view.MusicShapeEditor; public class InsToot09 extends JFrame implements ActionListener { MusicShape sineShape; TextArea myTextArea; Button startButton; Button stopButton; SineInstrument sineInstrument; JMSLMixerContainer mixer; MusicShapeEditor se; public void start() { initMusicDeviceAndInstrument(); buildMixer(); buildGUI(); buildSineShape(); pack(); setVisible(true); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } 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"); se = new MusicShapeEditor(); 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); add(BorderLayout.SOUTH, se.getComponent()); JMSL.setSTDOut(new TextAreaSTDOut(myTextArea)); } private void initMusicDeviceAndInstrument() { try { sineInstrument = new SineInstrument(); MusicDevice dev = sineInstrument.getMusicDevice(); dev.edit(new Frame()); dev.open(); JMSL.clock.setAdvance(0.2); 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.setDimensionName(1, "frequency"); sineShape.setLimits(1, 0, 1000); sineShape.setRepeats(1000); // Four dimensions: duration, frequency, amplitude, holdtime for (int el = 0; el < 117; el++) { double dur = 4.0 * (oof.next() / 255.0); double freq = JMSLRandom.gaussVar(500, centerFreq); double amp = JMSLRandom.choose(0.3, 0.8); double hold = dur * 0.8; sineShape.add(dur, freq, amp, hold); } sineShape.setInstrument(sineInstrument); se.addMusicShape(sineShape); sineShape.print(); } 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(); } public static void main(String args[]) { InsToot09 toot = new InsToot09(); toot.start(); } }