Skip to content

Instantly share code, notes, and snippets.

@tovbinm
Created January 26, 2014 05:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tovbinm/8628807 to your computer and use it in GitHub Desktop.
Save tovbinm/8628807 to your computer and use it in GitHub Desktop.
Breaking waves sound (Open (http://myjavatools.com/js.html) in Chrome paste the code and hit run)
var AMP = 0.5; // amplitude
var len = 15; // seconds
e = new webkitAudioContext();
var source = e.createBufferSource();
var SR = e.sampleRate;
source.buffer = e.createBuffer(1, len * SR, SR);
var dat = source.buffer.getChannelData(0);
for (var i = 0; i < len * SR; i++) {
dat[i] = AMP * (Math.random() * 2 - 1);
}
var MOD_FREQ = 0.2; // modulation frequency
var Q = 0.1; // resonance quality (unitless)
dat[0] = 0;
dat[1] = 0;
for (var i = 2; i < len * SR; i++) {
// resonant frequency
var res = 1000 + 3000 * (1 - Math.cos(MOD_FREQ * 2 * Math.PI * i / SR)) / 2;
// normalizing factor
var norm = 1 / (SR * SR + SR * res / Q + res * res);
dat[i] = norm * (res * res * dat[i]
+ (res / Q + 2 * SR) * SR * dat[i-1]
- SR * SR * dat[i-2]);
}
source.connect(e.destination);
source.loop = true;
source.start(0);
document.onmousedown = function() {
source.disconnect(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment