peacock/plugin/voice.cpp

225 lines
6.4 KiB
C++

/*
Peacock-8 VA polysynth
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 <math.h>
#include <stdio.h>
#include "module.hpp"
#include "tables.hpp"
// antialiasing using polybleps, as described in KVRAudio forum by Mystran
static inline float poly3blep0(float t) {
float t2 = t * t;
return 2 * (t * t2 - 0.5f * t2 * t2);
}
static inline float poly3blep1(float t) {
return -poly3blep0(1 - t);
}
Voice::Voice() {
omega = 0.0;
theta = 0.0;
env = 0;
}
void Voice::on(uint8_t midiNote) {
while (midiNote < 24) midiNote += 12;
while (midiNote > 108) midiNote -=12;
note = midiNote - 24;
envPhase = 1;
}
void Voice::off() {
envPhase = 0;
}
// tanh(x)/x approximation, flatline at very high inputs
// so might not be safe for very large feedback gains
// [limit is 1/15 so very large means ~15 or +23dB]
double tanhXdX(double x) {
return 1-0.1*abs(x);
double a = x*x;
// IIRC I got this as Pade-approx for tanh(sqrt(x))/sqrt(x)
return ((a + 105)*a + 945) / ((15*a + 420)*a + 945);
}
void Voice::run(Module* m, float* buffer, uint32_t samples) {
// carry out per-voice calculations for each block of samples
float out, t, fb;
double zi;
// calculate cutoff frequency
float cut = 248.0f * (powf(2, (vcfCut - 0x1880) / 1143.0f));
cut = M_PI * cut / sampleRate;
cut = cut / (1 + cut); // correct tuning warp
// printf("%f\n", cut);
//if (cut > 0.5) cut = 0.5;
// double f = tan(cut);
//printf("cut = %4f f = %4f\n", cut, f);
double r = (40.0/9.0) * m->res;
float amp = vcaEnv / 4096.0f;
for (uint32_t i = 0; i < samples; i++) {
out = delay;
delay = 0;
theta += omega;
while (true) {
if (pulseStage == 0) {
if (theta < m->pwmBuf[i]) break;
t = (theta - m->pwmBuf[i]) / (lastpw - m->pwmBuf[i] + omega);
out -= poly3blep0(t) * m->square;
delay -= poly3blep1(t) * m->square;
pulseStage = 1;
}
if (pulseStage == 1) {
if (theta < 1) break; // no need to blep yet
t = (theta - 1) / omega; // scaled remainder of phase
out += poly3blep0(t) * (m->saw + m->square);
delay += poly3blep1(t) * (m->saw + m->square);
out -= poly3blep0(t) * (m->subBuf[i] * subosc);
delay -= poly3blep1(t) * (m->subBuf[i] * subosc);
pulseStage = 0;
subosc = -subosc;
theta -= 1;
}
}
// FIXME DC offset removal
delay += m->saw * (1 - (2 * theta));
delay += m->square * ((pulseStage ? -1.f : 1.f) - m->pwmBuf[i] + 0.5);
delay += m->subBuf[i] * subosc ;
out += m->noise * (0.8 - 1.6 * (rand() & 0xffff) / 65536.0);
out *= 0.01;
// same time constant for both VCF and VCF RC circuits
vcfRC = (cut - vcfRC) * m->vcaTC + vcfRC;
#if 1
//// LICENSE TERMS: Copyright 2012 Teemu Voipio
//
// You can use this however you like for pretty much any purpose,
// as long as you don't claim you wrote it. There is no warranty.
//
// Distribution of substantial portions of this code in source form
// must include this copyright notice and list of conditions.
//
// input delay and state for member variables
// cutoff as normalized frequency (eg 0.5 = Nyquist)
// resonance from 0 to 1, self-oscillates at settings over 0.9
//void transistorLadder(
// double cutoff, double resonance,
// double * in, double * out, unsigned nsamples)
//{
// tuning and feedback
//------------------------------------------------------------------------------ sample loop
//for(unsigned n = 0; n < nsamples; ++n)
//{
// input with half delay, for non-linearities
double ih = 0.5 * (out + zi); zi = out;
//double ih = out;
// evaluate the non-linear gains
double t0 = tanhXdX((ih * (r+1))- r * s[3]);
double t1 = tanhXdX(s[0]);
double t2 = tanhXdX(s[1]);
double t3 = tanhXdX(s[2]);
double t4 = tanhXdX(s[3]);
double f = vcfRC;
// g# the denominators for solutions of individual stages
double g0 = 1 / (1 + f*t1), g1 = 1 / (1 + f*t2);
double g2 = 1 / (1 + f*t3), g3 = 1 / (1 + f*t4);
// f# are just factored out of the feedback solution
double f3 = f*t3*g3, f2 = f*t2*g2*f3, f1 = f*t1*g1*f2, f0 = f*t0*g0*f1;
// solve feedback
double y3 = (g3*s[3] + f3*g2*s[2] + f2*g1*s[1] + f1*g0*s[0] + f0*out) / (1 + r*f0);
// then solve the remaining outputs (with the non-linear gains here)
double xx = t0*((out * (r+1)) - r*y3);
double y0 = t1*g0*(s[0] + f*xx);
double y1 = t2*g1*(s[1] + f*y0);
double y2 = t3*g2*(s[2] + f*y1);
// update state
s[0] += 2*f * (xx - y0);
s[1] += 2*f * (y0 - y1);
s[2] += 2*f * (y1 - y2);
s[3] += 2*f * (y2 - t4*y3);
//out[n] = y3;
// }
#else
for (uint8_t ovs = 0; ovs < 4; ovs++) {
fb = y3;
// hard clip
fb = ((out * 0.5) - fb) * m->res;
if (fb > 4) fb = 4;
if (fb < -4) fb = -4;
// fb = 1.5 * fb - 0.5 * fb * fb * fb;
//
y0 = ((out + fb - y0) * vcfRC) + y0;
y1 = ((y0 - y1) * vcfRC) + y1;
y2 = ((y1 - y2) * vcfRC) + y2;
y3 = ((y2 - y3) * vcfRC) + y3;
}
#endif
vcaRC = (amp - vcaRC) * m->vcaTC + vcaRC;
buffer[i] += 1 * m->vcaBuf[i] * vcaRC * y3;
lastpw = m->pwmBuf[i];
}
// buffer[0] += 1;
}