package rice2604hw; /* =============================================================== 2604 KeyInstrument J.Rice --------------------------------------------------------------- A JMSL instrument file which acts as intermediary between a music applet and a Jsyn circuit. This file is instantiated by the Key- PlayerApplet, creates a circuit, makes amplitude envelopes and allows JMSL note data to get to the fundamental circuit. --------------------------------------------------------------- Edited 11/05/00 =============================================================== */ import com.softsynth.jmsl.*; import com.softsynth.jsyn.*; class KeyInstrument extends Instrument { SawTriLFO myOsc; LineOut myOut; SynthEnvelope ampEnvData; EnvelopePlayer ampEnvPlayer; public KeyInstrument() { myOsc = new SawTriLFO(); myOut = new LineOut(); setParameters(); ampEnvData = new SynthEnvelope(makeEnvelope2()); ampEnvPlayer = new EnvelopePlayer(); ampEnvPlayer.output.connect(0, myOsc.amplitude, 0); myOsc.output0.connect(0, myOut.input, 0); myOsc.output1.connect(0, myOut.input, 1); } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public void setParameters() { double vari; myOsc.freqLFO.set(JMSLRandom.choose(10.0)); myOsc.freqLFO2.set(JMSLRandom.choose(10.0)); myOsc.ampLFO.set(JMSLRandom.choose(20)); myOsc.ampLFO2.set(JMSLRandom.choose(20)); vari = (JMSLRandom.choose(2000)+50); myOsc.cutoff.set((int)vari); } //------------------------------------- public double[] makeEnvelope2() { double[] d = new double[8]; double x, y, z, dur; x = JMSLRandom.choose(2.0); z = JMSLRandom.choose(2.0); d[0] = z; d[1] = .75; //down from .85, 11/05 d[2] = x - z; d[3] = (JMSLRandom.choose(0.3) + 0.4); d[4] = x; d[5] = (JMSLRandom.choose(0.3) + 0.4); d[6] = x; d[7] = 0.0; dur = ((int)(x * 3000.0) / 1000.0); JMSL.out.print(" [envelope of dur: " + dur + "] "); return d; } //-------------------------------------------------------- public double open(double playTime) { int itime = (int)JMSL.clock.timeToNative(playTime); myOsc.start(itime); myOut.start(itime); ampEnvPlayer.start(itime); return playTime; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public double close(double playTime) { int itime = (int)JMSL.clock.timeToNative(playTime); myOsc.stop(itime); myOut.stop(itime); ampEnvPlayer.stop(itime); return playTime; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ public double play(double playTime, double timeStretch, double dar[]) { int itime = (int)JMSL.clock.timeToNative(playTime); myOsc.frequency.set(itime, dar[1]); ampEnvPlayer.amplitude.set(itime, dar[2]); ampEnvPlayer.envelopePort.clear(itime); ampEnvPlayer.envelopePort.queue(itime, ampEnvData, 0, ampEnvData.getNumFrames()); return playTime + dar[0] * timeStretch; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ }