chassis/plugin/voice.hpp

179 lines
7.5 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.
*/
#pragma once
2024-09-03 11:42:16 +00:00
#include <cstdint>
// uint16_t attack_table[128];
// uint16_t decay_table[128];
2024-09-08 00:08:00 +00:00
2024-09-03 22:41:01 +00:00
class Synth;
2024-09-03 11:42:16 +00:00
class Voice {
public:
2024-09-08 00:08:00 +00:00
// uint8_t note = 72;
uint8_t note = 60;
2024-09-03 12:50:46 +00:00
void on(uint32_t key, bool reset);
2024-09-03 12:50:46 +00:00
void off();
2024-09-03 12:50:46 +00:00
2024-09-03 14:31:54 +00:00
bool isFree();
2024-09-03 12:50:46 +00:00
2024-09-03 22:41:01 +00:00
void run(Synth &s, float *buffer, uint32_t samples);
2024-09-03 12:50:46 +00:00
2024-09-08 00:08:00 +00:00
void gate(Synth &s);
2024-09-05 22:18:41 +00:00
2024-09-03 12:50:46 +00:00
private:
enum { ATTACK,
DECAY,
SUSTAIN,
RELEASE } envState = RELEASE;
enum { K_OFF,
2024-09-05 22:18:41 +00:00
K_WAIT,
2024-09-03 12:50:46 +00:00
K_ON,
K_SUSTAIN } keyState = K_OFF;
2024-09-05 22:18:41 +00:00
2024-09-08 23:55:14 +00:00
bool ff00 = 0; // reset DCO clock flag
bool ff07 = 0; // set to indicate attack phase
bool ff08 = 0; // set to indicate decay/sustain phase
bool ff10 = 0; // note on bit
2024-09-08 23:55:14 +00:00
bool ff11 = 0; // drives the "gate" signal for VCA
bool ff33 = 0; // releasing flag
2024-09-08 00:08:00 +00:00
uint16_t env;
2024-09-07 19:46:57 +00:00
float phase = 0, omega = 260 / 48000.0, subosc = 1;
2024-09-08 00:08:00 +00:00
// float env, target;
2024-09-06 20:44:41 +00:00
float delay;
2024-09-08 00:08:00 +00:00
uint8_t pulseStage = 0;
2024-09-04 20:08:27 +00:00
2024-09-08 23:55:14 +00:00
float lastpw = 0;
2024-09-07 19:46:57 +00:00
float vr58c106 = 0;
uint16_t attack_table[128] = {
2024-09-08 23:55:14 +00:00
0x4000, 0x2000, 0x1000, 0x0aaa, 0x0800, 0x0666, 0x0555, 0x0492, 0x0400,
0x038e, 0x0333, 0x02e9, 0x02ab, 0x0276, 0x0249, 0x0222, 0x0200, 0x01e2,
0x01c7, 0x01af, 0x0199, 0x0186, 0x0174, 0x0164, 0x0155, 0x0148, 0x013b,
0x012f, 0x0124, 0x011a, 0x0111, 0x0108, 0x0100, 0x00f8, 0x00f1, 0x00ea,
0x00e4, 0x00dd, 0x00d8, 0x00d2, 0x00cd, 0x00c8, 0x00c3, 0x00bf, 0x00ba,
0x00b6, 0x00b2, 0x00ae, 0x00ab, 0x00a7, 0x00a4, 0x00a1, 0x009e, 0x009b,
0x0098, 0x0095, 0x0092, 0x0090, 0x008d, 0x008b, 0x0089, 0x0086, 0x0084,
0x0082, 0x007f, 0x007d, 0x007a, 0x0077, 0x0074, 0x0072, 0x006f, 0x006c,
0x0069, 0x0067, 0x0064, 0x0061, 0x005e, 0x005c, 0x0059, 0x0056, 0x0053,
0x0050, 0x004e, 0x004b, 0x0048, 0x0045, 0x0042, 0x0040, 0x003f, 0x003d,
0x003c, 0x003a, 0x0039, 0x0037, 0x0036, 0x0034, 0x0033, 0x0031, 0x0030,
0x002e, 0x002d, 0x002b, 0x002a, 0x0028, 0x0027, 0x0025, 0x0024, 0x0022,
0x0021, 0x0021, 0x0020, 0x0020, 0x001f, 0x001f, 0x001e, 0x001e, 0x001d,
0x001d, 0x001c, 0x001c, 0x001b, 0x001b, 0x001a, 0x0019, 0x0018, 0x0017,
0x0016, 0x0015};
uint16_t decay_table[128] = {
0x1000, 0x3000, 0x5000, 0x7000, 0x9000, 0xa000, 0xa800, 0xb000, 0xb800,
0xc000, 0xc800, 0xd000, 0xd800, 0xe000, 0xe800, 0xf000, 0xf080, 0xf100,
0xf180, 0xf200, 0xf280, 0xf300, 0xf380, 0xf400, 0xf480, 0xf500, 0xf580,
0xf600, 0xf680, 0xf700, 0xf780, 0xf800, 0xf880, 0xf900, 0xf980, 0xfa00,
0xfa80, 0xfb00, 0xfb80, 0xfc00, 0xfc80, 0xfd00, 0xfd80, 0xfe00, 0xfe0c,
0xfe18, 0xfe24, 0xfe30, 0xfe3c, 0xfe48, 0xfe54, 0xfe60, 0xfe6c, 0xfe78,
0xfe84, 0xfe90, 0xfe9c, 0xfea8, 0xfeb4, 0xfec0, 0xfecc, 0xfed8, 0xfee4,
0xfef0, 0xfefc, 0xff08, 0xff0c, 0xff10, 0xff14, 0xff18, 0xff1c, 0xff20,
0xff24, 0xff28, 0xff2c, 0xff30, 0xff34, 0xff38, 0xff3c, 0xff40, 0xff44,
0xff48, 0xff4c, 0xff50, 0xff54, 0xff58, 0xff5c, 0xff60, 0xff64, 0xff68,
0xff6c, 0xff70, 0xff74, 0xff78, 0xff7c, 0xff80, 0xff84, 0xff88, 0xff8c,
0xff90, 0xff94, 0xff98, 0xff9c, 0xffa0, 0xffa4, 0xffa8, 0xffac, 0xffb0,
0xffb4, 0xffb8, 0xffbc, 0xffc0, 0xffc4, 0xffc8, 0xffcc, 0xffd0, 0xffd4,
0xffd8, 0xffdc, 0xffe0, 0xffe4, 0xffe8, 0xffec, 0xfff0, 0xfff1, 0xfff2,
0xfff3, 0xfff4};
2024-09-03 11:42:16 +00:00
};
2024-09-03 22:30:08 +00:00
class Synth {
2024-09-04 20:08:27 +00:00
public:
Voice voice[8];
float lfo = 0, lfosw = 0.01;
float lastpw = 0;
uint32_t blockLeft;
uint32_t framesLeft = 0;
2024-09-08 00:08:00 +00:00
2024-09-08 23:55:14 +00:00
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
// 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
// real one.
// the horrifying implication here is that we can just shunt this
// around and memcpy() values from sysex on top of it
struct {
uint8_t lfoRate = 57;
uint8_t lfoDelay = 45;
uint8_t vcoLfo = 0;
uint8_t pwmLfo = 55;
uint8_t noise = 0;
uint8_t vcfFreq = 85;
2024-09-08 21:21:03 +00:00
uint8_t vcfReso = 0;
uint8_t vcfEnv = 0;
uint8_t vcfLfo = 0;
uint8_t vcfKey = 108;
uint8_t vca = 52;
uint8_t env_a = 59;
uint8_t env_d = 32;
uint8_t env_s = 86;
uint8_t env_r = 40;
uint8_t sub = 0;
uint8_t switch1 = 26;
uint8_t switch2 = 24;
} patchRam;
2024-09-08 23:55:14 +00:00
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();
};