[go: up one dir, main page]

Menu

[r98]: / MolossII / Source / VCO.h  Maximize  Restore  History

Download this file

172 lines (138 with data), 4.6 kB

  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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**************************************************************************
Copyright (C) 2019 Arnaud Champenois arthelion@free.fr
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/
#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
#include <juce_dsp/juce_dsp.h>
#include "WaveGenerator.h"
#include <string>
#include <thread>
#include "LFOModulated.h"
/*!
* Modulated wave generator.
* Contains a sine with harmonics, a square sub, a sawtooth and a modulated PWM
*
*/
class VCO : public LFOModulated {
public:
VCO(const VCOParameters&vcoParams) :
LFOModulated(vcoParams),mVCOParam(vcoParams) {
}
~VCO() {}
/*!
* Initializes the Wave Generators in 2 threads (for the first call).
*
* \param sampleRate Sample rate of audio flow
*/
void init(double sampleRate) override;
virtual void noteOn(int note) override;
/*!
* Generates a sample with a given input frequency
*
* \param freq The frequency (which can be modulated)
* \return The sample value
*/
double next(double freq) {
double value = 0.0;
if (mCountBeforePhaseReset > 0) {
mCountBeforePhaseReset--;
if(mCountBeforePhaseReset == 0)
resetPhase();
}
if(mPhaseJitter>0.0) {
double phaseShift = (RndPhaseSinCoef * mPhaseSineGenerator.next(mPhaseShiftFreq) +
RndPhaseNoiseCoef * mPhaseNoiseGenerator.next())*440.0;
freq = freq + mPhaseJitter * phaseShift;
}
// Sine generator
if (mSineGain != 0.0)
value += mSineGain*mSineGenerator.next(freq);
else
mSineGenerator.move(freq);
if (mHOddGain != 0.0)
value += mHOddGain * mHOddGenerator.next(freq);
else
mHOddGenerator.move(freq);
if (mHEvenGain != 0.0)
value += mHEvenGain * mHEvenGenerator.next(freq);
else
mHEvenGenerator.move(freq);
// Saw generator
if (mSawGain != 0.0) {
double freq2 = freq * mOctaveSaw;
if(freq2 < mSawtoothGenerator.getSamplingFreq()*0.5)
value += mSawGain * mSawtoothGenerator.next(freq2);
}
else
mSawtoothGenerator.move(freq*2.0); // keep the phase
// Sub generator
if (mSubGain != 0.0) {
value += mSubGain * mSubGenerator.next(freq*0.5);
}
else
mSubGenerator.move(freq); // keep the phase
// PWM square generator
if (mPWMGain != 0.0) {
if (!isLFOBypassed())
value += mPWMGain * mPWMGenerator.next(freq,mPWMRate*nextLFO());
else
value += mPWMGain * mPWMGenerator.next(freq, mPWMRate);
}
else
mPWMGenerator.move(freq); // keep the phase
if (mNoiseGain != 0.0)
value += mNoiseGain*mNoiseGenerator.next();
if (mSlope < 1.0) {
mSlope += 0.01;
value *= mSlope; // 100 samples of slope
}
return value*mOverallGain;
}
double getWavesGain() const
{ return mSineGain + mNoiseGain + mHOddGain + mHEvenGain + mSawGain + mSubGain + mPWMGain; }
double getOscGain() const {
double waveGain = getWavesGain();
if (waveGain > 1.0) return 1.0;
else return waveGain;
}
virtual void updateInternalValues(int controller = 0) override;
void resetPhase();
private:
const VCOParameters&mVCOParam;
SineGenerator mSineGenerator;
OddHarmonicsGenerator mHOddGenerator;
EvenHarmonicsGenerator mHEvenGenerator;
SawToothGenerator mSawtoothGenerator;
SquareGenerator mSubGenerator;
PWMGenerator mPWMGenerator;
NoiseGenerator mNoiseGenerator;
NoiseGenerator mPhaseNoiseGenerator;
SineGenerator mPhaseSineGenerator;
double mSineGain = 1.0;
double mNoiseGain = 0.0;
double mSawGain = 0.0;
double mSubGain = 0.0;
double mPWMGain = 0.0;
double mPWMRate = 0.5;
double mOverallGain = 1.0;
double mSawPhase = 0.0;
double mSubPhase = 0.0;
double mPWMPhase = 0.0;
double mPhaseJitter = 0.0;
double mHOddGain = 0.0;
double mHEvenGain = 0.0;
double mSlope = 0.0;
double mPhaseShiftFreq = 0.01;
double mOctaveSaw = 2.0;
int mCountBeforePhaseReset = 0.0;
};