EPTRTri

Low CPU Anti Aliased Triangle using second order EPTR algoroithm (one correction point per transition).
Author: Smashed Transistors, Ricard Wanderlof
License: LGPL
Github: ricard/osc/EPTRTri.axo

Inlets

frac32.bipolar pitch

Outlets

frac32buffer.bipolar selfPM wave

Parameters

frac32.s.map.pitch pitch

Declaration
float p, dp, y, A, B;
int dir;
// Triangle EPTR polynomial coefficients
#define a2 (-1.0f / (4 * (A - 1) * dp))
#define a1 ((2 * A * dp - 4 * dp + 2) / (4 * (A - 1) * dp))
#define a0 (-(A * dp - 1) * (A * dp - 1) / (4 * dp * (A - 1)))
#define b2 (-1.0f / (4 * (B + 1) * dp))
#define b1 ((2 * B * dp + 4 * dp - 2) / (4 * (B + 1) * dp))
#define b0 (-(B * dp + 1) * (B * dp + 1) / (4 * dp * (B + 1)))
// correction polynomials
float corr_max(float p, float dp) { return a2 * p * p + a1 * p + a0; }
float corr_min(float p, float dp) { return b2 * p * p + b1 * p + b0; }
Init
p = 0;
;
dir = 1;
Control Rate
int32_t idp;
MTOFEXTENDED(param_pitch + inlet_pitch, idp);
A = 2;
B = -2;
dp = idp * (0.25f / (1 << 30));
Audio Rate
if (dir == 1) {
  p += 2 * A * dp;
  if (p > 1 - A * dp) {
    y = corr_max(p, dp);
    p = 1 + (p - 1) * B / A;
    dir = -1;
  } else {
    y = p; // linear region
  }
} else {
  p += 2 * B * dp;
  if (p < -1 - B * dp) {
    y = corr_min(p, dp);
    p = -1 + (p + 1) * A / B;
    dir = 1;
  } else {
    y = p; // linear region
  }
}
// p += dp; if (p > 1) p = -1;
// y = p;
outlet_tri = (int32_t)(y * (1 << 27));

Privacy

© 2024 Zrna Research