128 lines
3.8 KiB
C++
128 lines
3.8 KiB
C++
/*
|
|
Peacock-8 VA polysynth
|
|
|
|
Copyright 2025 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 <math.h>
|
|
#include <stdio.h>
|
|
|
|
#include "module.hpp"
|
|
#include "tables.hpp"
|
|
|
|
// antialiasing using polybleps, as described in KVRAudio forum by Mystran
|
|
|
|
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);
|
|
}
|
|
|
|
Voice::Voice() {
|
|
omega = 0.0;
|
|
theta = 0.0;
|
|
env = 0;
|
|
}
|
|
|
|
void Voice::on(uint8_t midiNote) {
|
|
while (midiNote < 24) midiNote += 12;
|
|
while (midiNote > 108) midiNote -=12;
|
|
note = midiNote - 24;
|
|
envPhase = 1;
|
|
}
|
|
|
|
void Voice::off() {
|
|
envPhase = 0;
|
|
}
|
|
|
|
void Voice::run(Module* m, float* buffer, uint32_t samples) {
|
|
// carry out per-voice calculations for each block of samples
|
|
float out, t, fb;
|
|
|
|
// FIXME incorrect
|
|
// calculate cutoff frequency
|
|
float cut = 248.0f * (powf(2, (vcfCut - 0x1880) / 1143.0f));
|
|
cut = 0.25 * 6.2832 * cut / 48000.0f; // FIXME hardcoded values
|
|
cut = cut / (1 + cut); // correct tuning warp
|
|
|
|
float amp = vcaEnv / 4096.0f;
|
|
|
|
for (uint32_t i = 0; i < samples; i++) {
|
|
out = delay;
|
|
delay = 0;
|
|
|
|
theta += omega;
|
|
|
|
while (true) {
|
|
if (pulseStage == 0) {
|
|
if (theta < m->pwmBuf[i]) break;
|
|
t = (theta - m->pwmBuf[i]) / (lastpw - m->pwmBuf[i] + omega);
|
|
out -= poly3blep0(t) * m->square;
|
|
delay -= poly3blep1(t) * m->square;
|
|
pulseStage = 1;
|
|
}
|
|
|
|
if (pulseStage == 1) {
|
|
if (theta < 1) break; // no need to blep yet
|
|
t = (theta - 1) / omega; // scaled remainder of phase
|
|
out += poly3blep0(t) * (m->saw + m->square);
|
|
delay += poly3blep1(t) * (m->saw + m->square);
|
|
|
|
out -= poly3blep0(t) * (m->subBuf[i] * subosc);
|
|
delay -= poly3blep1(t) * (m->subBuf[i] * subosc);
|
|
pulseStage = 0;
|
|
subosc = -subosc;
|
|
|
|
theta -= 1;
|
|
}
|
|
}
|
|
|
|
// FIXME DC offset removal
|
|
delay += m->saw * (1 - (2 * theta));
|
|
delay += m->square * ((pulseStage ? -1.f : 1.f) - m->pwmBuf[i] + 0.5);
|
|
delay += m->subBuf[i] * subosc ;
|
|
|
|
out += m->noise * (0.8 - 1.6 * (rand() & 0xffff) / 65536.0);
|
|
out *= 0.5;
|
|
|
|
// same time constant for both VCF and VCF RC circuits
|
|
vcfRC = (cut - vcfRC) * m->vcaTC + vcfRC;
|
|
|
|
for (uint8_t ovs = 0; ovs < 4; ovs++) {
|
|
fb = b4;
|
|
// hard clip
|
|
fb = ((out * 0.5) - fb) * m->res;
|
|
if (fb > 4) fb = 4;
|
|
if (fb < -4) fb = -4;
|
|
// fb = 1.5 * fb - 0.5 * fb * fb * fb;
|
|
//
|
|
|
|
b1 = ((out + fb - b1) * vcfRC) + b1;
|
|
b2 = ((b1 - b2) * vcfRC) + b2;
|
|
b3 = ((b2 - b3) * vcfRC) + b3;
|
|
b4 = ((b3 - b4) * vcfRC) + b4;
|
|
}
|
|
|
|
vcaRC = (amp - vcaRC) * m->vcaTC + vcaRC;
|
|
buffer[i] += 0.09367 * m->vcaBuf[i] * vcaRC * b4;
|
|
|
|
lastpw = m->pwmBuf[i];
|
|
}
|
|
// buffer[0] += 1;
|
|
}
|