chassis/plugin/voice.cpp
2024-09-11 23:25:41 +01:00

131 lines
3.7 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;
// 325, because it needs PW to be from 0 to 0.5
// but really the control range is limited from 0 to 100
// there's a resistor on the panel board to sprag the range
float pw = s.ff4f / 32768.0f;
float fb, res = s.patchRam.vcfReso / 28.0f; // guess
// float cut = s.patchRam.vcfFreq / 400.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.63 : 0; //? 0.175 : 0;
float saw = (s.patchRam.switch1 & 0x10) ? 0.8 : 0; //? 0.220 : 0;
float sub = (s.patchRam.sub / 127.0f); // * 0.275;
float gain = 0.5 * powf(2, (s.patchRam.vca / 64.0f) - 1);
//printf("patchram.switches= %3d %3d\n", s.patchRam.switch1, s.patchRam.switch2);
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;
out = b4 * (0.25);
buffer[i] += (gain * out * vr58c106);
}
}