sonnenlicht/plugin/chorus.cpp

128 lines
3.9 KiB
C++

/*
sonnenlicht poly ensemble
Copyright 2025 Gordon JC Pearce <gordonjcp@gjcp.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "chorus.hpp"
#include <math.h>
#include <string.h>
#include <cstdio>
extern double sampleRate;
extern uint32_t bufferSize;
Chorus::Chorus() { // no parameters, programs, or states
lpfOut1 = new float[bufferSize];
lpfOut2 = new float[bufferSize];
ram = new float[DELAYSIZE]; // probably needs to be calculated based on sample rate
fastPhase = 0;
slowPhase = 0;
postFilter1l = new SVF(POSTCUTOFF, .546);
postFilter2l = new SVF(POSTCUTOFF, 1.324);
postFilter1r = new SVF(POSTCUTOFF, .546);
postFilter2r = new SVF(POSTCUTOFF, 1.324);
// lfo values taken from a rough simulation
fastOmega = 6.283 * 5.7 / sampleRate; // approximate, can be adjusted
slowOmega = 6.283 * 0.7 / sampleRate; // again approximate
// zero out the delay buffer
memset(ram, 0, sizeof(float) * DELAYSIZE);
memset(lpfOut1, 0, sizeof(float) * bufferSize);
memset(lpfOut2, 0, sizeof(float) * bufferSize);
}
Chorus::~Chorus() {
delete lpfOut1;
delete lpfOut2;
delete ram;
delete postFilter1l;
delete postFilter2l;
delete postFilter1r;
delete postFilter2r;
}
void Chorus::run(const float *input, float **outputs, uint32_t frames) {
// actual effects here
// now run the DSP
float out0 = 0, out120 = 0, out240 = 0, s0 = 0, s1 = 0;
float lfoMod, dly1, frac;
uint16_t tap, delay;
for (uint32_t i = 0; i < frames; i++) {
// run a step of LFO
fastPhase += fastOmega;
if (fastPhase > 6.283) fastPhase -= 6.283;
slowPhase += slowOmega;
if (slowPhase > 6.283) slowPhase -= 6.283;
ram[delayptr] = input[i];
#define BASE 0.05
#define AMT 0.00175
// 0 degree delay line
lfoMod = 0.203 * sin(fastPhase) + 0.835 * sin(slowPhase);
dly1 = (BASE + (AMT * lfoMod)) * sampleRate;
delay = (int)dly1;
frac = dly1 - delay;
tap = delayptr - delay;
s1 = ram[(tap - 1) & 0x3ff];
s0 = ram[tap & 0x3ff];
out0 = ((s1 - s0) * frac) + s0;
// 120 degree delay line
lfoMod = 0.248 * sin(fastPhase + 2.09) + 0.745 * sin(slowPhase + 2.09);
dly1 = (BASE + (AMT * lfoMod)) * sampleRate;
delay = (int)dly1;
frac = dly1 - delay;
tap = delayptr - delay;
s1 = ram[(tap - 1) & 0x3ff];
s0 = ram[tap & 0x3ff];
out120 = ((s1 - s0) * frac) + s0;
// 240 degree delay line
lfoMod = 0.252 * sin(fastPhase + 4.18) + 0.809 * sin(slowPhase + 4.18);
dly1 = (BASE + (AMT * lfoMod)) * sampleRate;
delay = (int)dly1;
frac = dly1 - delay;
tap = delayptr - delay;
s1 = ram[(tap - 1) & 0x3ff];
s0 = ram[tap & 0x3ff];
out240 = ((s1 - s0) * frac) + s0;
lpfOut1[i] = (out0 + (out120 * 0.66) + (out240 * 0.33));
lpfOut2[i] = (out0 + (out120 * 0.33) + (out240 * 0.66));
delayptr++;
delayptr &= 0x3ff;
}
postFilter1l->runSVF(lpfOut1, lpfOut1, frames);
postFilter2l->runSVF(lpfOut1, outputs[0], frames);
postFilter1r->runSVF(lpfOut2, lpfOut2, frames);
postFilter2r->runSVF(lpfOut2, outputs[1], frames);
}