From 580fea450ec0982d0bd8be589f00566267e7b0d1 Mon Sep 17 00:00:00 2001 From: Phil Burk Date: Tue, 2 Aug 2016 07:52:17 -0700 Subject: Instruments: add better synth, pitch control --- src/com/jsyn/unitgen/PowerOfTwo.java | 79 ++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 35 deletions(-) (limited to 'src/com/jsyn/unitgen/PowerOfTwo.java') 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; + } } -- cgit v1.2.3