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-03 14:22:56 +00:00
|
|
|
|
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-06 19:04:56 +00:00
|
|
|
#include <cstdio>
|
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
|
|
|
|
|
|
|
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) {
|
2024-09-08 20:50:42 +00:00
|
|
|
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
|
2024-09-08 23:55:14 +00:00
|
|
|
float pw = s.ff4f / 32768.0f;
|
2024-09-08 19:48:10 +00:00
|
|
|
|
2024-09-08 23:55:14 +00:00
|
|
|
|
|
|
|
float sqr = 0.175;
|
2024-09-08 19:48:10 +00:00
|
|
|
float saw = (s.patchRam.switch1 & 0x10) ? 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-08 23:55:14 +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
|
|
|
|
2024-09-08 19:48:10 +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) {
|
2024-09-08 19:48:10 +00:00
|
|
|
if (phase < pw) break;
|
2024-09-08 23:55:14 +00:00
|
|
|
#if 0
|
2024-09-08 19:48:10 +00:00
|
|
|
t = (phase - pw) / omega;
|
2024-09-06 20:44:41 +00:00
|
|
|
#else
|
2024-09-08 23:55:14 +00:00
|
|
|
t = (phase - pw) / (lastpw - pw + omega);
|
2024-09-06 20:44:41 +00:00
|
|
|
#endif
|
2024-09-08 19:48:10 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
float t = (phase - 1) / omega;
|
2024-09-08 19:48:10 +00:00
|
|
|
y += poly3blep0(t) * (saw + sqr);
|
|
|
|
delay += poly3blep1(t) * (saw + sqr);
|
2024-09-07 19:46:57 +00:00
|
|
|
|
2024-09-08 19:48:10 +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
|
|
|
|
2024-09-08 19:48:10 +00:00
|
|
|
delay += saw * (1 - (2 * phase));
|
2024-09-04 20:08:27 +00:00
|
|
|
|
2024-09-08 19:48:10 +00:00
|
|
|
delay += sqr * (pulseStage ? -1.f : 1.f);
|
2024-09-04 20:08:27 +00:00
|
|
|
|
2024-09-08 19:48:10 +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;
|
2024-09-08 19:48:10 +00:00
|
|
|
// widthDelay = pw;
|
2024-09-04 20:08:27 +00:00
|
|
|
|
2024-09-08 23:55:14 +00:00
|
|
|
vr58c106 += ((vcaEnv - vr58c106) * 0.0075);
|
|
|
|
lastpw = pw;
|
2024-09-08 19:48:10 +00:00
|
|
|
buffer[i] += (gain * out * vr58c106);
|
2024-09-03 14:22:56 +00:00
|
|
|
}
|
|
|
|
}
|