biotikc h
biotikc h

Reputation: 155

WEB AUDIO API creating rain crackling noises

I'm trying to create rain in the JavaScript Web Audio API. So far I've created a low frequency rumbling noise for the background and I'm working on a high frequency noise which will imitate the sound of rain droplets. However, right now the high-frequency noise is very much like white noise and too intense to be individual droplets. Does anyone know how to "separate" the sound a little so it almost sounds like crackling. Here is a link to what I would like the high-frequency noise to sound like if you increase the last slider (violet) you can hear it.

And here is my HTML code so far

<script>
let context= new AudioContext();
let context2= new AudioContext();

let lowpass = context.createBiquadFilter();
  lowpass.type = 'lowpass';
  //lowpass.Q.value = -7.01;
  lowpass.frequency.setValueAtTime(80, context2.currentTime);

let gain = new GainNode(context);
  gain.gain.value= 0.4;

let gain2 = new GainNode(context2);
  gain2.gain.value= 0.02;

let highpass=context2.createBiquadFilter();
  highpass.type = 'highpass';
  highpass.Q.value = 2;
  //highpass.frequency.setValueAtTime(6000, context2.currentTime);

let distortion = context2.createWaveShaper();

let delay = context2.createDelay(90.0);




function StartAudio() {context.resume()};
context.audioWorklet.addModule('basicnoise.js').then(() => {
  let myNoise = new AudioWorkletNode(context,'noise-generator');

  myNoise.connect(lowpass);
  lowpass.connect(gain);
  gain.connect(context.destination);
});

function StartAudio2() {context2.resume()};
context2.audioWorklet.addModule('basicnoise.js').then(() => {
  let myNoise2 = new AudioWorkletNode(context2,'noise-generator');

  myNoise2.connect(highpass);
  highpass.connect(gain2);
  gain2.connect(delay);
  delay.connect(context2.destination);
});

I've been playing around with different functions, some of them didn't do much or I simply am not using them correctly as I am very new to the audio API scene. Any help is appreciated as this is for a school project and I know some other students want to make fire sounds and could also benefit from the crackling noise!

Upvotes: 2

Views: 1140

Answers (1)

AKX
AKX

Reputation: 168913

If you think of rain as a physical process, it's basically lots of surface impact sounds (and probably some additional ambience created by airflow). When enough raindrops hit surface(s) at a rapid enough clip, the end result ends up being noise-ish.

I think a realistic rain generator would simulate lots of single droplets hitting a surface at different distances from the listener (which causes attenuation and filtering).

That said, if you want to try just "crackling" the noise generator you have going on now, try modulating the gain node's gain value randomly; here, there's a 25% chance for the generator to be effectively muted every 20 milliseconds (or so, considering timers aren't exactly precise).

setInterval(() => {
  gain.gain.value=(Math.random() < 0.75 ? 0.4 : 0);
}, 20)

Upvotes: 1

Related Questions