2024-10-15 21:56:18 +00:00
|
|
|
/*
|
|
|
|
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"
|
|
|
|
|
|
|
|
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 samples) {
|
|
|
|
// generate a full block of samples for the oscillator
|
|
|
|
|
2024-10-16 23:01:46 +00:00
|
|
|
float y, out, t;
|
2024-10-15 21:56:18 +00:00
|
|
|
float gain = env.level / 16384.0;
|
|
|
|
|
2024-10-17 22:57:28 +00:00
|
|
|
if (subosc > 0) subosc = sub; else subosc = -sub;
|
2024-10-16 23:01:46 +00:00
|
|
|
|
|
|
|
|
2024-10-15 21:56:18 +00:00
|
|
|
// this uses an adaptation of Mystran's Polyblep oscillator
|
|
|
|
for (uint32_t i = 0; i < samples; i++) {
|
|
|
|
y = delay;
|
|
|
|
delay = 0;
|
|
|
|
phase += omega;
|
|
|
|
|
2024-10-16 23:01:46 +00:00
|
|
|
pwrc = ((pw - pwrc) *.01 ) + pwrc;
|
|
|
|
|
2024-10-15 21:56:18 +00:00
|
|
|
// this is the clever bit
|
|
|
|
while (true) {
|
|
|
|
if (!pulseStage) {
|
2024-10-16 23:01:46 +00:00
|
|
|
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
|
2024-10-15 21:56:18 +00:00
|
|
|
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;
|
2024-10-16 20:41:08 +00:00
|
|
|
y += poly3blep0(t) * (0.8 * saw + 0.63 - subosc);
|
|
|
|
delay += poly3blep1(t) * (0.8 * saw + 0.63 - subosc);
|
2024-10-15 21:56:18 +00:00
|
|
|
pulseStage = 0;
|
|
|
|
phase -= 1;
|
|
|
|
subosc = -subosc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-16 23:01:46 +00:00
|
|
|
delay += saw * (0.8 - (1.6 * phase)); // magic numbers observed on oscilloscope from real synth
|
2024-10-15 21:56:18 +00:00
|
|
|
delay += (0.63 - (pw * 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;
|
|
|
|
|
|
|
|
out = y * 0.15;
|
2024-10-16 23:01:46 +00:00
|
|
|
lastpw = pwrc;
|
2024-10-15 21:56:18 +00:00
|
|
|
buffer[i] += out * gain;
|
|
|
|
}
|
2024-10-16 23:01:46 +00:00
|
|
|
|
2024-10-15 21:56:18 +00:00
|
|
|
}
|