chassis/plugin/voice.cpp
Gordon JC Pearce 3636ae356a better scaling
2024-09-14 16:59:02 +01:00

130 lines
3.5 KiB
C++

/*
Chassis polysynth framework
Copyright 2024 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.
*/
// contains the actual sound generation code
#include "voice.hpp"
#include <math.h>
#include <cstdio>
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);
}
void Voice::run(Synth &s, float *buffer, uint32_t samples) {
float y, out, t;
float pw = s.ff4f / 32768.0f;
float fb, res = s.patchRam.vcfReso / 28.0f; // guess
float cut = 248.0f * (powf(2, (vcfenv - 0x1880) / 1143.0f));
// now radians
cut = 0.25 * 6.2832 * cut / 48000.0f;
// now correct
cut = cut / (1 + cut);
float sqr = (s.patchRam.switch1 & 0x08) ? 0.65 : 0; //? 0.175 : 0;
float saw = (s.patchRam.switch1 & 0x10) ? 0.83 : 0; //? 0.220 : 0;
float sub = (s.patchRam.sub / 127.0f); // * 0.275;
// noise is 0.8
float gain = 0.5*powf(2, (4*s.patchRam.vca / 127.0f) - 1.0f);
printf("%f %f\n",s.patchRam.vca / 127.0f, gain);
float vcaEnv = (s.patchRam.switch2 & 0x04) ? (float)ff11 : (env / 16384.0f);
for (uint32_t i = 0; i < samples; i++) {
y = delay;
delay = 0;
phase += omega;
// if(pw > 1) pw = 1;
// if(pw < 0) pw = 0;
while (true) {
if (pulseStage == 0) {
if (phase < pw) break;
t = (phase - pw) / (lastpw - pw + omega);
y -= poly3blep0(t) * sqr;
delay -= poly3blep1(t) * sqr;
pulseStage = 1;
}
if (pulseStage == 1) {
if (phase < 1) break;
t = (phase - 1) / omega;
y += poly3blep0(t) * (saw + sqr);
delay += poly3blep1(t) * (saw + sqr);
y -= poly3blep0(t) * (sub * subosc);
delay -= poly3blep1(t) * (sub * subosc);
pulseStage = 0;
phase -= 1;
subosc = -subosc;
}
}
delay += saw * (1 - (2 * phase));
delay += sqr * (pulseStage ? -1.f : 1.f);
delay += sub * subosc;
// out = (2 * y) - 1;
out = y;
// widthDelay = pw;
out *= 0.707;
for (uint8_t ovs = 0; ovs < 4; ovs++) {
fb = b4;
// hard clip
if (fb > 1) fb = 1;
if (fb < -1) fb = -1;
fb = out - (fb * res);
b1 = ((fb - b1) * cut) + b1;
b2 = ((b1 - b2) * cut) + b2;
b3 = ((b2 - b3) * cut) + b3;
b4 = ((b3 - b4) * cut) + b4;
}
vr58c106 += ((vcaEnv - vr58c106) * 0.005);
lastpw = pw;
// gain for fully resonant needs to be 128mV output
out = b4 * (0.625);
buffer[i] += (gain * out * vr58c106);
}
}