Skip to content

Instantly share code, notes, and snippets.

@grifdail
Created November 4, 2014 11:34
Show Gist options
  • Save grifdail/1738a58cd0c4ab300939 to your computer and use it in GitHub Desktop.
Save grifdail/1738a58cd0c4ab300939 to your computer and use it in GitHub Desktop.
CouldSound
/*!
*
* welcome to wavepot
* ------------------
*
* this is a live editor. you create a function named `dsp`
* that accepts the parameter `t`, the coefficient of time,
* which you use to generate a single sample (range -1..1)
*
* below is the smallest example possible, a simple sine wave
* generator. check out more complex demos on the right ---->
*
* have fun!
*
*/
var tau = Math.PI*2
function sub(wave, mul, t){
return Math.sin(wave * mul + tau * t);
}
function sin(x, t){
return Math.sin(tau * t * x);
}
function tri(x, t){
return Math.abs(1 - (2 * t * x) % 2) * 2 - 1;
}
function perc(wave, decay, o, t){
var env = Math.max(0, 0.889 - (o * decay) / ((o * decay) + 1));
return wave * env;
}
function arp(measure, x, y, z, t){
var ts = t / 2 % measure;
return Math.sin(x * (Math.exp(-ts * y))) * Math.exp(-ts * z);
}
function midi(n,octave) {
return Math.pow(2, (
n + 0 - 33 + (12 * (octave || 0))
) / 12) * 440; // A4 tuning
}
function sequence(t,m,s) {
return s[((t/m)%s.length)|0];
}
var note = [midi(32),midi(34),midi(36),midi(37),midi(39),midi(37),midi(34)];
var t, pt;
function dsp(_t) {
t = _t;
pt = (t%0.5)*2;
return 0
//+ sub(sin(440,t),sin(0.2,t)*5,t*8)
+ arp(1/4, 100 + pt*0.5, 50+pt*2, 10,t)
+ sub(sin(sequence(t, t%1/2,note),t),0.5,t)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment