diff --git a/include/dsp/functions.hpp b/include/dsp/common.hpp similarity index 79% rename from include/dsp/functions.hpp rename to include/dsp/common.hpp index 617d33bc..d321db54 100644 --- a/include/dsp/functions.hpp +++ b/include/dsp/common.hpp @@ -3,6 +3,11 @@ namespace rack { +namespace dsp { + + +static const float FREQ_C4 = 261.6256f; +static const float FREQ_A4 = 440.0000f; /** The normalized sinc function. https://en.wikipedia.org/wiki/Sinc_function */ @@ -42,13 +47,14 @@ inline float exponentialBipolar(float b, float x) { return (std::pow(b, x) - std::pow(b, -x)) / a; } -inline float gainToDb(float gain) { - return std::log10(gain) * 20.f; +inline float amplitudeToDb(float amp) { + return std::log10(amp) * 20.f; } -inline float dbToGain(float db) { +inline float dbToAmplitude(float db) { return std::pow(10.f, db / 20.f); } +} // namespace dsp } // namespace rack diff --git a/include/dsp/decimator.hpp b/include/dsp/decimator.hpp deleted file mode 100644 index ec4640d2..00000000 --- a/include/dsp/decimator.hpp +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once -#include "resampler.hpp" \ No newline at end of file diff --git a/include/dsp/digital.hpp b/include/dsp/digital.hpp index a41826db..3f38cf11 100644 --- a/include/dsp/digital.hpp +++ b/include/dsp/digital.hpp @@ -1,8 +1,9 @@ #pragma once -#include "math.hpp" +#include "dsp/common.hpp" namespace rack { +namespace dsp { /** Turns HIGH when value reaches 1.f, turns LOW when value reaches 0.f. */ @@ -102,4 +103,5 @@ struct PulseGenerator { }; +} // namespace dsp } // namespace rack diff --git a/include/dsp/filter.hpp b/include/dsp/filter.hpp index 6990dd13..47727c49 100644 --- a/include/dsp/filter.hpp +++ b/include/dsp/filter.hpp @@ -1,8 +1,10 @@ #pragma once -#include "math.hpp" +#include "dsp/common.hpp" namespace rack { +namespace dsp { + struct RCFilter { float c = 0.f; @@ -81,4 +83,5 @@ struct ExponentialFilter { }; +} // namespace dsp } // namespace rack diff --git a/include/dsp/fir.hpp b/include/dsp/fir.hpp index a69aa197..0c5ddde3 100644 --- a/include/dsp/fir.hpp +++ b/include/dsp/fir.hpp @@ -1,9 +1,11 @@ #pragma once -#include "dsp/functions.hpp" +#include "dsp/common.hpp" #include namespace rack { +namespace dsp { + /** Performs a direct sum convolution */ inline float convolveNaive(const float *in, const float *kernel, int len) { @@ -145,4 +147,5 @@ struct RealTimeConvolver { }; +} // namespace dsp } // namespace rack diff --git a/include/dsp/frame.hpp b/include/dsp/frame.hpp index 4862093c..dca99b4f 100644 --- a/include/dsp/frame.hpp +++ b/include/dsp/frame.hpp @@ -1,8 +1,10 @@ #pragma once -#include +#include "dsp/common.hpp" namespace rack { +namespace dsp { + /** Useful for storing arrays of samples in ring buffers and casting them to `float*` to be used by interleaved processors, like SampleRateConverter */ template @@ -10,4 +12,6 @@ struct Frame { float samples[CHANNELS]; }; + +} // namespace dsp } // namespace rack diff --git a/include/dsp/minblep.hpp b/include/dsp/minblep.hpp index fd7b9127..ab0a84ca 100644 --- a/include/dsp/minblep.hpp +++ b/include/dsp/minblep.hpp @@ -1,8 +1,10 @@ #pragma once -#include "math.hpp" +#include "dsp/common.hpp" namespace rack { +namespace dsp { + // Pre-made minBLEP samples in minBLEP.cpp extern const float minblep_16_32[]; @@ -33,4 +35,6 @@ struct MinBLEP { } }; + +} // namespace dsp } // namespace rack diff --git a/include/dsp/ode.hpp b/include/dsp/ode.hpp index 016e6f40..8c1b4975 100644 --- a/include/dsp/ode.hpp +++ b/include/dsp/ode.hpp @@ -1,8 +1,10 @@ #pragma once +#include "dsp/common.hpp" namespace rack { -namespace ode { +namespace dsp { + /** The callback function `f` in each of these stepping functions must have the signature @@ -84,5 +86,6 @@ void stepRK4(float t, float dt, float x[], int len, F f) { } } -} // namespace ode + +} // namespace dsp } // namespace rack diff --git a/include/dsp/resampler.hpp b/include/dsp/resampler.hpp index bcc114ba..03c27798 100644 --- a/include/dsp/resampler.hpp +++ b/include/dsp/resampler.hpp @@ -1,13 +1,16 @@ #pragma once -#include "frame.hpp" -#include "ringbuffer.hpp" -#include "fir.hpp" +#include "dsp/common.hpp" +#include "dsp/frame.hpp" +#include "dsp/ringbuffer.hpp" +#include "dsp/fir.hpp" #include #include #include namespace rack { +namespace dsp { + template struct SampleRateConverter { @@ -170,4 +173,5 @@ struct Upsampler { }; +} // namespace dsp } // namespace rack diff --git a/include/dsp/ringbuffer.hpp b/include/dsp/ringbuffer.hpp index 403a49e6..aed5b607 100644 --- a/include/dsp/ringbuffer.hpp +++ b/include/dsp/ringbuffer.hpp @@ -1,9 +1,11 @@ #pragma once -#include "common.hpp" +#include "dsp/common.hpp" #include namespace rack { +namespace dsp { + /** A simple cyclic buffer. S must be a power of 2. @@ -196,4 +198,6 @@ struct AppleRingBuffer { } }; + +} // namespace dsp } // namespace rack diff --git a/include/dsp/samplerate.hpp b/include/dsp/samplerate.hpp deleted file mode 100644 index ec4640d2..00000000 --- a/include/dsp/samplerate.hpp +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once -#include "resampler.hpp" \ No newline at end of file diff --git a/include/dsp/vumeter.hpp b/include/dsp/vumeter.hpp index 421c615e..7ef937d4 100644 --- a/include/dsp/vumeter.hpp +++ b/include/dsp/vumeter.hpp @@ -1,8 +1,9 @@ #pragma once -#include "math.hpp" +#include "dsp/common.hpp" namespace rack { +namespace dsp { struct VUMeter { @@ -28,4 +29,5 @@ struct VUMeter { }; +} // namespace dsp } // namespace rack diff --git a/include/rack.hpp b/include/rack.hpp index f1490125..7e944204 100644 --- a/include/rack.hpp +++ b/include/rack.hpp @@ -80,6 +80,16 @@ #include "plugin/Model.hpp" #include "plugin/callbacks.hpp" +#include "dsp/common.hpp" +#include "dsp/digital.hpp" +#include "dsp/filter.hpp" +#include "dsp/fir.hpp" +#include "dsp/frame.hpp" +#include "dsp/minblep.hpp" +#include "dsp/ode.hpp" +#include "dsp/resampler.hpp" +#include "dsp/ringbuffer.hpp" +#include "dsp/vumeter.hpp" namespace rack { diff --git a/include/rack0.hpp b/include/rack0.hpp index 32e933e8..1e312f8c 100644 --- a/include/rack0.hpp +++ b/include/rack0.hpp @@ -147,4 +147,19 @@ DEPRECATED inline float engineGetSampleTime() { return context()->engine->getSampleTime(); } +//////////////////// +// dsp +//////////////////// + +using namespace dsp; + +inline float gainToDb(float gain) { + return dsp::amplitudeToDb(gain); +} + +inline float dbToGain(float db) { + return dsp::dbToAmplitude(db); +} + + } // namespace rack diff --git a/src/Core/AudioInterface.cpp b/src/Core/AudioInterface.cpp index f2a6729d..325f6b41 100644 --- a/src/Core/AudioInterface.cpp +++ b/src/Core/AudioInterface.cpp @@ -1,7 +1,5 @@ #include "Core.hpp" #include "audio.hpp" -#include "dsp/resampler.hpp" -#include "dsp/ringbuffer.hpp" #include #include #include @@ -23,9 +21,9 @@ struct AudioInterfaceIO : audio::IO { std::mutex audioMutex; std::condition_variable audioCv; // Audio thread produces, engine thread consumes - DoubleRingBuffer, (1<<15)> inputBuffer; + dsp::DoubleRingBuffer, (1<<15)> inputBuffer; // Audio thread consumes, engine thread produces - DoubleRingBuffer, (1<<15)> outputBuffer; + dsp::DoubleRingBuffer, (1<<15)> outputBuffer; bool active = false; ~AudioInterfaceIO() { @@ -46,7 +44,7 @@ struct AudioInterfaceIO : audio::IO { for (int i = 0; i < frames; i++) { if (inputBuffer.full()) break; - Frame inputFrame; + dsp::Frame inputFrame; memset(&inputFrame, 0, sizeof(inputFrame)); memcpy(&inputFrame, &input[numInputs * i], numInputs * sizeof(float)); inputBuffer.push(inputFrame); @@ -62,7 +60,7 @@ struct AudioInterfaceIO : audio::IO { if (audioCv.wait_for(lock, timeout, cond)) { // Consume audio block for (int i = 0; i < frames; i++) { - Frame f = outputBuffer.shift(); + dsp::Frame f = outputBuffer.shift(); for (int j = 0; j < numOutputs; j++) { output[numOutputs*i + j] = clamp(f.samples[j], -1.f, 1.f); } @@ -112,12 +110,12 @@ struct AudioInterface : Module { int lastNumOutputs = -1; int lastNumInputs = -1; - SampleRateConverter inputSrc; - SampleRateConverter outputSrc; + dsp::SampleRateConverter inputSrc; + dsp::SampleRateConverter outputSrc; // in rack's sample rate - DoubleRingBuffer, 16> inputBuffer; - DoubleRingBuffer, 16> outputBuffer; + dsp::DoubleRingBuffer, 16> inputBuffer; + dsp::DoubleRingBuffer, 16> outputBuffer; AudioInterface() { setup(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS); @@ -177,7 +175,7 @@ void AudioInterface::step() { } // Take input from buffer - Frame inputFrame; + dsp::Frame inputFrame; if (!inputBuffer.empty()) { inputFrame = inputBuffer.shift(); } @@ -195,7 +193,7 @@ void AudioInterface::step() { if (audioIO.active && audioIO.numOutputs > 0) { // Get and push output SRC frame if (!outputBuffer.full()) { - Frame outputFrame; + dsp::Frame outputFrame; for (int i = 0; i < AUDIO_OUTPUTS; i++) { outputFrame.samples[i] = inputs[AUDIO_INPUT + i].getVoltage() / 10.f; } diff --git a/src/Core/MIDICCToCVInterface.cpp b/src/Core/MIDICCToCVInterface.cpp index 0fee1632..ab295705 100644 --- a/src/Core/MIDICCToCVInterface.cpp +++ b/src/Core/MIDICCToCVInterface.cpp @@ -1,6 +1,5 @@ #include "Core.hpp" #include "midi.hpp" -#include "dsp/filter.hpp" struct MIDICCToCVInterface : Module { @@ -20,7 +19,7 @@ struct MIDICCToCVInterface : Module { midi::InputQueue midiInput; int8_t ccs[128]; - ExponentialFilter ccFilters[16]; + dsp::ExponentialFilter ccFilters[16]; int learningId = -1; int learnedCcs[16] = {}; diff --git a/src/Core/MIDIToCVInterface.cpp b/src/Core/MIDIToCVInterface.cpp index d954f7ae..77e7d725 100644 --- a/src/Core/MIDIToCVInterface.cpp +++ b/src/Core/MIDIToCVInterface.cpp @@ -1,7 +1,5 @@ #include "Core.hpp" #include "midi.hpp" -#include "dsp/digital.hpp" -#include "dsp/filter.hpp" #include @@ -35,14 +33,14 @@ struct MIDIToCVInterface : Module { midi::InputQueue midiInput; uint8_t mod = 0; - ExponentialFilter modFilter; + dsp::ExponentialFilter modFilter; uint16_t pitch = 8192; - ExponentialFilter pitchFilter; - PulseGenerator retriggerPulse; - PulseGenerator clockPulses[2]; - PulseGenerator startPulse; - PulseGenerator stopPulse; - PulseGenerator continuePulse; + dsp::ExponentialFilter pitchFilter; + dsp::PulseGenerator retriggerPulse; + dsp::PulseGenerator clockPulses[2]; + dsp::PulseGenerator startPulse; + dsp::PulseGenerator stopPulse; + dsp::PulseGenerator continuePulse; int clock = 0; int divisions[2]; diff --git a/src/Core/QuadMIDIToCVInterface.cpp b/src/Core/QuadMIDIToCVInterface.cpp index 682ba425..fd21c456 100644 --- a/src/Core/QuadMIDIToCVInterface.cpp +++ b/src/Core/QuadMIDIToCVInterface.cpp @@ -1,6 +1,5 @@ #include "Core.hpp" #include "midi.hpp" -#include "dsp/digital.hpp" #include