Created
July 25, 2014 11:52
-
-
Save cheery/4651e954e7ca92765b2f to your computer and use it in GitHub Desktop.
Frequency modulation
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
var pi = Math.PI; | |
var tau = Math.PI*2; | |
function arp(ts, x, y, z) { | |
return Math.sin(x * Math.exp(-ts * y)) * Math.exp(-ts * z); | |
} | |
var sin = Math.sin; | |
var cos = Math.cos; | |
function tri(x) { | |
return Math.abs(1 - (x % tau) / pi) * 2.0 - 1.0; | |
} | |
function saw(x) { | |
return 1 - (x % tau) / pi; | |
} | |
function sqr(x) { | |
return cos(x) > 0 ? 1 : -1; | |
} | |
function upol(x) { | |
return x*0.5 + 0.5; | |
} | |
function bipol(x) { | |
return x*2 - 1; | |
} | |
function clamp(x, mi, ma) { | |
return Math.max(mi, Math.min(ma, x)); | |
} | |
var c0 = 7, c1 = 2, c2 = sqr; | |
function dsp(t) { | |
var dt = 1 / sampleRate; | |
var ts = t % 3.0; | |
if (ts - dt <= 0) { | |
c0 = Math.random() * 2 + 7; | |
c1 = Math.random() * 5 - 2.5 + 2; | |
if (c1 < 0) c1 = 1 / -c1; | |
c2 = [tri, saw, sqr, cos][Math.floor(Math.random()*4)]; | |
} | |
var s = 301.467; | |
var i = upol(tri(t*tau*c1))*c0; | |
var b = t*tau*s / 7; | |
var a = t*tau*s; | |
return 0.5 * arp(ts, c2(a + i*cos(b)), tau/4, 0.1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment