SquidgeDSP/core/Oscillator.cpp

44 lines
No EOL
1.2 KiB
C++

#include "Oscillator.hpp"
#include <cmath>
Oscillator::Oscillator(float initFreq)
: freq_(initFreq), gain_(100.0f), waveShape_(kWaveShapeSine),
sampleRate_(48000.f), phase_(0.0f), inc_(0.0f) {
recalc_();
};
void Oscillator::process(float *out, uint32_t frames) {
for (uint32_t i = 0; i < frames; ++i) {
switch (waveShape_) {
default:
case kWaveShapeSine:
out[i] = std::sinf(phase_ * 2.0f * M_PI) * gain_;
break;
}
phase_ += inc_;
if (phase_ >= 1.0f)
phase_ -= 1.0f;
}
}
void Oscillator::recalc_() { inc_ = freq_ / sampleRate_; }
void Oscillator::setFrequency(float newFreq) {
freq_ = std::fmax(newFreq, 0.0f);
}
float Oscillator::getFrequency() const { return freq_; };
void Oscillator::setGain(float newGain) {
gain_ = newGain > 0 ? newGain : 1.0f;
}
float Oscillator::getGain() const { return gain_; };
void Oscillator::setSampleRate(float newSampleRate) {
sampleRate_ = newSampleRate > 0 ? newSampleRate : 48000.0f;
recalc_();
}
float Oscillator::getSampleRate() const { return sampleRate_; }
void Oscillator::setWaveShape(WaveShape shape) { waveShape_ = shape; }
Oscillator::WaveShape Oscillator::getWaveShape() const { return waveShape_; }