package rice2604hw; /* =========================================================== 2604-10/25/99 FMFactory Applet J.Rice ----------------------------------------------------------- This applet allows the user to repetitively create an FM SynthCircuit, each time with new random parameters. Each time the object is created, the specifications are written to a TextArea where the user may view/copy them. Numerator and denominator of the Fc/Fm ratio and the ModIndex are doubles within the range 1.0 - 10.0. And an initial note duration is set to 2.0 seconds and can be changed by the user before a new Object is created. The random envelope maker will create points and times which add up to the current duration and ends with a level of zero. The number of envelope points is not fixed as the time values are randomly determined and the last time is the last value trimmed to fit. The modAmplitude and the freqAmp have their own envelopes. THE ENVELOPE MAKER is called from the FMSynthNote subclass and data retrieved via accessor methods, but the numer, denom and index are done in the applet. This is only a partial implementation of a self-contained FMRandNote object. ============================================================*/ import java.awt.*; import java.applet.Applet; import java.awt.event.*; import com.softsynth.jsyn.*; public class FMFactory extends Applet implements ActionListener{ Button newButton, noteOnButton, noteOffButton, randPitchButton; TextField freqField, ampField, durField; double freq = 440.0; double amp = 0.5; double dur = 2.0; TextArea dumpArea; Panel p; FMRandNote currentNote; double numer, denom, index; LineOut myOut; //------------------------------------------------------------- public void init() { setBackground(Color.magenta); setLayout(new GridLayout(5,1)); //1===== p = new Panel(); p.setLayout(new GridLayout(3, 1)); p.add(new Label("RANDOM FM NOTE PLAYER", Label.CENTER)); p.add(new Label("=========================", Label.CENTER)); p.add(newButton = new Button("**NEW RANDOM FM SETUP**")); newButton.addActionListener(this); add(p); //2====== p = new Panel(); p.setLayout(new GridLayout(3, 2)); p.add(new Label("==================", Label.CENTER)); p.add(new Label("==================", Label.CENTER)); p.add(noteOnButton = new Button("NOTEON()")); p.add(noteOffButton = new Button("NOTEOFF()")); p.add(new Label("==================", Label.CENTER)); p.add(new Label("==================", Label.CENTER)); noteOnButton.addActionListener(this); noteOffButton.addActionListener(this); add(p); //3====== p = new Panel(); p.setLayout(new GridLayout(3, 3)); p.add(new Label("Set Frequency", Label.CENTER)); p.add(new Label("Set Amplitude", Label.CENTER)); p.add(new Label("Set Duration", Label.CENTER)); p.add(freqField = new TextField(freq+"")); p.add(ampField = new TextField(amp+"")); p.add(durField = new TextField(dur+"")); p.add(new Label("============", Label.CENTER)); p.add(new Label("============", Label.CENTER)); p.add(new Label("============", Label.CENTER)); freqField.addActionListener(this); ampField.addActionListener(this); durField.addActionListener(this); add(p); //4======= p = new Panel(); p.setBackground(Color.yellow); p.setLayout(new GridLayout(1,1)); p.add(dumpArea = new TextArea("Code will go here..")); add(p); //5======= p = new Panel(); p.setLayout(new GridLayout(2, 1)); p.add(randPitchButton = new Button("Play Random Pitch")); p.add(new Label("Enter your own duration, copy the data!", Label.CENTER)); randPitchButton.addActionListener(this); add(p); } //==================================================== public void start() { try { Synth.startEngine(0); setupSynthesis(); showData(); } catch (SynthException e) { SynthAlert.showError(this, e); } } //---------------------------------------------------- public void setupSynthesis() { try { currentNote = new FMRandNote(dur); myOut = new LineOut(); currentNote.amplitude.set(0); currentNote.output.connect( 0, myOut.input, 0); currentNote.output.connect( 0, myOut.input, 1); myOut.start(); currentNote.start(); } catch (SynthException e) { SynthAlert.showError(this, e); } } //------------------------------------- public void showData() { int i; double[] d1 = new double[30]; dumpArea.setText("myNote = new FMRandNote("); dumpArea.appendText(((int)(currentNote.getNumer()*100)/100.0)+", "); dumpArea.appendText(((int)(currentNote.getDenom()*100)/100.0)+", "); dumpArea.appendText(((int)(currentNote.getIndex()*100)/100.0)+", "); dumpArea.appendText(((int)(dur*100)/100.0)+");\n"); //--------- dumpArea.appendText("double carEnv = {"); d1 = currentNote.getCarEnv(); for (i = 0; d1[i] > 0.0; i++){ dumpArea.appendText(((int)(d1[i]*100)/100.0)+", "); } dumpArea.appendText("0.0};\n"); //--------- dumpArea.appendText("double modEnv = {"); d1 = currentNote.getModEnv(); for (i = 0; d1[i] > 0.0; i++){ dumpArea.appendText(((int)(d1[i]*100)/100.0)+", "); } dumpArea.appendText("0.0};\n"); //--------- dumpArea.appendText("carEnv / modEnv = RandomEnv.getEnv(duration);"); } //------------------------------------- public void deleteAll() { try { currentNote.delete(); currentNote = null; myOut.delete(); myOut = null; } catch (SynthException e) { SynthAlert.showError(this, e); } } //------------------------------------- public void stop() { try { deleteAll(); Synth.verbosity = Synth.SILENT; Synth.stopEngine(); } catch (SynthException e) { SynthAlert.showError(this, e); } } //==================================================== void handleNewButton() { deleteAll(); setupSynthesis(); showData(); } //---------------------- void handleNoteOnButton() { //try { //HAD ERROR THAT S.E. NOT THROWN... int when = Synth.getTickCount(); currentNote.noteOn(when, freq, amp); /* } catch (SynthException e) { System.out.println("Trouble in handleNoteOn() " + e); }*/ } //---------------------- void handleNoteOffButton() { currentNote.noteOff(Synth.getTickCount()); } //---------------------- void handleFreqFieldButton() { try { freq = ((new Double(freqField.getText())).doubleValue()); } catch (NumberFormatException e) { freqField.setText("NFE..."); } } //---------------------- void handleAmpFieldButton() { try { amp = ((new Double(ampField.getText())).doubleValue()); } catch (NumberFormatException e) { ampField.setText("NFE..."); } } //---------------------- void handleDurFieldButton() { try { dur = ((new Double(durField.getText())).doubleValue()); } catch (NumberFormatException e) { durField.setText("NFE..."); } } //---------------------- void handleRandPitchButton() { int when = Synth.getTickCount(); freq = (int)((Math.random() * 950) + 50); currentNote.noteOn(when, freq, amp); freqField.setText(freq+""); } //---------------------- public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source == newButton) handleNewButton(); if (source == noteOnButton) handleNoteOnButton(); if (source == noteOffButton) handleNoteOffButton(); if (source == freqField) handleFreqFieldButton(); if (source == ampField) handleAmpFieldButton(); if (source == durField) handleDurFieldButton(); if (source == randPitchButton) handleRandPitchButton(); } //======================STAND-ALONE-MAIN==================== public static void main(String args[]) { FMFactory applet = new FMFactory(); AppletFrame frame = new AppletFrame("FMFactory", applet); frame.setSize(300, 400); frame.setVisible(true); frame.test(); } //=========================END APPLET======================= }