chassis/plugin/digital.cpp
Gordon JC Pearce 1d4e6ab2bd PWM LFO working
2024-09-09 00:55:14 +01:00

186 lines
4.0 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 emulation of the digital bits
#include <cmath>
#include <cstdio>
#include "voice.hpp"
bool Voice::isFree() {
return ff10 == false;
}
void Voice::on(uint32_t key, bool reset = 0) {
// what's with the crazy private variables and all the gotos with crazy labels?
// this code emulates the 78C11 code directly (probably inefficiently)
// to allow for documenting what the variables actually do
// they're really bitfields holding a bit for each voice
// this current implementation doesn't reset the voice
(void)reset;
ff10 = true; // note held from keyboard
ff07 = true; // attack phase
if (note == key) goto h0144;
note = key;
if (ff11) goto h013e;
h0132:
if (ff33) goto h0149; // sustained
ff33 = false;
goto h0149;
h013e:
ff00 = true; // in a real one, voice counter needs programmed
goto h0149;
h0144:
if (!ff11) goto h0132; // unsure, copied from ff10 at start of mainloop
h0149:
// this is in the wrong place really but is the equivalent of programming the counter
// and VCO ramp DAC
omega = (261.63 * powf(2, (note - 60) / 12.0f)) / 48000.0f;
}
void Voice::off() {
bool sustain = false;
ff10 = false;
if (!sustain) { // dummy sustain
ff33 = false;
}
}
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;
h0517:
ff07 = false;
h051e:
bc = s.patchRam.env_s << 7; // half scale
if (ea < bc) ea = bc;
ea -= bc;
bc = ea;
ea = (ea * decay_table[s.patchRam.env_d]) >> 16;
ea += s.patchRam.env_s << 7;
// 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 * decay_table[s.patchRam.env_r]) >> 16;
// printf("returning from release phase\n");
goto h0590;
h0563:
// printf("attack phase\n");
ff08 = false;
ea += attack_table[s.patchRam.env_a];
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);
}
void Synth::runLFO() {
// compute a loop's worth of LFO
uint16_t a, b, c, d;
uint16_t bc, hl, ea, tos;
// 074e
ea = ff4d; // lfo value
bc = lfoRateTable[patchRam.lfoRate];
if (!(ff4a & 0x01)) goto h078b;
ea -= bc;
if (ea < bc) goto h079a;
h075f:
ff4d = ea;
if (!(ff4a & 0x02)) goto h07a2;
// 0765
bc = ea;
ea = 0x2000;
ea -= bc;
h076b:
bc = ea;
if (patchRam.switch2 & 0x01) {
bc = 0x3fff;
}
bc = (bc * patchRam.pwmLfo) >> 7;
bc = 0x3fff - bc;
if (!(patchRam.switch1 & 0x08)) bc = 0x0000; // square off
ff4f = bc;
// 078a
goto h07a9;
h078b:
ea += bc;
if (ea & 0xe000) {
ea = 0x1fff;
ff4a++;
}
goto h075f;
h079a:
ea = 0;
ff4a++;
goto h075f;
h07a2:
ea += 0x2000;
goto h076b;
h07a9:
return;
}