Jason (jcreed) wrote,
Jason
jcreed

Fun with subtractive synthesis in javascript:
var AMP = 0.5; // amplitude
var len = 10; // 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);
}


this variant (MOD_FREQ=40 Q=10) sounds like a leaf blower
Tags: audio, javascript, synthesis
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 1 comment