1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 * This code was translated from the JSyn Java code. 17 * JSyn is Copyright 2009 Phil Burk, Mobileer Inc 18 * JSyn is licensed under the Apache License, Version 2.0 19 */ 20 21 #ifndef SYNTHMARK_SQUARE_OSCILLATOR_DPW_H 22 #define SYNTHMARK_SQUARE_OSCILLATOR_DPW_H 23 24 #include <cstdint> 25 #include <math.h> 26 #include "SynthTools.h" 27 #include "DifferentiatedParabola.h" 28 29 namespace marksynth { 30 /** 31 * Square waves contains the odd partials of a fundamental. 32 * The square wave is generated by combining two sawtooth waveforms 33 * that are 180 degrees out of phase. This causes the even partials 34 * to be cancelled out. 35 */ 36 class SquareOscillatorDPW : public SawtoothOscillator 37 { 38 public: SquareOscillatorDPW()39 SquareOscillatorDPW() 40 : SawtoothOscillator() 41 , dpw1() 42 , dpw2() {} 43 44 virtual ~SquareOscillatorDPW() = default; 45 translatePhase(synth_float_t phase1,synth_float_t phaseIncrement)46 virtual inline synth_float_t translatePhase(synth_float_t phase1, 47 synth_float_t phaseIncrement) { 48 synth_float_t val1 = dpw1.next(phase1, phaseIncrement); 49 50 /* Generate second sawtooth so we can add them together. */ 51 synth_float_t phase2 = phase1 + 1.0; /* 180 degrees out of phase. */ 52 if (phase2 >= 1.0) 53 phase2 -= 2.0; 54 synth_float_t val2 = dpw1.next(phase2, phaseIncrement); 55 56 /* 57 * Need to adjust amplitude based on positive phaseInc. little less than half at 58 * Nyquist/2.0! 59 */ 60 const synth_float_t STARTAMP = 0.92; // derived empirically 61 synth_float_t positivePhaseIncrement = (phaseIncrement < 0.0) 62 ? phaseIncrement 63 : 0.0 - phaseIncrement; 64 synth_float_t scale = STARTAMP - positivePhaseIncrement; 65 return scale * (val1 - val2); 66 } 67 68 private: 69 DifferentiatedParabola dpw1; 70 DifferentiatedParabola dpw2; 71 }; 72 }; 73 #endif // SYNTHMARK_SQUARE_OSCILLATOR_DPW_H 74