/** * Etjsyn.java * @author (C) 2003 Karl Brown, All Rights Reserved * Cloned from Phil Burk's Beep applet. * Plays equal tempered interval when a button is pressed. * * Plays equal tempered interval when a button is pressed. * Demonstrates equal tempered scales using JSyn. * Allows user to specify number of divisions per octave * and number of divisions of the interval. * */ package mystuff; import java.util.*; import java.awt.*; import java.applet.Applet; import com.softsynth.jsyn.*; /* DO: Change Boing to match the name of your class. Must match file name! */ public class Etjsyn extends Applet { /* DO: Declare Synthesis Objects here */ SineOscillator myBeep; SineOscillator myBeep2; EnvelopePlayer myEnvPlayer; EnvelopePlayer myEnvPlayer2; SynthEnvelope myEnv; SynthEnvelope myEnv2; BusReader myBusReader; BusWriter myBusWriter; BusWriter myBusWriter2; LineOut myOut; GridLayout gl; Button hitme; Scrollbar so; Scrollbar si; Label lo; Label li; Label lb; Label lr; AWTEventMulticaster ali; AWTEventMulticaster alo; double myFreq; /* Can be run as either an application or as an applet. */ public static void main(String args[]) { /* DO: Change Boing to match the name of your class. Must match file name! */ Etjsyn applet = new Etjsyn(); AppletFrame frame = new AppletFrame("Equal Tempered JSyn", applet); frame.resize(300,100); frame.show(); /* Begin test after frame opened so that DirectSound will use Java window. */ frame.test(); } /* * Setup synthesis by overriding start() method. */ public void start() { try { Synth.startEngine(0); /* DO: Your setup code goes here. ******************/ /* Create unit generators. */ myBeep = new SineOscillator() ; myBeep2 = new SineOscillator() ; myEnvPlayer = new EnvelopePlayer(); myEnvPlayer2 = new EnvelopePlayer(); myOut = new LineOut(); /* Connect units together. */ myBeep.output.connect( myEnvPlayer.amplitude ); myBeep2.output.connect( myEnvPlayer2.amplitude ); myEnvPlayer.output.connect( 0, myOut.input, 0 ); myEnvPlayer2.output.connect( 0, myOut.input, 1 ); /* Create Envelope to be played. */ double[] data = { 0.10, 1.0, /* duration,value pair for frame[0] */ 0.30, 0.5, 0.80, 0.0 }; myEnv = new SynthEnvelope( data ); myEnv2 = new SynthEnvelope( data ); myBeep.amplitude.set( 0.5 ); myBeep2.amplitude.set( 0.5 ); gl = new GridLayout(3,3); gl.setHgap(10); gl.setVgap(10); setLayout(gl); add(new Label("divisions")); add(new Label("interval")); add( lr = new Label("")); /* Create a scrollbar that determines number of steps per octave. */ so = new Scrollbar(Scrollbar.HORIZONTAL, 12, 1, 1, 101); add(so); /* Create a scrollbar that determines number of steps in this interval. */ add( si = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 13) ); /* Create a button that causes a beep. */ add( hitme = new Button("Play") ); add( lo = new Label("12")); add( li = new Label("0")); add( lb = new Label("")); /* Start units. */ myEnvPlayer.start(); myEnvPlayer2.start(); myBeep.start(); myBeep2.start(); myOut.start(); /* *****************************************/ /* Synchronize Java display to make buttons appear. */ getParent().validate(); getToolkit().sync(); } catch(SynthException ex) { SynthAlert.showError(this,ex); } } /* * Clean up synthesis by overriding stop() method. */ public void stop() { try { /* Your cleanup code goes here. */ removeAll(); // remove components from Applet panel. myBeep.stop(); myBeep2.stop(); Synth.stopEngine(); } catch(SynthException e) { SynthAlert.showError(this,e); } } public boolean handleEvent( Event e) { if ( e.target == so ) { int current = so.getValue(); lo.setText(String.valueOf(current)); si.setValue(0); li.setText("0"); si.setMaximum(current + 1); playNotes(); return true; } else if ( e.target == si ) { int current = si.getValue(); li.setText(String.valueOf(current)); playNotes(); return true; } else if ( e.target == hitme && e.id == Event.ACTION_EVENT ) { playNotes(); return true; } return false; } public void playNotes () { try { myBeep.frequency.set(440.0); double octaveFraction = (double)((double)si.getValue()/(double)so.getValue()); double freqMultiplier = Math.pow(2,octaveFraction); myFreq = 440.0 * freqMultiplier; myBeep2.frequency.set( myFreq ); StringBuffer sb = new StringBuffer(); sb.append(myFreq); lb.setText(sb.toString()); double myRatio =(double)( myFreq / (double)440.0 ); StringBuffer rb = new StringBuffer(); rb.append(myRatio); lr.setText(rb.toString()); // Queue envelope to trigger sound. myEnvPlayer.envelopePort.clear(); myEnvPlayer2.envelopePort.clear(); myEnvPlayer.envelopePort.queue( myEnv ); myEnvPlayer2.envelopePort.queue( myEnv2 ); } catch (SynthException ex) { SynthAlert.showError(this,ex); } } }