yankov
1/26/2014 - 5:24 AM

Breaking waves sound (Open (http://myjavatools.com/js.html) in Chrome paste the code and hit run)

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);
}