Last active
July 3, 2019 19:52
-
-
Save devin-dominguez/1354266428d9f2f68394a04c242f7680 to your computer and use it in GitHub Desktop.
Polynomial band limited step
This file contains 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
// phase in range 0 <= phase < 1 | |
// pwm in range -1 <= pwm <= 1 | |
dt = frequency / samplerate | |
saw(phase, dt) { | |
return phase * 2 - 1 - polyBlep(phase, dt) | |
} | |
pulse(phase, dt, pwm = 0) { | |
return saw(phase) - saw(wrap(phase + (pwm + 1 / 2))) + pwm | |
} | |
polyBlep(phase, dt) { | |
if (phase < dt) { | |
pdt = phase / dt | |
return (pdt * 2) - (pdt * pdt) - 1 | |
} | |
if (phase > 1 - dt) { | |
pdt = (phase - 1) / dt | |
return (pdt * pdt) + (pdt * 2) + 1 | |
} | |
return 0 | |
} | |
wrap(value) { | |
while (value >= 1) { | |
value -= 1 | |
} | |
while (value < 0) { | |
value += 1 | |
} | |
return value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment