POC - Synthesizing Rain

by Paulo Ávila

JavaScript

/* http://obiwannabe.co.uk/tutorials/html/tutorial_rain.html

noise~
lop~ 16  noise~
       \/
        *
    bp~ 333 4
     *~ 0.4
      *~ 1
    lop~ 12000
       dac~
*/




// http://jsfiddle.net/nPD3W/
var context = new webkitAudioContext();

//var noise;

var bandPassFilter = context.createBiquadFilter();
bandPassFilter.type = bandPassFilter.BANDPASS; // 2
bandPassFilter.frequency.value = 440; // Set cutoff to 440 HZ
//.type
//.frequency (in Hertz)
//.Q (Quality factor)
//.gain (in Decibels)

noise.connect(bandPassFilter);
bandPassFilter.connect(context.destination);


//var osc = context.createOscillator();
//osc.frequency.value = 300;
//osc.type = 0; // sinus

(noise.connect(LOWPASS(16)) * noise).connect(
    BANDPASS(333, 4)
).connect(
    *= 0.4
).connect(
    *= 1
).connect(
    LOWPASS(12000)
).start(0);

//const unsigned short LOWPASS = 0;
//const unsigned short BANDPASS = 2;






// FROM: http://noisehack.com/generate-noise-web-audio-api/

/* WHITE NOISE
var bufferSize = 2 * audioContext.sampleRate,
    noiseBuffer = audioContext.createBuffer(1, bufferSize, audioContext.sampleRate),
    output = noiseBuffer.getChannelData(0);
for (var i = 0; i < bufferSize; i++) {
    output[i] = Math.random() * 2 - 1;
}

var whiteNoise = audioContext.createBufferSource();
whiteNoise.buffer = noiseBuffer;
whiteNoise.loop = true;
whiteNoise.start(0);

whiteNoise.connect(audioContext.destination);
*/


/* PINK NOISE
var bufferSize = 4096;
var pinkNoise = (function() {
    var b0, b1, b2, b3, b4, b5, b6;
    b0 = b1 = b2 = b3 = b4 = b5 = b6 = 0.0;
    var node = audioContext.createScriptProcessor(bufferSize, 1, 1);
    node.onaudioprocess = function(e) {
        var output = e.outputBuffer.getChannelData(0);
        for (var i = 0; i < bufferSize; i++) {
            var white = Math.random() * 2 - 1;
            b0 = 0.99886 * b0 + white * 0.0555179;
            b1 = 0.99332 * b1 + white * 0.0750759;
            b2 = 0.96900 * b2 + white * 0.1538520;
            b3 =...