blob: 5f1b4cd6e0d2fae6b130588740184445fa250f8d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
/*
* Copyright 2010 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.ports.UnitInputPort;
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
* 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 {
public UnitInputPort input;
public UnitOutputPort output;
private static double[] table;
private static final int NUM_VALUES = 1024;
// Cached computation.
private double lastInput = 0.0;
private double lastOutput = 1.0;
static {
// Add guard point for faster interpolation.
// Add another point to handle inputs like -1.5308084989341915E-17,
// which generate indices above range.
table = new double[NUM_VALUES + 2];
// Fill one octave of the table.
for (int i = 0; i < table.length; i++) {
double value = Math.pow(2.0, ((double) i) / NUM_VALUES);
table[i] = value;
}
}
public PowerOfTwo() {
addPort(input = new UnitInputPort("Input"));
input.setup(-8.0, 0.0, 8.0);
addPort(output = new UnitOutputPort("Output"));
}
@Override
public void generate(int start, int limit) {
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]));
// 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;
}
}
} else {
for (int i = start; i < limit; i++) {
outputs[i] = Math.pow(2.0, inputs[i]);
}
}
}
}
|