commit d523dd4a3367eea29dc72245d71162129405df88
parent d3c93a9c3869123a16a367813239312e0608b192
Author: Matt Demanett <matt@demanett.net>
Date: Fri, 20 Jul 2018 01:15:16 -0400
Unfinished interpolator dsp.
Diffstat:
4 files changed, 111 insertions(+), 2 deletions(-)
diff --git a/src/Test.cpp b/src/Test.cpp
@@ -268,6 +268,27 @@ void Test::step() {
// outputs[OUT2_OUTPUT].value = _lpfDecimator.next(buf) * 5.0f;
outputs[OUT2_OUTPUT].value = _rackDecimator.process(buf) * 5.0f;
+#elif INTERPOLATOR
+ const int quality = 12;
+ float sampleRate = engineGetSampleRate();
+ float frequency = oscillatorPitch();
+ _saw.setSampleRate(sampleRate);
+ _saw.setFrequency(frequency);
+ _saw.setQuality(quality);
+ _decimator.setParams(sampleRate, FACTOR);
+ _interpolator.setParams(sampleRate, FACTOR);
+
+ if (_steps >= FACTOR) {
+ _steps = 0;
+ for (int i = 0; i < FACTOR; ++i) {
+ _rawSamples[i] = _saw.next();
+ }
+ _interpolator.next(_decimator.next(_rawSamples), _processedSamples);
+ }
+ outputs[OUT_OUTPUT].value = _processedSamples[_steps] * 5.0f;
+ outputs[OUT2_OUTPUT].value = _rawSamples[_steps] * 5.0f;
+ ++_steps;
+
#elif FM
const float amplitude = 5.0f;
float baseHz = oscillatorPitch();
diff --git a/src/Test.hpp b/src/Test.hpp
@@ -17,6 +17,7 @@ extern Model* modelTest;
// #define OVERSAMPLED_BL 1
// #define ANTIALIASING 1
// #define DECIMATORS 1
+#define INTERPOLATOR 1
// #define FM 1
// #define PM 1
// #define FEEDBACK_PM 1
@@ -25,7 +26,7 @@ extern Model* modelTest;
// #define SLEW 1
// #define RMS 1
// #define RAVG 1
-#define SATURATOR 1
+// #define SATURATOR 1
#include "pitch.hpp"
#ifdef LPF
@@ -63,6 +64,9 @@ extern Model* modelTest;
#include "dsp/oscillator.hpp"
#include "dsp/filter.hpp"
#include "dsp/decimator.hpp" // rack
+#elif INTERPOLATOR
+#include "dsp/oscillator.hpp"
+#include "dsp/filter.hpp"
#elif FM
#include "dsp/oscillator.hpp"
#elif PM
@@ -171,6 +175,15 @@ struct Test : Module {
bogaudio::dsp::CICDecimator _cicDecimator;
bogaudio::dsp::LPFDecimator _lpfDecimator;
rack::Decimator<OVERSAMPLEN, OVERSAMPLEN> _rackDecimator;
+#elif INTERPOLATOR
+ #define FACTOR 8
+ #define STAGES 4
+ BandLimitedSawOscillator _saw;
+ bogaudio::dsp::CICDecimator _decimator;
+ bogaudio::dsp::CICInterpolator _interpolator;
+ int _steps;
+ float _rawSamples[FACTOR] {};
+ float _processedSamples[FACTOR] {};
#elif FM
float _baseHz = 0.0f;
float _ratio = 0.0f;
@@ -211,6 +224,9 @@ struct Test : Module {
, _sine2(_table)
#elif DECIMATORS
, _cicDecimator(STAGES)
+#elif INTERPOLATOR
+ , _decimator(STAGES)
+ , _interpolator(STAGES)
#elif TABLES
, _table(StaticBlepTable::table(), 44100.0, 1000.0)
#elif RAVG
diff --git a/src/dsp/filter.cpp b/src/dsp/filter.cpp
@@ -1,6 +1,6 @@
-#include <math.h>
#include <assert.h>
+#include <algorithm>
#include "filter.hpp"
@@ -234,3 +234,50 @@ float CICDecimator::next(const float* buf) {
}
return _gainCorrection * (s / (float)scale);
}
+
+
+CICInterpolator::CICInterpolator(int stages, int factor) {
+ assert(stages > 0);
+ _stages = stages;
+ _integrators = new T[_stages + 1] {};
+ _combs = new T[_stages] {};
+ _buffer = NULL;
+ setParams(0.0f, factor);
+}
+
+CICInterpolator::~CICInterpolator() {
+ delete[] _integrators;
+ delete[] _combs;
+ delete[] _buffer;
+}
+
+void CICInterpolator::setParams(float _sampleRate, int factor) {
+ assert(factor > 0);
+ if (_factor != factor) {
+ _factor = factor;
+ _gainCorrection = 1.0f / 512.0f; // (float)(pow(_factor, _stages / 2));
+ if (_buffer) {
+ delete[] _buffer;
+ }
+ _buffer = new T[_factor] {};
+ }
+}
+
+void CICInterpolator::next(float sample, float* buf) {
+ T s = sample * scale;
+ for (int i = 0; i < _stages; ++i) {
+ T t = s;
+ s -= _combs[i];
+ _combs[i] = t;
+ }
+
+ std::fill(_buffer, _buffer + _factor, (T)0);
+ _buffer[0] = s;
+ for (int i = 0; i < _factor; ++i) {
+ _integrators[0] = _buffer[i];
+ for (int j = 1; j <= _stages; ++j) {
+ _integrators[j] += _integrators[j - 1];
+ }
+ buf[i] = _gainCorrection * (_integrators[_stages] / (float)scale);
+ }
+}
diff --git a/src/dsp/filter.hpp b/src/dsp/filter.hpp
@@ -162,5 +162,30 @@ struct CICDecimator : Decimator {
float next(const float* buf) override;
};
+struct Interpolator {
+ Interpolator() {}
+ virtual ~Interpolator() {}
+
+ virtual void setParams(float sampleRate, int factor) = 0;
+ virtual void next(float sample, float* buf) = 0;
+};
+
+struct CICInterpolator : Interpolator {
+ typedef int64_t T;
+ static constexpr T scale = ((T)1) << 32;
+ int _stages;
+ T* _integrators;
+ T* _combs;
+ T* _buffer;
+ int _factor = 0;
+ float _gainCorrection;
+
+ CICInterpolator(int stages = 4, int factor = 8);
+ virtual ~CICInterpolator();
+
+ void setParams(float sampleRate, int factor) override;
+ void next(float sample, float* buf) override;
+};
+
} // namespace dsp
} // namespace bogaudio