74 lines
2.8 KiB
C++
74 lines
2.8 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"
|
||
|
|
||
|
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
|
||
|
|
||
|
float y, out, pw = 0.0, t;
|
||
|
|
||
|
float gain = env.level / 16384.0;
|
||
|
|
||
|
// this uses an adaptation of Mystran's Polyblep oscillator
|
||
|
for (uint32_t i = 0; i < samples; i++) {
|
||
|
y = delay;
|
||
|
delay = 0;
|
||
|
phase += omega;
|
||
|
|
||
|
// this is the clever bit
|
||
|
while (true) {
|
||
|
if (!pulseStage) {
|
||
|
if (phase < pw) break; // it's not time for the PWM output to step
|
||
|
t = (phase - pw) / (lastpw - pw + 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 + 0.63 - subosc);
|
||
|
delay += poly3blep1(t) * (0.8 + 0.63 - subosc);
|
||
|
pulseStage = 0;
|
||
|
phase -= 1;
|
||
|
subosc = -subosc;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
delay += (0.8 - (1.6 * phase)); // magic numbers observed on oscilloscope from real synth
|
||
|
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;
|
||
|
lastpw = pw;
|
||
|
buffer[i] += out * gain;
|
||
|
}
|
||
|
}
|