chassis/plugin/voice.cpp

200 lines
4.6 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
bool Voice::isFree() {
2024-09-08 00:08:00 +00:00
return ff10 == false;
}
void Voice::on(uint32_t key, bool reset = 0) {
2024-09-08 00:08:00 +00:00
//printf("======================================================================================\n");
ff10 = true;
ff07 = true;
if (note == key) goto h0144;
note = key;
2024-09-08 00:08:00 +00:00
if (ff11) goto h013e;
h0132:
if (ff33) goto h0149;
ff33 = false;
goto h0149;
h013e:
ff00 = true;
goto h0149;
h0144:
if (!ff11) goto h0132;
h0149:
//printf("after 0144h, %d %x %x %x %x\n", note, ff07, ff10, ff11, ff33);
omega = (261.63 * powf(2, (note - 60) / 12.0f)) / 48000.0f;
}
void Voice::off() {
2024-09-08 00:08:00 +00:00
bool sustain = false;
ff10 = false;
if (!sustain) { // dummy sustain
ff33 = false;
}
//printf("after note off, %d %x %x %x %x\n", note, ff07, ff10, ff11, ff33);
}
2024-09-08 00:08:00 +00:00
void Voice::gate(Synth &s) {
uint16_t bc, ea=env;
ff11 = ff10;
// 0509
if (!ff11) goto h0538;
// 050e
if (!ff33) goto h0563;
// 0513
if (!ff07) goto h051e;
// 0517
h0517:
//printf("got to 0517\n");
ff07 = false;
h051e:
//printf("got to 051e\n");
bc = s.patch.sustain; // half scale
if (ea < bc) ea = bc;
ea -= bc;
bc = ea;
ea = (ea * s.patch.decay) >> 16;
ea += s.patch.sustain;
//printf("returning from decay phase\n");
goto h0590;
h0538:
//printf("got to 0x0538\n");
if (!ff07) goto h054a; // note on? if not skip ahead
// 053c
if (ff08) goto h0517;
ff07 = false;
h054a:
//printf("release phase\n");
ff33 = false;
ff08 = false;
bc = ea;
ea = (ea*s.patch.release)>>16;
// printf("returning from release phase\n");
goto h0590;
h0563:
// printf("attack phase\n");
ff08 = false;
ea += s.patch.attack;
if (ea & 0xc000) {
ea = 0x3fff;
ff33 = true;
ff08 = true;
}
h0590:
env = ea;
//printf("%04x %d %d %d %d %d \n", ea, ff07, ff08, ff10, ff11, ff33);
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-06 20:44:41 +00:00
float y, out, pw = 0, t;
2024-09-04 20:08:27 +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-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
2024-09-07 19:46:57 +00:00
y -= poly3blep0(t) * s.patch.sqr;
delay -= poly3blep1(t) * s.patch.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-07 19:46:57 +00:00
y += poly3blep0(t) * (s.patch.saw + s.patch.sqr);
delay += poly3blep1(t) * (s.patch.saw + s.patch.sqr);
y -= poly3blep0(t) * (s.patch.sub * subosc);
delay -= poly3blep1(t) * (s.patch.sub * subosc);
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-07 19:46:57 +00:00
delay += s.patch.saw * (1 - (2 * phase));
2024-09-04 20:08:27 +00:00
2024-09-07 19:46:57 +00:00
delay += s.patch.sqr * (pulseStage ? -1.f : 1.f);
2024-09-04 20:08:27 +00:00
2024-09-07 19:46:57 +00:00
delay += s.patch.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 = pulseWidth;
2024-09-04 20:08:27 +00:00
2024-09-08 00:08:00 +00:00
vr58c106 += (((env/16383.0) - vr58c106) * 0.0075);
2024-09-07 19:46:57 +00:00
buffer[i] += (0.25 * out * vr58c106);
}
}
2024-09-08 00:08:00 +00:00