chassis/plugin/voice.cpp

126 lines
3.4 KiB
C++
Raw Normal View History

2024-09-03 11:42:16 +00:00
/*
2024-09-03 15:17:32 +00:00
Chassis polysynth framework
2024-09-03 11:42:16 +00:00
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.
*/
2024-09-08 23:55:14 +00:00
// contains the actual sound generation code
2024-09-06 20:44:41 +00:00
2024-09-08 23:55:14 +00:00
#include "voice.hpp"
2024-09-03 14:31:54 +00:00
2024-09-08 23:55:14 +00:00
#include <math.h>
2024-09-06 20:44:41 +00:00
2024-09-09 12:34:02 +00:00
#include <cstdio>
2024-09-06 20:44:41 +00:00
static inline float poly3blep0(float t) {
float t2 = t * t;
2024-09-07 19:46:57 +00:00
return 2 * (t * t2 - 0.5f * t2 * t2);
2024-09-06 20:44:41 +00:00
}
static inline float poly3blep1(float t) {
return -poly3blep0(1 - t);
2024-09-05 22:18:41 +00:00
}
2024-09-03 22:41:01 +00:00
void Voice::run(Synth &s, float *buffer, uint32_t samples) {
float y, out, t;
2024-09-11 22:30:06 +00:00
2024-09-08 23:55:14 +00:00
float pw = s.ff4f / 32768.0f;
float fb, res = s.patchRam.vcfReso / 28.0f; // guess
2024-09-11 20:40:11 +00:00
float cut = 248.0f * (powf(2, (vcfenv - 0x1880) / 1143.0f));
// now radians
cut = 0.25 * 6.2832 * cut / 48000.0f;
// now correct
2024-09-11 20:40:11 +00:00
cut = cut / (1 + cut);
2024-09-09 15:31:43 +00:00
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);
2024-09-04 20:08:27 +00:00
2024-09-11 22:30:06 +00:00
float vcaEnv = (s.patchRam.switch2 & 0x04) ? (float)ff11 : (env / 16384.0f);
2024-09-08 19:57:11 +00:00
2024-09-06 20:44:41 +00:00
for (uint32_t i = 0; i < samples; i++) {
y = delay;
delay = 0;
phase += omega;
2024-09-06 19:04:56 +00:00
// if(pw > 1) pw = 1;
// if(pw < 0) pw = 0;
2024-09-06 20:44:41 +00:00
while (true) {
if (pulseStage == 0) {
if (phase < pw) break;
2024-09-08 23:55:14 +00:00
t = (phase - pw) / (lastpw - pw + omega);
y -= poly3blep0(t) * sqr;
delay -= poly3blep1(t) * sqr;
2024-09-06 20:44:41 +00:00
pulseStage = 1;
}
if (pulseStage == 1) {
if (phase < 1) break;
2024-09-09 19:40:16 +00:00
t = (phase - 1) / omega;
y += poly3blep0(t) * (saw + sqr);
delay += poly3blep1(t) * (saw + sqr);
2024-09-07 19:46:57 +00:00
y -= poly3blep0(t) * (sub * subosc);
delay -= poly3blep1(t) * (sub * subosc);
2024-09-07 19:46:57 +00:00
2024-09-06 20:44:41 +00:00
pulseStage = 0;
phase -= 1;
2024-09-07 19:46:57 +00:00
subosc = -subosc;
2024-09-06 20:44:41 +00:00
}
}
2024-09-06 19:04:56 +00:00
delay += saw * (1 - (2 * phase));
2024-09-04 20:08:27 +00:00
delay += sqr * (pulseStage ? -1.f : 1.f);
2024-09-04 20:08:27 +00:00
delay += sub * subosc;
2024-09-04 20:08:27 +00:00
2024-09-07 19:46:57 +00:00
// out = (2 * y) - 1;
out = y;
// widthDelay = pw;
2024-09-04 20:08:27 +00:00
2024-09-11 20:40:11 +00:00
out *= 0.707;
2024-09-09 15:31:43 +00:00
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;
}
2024-09-11 20:40:11 +00:00
vr58c106 += ((vcaEnv - vr58c106) * 0.005);
2024-09-08 23:55:14 +00:00
lastpw = pw;
2024-09-09 15:31:43 +00:00
2024-09-11 22:25:41 +00:00
out = b4 * (0.25);
2024-09-09 15:31:43 +00:00
2024-09-11 20:40:11 +00:00
buffer[i] += (gain * out * vr58c106);
}
}