BogaudioModules

BogaudioModules for VCV Rack
Log | Files | Refs | README | LICENSE

commit 8ab34b06c61eaf45cb6913759ba6666bebf0af17
parent 50aa09a006db42dd101eb7121aa72b9e247657c0
Author: Matt Demanett <matt@demanett.net>
Date:   Thu,  8 Feb 2018 01:11:37 -0500

Reset partial phases periodically in SineBankOscillator.

Diffstat:
Msrc/Test.cpp | 16++++++++++++++--
Msrc/dsp/oscillator.cpp | 14++++++++++++++
Msrc/dsp/oscillator.hpp | 9+++++----
3 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/src/Test.cpp b/src/Test.cpp @@ -83,11 +83,12 @@ struct Test : Module { #ifdef SINEBANK const float baseAmplitude = 5.0; - switch (4) { + switch (1) { case 1: { // saw + float phase = M_PI; for (int i = 1, n = _sineBank.partialCount(); i <= n; ++i) { - _sineBank.setPartial(i, i, baseAmplitude / (float)i); + _sineBank.setPartial(i, i, baseAmplitude / (float)i, &phase); } break; } @@ -116,6 +117,17 @@ struct Test : Module { } break; } + + case 5: { + // ? + float phase = M_PI; + float multiple = 1.0; + for (int i = 1, n = _sineBank.partialCount(); i <= n; ++i) { + _sineBank.setPartial(i, multiple, baseAmplitude / multiple, &phase); + multiple += 0.9; + } + break; + } } #endif } diff --git a/src/dsp/oscillator.cpp b/src/dsp/oscillator.cpp @@ -156,6 +156,8 @@ void SineBankOscillator::disablePartial(int i) { } void SineBankOscillator::_sampleRateChanged() { + Phasor::_sampleRateChanged(); + _maxPartialFrequency = _maxPartialFrequencySRRatio * _sampleRate; for (Partial& p : _partials) { p.sine.setSampleRate(_sampleRate); @@ -163,6 +165,8 @@ void SineBankOscillator::_sampleRateChanged() { } void SineBankOscillator::_frequencyChanged() { + Phasor::_frequencyChanged(); + for (Partial& p : _partials) { p.frequency = _frequency * p.frequencyRatio; p.sine.setFrequency(_frequency * p.frequencyRatio); @@ -170,6 +174,16 @@ void SineBankOscillator::_frequencyChanged() { } float SineBankOscillator::_next() { + Phasor::_next(); + + if (++_steps >= _stepsToReset) { + _steps = 0; + float phase = _phase * M_PI; + for (Partial& p : _partials) { + p.sine.setPhase(phase * p.frequencyRatio); + } + } + float next = 0.0; for (Partial& p : _partials) { if (p.enabled && p.frequency < _maxPartialFrequency) { diff --git a/src/dsp/oscillator.hpp b/src/dsp/oscillator.hpp @@ -160,7 +160,7 @@ struct TriangleOscillator : Phasor { virtual float _next() override; }; -struct SineBankOscillator : OscillatorGenerator { +struct SineBankOscillator : Phasor { struct Partial { bool enabled; float frequency; @@ -178,7 +178,9 @@ struct SineBankOscillator : OscillatorGenerator { }; const float _maxPartialFrequencySRRatio = 0.48; - float _maxPartialFrequency; + float _maxPartialFrequency = 0.0; + unsigned _steps = 0; + unsigned _stepsToReset = 1000; std::vector<Partial> _partials; SineBankOscillator( @@ -186,8 +188,7 @@ struct SineBankOscillator : OscillatorGenerator { float frequency, int partialCount ) - : OscillatorGenerator(sampleRate, frequency) - , _maxPartialFrequency(0.0) + : Phasor(sampleRate, frequency) , _partials(partialCount) { _sampleRateChanged();