chassis/plugin/voice.hpp

148 lines
5.1 KiB
C++
Raw Permalink 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 22:30:08 +00:00
class Patch {
2024-09-04 20:08:27 +00:00
public:
float saw, sqr, sub;
2024-09-08 10:13:23 +00:00
uint16_t attack, decay, sustain, release;
2024-09-03 22:30:08 +00:00
};
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
bool ff00 = 0; // reset bit
bool ff07 = 0; // status bit
bool ff08 = 0; // set to indicate attack phase complete
bool ff10 = 0; // note on bit
bool ff11 = 0; // sustain flag
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
float pw_rc = 0;
2024-09-07 19:46:57 +00:00
float vr58c106 = 0;
uint16_t attack_table[128] = {
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:
2024-09-07 19:46:57 +00:00
Patch patch;
2024-09-04 20:08:27 +00:00
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
// 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 lfo_rate = 57;
uint8_t lfo_delay = 45;
uint8_t lfo_dco = 0;
uint8_t lfo_pwm = 55;
uint8_t noise = 0;
uint8_t vcf_cut = 85;
uint8_t vcf_res = 0;
uint8_t vcf_env = 0;
uint8_t vcf_lfo = 0;
uint8_t vcf_kyb = 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-03 22:30:08 +00:00
};
2024-09-08 00:08:00 +00:00