peacock/plugin/oscillator.cpp
2024-10-19 23:11:28 +01:00

78 lines
2.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 "voiceboard.hpp"
static inline float poly3blep0(float t) {
float t2 = t * t;
return 2 * (t * t2 - 0.5f * t2 * t2);
}
static inline float poly3blep1(float t) {
return -poly3blep0(1 - t);
}
void Voice::run(float *buffer, uint32_t pos, uint32_t samples) {
// generate a full block of samples for the oscillator
float y, t;
if (subosc > 0) subosc = sub; else subosc = -sub;
// this uses an adaptation of Mystran's Polyblep oscillator
for (uint32_t i = 0; i < samples; i++) {
y = delay;
delay = 0;
phase += omega;
pwrc = ((pw - pwrc) *.01 ) + pwrc;
// this is the clever bit
while (true) {
if (!pulseStage) {
if (phase < pwrc) break; // it's not time for the PWM output to step
t = (phase - pwrc) / (lastpw - pwrc + omega); // calculate fractional sample allowing for PW amount
y -= 0.63 * poly3blep0(t); // magic numbers observed on oscilloscope from real synth
delay -= 0.63 * poly3blep1(t);
pulseStage = true;
}
if (pulseStage) {
if (phase < 1) break; // it's not time to reset the saw
t = (phase - 1) / omega;
y += poly3blep0(t) * (0.8 * saw + 0.63 - subosc);
delay += poly3blep1(t) * (0.8 * saw + 0.63 - subosc);
pulseStage = 0;
phase -= 1;
subosc = -subosc;
}
}
delay += saw * (0.8 - (1.6 * phase)); // magic numbers observed on oscilloscope from real synth
delay += (0.63 - (pwrc * 1.26)) + (pulseStage ? -0.63f : 0.63f); // add in the scaled pulsewidth to restore DC level
// the DC correction is important because the hardware synth is AC-coupled effectively high-passing
// the signal at about 10Hz or so, preventing any PWM rumble from leaking through!
delay += subosc;
delay += ic29.noise[i+pos];
lastpw = pwrc;
buffer[i + pos] = y * 0.707;
}
}