aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/jsyn/unitgen
diff options
context:
space:
mode:
authorPhil Burk <[email protected]>2016-08-02 07:52:17 -0700
committerPhil Burk <[email protected]>2016-10-24 08:29:20 -0700
commit580fea450ec0982d0bd8be589f00566267e7b0d1 (patch)
tree0420f768fc7c63208b1720232c447e17af9017af /src/com/jsyn/unitgen
parenta6583e89166f7477a675cf3094a91b303ba7850a (diff)
Instruments: add better synth, pitch control
Diffstat (limited to 'src/com/jsyn/unitgen')
-rw-r--r--src/com/jsyn/unitgen/Circuit.java42
-rw-r--r--src/com/jsyn/unitgen/EnvelopeDAHDSR.java6
-rw-r--r--src/com/jsyn/unitgen/FilterFourPoles.java49
-rw-r--r--src/com/jsyn/unitgen/LineOut.java6
-rw-r--r--src/com/jsyn/unitgen/LinearRamp.java50
-rw-r--r--src/com/jsyn/unitgen/MorphingOscillatorBL.java72
-rw-r--r--src/com/jsyn/unitgen/PitchToFrequency.java26
-rw-r--r--src/com/jsyn/unitgen/PowerOfTwo.java79
-rw-r--r--src/com/jsyn/unitgen/PulseOscillatorBL.java14
-rw-r--r--src/com/jsyn/unitgen/SawtoothOscillatorDPW.java6
-rw-r--r--src/com/jsyn/unitgen/SquareOscillatorBL.java9
-rw-r--r--src/com/jsyn/unitgen/TunableFilter.java10
-rw-r--r--src/com/jsyn/unitgen/UnitGenerator.java32
-rw-r--r--src/com/jsyn/unitgen/UnitOscillator.java16
14 files changed, 316 insertions, 101 deletions
diff --git a/src/com/jsyn/unitgen/Circuit.java b/src/com/jsyn/unitgen/Circuit.java
index a501600..c5a1dcf 100644
--- a/src/com/jsyn/unitgen/Circuit.java
+++ b/src/com/jsyn/unitgen/Circuit.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -17,17 +17,21 @@
package com.jsyn.unitgen;
import java.util.ArrayList;
+import java.util.LinkedHashMap;
import com.jsyn.engine.SynthesisEngine;
+import com.jsyn.ports.UnitPort;
/**
* Contains a list of units that are executed together.
- *
+ *
* @author Phil Burk (C) 2009 Mobileer Inc
*/
public class Circuit extends UnitGenerator {
private ArrayList<UnitGenerator> units = new ArrayList<UnitGenerator>();
+ private final LinkedHashMap<String, UnitPort> portAliases = new LinkedHashMap<String, UnitPort>();
+
@Override
public void generate(int start, int limit) {
for (UnitGenerator unit : units) {
@@ -57,6 +61,11 @@ public class Circuit extends UnitGenerator {
}
}
+ /**
+ * @deprecated ignored, frameRate comes from the SynthesisEngine
+ * @param rate
+ */
+ @Deprecated
@Override
public void setFrameRate(int frameRate) {
super.setFrameRate(frameRate);
@@ -83,4 +92,31 @@ public class Circuit extends UnitGenerator {
public void usePreset(int presetIndex) {
}
+
+
+ /**
+ * Add an alternate name for looking up a port.
+ * @param port
+ * @param alias
+ */
+ public void addPortAlias(UnitPort port, String alias) {
+ // Store in a hash table by an alternate name.
+ portAliases.put(alias.toLowerCase(), port);
+ }
+
+
+ /**
+ * Case-insensitive search for a port by its name or alias.
+ * @param portName
+ * @return matching port or null
+ */
+ @Override
+ public UnitPort getPortByName(String portName) {
+ UnitPort port = super.getPortByName(portName);
+ if (port == null) {
+ port = portAliases.get(portName.toLowerCase());
+ }
+ return port;
+ }
+
}
diff --git a/src/com/jsyn/unitgen/EnvelopeDAHDSR.java b/src/com/jsyn/unitgen/EnvelopeDAHDSR.java
index 6acd763..c5ebe83 100644
--- a/src/com/jsyn/unitgen/EnvelopeDAHDSR.java
+++ b/src/com/jsyn/unitgen/EnvelopeDAHDSR.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -30,7 +30,7 @@ import com.jsyn.ports.UnitOutputPort;
* exponential Release will never reach 0.0. But when it reaches -96 dB the DAHDSR just sets its
* output to 0.0 and stops. There is an example program in the ZIP archive called HearDAHDSR. It
* drives a DAHDSR with a square wave.
- *
+ *
* @author Phil Burk (C) 2010 Mobileer Inc
* @see SegmentedEnvelope
*/
diff --git a/src/com/jsyn/unitgen/FilterFourPoles.java b/src/com/jsyn/unitgen/FilterFourPoles.java
index d4f80f4..39a47c7 100644
--- a/src/com/jsyn/unitgen/FilterFourPoles.java
+++ b/src/com/jsyn/unitgen/FilterFourPoles.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,13 +20,13 @@ import com.jsyn.ports.UnitInputPort;
/**
* Resonant filter in the style of the Moog ladder filter. This implementation is loosely based on:
- * http://www.musicdsp.org/archive.php?classid=3#26
+ * http://www.musicdsp.org/archive.php?classid=3#26
* More interesting reading:
* http://dafx04.na.infn.it/WebProc/Proc/P_061.pdf
* http://www.acoustics.ed.ac.uk/wp-content/uploads/AMT_MSc_FinalProjects
* /2012__Daly__AMT_MSc_FinalProject_MoogVCF.pdf
* http://www.music.mcgill.ca/~ich/research/misc/papers/cr1071.pdf
- *
+ *
* @author Phil Burk (C) 2014 Mobileer Inc
* @see FilterLowPass
*/
@@ -37,6 +37,14 @@ public class FilterFourPoles extends TunableFilter {
private static final double MINIMUM_FREQUENCY = 1.0; // blows up if near 0.01
private static final double MINIMUM_Q = 0.00001;
+ //private static final double SATURATION_COEFFICIENT = 0.1666667;
+ private static final double SATURATION_COEFFICIENT = 0.2;
+ // Inflection point where slope is zero.
+ private static final double SATURATION_UPPER_INPUT = 1.0 / Math.sqrt(3.0 * SATURATION_COEFFICIENT);
+ private static final double SATURATION_LOWER_INPUT = 0.0 - SATURATION_UPPER_INPUT;
+ private static final double SATURATION_UPPER_OUTPUT = cubicPolynomial(SATURATION_UPPER_INPUT);
+ private static final double SATURATION_LOWER_OUTPUT = cubicPolynomial(SATURATION_LOWER_INPUT);
+
private double x1;
private double x2;
private double x3;
@@ -57,6 +65,7 @@ public class FilterFourPoles extends TunableFilter {
public FilterFourPoles() {
addPort(Q = new UnitInputPort("Q"));
+ frequency.setup(40.0, DEFAULT_FREQUENCY, 4000.0);
Q.setup(0.1, 2.0, 10.0);
}
@@ -111,7 +120,6 @@ public class FilterFourPoles extends TunableFilter {
oneSample(0.0);
}
oneSample(x0);
-
outputs[i] = y4;
}
@@ -143,8 +151,35 @@ public class FilterFourPoles extends TunableFilter {
this.oversampled = oversampled;
}
- private double clip(double x) {
- return x - (x * x * x * 0.1666667);
+ // Soft saturation. This used to blow up the filter!
+ private static double cubicPolynomial(double x) {
+ return x - (x * x * x * SATURATION_COEFFICIENT);
}
+ private static double clip(double x) {
+ if (x > SATURATION_UPPER_INPUT) {
+ return SATURATION_UPPER_OUTPUT;
+ } else if (x < SATURATION_LOWER_INPUT) {
+ return SATURATION_LOWER_OUTPUT;
+ } else {
+ return cubicPolynomial(x);
+ }
+ }
+
+ public void reset() {
+ x1 = 0.0;
+ x2 = 0.0;
+ x3 = 0.0;
+ x4 = 0.0;
+ y1 = 0.0;
+ y2 = 0.0;
+ y3 = 0.0;
+ y4 = 0.0;
+
+ previousFrequency = 0.0;
+ previousQ = 0.0;
+ f = 0.0;
+ fTo4th = 0.0;
+ feedback = 0.0;
+ }
}
diff --git a/src/com/jsyn/unitgen/LineOut.java b/src/com/jsyn/unitgen/LineOut.java
index 489033e..d58f211 100644
--- a/src/com/jsyn/unitgen/LineOut.java
+++ b/src/com/jsyn/unitgen/LineOut.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,7 +20,7 @@ import com.jsyn.ports.UnitInputPort;
/**
* Input audio is sent to the external audio output device.
- *
+ *
* @author Phil Burk (C) 2009 Mobileer Inc
*/
public class LineOut extends UnitGenerator implements UnitSink {
diff --git a/src/com/jsyn/unitgen/LinearRamp.java b/src/com/jsyn/unitgen/LinearRamp.java
index 438adf6..cad53d5 100644
--- a/src/com/jsyn/unitgen/LinearRamp.java
+++ b/src/com/jsyn/unitgen/LinearRamp.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -26,7 +26,7 @@ import com.jsyn.ports.UnitVariablePort;
* value toward the value of input. An internal phase value will go from 0.0 to 1.0 at a rate
* controlled by time. When the internal phase reaches 1.0, the output will equal input.
* <P>
- *
+ *
* @author (C) 1997 Phil Burk, SoftSynth.com
* @see ExponentialRamp
* @see AsymptoticRamp
@@ -52,33 +52,41 @@ public class LinearRamp extends UnitFilter {
public void generate(int start, int limit) {
double[] outputs = output.getValues();
double currentInput = input.getValues()[0];
- double currentTime = time.getValues()[0];
double currentValue = current.getValue();
- if (currentTime != timeHeld) {
- rate = convertTimeToRate(currentTime);
- timeHeld = currentTime;
- }
-
- /* If input has changed, start new segment */
- if (currentInput != target) /*
- * Equality check is OK because we set them exactly equal below.
- */
+ // If input has changed, start new segment.
+ // Equality check is OK because we set them exactly equal below.
+ if (currentInput != target)
{
source = currentValue;
phase = 0.0;
target = currentInput;
}
- for (int i = start; i < limit; i++) {
- if (phase < 1.0) {
- /* Interpolate current. */
- currentValue = source + (phase * (target - source));
- phase += rate;
- } else {
- currentValue = target;
+ if (currentValue == target) {
+ // at end of ramp
+ for (int i = start; i < limit; i++) {
+ outputs[i] = currentValue;
+ }
+ } else {
+ // in middle of ramp
+ double currentTime = time.getValues()[0];
+ // Has time changed?
+ if (currentTime != timeHeld) {
+ rate = convertTimeToRate(currentTime);
+ timeHeld = currentTime;
+ }
+
+ for (int i = start; i < limit; i++) {
+ if (phase < 1.0) {
+ /* Interpolate current. */
+ currentValue = source + (phase * (target - source));
+ phase += rate;
+ } else {
+ currentValue = target;
+ }
+ outputs[i] = currentValue;
}
- outputs[i] = currentValue;
}
current.setValue(currentValue);
diff --git a/src/com/jsyn/unitgen/MorphingOscillatorBL.java b/src/com/jsyn/unitgen/MorphingOscillatorBL.java
new file mode 100644
index 0000000..7ca440d
--- /dev/null
+++ b/src/com/jsyn/unitgen/MorphingOscillatorBL.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2009 Phil Burk, Mobileer Inc
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.jsyn.unitgen;
+
+import com.jsyn.engine.MultiTable;
+import com.jsyn.ports.UnitInputPort;
+
+/**
+ * Oscillator that can change its shape from sine to sawtooth to pulse.
+ *
+ * @author Phil Burk (C) 2016 Mobileer Inc
+ */
+public class MorphingOscillatorBL extends PulseOscillatorBL {
+ /**
+ * Controls the shape of the waveform.
+ * The shape varies continuously from a sine wave at -1.0,
+ * to a sawtooth at 0.0 to a pulse wave at 1.0.
+ */
+ public UnitInputPort shape;
+
+ public MorphingOscillatorBL() {
+ addPort(shape = new UnitInputPort("Shape"));
+ shape.setMinimum(-1.0);
+ shape.setMaximum(1.0);
+ }
+
+ @Override
+ protected double generateBL(MultiTable multiTable, double currentPhase,
+ double positivePhaseIncrement, double flevel, int i) {
+ double[] shapes = shape.getValues();
+ double shape = shapes[i];
+
+ if (shape < 0.0) {
+ // Squeeze flevel towards the pure sine table.
+ flevel += flevel * shape;
+ return multiTable.calculateSawtooth(currentPhase, positivePhaseIncrement, flevel);
+ } else {
+ double[] widths = width.getValues();
+ double width = widths[i];
+ width = (width > 0.999) ? 0.999 : ((width < -0.999) ? -0.999 : width);
+
+ double val1 = multiTable.calculateSawtooth(currentPhase, positivePhaseIncrement, flevel);
+ // Generate second sawtooth so we can add them together.
+ double phase2 = currentPhase + 1.0 - width; // 180 degrees out of phase
+ if (phase2 >= 1.0) {
+ phase2 -= 2.0;
+ }
+ double val2 = multiTable.calculateSawtooth(phase2, positivePhaseIncrement, flevel);
+
+ /*
+ * Need to adjust amplitude based on positive phaseInc. little less than half at
+ * Nyquist/2.0!
+ */
+ double scale = 1.0 - positivePhaseIncrement;
+ return scale * (val1 - ((val2 + width) * shape)); // apply shape morphing
+ }
+ }
+}
diff --git a/src/com/jsyn/unitgen/PitchToFrequency.java b/src/com/jsyn/unitgen/PitchToFrequency.java
new file mode 100644
index 0000000..9086749
--- /dev/null
+++ b/src/com/jsyn/unitgen/PitchToFrequency.java
@@ -0,0 +1,26 @@
+package com.jsyn.unitgen;
+
+import com.softsynth.math.AudioMath;
+
+public class PitchToFrequency extends PowerOfTwo {
+
+ public PitchToFrequency() {
+ input.setup(0.0, 60.0, 127.0);
+ }
+
+ /**
+ * Convert from MIDI pitch to an octave offset from Concert A.
+ */
+ @Override
+ public double adjustInput(double in) {
+ return (in - AudioMath.CONCERT_A_PITCH) * (1.0/12.0);
+ }
+
+ /**
+ * Convert scaler to a frequency relative to Concert A.
+ */
+ @Override
+ public double adjustOutput(double out) {
+ return out * AudioMath.getConcertAFrequency();
+ }
+}
diff --git a/src/com/jsyn/unitgen/PowerOfTwo.java b/src/com/jsyn/unitgen/PowerOfTwo.java
index 5f1b4cd..5916860 100644
--- a/src/com/jsyn/unitgen/PowerOfTwo.java
+++ b/src/com/jsyn/unitgen/PowerOfTwo.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -22,18 +22,23 @@ import com.jsyn.ports.UnitOutputPort;
/**
* output = (2.0^input) This is useful for converting a pitch modulation value into a frequency
* scaler. An input value of +1.0 will output 2.0 for an octave increase. An input value of -1.0
- * will output 0.5 for an octave decrease. This implementation uses a table lookup to optimize for
+ * will output 0.5 for an octave decrease.
+ *
+ * This implementation uses a table lookup to optimize for
* speed. It is accurate enough for tuning. It also checks to see if the current input value is the
* same as the previous input value. If so then it reuses the previous computed value.
- *
+ *
* @author Phil Burk (C) 2010 Mobileer Inc
*/
public class PowerOfTwo extends UnitGenerator {
+ /**
+ * Offset in octaves.
+ */
public UnitInputPort input;
public UnitOutputPort output;
private static double[] table;
- private static final int NUM_VALUES = 1024;
+ private static final int NUM_VALUES = 2048;
// Cached computation.
private double lastInput = 0.0;
private double lastOutput = 1.0;
@@ -61,39 +66,43 @@ public class PowerOfTwo extends UnitGenerator {
double[] inputs = input.getValues();
double[] outputs = output.getValues();
- if (true) {
- for (int i = start; i < limit; i++) {
- double in = inputs[i];
- // Can we reuse a previously computed value?
- if (in == lastInput) {
- outputs[i] = lastOutput;
- } else {
- int octave = (int) Math.floor(in);
- double normal = in - octave;
- // Do table lookup.
- double findex = normal * NUM_VALUES;
- int index = (int) findex;
- double fraction = findex - index;
- double value = table[index] + (fraction * (table[index + 1] - table[index]));
+ for (int i = start; i < limit; i++) {
+ double in = inputs[i];
+ // Can we reuse a previously computed value?
+ if (in == lastInput) {
+ outputs[i] = lastOutput;
+ } else {
+ lastInput = in;
+ double adjustedInput = adjustInput(in);
+ int octave = (int) Math.floor(adjustedInput);
+ double normal = adjustedInput - octave;
+ // Do table lookup.
+ double findex = normal * NUM_VALUES;
+ int index = (int) findex;
+ double fraction = findex - index;
+ double value = table[index] + (fraction * (table[index + 1] - table[index]));
- // Adjust for octave.
- while (octave > 0) {
- octave -= 1;
- value *= 2.0;
- }
- while (octave < 0) {
- octave += 1;
- value *= 0.5;
- }
- outputs[i] = value;
- lastInput = in;
- lastOutput = value;
+ // Adjust for octave.
+ while (octave > 0) {
+ octave -= 1;
+ value *= 2.0;
}
- }
- } else {
- for (int i = start; i < limit; i++) {
- outputs[i] = Math.pow(2.0, inputs[i]);
+ while (octave < 0) {
+ octave += 1;
+ value *= 0.5;
+ }
+ double adjustedOutput = adjustOutput(value);
+ outputs[i] = adjustedOutput;
+ lastOutput = adjustedOutput;
}
}
}
+
+ public double adjustInput(double in) {
+ return in;
+ }
+
+ public double adjustOutput(double out) {
+ return out;
+ }
}
diff --git a/src/com/jsyn/unitgen/PulseOscillatorBL.java b/src/com/jsyn/unitgen/PulseOscillatorBL.java
index 43fe27b..c0e234c 100644
--- a/src/com/jsyn/unitgen/PulseOscillatorBL.java
+++ b/src/com/jsyn/unitgen/PulseOscillatorBL.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,11 +20,15 @@ import com.jsyn.engine.MultiTable;
import com.jsyn.ports.UnitInputPort;
/**
- * Pulse oscillator that uses two band limited sawtooth oscillators.
- *
+ * Pulse oscillator that uses two band limited sawtooth waveforms.
+ *
* @author Phil Burk (C) 2009 Mobileer Inc
*/
public class PulseOscillatorBL extends SawtoothOscillatorBL {
+ /** Controls the duty cycle of the pulse waveform.
+ * The width varies from -1.0 to +1.0.
+ * When width is zero the output is a square wave.
+ */
public UnitInputPort width;
public PulseOscillatorBL() {
@@ -48,7 +52,7 @@ public class PulseOscillatorBL extends SawtoothOscillatorBL {
double val2 = multiTable.calculateSawtooth(phase2, positivePhaseIncrement, flevel);
/*
- * Need to adjust amplitude based on positive phaseInc. little less than half at
+ * Need to adjust amplitude based on positive phaseInc and width. little less than half at
* Nyquist/2.0!
*/
double scale = 1.0 - positivePhaseIncrement;
diff --git a/src/com/jsyn/unitgen/SawtoothOscillatorDPW.java b/src/com/jsyn/unitgen/SawtoothOscillatorDPW.java
index 6868c15..27d0c5a 100644
--- a/src/com/jsyn/unitgen/SawtoothOscillatorDPW.java
+++ b/src/com/jsyn/unitgen/SawtoothOscillatorDPW.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,7 +20,7 @@ package com.jsyn.unitgen;
* Sawtooth DPW oscillator (a sawtooth with reduced aliasing).
* Based on a paper by Antti Huovilainen and Vesa Valimaki:
* http://www.scribd.com/doc/33863143/New-Approaches-to-Digital-Subtractive-Synthesis
- *
+ *
* @author Phil Burk and Lisa Tolentino (C) 2009 Mobileer Inc
*/
public class SawtoothOscillatorDPW extends UnitOscillator {
diff --git a/src/com/jsyn/unitgen/SquareOscillatorBL.java b/src/com/jsyn/unitgen/SquareOscillatorBL.java
index cfe8541..cb9e141 100644
--- a/src/com/jsyn/unitgen/SquareOscillatorBL.java
+++ b/src/com/jsyn/unitgen/SquareOscillatorBL.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -21,7 +21,7 @@ import com.jsyn.engine.MultiTable;
/**
* Band-limited square wave oscillator. This requires more CPU than a SquareOscillator but is less
* noisy at high frequencies.
- *
+ *
* @author Phil Burk (C) 2009 Mobileer Inc
*/
public class SquareOscillatorBL extends SawtoothOscillatorBL {
@@ -32,8 +32,9 @@ public class SquareOscillatorBL extends SawtoothOscillatorBL {
/* Generate second sawtooth so we can add them together. */
double phase2 = currentPhase + 1.0; /* 180 degrees out of phase. */
- if (phase2 >= 1.0)
+ if (phase2 >= 1.0) {
phase2 -= 2.0;
+ }
double val2 = multiTable.calculateSawtooth(phase2, positivePhaseIncrement, flevel);
/*
diff --git a/src/com/jsyn/unitgen/TunableFilter.java b/src/com/jsyn/unitgen/TunableFilter.java
index 1724ec1..31f6631 100644
--- a/src/com/jsyn/unitgen/TunableFilter.java
+++ b/src/com/jsyn/unitgen/TunableFilter.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,7 +15,7 @@
*/
/**
* Aug 26, 2009
- * com.jsyn.engine.units.TunableFilter.java
+ * com.jsyn.engine.units.TunableFilter.java
*/
package com.jsyn.unitgen;
@@ -24,13 +24,13 @@ import com.jsyn.ports.UnitInputPort;
/**
* A UnitFilter with a frequency port.
- *
+ *
* @author Phil Burk (C) 2009 Mobileer Inc Translated from 'C' to Java by Lisa
* Tolenti.
*/
public abstract class TunableFilter extends UnitFilter {
- private static final double DEFAULT_FREQUENCY = 400;
+ static final double DEFAULT_FREQUENCY = 400;
public UnitInputPort frequency;
public TunableFilter() {
diff --git a/src/com/jsyn/unitgen/UnitGenerator.java b/src/com/jsyn/unitgen/UnitGenerator.java
index a9a7459..f8278ae 100644
--- a/src/com/jsyn/unitgen/UnitGenerator.java
+++ b/src/com/jsyn/unitgen/UnitGenerator.java
@@ -36,6 +36,20 @@ import com.softsynth.shared.time.TimeStamp;
*/
public abstract class UnitGenerator {
protected static final double VERY_SMALL_FLOAT = 1.0e-26;
+
+ // Some common port names.
+ public static final String PORT_NAME_INPUT = "Input";
+ public static final String PORT_NAME_OUTPUT = "Output";
+ public static final String PORT_NAME_PHASE = "Phase";
+ public static final String PORT_NAME_FREQUENCY = "Frequency";
+ public static final String PORT_NAME_FREQUENCY_SCALER = "FreqScaler";
+ public static final String PORT_NAME_AMPLITUDE = "Amplitude";
+ public static final String PORT_NAME_PAN = "Pan";
+ public static final String PORT_NAME_TIME = "Time";
+ public static final String PORT_NAME_CUTOFF = "Cutoff";
+ public static final String PORT_NAME_PRESSURE = "Pressure";
+ public static final String PORT_NAME_TIMBRE = "Timbre";
+
public static final double FALSE = 0.0;
public static final double TRUE = 1.0;
protected SynthesisEngine synthesisEngine;
@@ -75,6 +89,11 @@ public abstract class UnitGenerator {
addPort(port);
}
+ /**
+ * Case-insensitive search for a port by name.
+ * @param portName
+ * @return matching port or null
+ */
public UnitPort getPortByName(String portName) {
return ports.get(portName.toLowerCase());
}
@@ -141,8 +160,7 @@ public abstract class UnitGenerator {
if (halfLife < (2.0 * getFramePeriod())) {
return 1.0;
} else {
- // Strangely enough, this code is valid for both PeakFollower
- // and AsymptoticRamp.
+ // Oddly enough, this code is valid for both PeakFollower and AsymptoticRamp.
return 1.0 - Math.pow(0.5, 1.0 / (halfLife * getSynthesisEngine().getFrameRate()));
}
}
@@ -150,10 +168,11 @@ public abstract class UnitGenerator {
protected double incrementWrapPhase(double currentPhase, double phaseIncrement) {
currentPhase += phaseIncrement;
- if (currentPhase >= 1.0)
+ if (currentPhase >= 1.0) {
currentPhase -= 2.0;
- else if (currentPhase < -1.0)
+ } else if (currentPhase < -1.0) {
currentPhase += 2.0;
+ }
return currentPhase;
}
@@ -285,6 +304,11 @@ public abstract class UnitGenerator {
getSynthesisEngine().stopUnit(this, timeStamp);
}
+ /**
+ * @deprecated ignored, frameRate comes from the SynthesisEngine
+ * @param rate
+ */
+ @Deprecated
public void setFrameRate(int rate) {
this.frameRate = rate;
this.framePeriod = 1.0 / rate;
diff --git a/src/com/jsyn/unitgen/UnitOscillator.java b/src/com/jsyn/unitgen/UnitOscillator.java
index 4c02f09..5d4c6fa 100644
--- a/src/com/jsyn/unitgen/UnitOscillator.java
+++ b/src/com/jsyn/unitgen/UnitOscillator.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -23,7 +23,7 @@ import com.softsynth.shared.time.TimeStamp;
/**
* Base class for all oscillators.
- *
+ *
* @author Phil Burk (C) 2009 Mobileer Inc
*/
public abstract class UnitOscillator extends UnitGenerator implements UnitVoice {
@@ -34,15 +34,15 @@ public abstract class UnitOscillator extends UnitGenerator implements UnitVoice
public UnitOutputPort output;
public static final double DEFAULT_FREQUENCY = 440.0;
- public static final double DEFAULT_AMPLITUDE = 0x7FFF / (double) 0x8000;
+ public static final double DEFAULT_AMPLITUDE = 1.0;
/* Define Unit Ports used by connect() and set(). */
public UnitOscillator() {
- addPort(frequency = new UnitInputPort("Frequency"));
+ addPort(frequency = new UnitInputPort(PORT_NAME_FREQUENCY));
frequency.setup(40.0, DEFAULT_FREQUENCY, 8000.0);
- addPort(amplitude = new UnitInputPort("Amplitude", DEFAULT_AMPLITUDE));
- addPort(phase = new UnitVariablePort("Phase"));
- addPort(output = new UnitOutputPort("Output"));
+ addPort(amplitude = new UnitInputPort(PORT_NAME_AMPLITUDE, DEFAULT_AMPLITUDE));
+ addPort(phase = new UnitVariablePort(PORT_NAME_PHASE));
+ addPort(output = new UnitOutputPort(PORT_NAME_OUTPUT));
}
/**