Skip to content

Instantly share code, notes, and snippets.

@tdhooper
Created October 5, 2014 13:08
Show Gist options
  • Save tdhooper/b19a0f5f5dd347ff69ae to your computer and use it in GitHub Desktop.
Save tdhooper/b19a0f5f5dd347ff69ae to your computer and use it in GitHub Desktop.
wavepot echo
/*!
*
* 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!
*
*/
function dsp(t) {
return echo(t, bleep);
}
function echo(t, soundFunc) {
var s = soundFunc(t);
var n = 10;
var l = 0.5;
for (var i = 0; i < n; i++) {
var offset = (l / n) * i;
var vol = (1 / n) * (n - i);
s += soundFunc(t - offset) * vol;
}
return s;
}
function vol(t, f, l) {
if (t % f > l) {
return 0;
}
var s = sin(t * (1 / l), 1, 1);
return s;
}
function bleep(t) {
t *= 2;
var v = vol(t, 4, 0.1);
var s = sin(t, 450, 0.5);
return v * s;
}
function sin(t, freq, amp) {
return Math.sin(Math.PI * t * freq) * amp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment