package jmsltutorial; import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import com.softsynth.jsyn.*; /** A SineOscillator SynthNote with an amplitude envelope. noteOn() sustains, noteOff() releases. @author Nick Didkovsky, 11/21/2000 10:12AM, copyright (c) 2000 Nick Didkovsky, all rights reserved. */ public class SineSynthNote extends SynthNote { SineOscillator osc; EnvelopePlayer ampEnvPlayer; SynthEnvelope ampEnv; public SineSynthNote() { add(osc = new SineOscillator()); add(ampEnvPlayer = new EnvelopePlayer()); ampEnvPlayer.output.connect(osc.amplitude); // expose the freq and amp of the sineOscillator as aliases addPort(frequency = osc.frequency); addPort(amplitude = ampEnvPlayer.amplitude); frequency.setup(0.0, 440.0, 1000.0); amplitude.setup(0.0, 0.5, 1.0); // expose an output port aliased to the oscillator's output addPort(output = osc.output, "Output"); double[] envData = { 0.06, 1.0, 0.2, 0.6, 0.1, 0.0 }; ampEnv = new SynthEnvelope(envData); } /** setStage will be called by noteOn after freq and ampo are set. setStage(1) will be called by noteOff() */ public void setStage(int time, int stage) { switch (stage) { case 0 : start(time); ampEnvPlayer.envelopePort.clear(time); ampEnvPlayer.envelopePort.queue(time, ampEnv, 0, 2); // queue first two frames, the attack break; case 1 : ampEnvPlayer.envelopePort.clear(time); ampEnvPlayer.envelopePort.queue(time, ampEnv, 2, 1, Synth.FLAG_AUTO_STOP); // release and shut off circuit break; default : break; } } /** Play some noteOn / noteOff()'s to test the SynthNote */ public static void main(String args[]) { // open a Frame that will shut down SynthEngine and system.exit() when closed. Frame f = new Frame("Test SynthNote"); f.setSize(320, 200); f.setVisible(true); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { Synth.stopEngine(); System.exit(0); } }); Synth.startEngine(0); SynthObject.enableTracking(true); // create a LineOut and a SynthNote, wire them together LineOut out = new LineOut(); SineSynthNote synthNote = new SineSynthNote(); synthNote.output.connect(0, out.input, 0); synthNote.output.connect(0, out.input, 1); out.start(); // Simple loop that uses JSyn's event buffer to play a note every two seconds, each with a 1.5 second sustain // frequency changes by whole number ratio int now = Synth.getTickCount(); int ticksPerSecond = (int) Synth.getTickRate(); double freq = 440.0; for (int i = 0; i < 12; i++) { int onTime = now + i * ticksPerSecond * 2; int offTime = onTime + ticksPerSecond * 3 / 2; double ratio = 3 / 2.0; if (i % 2 == 1) ratio = 3 / 5.0; freq *= ratio; synthNote.noteOn(onTime, freq, 1.0); synthNote.noteOff(offTime); } } }