peacock/plugin/ic29.cpp
2024-10-15 22:56:18 +01:00

160 lines
3.9 KiB
C++

/*
Peacock-8 VA polysynth
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 "ic29.hpp"
Synth ic29;
Synth::Synth() {
d_debug("initialising synth\n");
envAtk = 0x007f;
envDcy = envRls = 0xfe90;
envStn = 0x1fff;
portaCoeff = 0x0;
}
void Synth::buildTables(double sampleRate) {
for (uint8_t i = 0; i < 104; i++) {
// slightly flat middle C from ROM divider table
// actually adjusted a little so that the notes are bang on
// on the real synth the tuning knob is tweaked a little off to pull it in
pitchTable[i] = 260.15f * powf(2, (i - 36) / 12.0f) / sampleRate;
}
}
void Synth::run() {
// handle a "loop" worth of envelopes, pitch calculations, etc
// callled once every 4.3ms block of samples
for (uint8_t i = 0; i < NUM_VOICES; i++) {
ic29.voices[i].update();
}
}
void Synth::voiceOn(uint8_t voice, uint8_t note) {
// enable synth voice, start it all running
voice &= 0x7f;
ic29.voices[voice].on(note);
}
void Synth::voiceOff(uint8_t voice) {
// enable synth voice, start it all running
voice &= 0x7f;
ic29.voices[voice].off();
}
void Synth::basePitch() {
uint16_t pitch = 0x1818;
pitch += lfoPitch;
pitch += bendPitch;
// tuning too but that's zero by default;
masterPitch = pitch;
}
Envelope::Envelope() {
level = 0;
phase = ENV_IDLE;
}
void Envelope::run() {
switch (phase) {
case ENV_ATK:
level += ic29.envAtk;
if (level > 0x3fff) {
level = 0x3fff;
phase = ENV_DCY;
}
break;
case ENV_DCY:
if (level > ic29.envStn) {
level -= ic29.envStn;
level = (level * ic29.envDcy) >> 16;
level += ic29.envStn;
} else {
level = ic29.envStn;
}
break;
case ENV_RLS:
level = (level * ic29.envRls) >> 16;
break;
case ENV_IDLE:
default:
break;
}
}
Voice::Voice() {
subosc = 1;
}
void Voice::calcPitch() {
uint16_t target = note << 8;
if (ic29.portaCoeff != 0) {
// porta up
if (pitch < target) {
pitch += ic29.portaCoeff;
if (pitch > target) pitch = target;
}
// porta down
if (pitch > target) {
pitch -= ic29.portaCoeff;
if (pitch < target) pitch = target;
}
} else {
pitch = target;
}
pitch += 0x1818; //ic29.masterPitch;
if (pitch < 0x3000) pitch = 0x3000; // lowest note
if (pitch > 0x9700) pitch = 0x6700; // highest note
pitch -= 0x3000;
//pitch &= 0xff00;
double o1 = ic29.pitchTable[pitch >> 8];
double o2 = ic29.pitchTable[(pitch >> 8) + 1];
double frac = (pitch & 0xff) / 255.0;
omega = ((o2 - o1) * frac) + o1;
}
void Voice::update() {
// calculate the once-per-block values
env.run();
calcPitch();
// do filter values
}
void Voice::on(uint8_t key) {
voiceState = V_ON;
note = key;
env.on();
}
void Voice::off() {
// I need to rethink this bit FIXME
voiceState = V_OFF;
if (!ic29.sustained) {
env.off();
}
}