diff options
author | Phil Burk <[email protected]> | 2016-08-02 07:52:17 -0700 |
---|---|---|
committer | Phil Burk <[email protected]> | 2016-10-24 08:29:20 -0700 |
commit | 580fea450ec0982d0bd8be589f00566267e7b0d1 (patch) | |
tree | 0420f768fc7c63208b1720232c447e17af9017af /src/com/jsyn/unitgen/PowerOfTwo.java | |
parent | a6583e89166f7477a675cf3094a91b303ba7850a (diff) |
Instruments: add better synth, pitch control
Diffstat (limited to 'src/com/jsyn/unitgen/PowerOfTwo.java')
-rw-r--r-- | src/com/jsyn/unitgen/PowerOfTwo.java | 79 |
1 files changed, 44 insertions, 35 deletions
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; + } } |