Created
August 19, 2025 08:47
-
-
Save GlaireDaggers/e4d30432971ad3cd07bb74e558538431 to your computer and use it in GitHub Desktop.
PolyBLEP waveform implementation in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Oscillator | |
{ | |
public static double EvaluateSaw(ref double phase, double phaseInc) | |
{ | |
double value = SampleSaw(phase) - PolyBLEP(phase, phaseInc); | |
phase += phaseInc; | |
phase %= 1.0; | |
return value; | |
} | |
public static double EvaluateSquare(ref double phase, double dutyCycle, double phaseInc) | |
{ | |
double value = SampleSquare(phase, dutyCycle) | |
+ PolyBLEP(phase, phaseInc) | |
- PolyBLEP((phase + (1.0 - dutyCycle)) % 1.0, phaseInc); | |
phase += phaseInc; | |
phase %= 1.0; | |
return value; | |
} | |
private static double PolyBLEP(double t, double dt) | |
{ | |
// 0 <= t < 1 | |
if (t < dt) | |
{ | |
t /= dt; | |
return t+t - t*t - 1.0; | |
} | |
// -1 < t < 0 | |
else if (t > 1.0 - dt) | |
{ | |
t = (t - 1.0) / dt; | |
return t*t + t+t + 1.0; | |
} | |
// 0 otherwise | |
return 0.0; | |
} | |
private static double SampleSaw(double phase) | |
{ | |
return (2.0 * phase) - 1.0; | |
} | |
private static double SampleSquare(double phase, double dutyCycle) | |
{ | |
return phase < dutyCycle ? 1.0 : -1.0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment