/**************************************************************************
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/>.
**************************************************************************/
#include "VCO.h"
/*!
* Initializes the Wave Generators in 2 threads (for the first call).
*
* \param sampleRate Sample rate of audio flow
*/
void VCO::init(double sampleRate)
{
LFOModulated::init(sampleRate);
juce::Random random;
if (mSineGenerator.getSamplingFreq() != sampleRate) {
// Complete recompute
std::thread t1([this, sampleRate]() {
mSineGenerator.init(sampleRate);
mHOddGenerator.init(sampleRate);
mHEvenGenerator.init(sampleRate);
mNoiseGenerator.init(sampleRate);
});
std::thread t2([this, sampleRate]() {
mSawtoothGenerator.init(sampleRate);
mSubGenerator.init(sampleRate);
mPWMGenerator.init(sampleRate);
mPhaseNoiseGenerator.init(sampleRate);
mPhaseSineGenerator.init(sampleRate);
});
t1.join();
t2.join();
}
else
{
// Only table reaffectation
mSineGenerator.init(sampleRate);
mHOddGenerator.init(sampleRate);
mHEvenGenerator.init(sampleRate);
mSawtoothGenerator.init(sampleRate);
mSubGenerator.init(sampleRate);
mPWMGenerator.init(sampleRate);
mNoiseGenerator.init(sampleRate);
mPhaseNoiseGenerator.init(sampleRate);
mPhaseSineGenerator.init(sampleRate);
mPhaseSineGenerator.move(random.nextDouble()*sampleRate);
}
mPhaseShiftFreq = 1.0/(RndPhaseShiftPeriodMin+(RndPhaseShiftPeriodMax-RndPhaseShiftPeriodMin)*random.nextDouble());
}
void VCO::noteOn(int note)
{
LFOModulated::noteOn(note);
resetPhase();
mSlope = 0.0f;
}
void VCO::updateInternalValues(int controller)
{
LFOModulated::updateInternalValues(controller);
if (controller != 0)
return;
mSineGain = float(*mVCOParam.mSineGain);
mNoiseGain = float(*mVCOParam.mNoiseGain);
mHOddGain = float(*mVCOParam.mHOddGain);
mHEvenGain = float(*mVCOParam.mHEvenGain);
mSawGain = float(*mVCOParam.mSawGain);
mSubGain = float(*mVCOParam.mSubGain);
mPWMGain = float(*mVCOParam.mPWMGain);
mPWMRate = float(*mVCOParam.mPWMRate);
bool phaseHasChanged = false;
double phase;
mPhaseJitter = float(*mVCOParam.mPhaseJitter);
phase = float(*mVCOParam.mSawPhase);
if (phase != mSawPhase) {
phaseHasChanged = true;
mSawPhase = phase;
}
phase = float(*mVCOParam.mSubPhase);
if (phase != mSubPhase) {
phaseHasChanged = true;
mSubPhase = phase;
}
phase = float(*mVCOParam.mPWMPhase);
if (phase != mPWMPhase) {
phaseHasChanged = true;
mPWMPhase = phase;
}
if(phaseHasChanged)
resetPhase();
}
void VCO::resetPhase()
{
mSineGenerator.setPhase(0);
mHOddGenerator.setPhase(0);
mHEvenGenerator.setPhase(0);
mSawtoothGenerator.setPhase(mSawPhase);
mSubGenerator.setPhase(mSubPhase);
mPWMGenerator.setPhase(mPWMPhase);
}