BogaudioModules

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

commit 0869c830ff65f5243e0f7a9ef7f9a427d54083c9
parent c5ba1c6b7cc7d386cbe6d820496528513a7848c9
Author: Matt Demanett <matt@demanett.net>
Date:   Tue,  5 Jun 2018 22:04:46 -0400

Use table to speed up tanh; use that for saw saturation.

Diffstat:
Asrc/dsp/math.cpp | 25+++++++++++++++++++++++++
Asrc/dsp/math.hpp | 24++++++++++++++++++++++++
Msrc/dsp/oscillator.cpp | 3+--
Msrc/dsp/oscillator.hpp | 2++
4 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/src/dsp/math.cpp b/src/dsp/math.cpp @@ -0,0 +1,25 @@ + +#include <stdlib.h> +#include <math.h> + +#include "math.hpp" + +using namespace bogaudio::dsp; + +void FastTanhf::TanhfTable::_generate() { + _table[0] = -1.0f; + _table[_length - 1] = 1.0f; + for (int i = 1, n = _length - 1; i < n; ++i) { + _table[i] = tanhf((((i / (float)_length) * 2.0f) - 1.0f) * M_PI); + } +} + +float FastTanhf::value(float radians) { + if (radians <= -M_PI) { + return -1.0f; + } + if (radians >= M_PI) { + return 1.0f; + } + return _table.value(((radians + M_PI) / (2.0f * M_PI)) * _table.length()); +} diff --git a/src/dsp/math.hpp b/src/dsp/math.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include "base.hpp" +#include "table.hpp" + +namespace bogaudio { +namespace dsp { + +struct FastTanhf { + struct TanhfTable : Table { + TanhfTable(int n) : Table(n) {} + void _generate() override; + }; + struct StaticTanhfTable : StaticTable<TanhfTable, 11> {}; + const Table& _table; + + FastTanhf() : _table(StaticTanhfTable::table()) { + } + + float value(float radians); +}; + +} // namespace dsp +} // namespace bogaudio diff --git a/src/dsp/oscillator.cpp b/src/dsp/oscillator.cpp @@ -1,4 +1,3 @@ -#include <math.h> #include "oscillator.hpp" @@ -116,7 +115,7 @@ void SaturatingSawOscillator::setSaturation(float saturation) { float SaturatingSawOscillator::_nextForPhase(phase_t phase) { float sample = SawOscillator::_nextForPhase(phase); if (_saturation >= 0.1f) { - sample = tanhf(sample * _saturation * M_PI) * _saturationNormalization; + sample = _tanhf.value(sample * _saturation * M_PI) * _saturationNormalization; } return sample; } diff --git a/src/dsp/oscillator.hpp b/src/dsp/oscillator.hpp @@ -6,6 +6,7 @@ #include <vector> #include "base.hpp" +#include "math.hpp" #include "table.hpp" namespace bogaudio { @@ -172,6 +173,7 @@ struct SawOscillator : Phasor { struct SaturatingSawOscillator : SawOscillator { float _saturation; float _saturationNormalization; + FastTanhf _tanhf; SaturatingSawOscillator( float sampleRate = 1000.0f,