chassis/plugin/voice.cpp

176 lines
4.2 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.
*/
#include "voice.hpp"
2024-09-03 15:17:32 +00:00
#include <math.h>
2024-09-06 20:44:41 +00:00
2024-09-06 19:04:56 +00:00
#include <cstdio>
2024-09-03 14:31:54 +00:00
2024-09-06 19:04:56 +00:00
static float blep(float p, float theta) {
2024-09-03 14:31:54 +00:00
float t;
2024-09-06 19:04:56 +00:00
// low (late) side of step
if (p < theta) {
2024-09-06 20:44:41 +00:00
printf("phase < theta, %f %f\n", p, theta);
2024-09-06 19:04:56 +00:00
t = p / theta;
2024-09-03 14:31:54 +00:00
return (2 * t) - (t * t) - 1;
}
2024-09-06 19:04:56 +00:00
// high (early) side of step
if (p > (1 - theta)) {
2024-09-06 20:44:41 +00:00
printf("phase > 1-theta %f %f \n", p, theta);
2024-09-06 19:04:56 +00:00
t = (p - 1) / theta;
2024-09-03 14:31:54 +00:00
return (2 * t) + (t * t) + 1;
}
2024-09-06 20:44:41 +00:00
// printf("no action\n");
2024-09-03 14:31:54 +00:00
return 0;
}
bool Voice::isFree() {
return keyState == K_OFF;
}
void Voice::on(uint32_t key, bool reset = 0) {
2024-09-05 22:18:41 +00:00
keyState = K_WAIT;
// envState = ATTACK;
note = key;
if (reset) env = 0;
omega = (261.63 * powf(2, (note - 60) / 12.0f)) / 48000.0f;
target = 1;
2024-09-06 19:04:56 +00:00
env = 1;
}
void Voice::off() {
keyState = K_OFF;
2024-09-06 20:44:41 +00:00
envState = RELEASE;
target = 0;
}
2024-09-05 22:18:41 +00:00
void Voice::gate() {
2024-09-06 19:04:56 +00:00
/*
2024-09-05 22:18:41 +00:00
if (keyState == K_WAIT) {
envState = ATTACK;
keyState = K_ON;
target = 1;
}
if (keyState == K_OFF) {
envState = RELEASE;
target = 0;
}
2024-09-06 19:04:56 +00:00
*/
env = ((target - env) * 0.005f) + env;
2024-09-06 20:44:41 +00:00
if (env < 0.001) env = 0;
}
static inline float poly3blep0(float t) {
// these are just sanity checks
// correct code doesn't need them
if (t < 0) return 0;
if (t > 1) return 1;
float t2 = t * t;
return t * t2 - 0.5f * t2 * t2;
}
// And second sample as wrapper, optimize if you want.
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-06 20:44:41 +00:00
float y, out, pw = 0, t;
2024-09-04 20:08:27 +00:00
2024-09-05 22:18:41 +00:00
s.p.sqr = 1;
s.p.saw = 1;
2024-09-04 20:08:27 +00:00
2024-09-06 20:44:41 +00:00
float mix = 1;
for (uint32_t i = 0; i < samples; i++) {
#if 1
y = delay;
delay = 0;
phase += omega;
2024-09-06 19:04:56 +00:00
2024-09-06 20:44:41 +00:00
float pulseWidth = 0.5; // pwm[i];
// if(pulseWidth > 1) pulseWidth = 1;
// if(pulseWidth < 0) pulseWidth = 0;
while (true) {
if (pulseStage == 0) {
if (phase < pulseWidth) break;
#if 1
float t = (phase - pulseWidth) / omega;
#else
float t = (phase - pulseWidth) / (widthDelay - pulseWidth + freq);
#endif
y += mix * poly3blep0(t);
delay += mix * poly3blep1(t);
pulseStage = 1;
}
if (pulseStage == 1) {
if (phase < 1) break;
float t = (phase - 1) / omega;
y -= poly3blep0(t);
delay -= poly3blep1(t);
pulseStage = 0;
phase -= 1;
subosc = (1-subosc);
}
}
delay += (1 - mix) * phase + mix * (pulseStage ? 1.f : 0.f);
delay += subosc;
2024-09-06 19:04:56 +00:00
2024-09-06 20:44:41 +00:00
out = (2 * y) - 1;
// widthDelay = pulseWidth;
#else
2024-09-04 20:08:27 +00:00
// sawtooth
2024-09-06 20:44:41 +00:00
2024-09-04 20:08:27 +00:00
y = 1 - (2 * phase);
2024-09-06 20:44:41 +00:00
y += blep(phase, omega);
2024-09-04 20:08:27 +00:00
// need this if either saw or square is on;
y *= (s.p.saw + s.p.sqr);
// bodged for 16ms 48000Hz
2024-09-05 22:18:41 +00:00
// pw_rc = ((pw - pw_rc) * 0.000625) + pw_rc;
pw_rc = 0.5;
2024-09-04 20:08:27 +00:00
// phase shifted saw
offset = phase + pw_rc;
if (offset > 1) offset -= 1;
y -= (1 - (2 * offset)) * s.p.sqr;
2024-09-06 20:44:41 +00:00
y -= blep(offset, omega) * s.p.sqr;
2024-09-05 22:18:41 +00:00
// s.lastpw = pw_rc;
2024-09-04 20:08:27 +00:00
phase += omega;
2024-09-06 19:04:56 +00:00
if (phase > 1) {
2024-09-06 20:44:41 +00:00
// printf("step\n");
2024-09-06 19:04:56 +00:00
phase -= 1;
}
2024-09-06 20:44:41 +00:00
#endif
buffer[i] += (0.25 * out * env);
}
}