commit a9931bb17fb5cc75eb87b2fbb17869ba19aa87ef
parent 60b1e9bd95411b9b32c42b1383dc3e208c77987f
Author: Matt Demanett <matt@demanett.net>
Date: Wed, 7 Feb 2018 00:35:03 -0500
Add some normalization to SineOscillator to minimize floating error.
Diffstat:
2 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/src/dsp/oscillator.cpp b/src/dsp/oscillator.cpp
@@ -1,5 +1,7 @@
#include <math.h>
+#include "rack.hpp"
+
#include "oscillator.hpp"
using namespace bogaudio::dsp;
@@ -47,8 +49,20 @@ void SineOscillator::updateDeltaTheta() {
}
float SineOscillator::_next() {
+ ++_sampleCount;
+
if (_step == 0) {
const int n1 = _n - 1;
+ if (_sampleCount > _maxSamplesBeforeNormalize) {
+ _sampleCount = 0;
+
+ // https://dsp.stackexchange.com/questions/124/how-to-implement-a-digital-oscillator
+ // float g = (3.0f - (_x[n1]*_x[n1] + _y[n1]*_y[n1])) / 2.0f; // would work if _amplitude was always 1.0?
+ float g = _amplitude / sqrtf(_x[n1]*_x[n1] + _y[n1]*_y[n1]);
+ _x[n1] *= g;
+ _y[n1] *= g;
+ }
+
for (int i = 0; i < n1; ++i) {
_x[i] = _x[n1];
_y[i] = _y[n1];
diff --git a/src/dsp/oscillator.hpp b/src/dsp/oscillator.hpp
@@ -4,6 +4,7 @@
#include <vector>
#include "base.hpp"
+#include "noise.hpp"
namespace bogaudio {
namespace dsp {
@@ -75,7 +76,8 @@ struct SineOscillator : OscillatorGenerator {
float _y[_n];
float _sinDeltaTheta[_n];
float _cosDeltaTheta[_n];
-
+ unsigned int _sampleCount = 0;
+ unsigned int _maxSamplesBeforeNormalize = 1000 + (Seeds::next() % 100);
float _amplitude;
SineOscillator(