aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/jsyn/unitgen/SawtoothOscillatorDPW.java
blob: 27d0c5a13d0a32dd0c7d9d734fe87c6636824f9e (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
/*
 * 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;

/**
 * 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 {
    // At a very low frequency, switch from DPW to raw sawtooth.
    private static final double VERY_LOW_FREQUENCY = 2.0 * 0.1 / 44100.0;
    private double z1;
    private double z2;

    @Override
    public void generate(int start, int limit) {
        double[] frequencies = frequency.getValues();
        double[] amplitudes = amplitude.getValues();
        double[] outputs = output.getValues();

        // Variables have a single value.
        double currentPhase = phase.getValue();

        for (int i = start; i < limit; i++) {
            /* Generate raw sawtooth phaser. */
            double phaseIncrement = convertFrequencyToPhaseIncrement(frequencies[i]);
            currentPhase = incrementWrapPhase(currentPhase, phaseIncrement);

            /* Square the raw sawtooth. */
            double squared = currentPhase * currentPhase;
            // Differentiate using a delayed value.
            double diffed = squared - z2;
            z2 = z1;
            z1 = squared;

            /* Calculate scaling based on phaseIncrement */
            double pinc = phaseIncrement;
            // Absolute value.
            if (pinc < 0.0) {
                pinc = 0.0 - pinc;
            }

            double dpw;
            // If the frequency is very low then just use the raw sawtooth.
            // This avoids divide by zero problems and scaling problems.
            if (pinc < VERY_LOW_FREQUENCY) {
                dpw = currentPhase;
            } else {
                dpw = diffed * 0.25 / pinc;
            }

            outputs[i] = amplitudes[i] * dpw;
        }

        // Value needs to be saved for next time.
        phase.setValue(currentPhase);
    }

}