Created
July 30, 2014 19:48
-
-
Save smosher/8aa045d2045290e9628e to your computer and use it in GitHub Desktop.
Phase Distortion Synthesis
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
// https://en.wikipedia.org/wiki/Phase_distortion_synthesis | |
// NB. completely untested | |
Range | |
pi := 3.14159265 | |
tau := 2 * pi | |
FreqCtr := Object clone do( | |
factor := nil | |
new := method(srate, | |
rv := self clone | |
rv factor = tau / srate | |
self) | |
sample := method(freq, i, | |
freq * i * factor) | |
) | |
PeriodicLUT := Object clone do( | |
data := Sequence clone | |
factor ::= 1 | |
init := method( | |
self data := Sequence clone) | |
new := method(function, size, period, | |
self clone setFactor(period / size) populate(function, size)) | |
populate := method(f, size, | |
0 to(size) foreach(i, | |
self data atPut(i, f call(i)))) | |
at := method(n, | |
n := (n * self factor) % data size | |
data at(n floor) + data at(n ceil) / 2) | |
) | |
PhaseDist := Object clone do( | |
base := nil | |
res := nil | |
lut ::= PeriodicLUT new(block(x, (x * tau/1024) sin), 1024, tau) | |
operator ::= method(i, self lut at(i)) | |
new := method(srate, baseFreq, resFreq, | |
rv := self clone | |
self res := FreqCtr new(srate, resFreq) | |
self base := FreqCtr new(srate, baseFreq)) | |
sample := method(freq, i, | |
bsamp := base sample(freq, i) | |
val := res sample(freq, i % bsamp) | |
operator(val) * (1 - bsamp)) | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment