PWM LFO working

This commit is contained in:
Gordon JC Pearce 2024-09-09 00:55:14 +01:00
parent 1f6188f751
commit 1d4e6ab2bd
5 changed files with 169 additions and 65 deletions

View File

@ -121,6 +121,7 @@ void Chassis::run(const float **, float **outputs, uint32_t frames, const MidiEv
doMidi(midiEvents, midiEventCount, framePos + s.blockLeft); doMidi(midiEvents, midiEventCount, framePos + s.blockLeft);
// printf("compute params and reset block size\n"); // printf("compute params and reset block size\n");
s.runLFO();
for (uint32_t i = 0; i < NUM_VOICES; i++) { for (uint32_t i = 0; i < NUM_VOICES; i++) {
s.voice[i].gate(s); s.voice[i].gate(s);

View File

@ -1,14 +1,34 @@
#include "voice.hpp" /*
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 <cmath>
#include <cstdio>
#include "voice.hpp"
bool Voice::isFree() { bool Voice::isFree() {
return ff10 == false; return ff10 == false;
} }
void Voice::on(uint32_t key, bool reset = 0) { void Voice::on(uint32_t key, bool reset = 0) {
// printf("======================================================================================\n");
// what's with the crazy private variables and all the gotos with crazy labels? // what's with the crazy private variables and all the gotos with crazy labels?
// this code emulates the 78C11 code directly (probably inefficiently) // this code emulates the 78C11 code directly (probably inefficiently)
// to allow for documenting what the variables actually do // to allow for documenting what the variables actually do
@ -35,8 +55,6 @@ h0144:
if (!ff11) goto h0132; // unsure, copied from ff10 at start of mainloop if (!ff11) goto h0132; // unsure, copied from ff10 at start of mainloop
h0149: h0149:
// printf("after 0144h, %d %x %x %x %x\n", note, ff07, ff10, ff11, ff33);
// this is in the wrong place really but is the equivalent of programming the counter // this is in the wrong place really but is the equivalent of programming the counter
// and VCO ramp DAC // and VCO ramp DAC
omega = (261.63 * powf(2, (note - 60) / 12.0f)) / 48000.0f; omega = (261.63 * powf(2, (note - 60) / 12.0f)) / 48000.0f;
@ -48,12 +66,8 @@ void Voice::off() {
if (!sustain) { // dummy sustain if (!sustain) { // dummy sustain
ff33 = false; ff33 = false;
} }
// printf("after note off, %d %x %x %x %x\n", note, ff07, ff10, ff11, ff33);
} }
void Voice::gate(Synth &s) { void Voice::gate(Synth &s) {
uint16_t bc, ea = env; uint16_t bc, ea = env;
@ -113,3 +127,60 @@ h0590:
env = ea; env = ea;
// printf("%04x %d %d %d %d %d \n", ea, ff07, ff08, ff10, ff11, ff33); // 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;
}

View File

@ -265,7 +265,7 @@ void Chassis::setParameterValue(uint32_t index, float value) {
s.patchRam.vcoLfo = value; s.patchRam.vcoLfo = value;
break; break;
case paramPWMLFO: case paramPWMLFO:
s.patchRam.pwmLfo = value; s.patchRam.pwmLfo = value/1.27;
break; break;
case paramSub: case paramSub:
s.patchRam.sub = value; s.patchRam.sub = value;

View File

@ -16,13 +16,12 @@
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
// contains the actual sound generation code
#include "voice.hpp" #include "voice.hpp"
#include <math.h>
#include <cstdio> #include <cstdio>
#include <math.h>
static inline float poly3blep0(float t) { static inline float poly3blep0(float t) {
float t2 = t * t; float t2 = t * t;
@ -39,15 +38,16 @@ void Voice::run(Synth &s, float *buffer, uint32_t samples) {
// 325, because it needs PW to be from 0 to 0.5 // 325, because it needs PW to be from 0 to 0.5
// but really the control range is limited from 0 to 100 // but really the control range is limited from 0 to 100
// there's a resistor on the panel board to sprag the range // there's a resistor on the panel board to sprag the range
float pw = 0.5 - s.patchRam.pwmLfo/325.0f; float pw = s.ff4f / 32768.0f;
float sqr = (s.patchRam.switch1 & 0x08) ? 0.175 : 0;
float sqr = 0.175;
float saw = (s.patchRam.switch1 & 0x10) ? 0.220 : 0; float saw = (s.patchRam.switch1 & 0x10) ? 0.220 : 0;
float sub = (s.patchRam.sub / 127.0f) * 0.275; float sub = (s.patchRam.sub / 127.0f) * 0.275;
float gain = 0.5 * powf(2, (s.patchRam.vca / 64.0f) - 1); float gain = 0.5 * powf(2, (s.patchRam.vca / 64.0f) - 1);
float vcaEnv = (s.patchRam.switch2 & 0x04)?(float)ff11:(env / 16384.0f); float vcaEnv = (s.patchRam.switch2 & 0x04) ? (float)ff11 : (env / 16384.0f);
for (uint32_t i = 0; i < samples; i++) { for (uint32_t i = 0; i < samples; i++) {
y = delay; y = delay;
@ -61,10 +61,10 @@ void Voice::run(Synth &s, float *buffer, uint32_t samples) {
while (true) { while (true) {
if (pulseStage == 0) { if (pulseStage == 0) {
if (phase < pw) break; if (phase < pw) break;
#if 1 #if 0
t = (phase - pw) / omega; t = (phase - pw) / omega;
#else #else
t = (phase - pw) / (widthDelay - pw + freq); t = (phase - pw) / (lastpw - pw + omega);
#endif #endif
y -= poly3blep0(t) * sqr; y -= poly3blep0(t) * sqr;
delay -= poly3blep1(t) * sqr; delay -= poly3blep1(t) * sqr;
@ -97,7 +97,8 @@ void Voice::run(Synth &s, float *buffer, uint32_t samples) {
out = y; out = y;
// widthDelay = pw; // widthDelay = pw;
vr58c106 += ((vcaEnv- vr58c106) * 0.0075); vr58c106 += ((vcaEnv - vr58c106) * 0.0075);
lastpw = pw;
buffer[i] += (gain * out * vr58c106); buffer[i] += (gain * out * vr58c106);
} }
} }

View File

@ -24,12 +24,6 @@
class Synth; class Synth;
class Patch {
public:
float saw, sqr, sub;
uint16_t attack, decay, sustain, release;
};
class Voice { class Voice {
public: public:
// uint8_t note = 72; // uint8_t note = 72;
@ -55,11 +49,11 @@ class Voice {
K_ON, K_ON,
K_SUSTAIN } keyState = K_OFF; K_SUSTAIN } keyState = K_OFF;
bool ff00 = 0; // reset bit bool ff00 = 0; // reset DCO clock flag
bool ff07 = 0; // status bit bool ff07 = 0; // set to indicate attack phase
bool ff08 = 0; // set to indicate attack phase complete bool ff08 = 0; // set to indicate decay/sustain phase
bool ff10 = 0; // note on bit bool ff10 = 0; // note on bit
bool ff11 = 0; // sustain flag bool ff11 = 0; // drives the "gate" signal for VCA
bool ff33 = 0; // releasing flag bool ff33 = 0; // releasing flag
uint16_t env; uint16_t env;
@ -69,52 +63,60 @@ class Voice {
float delay; float delay;
uint8_t pulseStage = 0; uint8_t pulseStage = 0;
float pw_rc = 0; float lastpw = 0;
float vr58c106 = 0; float vr58c106 = 0;
uint16_t attack_table[128] = { uint16_t attack_table[128] = {
0x4000, 0x2000, 0x1000, 0x0aaa, 0x0800, 0x0666, 0x0555, 0x0492, 0x0400, 0x4000, 0x2000, 0x1000, 0x0aaa, 0x0800, 0x0666, 0x0555, 0x0492, 0x0400,
0x038e, 0x0333, 0x02e9, 0x02ab, 0x0276, 0x0249, 0x0222, 0x0200, 0x01e2, 0x038e, 0x0333, 0x02e9, 0x02ab, 0x0276, 0x0249, 0x0222, 0x0200, 0x01e2,
0x01c7, 0x01af, 0x0199, 0x0186, 0x0174, 0x0164, 0x0155, 0x0148, 0x013b, 0x01c7, 0x01af, 0x0199, 0x0186, 0x0174, 0x0164, 0x0155, 0x0148, 0x013b,
0x012f, 0x0124, 0x011a, 0x0111, 0x0108, 0x0100, 0x00f8, 0x00f1, 0x00ea, 0x012f, 0x0124, 0x011a, 0x0111, 0x0108, 0x0100, 0x00f8, 0x00f1, 0x00ea,
0x00e4, 0x00dd, 0x00d8, 0x00d2, 0x00cd, 0x00c8, 0x00c3, 0x00bf, 0x00ba, 0x00e4, 0x00dd, 0x00d8, 0x00d2, 0x00cd, 0x00c8, 0x00c3, 0x00bf, 0x00ba,
0x00b6, 0x00b2, 0x00ae, 0x00ab, 0x00a7, 0x00a4, 0x00a1, 0x009e, 0x009b, 0x00b6, 0x00b2, 0x00ae, 0x00ab, 0x00a7, 0x00a4, 0x00a1, 0x009e, 0x009b,
0x0098, 0x0095, 0x0092, 0x0090, 0x008d, 0x008b, 0x0089, 0x0086, 0x0084, 0x0098, 0x0095, 0x0092, 0x0090, 0x008d, 0x008b, 0x0089, 0x0086, 0x0084,
0x0082, 0x007f, 0x007d, 0x007a, 0x0077, 0x0074, 0x0072, 0x006f, 0x006c, 0x0082, 0x007f, 0x007d, 0x007a, 0x0077, 0x0074, 0x0072, 0x006f, 0x006c,
0x0069, 0x0067, 0x0064, 0x0061, 0x005e, 0x005c, 0x0059, 0x0056, 0x0053, 0x0069, 0x0067, 0x0064, 0x0061, 0x005e, 0x005c, 0x0059, 0x0056, 0x0053,
0x0050, 0x004e, 0x004b, 0x0048, 0x0045, 0x0042, 0x0040, 0x003f, 0x003d, 0x0050, 0x004e, 0x004b, 0x0048, 0x0045, 0x0042, 0x0040, 0x003f, 0x003d,
0x003c, 0x003a, 0x0039, 0x0037, 0x0036, 0x0034, 0x0033, 0x0031, 0x0030, 0x003c, 0x003a, 0x0039, 0x0037, 0x0036, 0x0034, 0x0033, 0x0031, 0x0030,
0x002e, 0x002d, 0x002b, 0x002a, 0x0028, 0x0027, 0x0025, 0x0024, 0x0022, 0x002e, 0x002d, 0x002b, 0x002a, 0x0028, 0x0027, 0x0025, 0x0024, 0x0022,
0x0021, 0x0021, 0x0020, 0x0020, 0x001f, 0x001f, 0x001e, 0x001e, 0x001d, 0x0021, 0x0021, 0x0020, 0x0020, 0x001f, 0x001f, 0x001e, 0x001e, 0x001d,
0x001d, 0x001c, 0x001c, 0x001b, 0x001b, 0x001a, 0x0019, 0x0018, 0x0017, 0x001d, 0x001c, 0x001c, 0x001b, 0x001b, 0x001a, 0x0019, 0x0018, 0x0017,
0x0016, 0x0015}; 0x0016, 0x0015};
uint16_t decay_table[128] = { uint16_t decay_table[128] = {
0x1000, 0x3000, 0x5000, 0x7000, 0x9000, 0xa000, 0xa800, 0xb000, 0xb800, 0x1000, 0x3000, 0x5000, 0x7000, 0x9000, 0xa000, 0xa800, 0xb000, 0xb800,
0xc000, 0xc800, 0xd000, 0xd800, 0xe000, 0xe800, 0xf000, 0xf080, 0xf100, 0xc000, 0xc800, 0xd000, 0xd800, 0xe000, 0xe800, 0xf000, 0xf080, 0xf100,
0xf180, 0xf200, 0xf280, 0xf300, 0xf380, 0xf400, 0xf480, 0xf500, 0xf580, 0xf180, 0xf200, 0xf280, 0xf300, 0xf380, 0xf400, 0xf480, 0xf500, 0xf580,
0xf600, 0xf680, 0xf700, 0xf780, 0xf800, 0xf880, 0xf900, 0xf980, 0xfa00, 0xf600, 0xf680, 0xf700, 0xf780, 0xf800, 0xf880, 0xf900, 0xf980, 0xfa00,
0xfa80, 0xfb00, 0xfb80, 0xfc00, 0xfc80, 0xfd00, 0xfd80, 0xfe00, 0xfe0c, 0xfa80, 0xfb00, 0xfb80, 0xfc00, 0xfc80, 0xfd00, 0xfd80, 0xfe00, 0xfe0c,
0xfe18, 0xfe24, 0xfe30, 0xfe3c, 0xfe48, 0xfe54, 0xfe60, 0xfe6c, 0xfe78, 0xfe18, 0xfe24, 0xfe30, 0xfe3c, 0xfe48, 0xfe54, 0xfe60, 0xfe6c, 0xfe78,
0xfe84, 0xfe90, 0xfe9c, 0xfea8, 0xfeb4, 0xfec0, 0xfecc, 0xfed8, 0xfee4, 0xfe84, 0xfe90, 0xfe9c, 0xfea8, 0xfeb4, 0xfec0, 0xfecc, 0xfed8, 0xfee4,
0xfef0, 0xfefc, 0xff08, 0xff0c, 0xff10, 0xff14, 0xff18, 0xff1c, 0xff20, 0xfef0, 0xfefc, 0xff08, 0xff0c, 0xff10, 0xff14, 0xff18, 0xff1c, 0xff20,
0xff24, 0xff28, 0xff2c, 0xff30, 0xff34, 0xff38, 0xff3c, 0xff40, 0xff44, 0xff24, 0xff28, 0xff2c, 0xff30, 0xff34, 0xff38, 0xff3c, 0xff40, 0xff44,
0xff48, 0xff4c, 0xff50, 0xff54, 0xff58, 0xff5c, 0xff60, 0xff64, 0xff68, 0xff48, 0xff4c, 0xff50, 0xff54, 0xff58, 0xff5c, 0xff60, 0xff64, 0xff68,
0xff6c, 0xff70, 0xff74, 0xff78, 0xff7c, 0xff80, 0xff84, 0xff88, 0xff8c, 0xff6c, 0xff70, 0xff74, 0xff78, 0xff7c, 0xff80, 0xff84, 0xff88, 0xff8c,
0xff90, 0xff94, 0xff98, 0xff9c, 0xffa0, 0xffa4, 0xffa8, 0xffac, 0xffb0, 0xff90, 0xff94, 0xff98, 0xff9c, 0xffa0, 0xffa4, 0xffa8, 0xffac, 0xffb0,
0xffb4, 0xffb8, 0xffbc, 0xffc0, 0xffc4, 0xffc8, 0xffcc, 0xffd0, 0xffd4, 0xffb4, 0xffb8, 0xffbc, 0xffc0, 0xffc4, 0xffc8, 0xffcc, 0xffd0, 0xffd4,
0xffd8, 0xffdc, 0xffe0, 0xffe4, 0xffe8, 0xffec, 0xfff0, 0xfff1, 0xfff2, 0xffd8, 0xffdc, 0xffe0, 0xffe4, 0xffe8, 0xffec, 0xfff0, 0xfff1, 0xfff2,
0xfff3, 0xfff4}; 0xfff3, 0xfff4};
}; };
class Synth { class Synth {
public: public:
Patch patch;
Voice voice[8]; Voice voice[8];
float lfo = 0, lfosw = 0.01; float lfo = 0, lfosw = 0.01;
float lastpw = 0; float lastpw = 0;
uint32_t blockLeft; uint32_t blockLeft;
uint32_t framesLeft = 0; uint32_t framesLeft = 0;
uint8_t ff1e;
uint8_t ff4a; // LFO flags
uint16_t ff4d = 0; // LFO output value
uint16_t ff4f; // computed PWM LFO
uint16_t ff51; // computed pitch LFO
uint16_t ff53; // computed VCF LFO
uint16_t ff56; // LFO Delay envelope
uint16_t ff5a; // LFO Delay holdoff
uint8_t ff64; // LFO mod sens amount
// okay, not the greatest, this right here // okay, not the greatest, this right here
// this struct contains the bytes that make up a Juno 106 patch in // this struct contains the bytes that make up a Juno 106 patch in
// sysex order, exactly as they'd be transmitted or received by a // sysex order, exactly as they'd be transmitted or received by a
@ -142,6 +144,35 @@ class Synth {
uint8_t switch1 = 26; uint8_t switch1 = 26;
uint8_t switch2 = 24; uint8_t switch2 = 24;
} patchRam; } patchRam;
uint8_t lfoDepthTable[128] = {
0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21,
0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x40, 0x42, 0x44, 0x46, 0x48, 0x4a, 0x4c,
0x4e, 0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, 0x60, 0x62, 0x64,
0x66, 0x68, 0x6a, 0x6c, 0x6e, 0x70, 0x72, 0x74, 0x76, 0x78, 0x7a, 0x7c,
0x80, 0x84, 0x88, 0x8c, 0x90, 0x94, 0x98, 0x9c, 0xa0, 0xa4, 0xa8, 0xac,
0xb0, 0xb4, 0xb8, 0xbc, 0xc0, 0xc4, 0xc8, 0xcc, 0xd0, 0xd4, 0xd8, 0xdc,
0xe0, 0xe4, 0xe8, 0xec, 0xf0, 0xf8, 0xff, 0xff};
uint16_t lfoRateTable[128] = {
0x0005, 0x000f, 0x0019, 0x0028, 0x0037, 0x0046, 0x0050, 0x005a, 0x0064,
0x006e, 0x0078, 0x0082, 0x008c, 0x0096, 0x00a0, 0x00aa, 0x00b4, 0x00be,
0x00c8, 0x00d2, 0x00dc, 0x00e6, 0x00f0, 0x00fa, 0x0104, 0x010e, 0x0118,
0x0122, 0x012c, 0x0136, 0x0140, 0x014a, 0x0154, 0x015e, 0x0168, 0x0172,
0x017c, 0x0186, 0x0190, 0x019a, 0x01a4, 0x01ae, 0x01b8, 0x01c2, 0x01cc,
0x01d6, 0x01e0, 0x01ea, 0x01f4, 0x01fe, 0x0208, 0x0212, 0x021c, 0x0226,
0x0230, 0x023a, 0x0244, 0x024e, 0x0258, 0x0262, 0x026c, 0x0276, 0x0280,
0x028a, 0x029a, 0x02aa, 0x02ba, 0x02ca, 0x02da, 0x02ea, 0x02fa, 0x030a,
0x031a, 0x032a, 0x033a, 0x034a, 0x035a, 0x036a, 0x037a, 0x038a, 0x039a,
0x03aa, 0x03ba, 0x03ca, 0x03da, 0x03ea, 0x03fa, 0x040a, 0x041a, 0x042a,
0x043a, 0x044a, 0x045a, 0x046a, 0x047a, 0x048a, 0x04be, 0x04f2, 0x0526,
0x055a, 0x058e, 0x05c2, 0x05f6, 0x062c, 0x0672, 0x06b8, 0x0708, 0x0758,
0x07a8, 0x07f8, 0x085c, 0x08c0, 0x0924, 0x0988, 0x09ec, 0x0a50, 0x0ab4,
0x0b18, 0x0b7c, 0x0be0, 0x0c58, 0x0cd0, 0x0d48, 0x0dde, 0x0e74, 0x0f0a,
0x0fa0, 0x1000};
void runLFO();
}; };