From 00ef3ee41d81242bc89fa256487c008c6d562a4d Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Wed, 22 Feb 2017 23:10:26 -0500 Subject: [PATCH] Initial commit --- LICENSE.txt | 11 ++ Makefile | 29 ++++ src/ABC.cpp | 103 +++++++++++++++ src/Befaco.cpp | 21 +++ src/Befaco.hpp | 34 +++++ src/EvenVCO.cpp | 170 ++++++++++++++++++++++++ src/Mixer.cpp | 81 ++++++++++++ src/Rampage.cpp | 50 +++++++ src/SlewLimiter.cpp | 91 +++++++++++++ src/SpringReverb.cpp | 305 +++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 895 insertions(+) create mode 100644 LICENSE.txt create mode 100644 Makefile create mode 100644 src/ABC.cpp create mode 100644 src/Befaco.cpp create mode 100644 src/Befaco.hpp create mode 100644 src/EvenVCO.cpp create mode 100644 src/Mixer.cpp create mode 100644 src/Rampage.cpp create mode 100644 src/SlewLimiter.cpp create mode 100644 src/SpringReverb.cpp diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..17df902 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,11 @@ +Copyright 2016 Andrew Belt + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..f545679 --- /dev/null +++ b/Makefile @@ -0,0 +1,29 @@ +ARCH ?= lin +FLAGS = -fPIC -g -Wall -O3 -msse -mfpmath=sse -ffast-math \ + -I../../include -I./pffft -DPFFFT_SIMD_DISABLE +LDFLAGS = + +SOURCES = $(wildcard src/*.cpp) pffft/pffft.c + + +ifeq ($(ARCH), lin) +LDFLAGS += -shared +TARGET = plugin.so +endif + +ifeq ($(ARCH), mac) +LDFLAGS += -shared -undefined dynamic_lookup +TARGET = plugin.dylib +endif + +ifeq ($(ARCH), win) +LDFLAGS += -shared -L../../ -lRack +TARGET = plugin.dll +endif + +all: $(TARGET) + +clean: + rm -rfv build $(TARGET) + +include ../../Makefile.inc diff --git a/src/ABC.cpp b/src/ABC.cpp new file mode 100644 index 0000000..aa024fb --- /dev/null +++ b/src/ABC.cpp @@ -0,0 +1,103 @@ +#include "Befaco.hpp" + + +struct ABC : Module { + enum ParamIds { + B1_LEVEL_PARAM, + C1_LEVEL_PARAM, + B2_LEVEL_PARAM, + C2_LEVEL_PARAM, + NUM_PARAMS + }; + enum InputIds { + A1_INPUT, + B1_INPUT, + C1_INPUT, + A2_INPUT, + B2_INPUT, + C2_INPUT, + NUM_INPUTS + }; + enum OutputIds { + OUT1_OUTPUT, + OUT2_OUTPUT, + NUM_OUTPUTS + }; + + float lights[2] = {}; + + ABC(); + void step(); +}; + + +ABC::ABC() { + params.resize(NUM_PARAMS); + inputs.resize(NUM_INPUTS); + outputs.resize(NUM_OUTPUTS); +} + +inline +float clip(float x) { + x = clampf(x, -2.0, 2.0); + return x / powf(1.0 + powf(x, 24.0), 1/24.0); +} + +void ABC::step() { + float a1 = getf(inputs[A1_INPUT]); + float b1 = getf(inputs[B1_INPUT], 5.0) * 2.0*exponentialBipolar(80.0, params[B1_LEVEL_PARAM]); + float c1 = getf(inputs[C1_INPUT], 10.0) * exponentialBipolar(80.0, params[C1_LEVEL_PARAM]); + float out1 = a1 * b1 / 5.0 + c1; + + float a2 = getf(inputs[A2_INPUT]); + float b2 = getf(inputs[B2_INPUT], 5.0) * 2.0*exponentialBipolar(80.0, params[B2_LEVEL_PARAM]); + float c2 = getf(inputs[C2_INPUT], 20.0) * exponentialBipolar(80.0, params[C2_LEVEL_PARAM]); + float out2 = a2 * b2 / 5.0 + c2; + + // Set outputs + if (outputs[OUT1_OUTPUT]) { + *outputs[OUT1_OUTPUT] = clip(out1 / 10.0) * 10.0; + } + else { + out2 += out1; + } + if (outputs[OUT2_OUTPUT]) { + *outputs[OUT2_OUTPUT] = clip(out2 / 10.0) * 10.0; + } + lights[0] = out1 / 5.0; + lights[1] = out2 / 5.0; +} + + +ABCWidget::ABCWidget() { + ABC *module = new ABC(); + setModule(module); + box.size = Vec(15*6, 380); + + { + Panel *panel = new DarkPanel(); + panel->box.size = box.size; + panel->backgroundImage = Image::load("plugins/Befaco/res/ABC.png"); + addChild(panel); + } + + addChild(createScrew(Vec(15, 0))); + addChild(createScrew(Vec(15, 365))); + + addParam(createParam(Vec(44, 37), module, ABC::B1_LEVEL_PARAM, -1.0, 1.0, 0.0)); + addParam(createParam(Vec(44, 107), module, ABC::C1_LEVEL_PARAM, -1.0, 1.0, 0.0)); + addParam(createParam(Vec(44, 204), module, ABC::B2_LEVEL_PARAM, -1.0, 1.0, 0.0)); + addParam(createParam(Vec(44, 274), module, ABC::C2_LEVEL_PARAM, -1.0, 1.0, 0.0)); + + addInput(createInput(Vec(2, 24), module, ABC::A1_INPUT)); + addInput(createInput(Vec(2, 66), module, ABC::B1_INPUT)); + addInput(createInput(Vec(2, 108), module, ABC::C1_INPUT)); + addOutput(createOutput(Vec(2, 150), module, ABC::OUT1_OUTPUT)); + addInput(createInput(Vec(2, 191), module, ABC::A2_INPUT)); + addInput(createInput(Vec(2, 233), module, ABC::B2_INPUT)); + addInput(createInput(Vec(2, 275), module, ABC::C2_INPUT)); + addOutput(createOutput(Vec(2, 317), module, ABC::OUT2_OUTPUT)); + + addChild(createValueLight>(Vec(38, 162), &module->lights[0])); + addChild(createValueLight>(Vec(38, 329), &module->lights[1])); +} diff --git a/src/Befaco.cpp b/src/Befaco.cpp new file mode 100644 index 0000000..189d010 --- /dev/null +++ b/src/Befaco.cpp @@ -0,0 +1,21 @@ +#include "Befaco.hpp" + + +struct BefacoPlugin : Plugin { + BefacoPlugin() { + slug = "Befaco"; + name = "Befaco"; + createModel(this, "EvenVCO", "EvenVCO"); + // createModel(this, "Rampage", "Rampage"); + createModel(this, "ABC", "A*B+C"); + createModel(this, "SpringReverb", "Spring Reverb"); + createModel(this, "Mixer", "Mixer"); + createModel(this, "SlewLimiter", "Slew Limiter"); + } +}; + + +Plugin *init() { + springReverbInit(); + return new BefacoPlugin(); +} diff --git a/src/Befaco.hpp b/src/Befaco.hpp new file mode 100644 index 0000000..fdcaf48 --- /dev/null +++ b/src/Befaco.hpp @@ -0,0 +1,34 @@ +#include "rack.hpp" + + +using namespace rack; + +void springReverbInit(); + +//////////////////// +// module widgets +//////////////////// + +struct EvenVCOWidget : ModuleWidget { + EvenVCOWidget(); +}; + +struct RampageWidget : ModuleWidget { + RampageWidget(); +}; + +struct ABCWidget : ModuleWidget { + ABCWidget(); +}; + +struct SpringReverbWidget : ModuleWidget { + SpringReverbWidget(); +}; + +struct MixerWidget : ModuleWidget { + MixerWidget(); +}; + +struct SlewLimiterWidget : ModuleWidget { + SlewLimiterWidget(); +}; diff --git a/src/EvenVCO.cpp b/src/EvenVCO.cpp new file mode 100644 index 0000000..b298b86 --- /dev/null +++ b/src/EvenVCO.cpp @@ -0,0 +1,170 @@ +#include "Befaco.hpp" +#include "dsp.hpp" + + +struct EvenVCO : Module { + enum ParamIds { + OCTAVE_PARAM, + TUNE_PARAM, + PWM_PARAM, + NUM_PARAMS + }; + enum InputIds { + PITCH1_INPUT, + PITCH2_INPUT, + FM_INPUT, + SYNC_INPUT, + PWM_INPUT, + NUM_INPUTS + }; + enum OutputIds { + TRI_OUTPUT, + SINE_OUTPUT, + EVEN_OUTPUT, + SAW_OUTPUT, + SQUARE_OUTPUT, + NUM_OUTPUTS + }; + + float phase = 0.0; + /** The value of the last sync input */ + float sync = 0.0; + /** The outputs */ + float tri = 0.0; + /** Whether we are past the pulse width already */ + bool halfPhase = false; + + MinBLEP<16> triSquareMinBLEP; + MinBLEP<16> triMinBLEP; + MinBLEP<16> sineMinBLEP; + MinBLEP<16> doubleSawMinBLEP; + MinBLEP<16> sawMinBLEP; + MinBLEP<16> squareMinBLEP; + + RCFilter triFilter; + + EvenVCO(); + void step(); +}; + + +EvenVCO::EvenVCO() { + params.resize(NUM_PARAMS); + inputs.resize(NUM_INPUTS); + outputs.resize(NUM_OUTPUTS); + + triSquareMinBLEP.minblep = minblep_16_32; + triSquareMinBLEP.oversample = 32; + triMinBLEP.minblep = minblep_16_32; + triMinBLEP.oversample = 32; + sineMinBLEP.minblep = minblep_16_32; + sineMinBLEP.oversample = 32; + doubleSawMinBLEP.minblep = minblep_16_32; + doubleSawMinBLEP.oversample = 32; + sawMinBLEP.minblep = minblep_16_32; + sawMinBLEP.oversample = 32; + squareMinBLEP.minblep = minblep_16_32; + squareMinBLEP.oversample = 32; +} + +void EvenVCO::step() { + // Compute frequency, pitch is 1V/oct + float pitch = 1.0 + roundf(params[OCTAVE_PARAM]) + params[TUNE_PARAM] / 12.0; + pitch += getf(inputs[PITCH1_INPUT]) + getf(inputs[PITCH2_INPUT]); + pitch += getf(inputs[FM_INPUT]) / 4.0; + float freq = 261.626 * powf(2.0, pitch); + freq = clampf(freq, 0.0, 20000.0); + + // Pulse width + float pw = params[PWM_PARAM] + getf(inputs[PWM_INPUT]) / 5.0; + const float minPw = 0.05; + pw = mapf(clampf(pw, -1.0, 1.0), -1.0, 1.0, minPw, 1.0-minPw); + + // Advance phase + float deltaPhase = clampf(freq / gSampleRate, 1e-6, 0.5); + float oldPhase = phase; + phase += deltaPhase; + + if (oldPhase < 0.5 && phase >= 0.5) { + float crossing = -(phase - 0.5) / deltaPhase; + triSquareMinBLEP.jump(crossing, 2.0); + doubleSawMinBLEP.jump(crossing, -2.0); + } + + if (!halfPhase && phase >= pw) { + float crossing = -(phase - pw) / deltaPhase; + squareMinBLEP.jump(crossing, 2.0); + halfPhase = true; + } + + // Reset phase if at end of cycle + if (phase >= 1.0) { + phase -= 1.0; + float crossing = -phase / deltaPhase; + triSquareMinBLEP.jump(crossing, -2.0); + doubleSawMinBLEP.jump(crossing, -2.0); + squareMinBLEP.jump(crossing, -2.0); + sawMinBLEP.jump(crossing, -2.0); + halfPhase = false; + } + + // Outputs + float triSquare = (phase < 0.5) ? -1.0 : 1.0; + triSquare += triSquareMinBLEP.shift(); + + // Integrate square for triangle + tri += 4.0 * triSquare * freq / gSampleRate; + tri *= (1.0 - 40.0 / gSampleRate); + + float sine = -cosf(2*M_PI * phase); + float doubleSaw = (phase < 0.5) ? (-1.0 + 4.0*phase) : (-1.0 + 4.0*(phase - 0.5)); + doubleSaw += doubleSawMinBLEP.shift(); + float even = 0.55 * (doubleSaw + 1.27 * sine); + float saw = -1.0 + 2.0*phase; + saw += sawMinBLEP.shift(); + float square = (phase < pw) ? -1.0 : 1.0; + square += squareMinBLEP.shift(); + + // Set outputs + setf(outputs[TRI_OUTPUT], 5.0*tri); + setf(outputs[SINE_OUTPUT], 5.0*sine); + setf(outputs[EVEN_OUTPUT], 5.0*even); + setf(outputs[SAW_OUTPUT], 5.0*saw); + setf(outputs[SQUARE_OUTPUT], 5.0*square); +} + + +EvenVCOWidget::EvenVCOWidget() { + EvenVCO *module = new EvenVCO(); + setModule(module); + box.size = Vec(15*8, 380); + + { + Panel *panel = new DarkPanel(); + panel->box.size = box.size; + panel->backgroundImage = Image::load("plugins/Befaco/res/EvenVCO.png"); + addChild(panel); + } + + addChild(createScrew(Vec(15, 0))); + addChild(createScrew(Vec(15, 365))); + addChild(createScrew(Vec(15*6, 0))); + addChild(createScrew(Vec(15*6, 365))); + + addParam(createParam(Vec(24-4+2, 35-4+1), module, EvenVCO::OCTAVE_PARAM, -5.0, 4.0, 0.0)); + addParam(createParam(Vec(72, 131), module, EvenVCO::TUNE_PARAM, -7.0, 7.0, 0.0)); + addParam(createParam(Vec(16, 230), module, EvenVCO::PWM_PARAM, -1.0, 1.0, 0.0)); + + addInput(createInput(Vec(13-7-1, 124-7), module, EvenVCO::PITCH1_INPUT)); + addInput(createInput(Vec(22-7, 162-7-1), module, EvenVCO::PITCH2_INPUT)); + addInput(createInput(Vec(51-7, 188-7-1), module, EvenVCO::FM_INPUT)); + addInput(createInput(Vec(88-7, 193-7), module, EvenVCO::SYNC_INPUT)); + + addInput(createInput(Vec(76-7, 240-7), module, EvenVCO::PWM_INPUT)); + + addOutput(createOutput(Vec(12-7, 285-7), module, EvenVCO::TRI_OUTPUT)); + addOutput(createOutput(Vec(88-7+1, 285-7), module, EvenVCO::SINE_OUTPUT)); + addOutput(createOutput(Vec(50-7+1, 308-7), module, EvenVCO::EVEN_OUTPUT)); + addOutput(createOutput(Vec(12-7, 329-7), module, EvenVCO::SAW_OUTPUT)); + addOutput(createOutput(Vec(88-7+1, 329-7), module, EvenVCO::SQUARE_OUTPUT)); +} diff --git a/src/Mixer.cpp b/src/Mixer.cpp new file mode 100644 index 0000000..e5451db --- /dev/null +++ b/src/Mixer.cpp @@ -0,0 +1,81 @@ +#include "Befaco.hpp" + + +struct Mixer : Module { + enum ParamIds { + CH1_PARAM, + CH2_PARAM, + CH3_PARAM, + CH4_PARAM, + NUM_PARAMS + }; + enum InputIds { + IN1_INPUT, + IN2_INPUT, + IN3_INPUT, + IN4_INPUT, + NUM_INPUTS + }; + enum OutputIds { + OUT1_OUTPUT, + OUT2_OUTPUT, + NUM_OUTPUTS + }; + + float lights[1] = {}; + + Mixer(); + void step(); +}; + + +Mixer::Mixer() { + params.resize(NUM_PARAMS); + inputs.resize(NUM_INPUTS); + outputs.resize(NUM_OUTPUTS); +} + +void Mixer::step() { + float in1 = getf(inputs[IN1_INPUT]) * params[CH1_PARAM]; + float in2 = getf(inputs[IN2_INPUT]) * params[CH2_PARAM]; + float in3 = getf(inputs[IN3_INPUT]) * params[CH3_PARAM]; + float in4 = getf(inputs[IN4_INPUT]) * params[CH4_PARAM]; + + float out = in1 + in2 + in3 + in4; + setf(outputs[OUT1_OUTPUT], out); + setf(outputs[OUT2_OUTPUT], -out); + lights[0] = out / 5.0; +} + + +MixerWidget::MixerWidget() { + Mixer *module = new Mixer(); + setModule(module); + box.size = Vec(15*5, 380); + + { + Panel *panel = new DarkPanel(); + panel->box.size = box.size; + panel->backgroundImage = Image::load("plugins/Befaco/res/Mixer.png"); + addChild(panel); + } + + addChild(createScrew(Vec(15, 0))); + addChild(createScrew(Vec(15, 365))); + + addParam(createParam(Vec(19, 32), module, Mixer::CH1_PARAM, 0.0, 1.0, 0.0)); + addParam(createParam(Vec(19, 85), module, Mixer::CH2_PARAM, 0.0, 1.0, 0.0)); + addParam(createParam(Vec(19, 137), module, Mixer::CH3_PARAM, 0.0, 1.0, 0.0)); + addParam(createParam(Vec(19, 190), module, Mixer::CH4_PARAM, 0.0, 1.0, 0.0)); + + addInput(createInput(Vec(4, 239), module, Mixer::IN1_INPUT)); + addInput(createInput(Vec(40, 239), module, Mixer::IN2_INPUT)); + + addInput(createInput(Vec(4, 278), module, Mixer::IN3_INPUT)); + addInput(createInput(Vec(40, 278), module, Mixer::IN4_INPUT)); + + addOutput(createOutput(Vec(4, 321), module, Mixer::OUT1_OUTPUT)); + addOutput(createOutput(Vec(40, 321), module, Mixer::OUT2_OUTPUT)); + + addChild(createValueLight>(Vec(31, 309), &module->lights[0])); +} diff --git a/src/Rampage.cpp b/src/Rampage.cpp new file mode 100644 index 0000000..90ceac2 --- /dev/null +++ b/src/Rampage.cpp @@ -0,0 +1,50 @@ +#include "Befaco.hpp" + + +struct Rampage : Module { + enum ParamIds { + NUM_PARAMS + }; + enum InputIds { + NUM_INPUTS + }; + enum OutputIds { + NUM_OUTPUTS + }; + + Rampage(); + void step(); +}; + + +Rampage::Rampage() { + params.resize(NUM_PARAMS); + inputs.resize(NUM_INPUTS); + outputs.resize(NUM_OUTPUTS); +} + +void Rampage::step() { +} + + +RampageWidget::RampageWidget() { + Rampage *module = new Rampage(); + setModule(module); + box.size = Vec(15*18, 380); + + { + Panel *panel = new DarkPanel(); + panel->box.size = box.size; + panel->backgroundImage = Image::load("plugins/Befaco/res/Rampage.png"); + addChild(panel); + } + + addChild(createScrew(Vec(15, 0))); + addChild(createScrew(Vec(box.size.x-30, 0))); + addChild(createScrew(Vec(15, 365))); + addChild(createScrew(Vec(box.size.x-30, 365))); + + // addParam(createParam(Vec(0, 43), module, Rampage::RADIOACTIVITY_PARAM, 0.0, 1.0, 0.0)); + // addInput(createInput(Vec(10, 248), module, Rampage::RADIOACTIVITY_INPUT)); + // addOutput(createOutput(Vec(10, 306), module, Rampage::PULSE_OUTPUT)); +} diff --git a/src/SlewLimiter.cpp b/src/SlewLimiter.cpp new file mode 100644 index 0000000..df212d1 --- /dev/null +++ b/src/SlewLimiter.cpp @@ -0,0 +1,91 @@ +#include "Befaco.hpp" + + +struct SlewLimiter : Module { + enum ParamIds { + SHAPE_PARAM, + RISE_PARAM, + FALL_PARAM, + NUM_PARAMS + }; + enum InputIds { + RISE_INPUT, + FALL_INPUT, + IN_INPUT, + NUM_INPUTS + }; + enum OutputIds { + OUT_OUTPUT, + NUM_OUTPUTS + }; + + float output = 0.0; + + SlewLimiter(); + void step(); +}; + + +SlewLimiter::SlewLimiter() { + params.resize(NUM_PARAMS); + inputs.resize(NUM_INPUTS); + outputs.resize(NUM_OUTPUTS); +} + +void SlewLimiter::step() { + float input = getf(inputs[IN_INPUT]); + float shape = params[SHAPE_PARAM]; + + // minimum and maximum slopes in volts per second + const float slewMin = 0.1; + const float slewMax = 40000.0; + // Amount of extra slew per voltage difference + const float shapeScale = 1/10.0; + + // Rise + if (input > output) { + float rise = getf(inputs[RISE_INPUT]) + params[RISE_PARAM]; + float slew = slewMax * powf(slewMin / slewMax, rise); + output += slew * crossf(1.0, shapeScale * (input - output), shape) / gSampleRate; + if (output > input) + output = input; + } + // Fall + else if (input < output) { + float fall = getf(inputs[FALL_INPUT]) + params[FALL_PARAM]; + float slew = slewMax * powf(slewMin / slewMax, fall); + output -= slew * crossf(1.0, shapeScale * (output - input), shape) / gSampleRate; + if (output < input) + output = input; + } + + setf(outputs[OUT_OUTPUT], output); +} + + +SlewLimiterWidget::SlewLimiterWidget() { + SlewLimiter *module = new SlewLimiter(); + setModule(module); + box.size = Vec(15*6, 380); + + { + Panel *panel = new DarkPanel(); + panel->box.size = box.size; + panel->backgroundImage = Image::load("plugins/Befaco/res/Slew Limiter.png"); + addChild(panel); + } + + addChild(createScrew(Vec(15, 0))); + addChild(createScrew(Vec(15, 365))); + + addParam(createParam(Vec(26, 39), module, SlewLimiter::SHAPE_PARAM, 0.0, 1.0, 0.0)); + + addParam(createParam(Vec(17, 100), module, SlewLimiter::RISE_PARAM, 0.0, 1.0, 0.0)); + addParam(createParam(Vec(61, 100), module, SlewLimiter::FALL_PARAM, 0.0, 1.0, 0.0)); + + addInput(createInput(Vec(6, 270), module, SlewLimiter::RISE_INPUT)); + addInput(createInput(Vec(52, 270), module, SlewLimiter::FALL_INPUT)); + + addInput(createInput(Vec(6, 320), module, SlewLimiter::IN_INPUT)); + addOutput(createOutput(Vec(52, 320), module, SlewLimiter::OUT_OUTPUT)); +} diff --git a/src/SpringReverb.cpp b/src/SpringReverb.cpp new file mode 100644 index 0000000..51fe6af --- /dev/null +++ b/src/SpringReverb.cpp @@ -0,0 +1,305 @@ +#include +#include "Befaco.hpp" +#include "dsp.hpp" +#include "pffft.h" + + +float *springReverbIR; +int springReverbIRLen; + +void springReverbInit() { + FILE *f = fopen("plugins/Befaco/res/SpringReverbIR.pcm", "rb"); + assert(f); + fseek(f, 0, SEEK_END); + int size = ftell(f); + fseek(f, 0, SEEK_SET); + + springReverbIRLen = size / sizeof(float); + springReverbIR = new float[springReverbIRLen]; + fread(springReverbIR, sizeof(float), springReverbIRLen, f); + fclose(f); + + // TODO Add springReverbDestroy() function once plugins have destroy() callbacks +} + + +struct RealTimeConvolver { + // `kernelBlocks` number of contiguous FFT blocks of size `blockSize` + // indexed by [i * blockSize*2 + j] + float *kernelFfts = NULL; + float *inputFfts = NULL; + float *outputTail = NULL; + float *tmpBlock = NULL; + size_t blockSize; + size_t kernelBlocks = 0; + size_t inputPos = 0; + // kiss_fftr_cfg fft_cfg; + // kiss_fftr_cfg ifft_cfg; + PFFFT_Setup *pffft; + + /** blocksize should be >=32 and a power of 2 */ + RealTimeConvolver(size_t blockSize) { + this->blockSize = blockSize; + pffft = pffft_new_setup(blockSize*2, PFFFT_REAL); + outputTail = new float[blockSize](); + tmpBlock = new float[blockSize*2](); + } + + ~RealTimeConvolver() { + clear(); + delete[] outputTail; + delete[] tmpBlock; + pffft_destroy_setup(pffft); + } + + void clear() { + if (kernelFfts) { + pffft_aligned_free(kernelFfts); + kernelFfts = NULL; + } + if (inputFfts) { + pffft_aligned_free(inputFfts); + inputFfts = NULL; + } + kernelBlocks = 0; + inputPos = 0; + } + + void setKernel(const float *kernel, size_t length) { + clear(); + + if (!kernel || length == 0) + return; + + // Round up to the nearest factor blockSize + kernelBlocks = (length - 1) / blockSize + 1; + + // Allocate blocks + kernelFfts = (float*) pffft_aligned_malloc(sizeof(float) * blockSize*2 * kernelBlocks); + inputFfts = (float*) pffft_aligned_malloc(sizeof(float) * blockSize*2 * kernelBlocks); + memset(inputFfts, 0, sizeof(float) * blockSize*2 * kernelBlocks); + + for (size_t i = 0; i < kernelBlocks; i++) { + // Pad each block with zeros + memset(tmpBlock, 0, sizeof(float) * blockSize*2); + size_t len = mini(blockSize, length - i*blockSize); + memcpy(tmpBlock, &kernel[i*blockSize], sizeof(float)*len); + // Compute fft + pffft_transform(pffft, tmpBlock, &kernelFfts[blockSize*2 * i], NULL, PFFFT_FORWARD); + } + } + + /** Applies reverb to input + input and output must be size blockSize + */ + void processBlock(const float *input, float *output) { + if (kernelBlocks == 0) { + memset(output, 0, sizeof(float) * blockSize); + return; + } + + // Step input position + inputPos = (inputPos + 1) % kernelBlocks; + // Pad block with zeros + memset(tmpBlock, 0, sizeof(float) * blockSize*2); + memcpy(tmpBlock, input, sizeof(float) * blockSize); + // Compute input fft + pffft_transform(pffft, tmpBlock, &inputFfts[blockSize*2 * inputPos], NULL, PFFFT_FORWARD); + // Create output fft + memset(tmpBlock, 0, sizeof(float) * blockSize*2); + // convolve input fft by kernel fft + // Note: This is the CPU bottleneck loop + for (size_t i = 0; i < kernelBlocks; i++) { + size_t pos = (inputPos - i + kernelBlocks) % kernelBlocks; + pffft_zconvolve_accumulate(pffft, &kernelFfts[blockSize*2 * i], &inputFfts[blockSize*2 * pos], tmpBlock, 1.0); + } + // Compute output + pffft_transform(pffft, tmpBlock, tmpBlock, NULL, PFFFT_BACKWARD); + // Add block tail from last output block + for (size_t i = 0; i < blockSize; i++) { + tmpBlock[i] += outputTail[i]; + } + // Copy output block to output + for (size_t i = 0; i < blockSize; i++) { + // Scale based on FFT + output[i] = tmpBlock[i] / blockSize; + } + // Set tail + for (size_t i = 0; i < blockSize; i++) { + outputTail[i] = tmpBlock[i + blockSize]; + } + } +}; + + +#define BLOCKSIZE 1024 + +struct SpringReverb : Module { + enum ParamIds { + WET_PARAM, + LEVEL1_PARAM, + LEVEL2_PARAM, + HPF_PARAM, + NUM_PARAMS + }; + enum InputIds { + CV1_INPUT, + CV2_INPUT, + IN1_INPUT, + IN2_INPUT, + MIX_CV_INPUT, + NUM_INPUTS + }; + enum OutputIds { + MIX_OUTPUT, + WET_OUTPUT, + NUM_OUTPUTS + }; + + RealTimeConvolver *convolver = NULL; + SampleRateConverter<1> inputSrc; + SampleRateConverter<1> outputSrc; + DoubleRingBuffer, 16*BLOCKSIZE> inputBuffer; + DoubleRingBuffer, 16*BLOCKSIZE> outputBuffer; + + RCFilter dryFilter; + PeakFilter vuFilter; + PeakFilter lightFilter; + float vuLights[7] = {}; + float lights[1] = {}; + + SpringReverb(); + ~SpringReverb(); + void step(); +}; + + +SpringReverb::SpringReverb() { + params.resize(NUM_PARAMS); + inputs.resize(NUM_INPUTS); + outputs.resize(NUM_OUTPUTS); + + convolver = new RealTimeConvolver(BLOCKSIZE); + convolver->setKernel(springReverbIR, springReverbIRLen); +} + +SpringReverb::~SpringReverb() { + delete convolver; +} + +void SpringReverb::step() { + float in1 = getf(inputs[IN1_INPUT]); + float in2 = getf(inputs[IN2_INPUT]); + const float levelScale = 0.030; + const float levelBase = 25.0; + float level1 = levelScale * exponentialBipolar(levelBase, params[LEVEL1_PARAM]) * getf(inputs[CV1_INPUT], 10.0) / 10.0; + float level2 = levelScale * exponentialBipolar(levelBase, params[LEVEL2_PARAM]) * getf(inputs[CV2_INPUT], 10.0) / 10.0; + float dry = in1 * level1 + in2 * level2; + + // HPF on dry + float dryCutoff = 200.0 * powf(20.0, params[HPF_PARAM]) / gSampleRate; + dryFilter.setCutoff(dryCutoff); + dryFilter.process(dry); + + // Add dry to input buffer + if (!inputBuffer.full()) { + Frame<1> inputFrame; + inputFrame.samples[0] = dryFilter.highpass(); + inputBuffer.push(inputFrame); + } + + + if (outputBuffer.empty()) { + float input[BLOCKSIZE] = {}; + float output[BLOCKSIZE]; + // Convert input buffer + { + inputSrc.setRatio(48000.0 / gSampleRate); + int inLen = inputBuffer.size(); + int outLen = BLOCKSIZE; + inputSrc.process(inputBuffer.startData(), &inLen, (Frame<1>*) input, &outLen); + inputBuffer.startIncr(inLen); + } + + // Convolve block + convolver->processBlock(input, output); + + // Convert output buffer + { + outputSrc.setRatio(gSampleRate / 48000.0); + int inLen = BLOCKSIZE; + int outLen = outputBuffer.capacity(); + outputSrc.process((Frame<1>*) output, &inLen, outputBuffer.endData(), &outLen); + outputBuffer.endIncr(outLen); + } + } + + // Set output + if (outputBuffer.empty()) + return; + float wet = outputBuffer.shift().samples[0]; + float crossfade = clampf(params[WET_PARAM] + getf(inputs[MIX_CV_INPUT]) / 10.0, 0.0, 1.0); + float mix = crossf(in1, wet, crossfade); + + setf(outputs[WET_OUTPUT], clampf(wet, -10.0, 10.0)); + setf(outputs[MIX_OUTPUT], clampf(mix, -10.0, 10.0)); + + // Set lights + float lightRate = 5.0 / gSampleRate; + vuFilter.setRate(lightRate); + vuFilter.process(fabsf(wet)); + lightFilter.setRate(lightRate); + lightFilter.process(fabsf(dry*50.0)); + + float vuValue = vuFilter.peak(); + for (int i = 0; i < 7; i++) { + float light = powf(1.413, i) * vuValue / 10.0 - 1.0; + vuLights[i] = clampf(light, 0.0, 1.0); + } + lights[0] = lightFilter.peak(); +} + + +SpringReverbWidget::SpringReverbWidget() { + SpringReverb *module = new SpringReverb(); + setModule(module); + box.size = Vec(15*8, 380); + + { + Panel *panel = new DarkPanel(); + panel->box.size = box.size; + panel->backgroundImage = Image::load("plugins/Befaco/res/Spring Reverb.png"); + addChild(panel); + } + + addChild(createScrew(Vec(15, 0))); + addChild(createScrew(Vec(15, 365))); + addChild(createScrew(Vec(15*6, 0))); + addChild(createScrew(Vec(15*6, 365))); + + addParam(createParam(Vec(22, 29), module, SpringReverb::WET_PARAM, 0.0, 1.0, 0.5)); + + addParam(createParam(Vec(13, 118-2), module, SpringReverb::LEVEL1_PARAM, 0.0, 1.0, 0.0)); + addParam(createParam(Vec(93, 118-2), module, SpringReverb::LEVEL2_PARAM, 0.0, 1.0, 0.0)); + + addParam(createParam(Vec(41, 209), module, SpringReverb::HPF_PARAM, 0.0, 1.0, 0.5)); + + addInput(createInput(Vec(6-3, 243-3), module, SpringReverb::CV1_INPUT)); + addInput(createInput(Vec(86-3, 243-3), module, SpringReverb::CV2_INPUT)); + addInput(createInput(Vec(26-3, 281-3), module, SpringReverb::IN1_INPUT)); + addInput(createInput(Vec(65-3, 281-3), module, SpringReverb::IN2_INPUT)); + + addOutput(createOutput(Vec(6-3, 317-3), module, SpringReverb::MIX_OUTPUT)); + addInput(createInput(Vec(46-3, 324-3), module, SpringReverb::MIX_CV_INPUT)); + addOutput(createOutput(Vec(87-3, 317-3), module, SpringReverb::WET_OUTPUT)); + + addChild(createValueLight>(Vec(55, 114), &module->vuLights[0])); + addChild(createValueLight>(Vec(55, 127), &module->vuLights[1])); + addChild(createValueLight>(Vec(55, 139), &module->vuLights[2])); + addChild(createValueLight>(Vec(55, 151), &module->vuLights[3])); + addChild(createValueLight>(Vec(55, 164), &module->vuLights[4])); + addChild(createValueLight>(Vec(55, 176), &module->vuLights[5])); + addChild(createValueLight>(Vec(55, 189), &module->vuLights[6])); + + addChild(createValueLight>(Vec(55, 270), &module->lights[0])); +}