Created
January 26, 2014 05:24
-
-
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)
This file contains hidden or 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 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