peacock/plugin/chorus.cpp

158 lines
4.4 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>
Chorus::Chorus() {
lpfOut1 = new float[bufferSize];
lpfOut2 = new float[bufferSize];
ram = new float[DELAYSIZE]; // probably needs to be calculated based on sample rate
fastPhase = 0;
// not quite Butterworth but you'd never hear the difference
postFilter1l = new SVF(9688, .549);
postFilter2l = new SVF(10377, 1.291);
postFilter1r = new SVF(9688, .549);
postFilter2r = new SVF(10377, 1.291);
// lfo values taken from a rough simulation
fastOmega = 6.283 * 0.7 / sampleRate; // approximate, can be adjusted
// 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(float* input, float** outputs, uint32_t frames) {
// actual effects here
// FIXME add highpass filter
// now run the DSP
float s0 = 0, s1 = 0;
float lfoMod, dly1, frac, flt;
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;
flt = ((hpdelay - input[i]) * hpcut) + input[i];
hpdelay = flt;
input[i] += (flt * hpgain);
ram[delayptr] = input[i];
#define BASE 0.005
#define AMT 0.00175
// FIXME make this triangle not sine, and move arithmetic out of mainloop
lfoMod = sin(fastPhase);
dly1 = (BASE + (AMT * lfoMod)) * sampleRate;
delay = (int)dly1;
frac = dly1 - delay;
tap = delayptr - delay;
s1 = ram[(tap - 1) & 0x3ff];
s0 = ram[tap & 0x3ff];
lpfOut1[i] = ((s1 - s0) * frac) + s0;
dly1 = (BASE - (AMT * lfoMod)) * sampleRate;
delay = (int)dly1;
frac = dly1 - delay;
tap = delayptr - delay;
s1 = ram[(tap - 1) & 0x3ff];
s0 = ram[tap & 0x3ff];
lpfOut2[i] = ((s1 - s0) * frac) + s0;
// lpfOut1[i] = input[i] + 1.2 * out0; //(out0 + (out120 * 0.66) + (out240 * 0.33));
// lpfOut2[i] = input[i] + 1.2 * out120; //(out0 + (out120 * 0.33) + (out240 * 0.66));
delayptr++;
delayptr &= 0x3ff;
}
postFilter1l->runSVF(lpfOut1, lpfOut1, frames);
postFilter2l->runSVF(lpfOut1, lpfOut1, frames);
postFilter1r->runSVF(lpfOut2, lpfOut2, frames);
postFilter2r->runSVF(lpfOut2, lpfOut2, frames);
for (uint32_t i = 0; i < frames; i++) {
float y = input[i];
outputs[0][i] = y + (gain * lpfOut1[i]);
outputs[1][i] = y + (gain * lpfOut2[i]);
}
}
void Chorus::setHpf(uint8_t mode) {
switch (mode) {
case 0x00:
hpcut = 0.91245;
hpgain = -1;
break;
case 0x08:
hpcut = 0.97097;
hpgain = -1;
break;
case 0x10:
hpcut = 1;
hpgain = 0;
break;
case 0x18:
hpcut = 0.99088;
hpgain = 1;
break;
}
}
void Chorus::setChorus(uint8_t mode) {
// switch chorus mode
switch (mode) {
case 0x60:
gain = 0;
break;
case 0x40:
gain = 1.2;
fastOmega = 6.283 * 0.5 / sampleRate;
break; // approximate, can be adjusted
case 0x00:
gain = 1.2;
fastOmega = 6.283 * 0.9 / sampleRate;
break; // approximate, can be adjusted
}
}