cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              
<h1>White noise - Web Audio API example</h1>

<p>A simple method to play white noise on user interactions</p>

<h2>Noise loop</h2>
<p>
  <button onclick="Noise.play(noise)">Play noise</button>
  <button onclick="Noise.fade(noise)">Fade noise</button>
  <button onclick="Noise.stop(noise)">Stop that bloody racket!</button>
</p>
<p>
  <button onmouseover="Noise.play(noise2={volume:.05,fadeIn:.5,fadeOut:1.3})" onmouseout="Noise.fade(noise2)">Hover noise</button>
</p>

<h2>Is the JavaScript no?</h2>

<figure>
  <figcaption>Detection scripts</figcaption>
  <pre><code spellcheck=false contenteditable=true>
const supportsES6=function(){try{return new Function("(a = 0) => a"),!0}catch(n){return!1}}();
</code></pre>
</figure>

<figure>
  <figcaption>Google Closure compiled (1.07KB &cong; 501b gzip)</figcaption>
  <pre><code spellcheck=false contenteditable=true>
// White noise generator: play, fade, stop : https://codepen.io/2kool2/pen/xrLeMq?editors=1000 - M.J. Foskett @ websemantics.uk
var Noise=function(){function d(a){a.audioSource&&(clearTimeout(c),a.audioSource.stop())}if(supportsES6){var b=new (window.AudioContext||window.webkitAudioContext),c;return{play:function(a){d(a);a.audioSource=b.createBufferSource();a.gainNode=b.createGain();a.audioSource.connect(a.gainNode);a.gainNode.connect(b.destination);a.canFade=!0;for(var c=2*b.sampleRate,f=b.createBuffer(1,c,b.sampleRate),g=f.getChannelData(0),e=0;e&lt;c;e++)g[e]=2*Math.random()-1;a.audioSource.buffer=f;a.volume=0&lt;=a.volume?a.volume:.5;a.fadeIn=a.fadeIn?0&lt;=a.fadeIn?a.fadeIn:.5:.5;a.gainNode.gain.setValueAtTime(0,b.currentTime);a.gainNode.gain.linearRampToValueAtTime(a.volume/4,b.currentTime+a.fadeIn/2);a.gainNode.gain.linearRampToValueAtTime(a.volume,b.currentTime+a.fadeIn);a.audioSource.loop=!0;a.audioSource.start()},stop:d,fade:function(a){a.fadeOut=a.fadeOut?0&lt;=a.fadeOut?a.fadeOut:.5:.5;a.canFade?(a.gainNode.gain.linearRampToValueAtTime(0,b.currentTime+a.fadeOut),a.canFade=!1,c=setTimeout(function(){d(a)},1E3*a.fadeOut)):d(a)}}}}();</code></pre>
</figure>


<figure>
  <figcaption>Usage</figcaption>
  <pre><code spellcheck=false contenteditable=true>
var noise = {
  volume : .5,   // 0.5 is default, may be omitted
  fadeIn : .5,   // 0 is default, may be omitted
  fadeOut : .5,  // 0 is default, may be omitted
};

if (supportsES6) {

  // Play
  Noise.play(track1);

  // Stop
  Noise.stop(track1);

  // Fade out
  Noise.fade(track1);

}</code></pre>
</figure>
            
          
!
            
              body {
  font-family: sans-serif;
  line-height: 1.5;
  margin: 1rem;
}
h1, h2{
  font-weight: 100;
}
h2 {
  margin-top: 3rem;
}
h1 + p {
  font-size:larger;
}
button {
  padding: .5rem;
  font-size: inherit;
}
figure {
  margin: 1rem 0;
  padding: 1rem;
  border: 1px solid #eee;
  background-color: #f7f7f7;
}
figcaption {
  font-weight: 700
}
pre {
  overflow: hidden;
  margin: 0;
}
pre > code {
  overflow-x: scroll;
  display: block;
  padding: 0;
  font-size:1rem;
}
            
          
!
            
              var supportsES6 = function() {
  try {
    new Function("(a = 0) => a");
    return true;
  }
  catch (err) {
    return false;
  }
}();

// Web Audio API - White noise, generate, play (volume, fadeIn, fadeOut, loop), stop, fade - v1.0 MJF @ websemantics.uk
//*
var Noise = (function () {

  "use strict";
  if (!supportsES6) {return;}

  const audioContext = new(window.AudioContext || window.webkitAudioContext);
  
  let fadeOutTimer;
  
  // https://noisehack.com/generate-noise-web-audio-api/
  function createNoise(track) {

    const bufferSize = 2 * audioContext.sampleRate;
    const noiseBuffer = audioContext.createBuffer(1, bufferSize, audioContext.sampleRate);
    const output = noiseBuffer.getChannelData(0);

    for (let i = 0; i < bufferSize; i++) {
      output[i] = Math.random() * 2 - 1;
    }
    
    track.audioSource.buffer = noiseBuffer;
  }

  function stopNoise(track) {
    if (track.audioSource) {
      clearTimeout(fadeOutTimer);
      track.audioSource.stop();
    }
  }
  
  function fadeNoise(track) {
    
    if (track.fadeOut) {
      track.fadeOut = (track.fadeOut >= 0) ? track.fadeOut : 0.5;
    } else {
      track.fadeOut = 0.5;
    }

    if (track.canFade) {
      
      track.gainNode.gain.linearRampToValueAtTime(0, audioContext.currentTime + track.fadeOut);

      track.canFade = false;

      fadeOutTimer = setTimeout(() => {
        stopNoise(track);
      }, track.fadeOut * 1000);

    } else {
      stopNoise(track);
    }

  }

  function buildTrack(track) {
    track.audioSource = audioContext.createBufferSource();
    track.gainNode = audioContext.createGain();
    track.audioSource.connect(track.gainNode);
    track.gainNode.connect(audioContext.destination);
    track.canFade = true; // used to prevent fadeOut firing twice
  }
  
  function setGain(track) {

    track.volume = (track.volume >= 0) ? track.volume : 0.5;
    
    if (track.fadeIn) {
      track.fadeIn = (track.fadeIn >= 0) ? track.fadeIn : 0.5;
    } else {
      track.fadeIn = 0.5;
    }

    track.gainNode.gain.setValueAtTime(0, audioContext.currentTime);

    track.gainNode.gain.linearRampToValueAtTime(track.volume / 4, audioContext.currentTime + track.fadeIn / 2);

    track.gainNode.gain.linearRampToValueAtTime(track.volume, audioContext.currentTime + track.fadeIn);

  }

  function playNoise(track) {

    stopNoise(track);
    buildTrack(track);
    createNoise(track);
    setGain(track);
    track.audioSource.loop = true;
    track.audioSource.start();
  }

  // Expose functions:
  return {
    play : playNoise,
    stop : stopNoise,
    fade : fadeNoise
  }

}());
//*/

/*
var Noise=function(){function d(a){a.audioSource&&(clearTimeout(c),a.audioSource.stop())}if(supportsES6){var b=new (window.AudioContext||window.webkitAudioContext),c;return{play:function(a){d(a);a.audioSource=b.createBufferSource();a.gainNode=b.createGain();a.audioSource.connect(a.gainNode);a.gainNode.connect(b.destination);a.canFade=!0;for(var c=2*b.sampleRate,f=b.createBuffer(1,c,b.sampleRate),g=f.getChannelData(0),e=0;e<c;e++)g[e]=2*Math.random()-1;a.audioSource.buffer=f;a.volume=0<=a.volume?a.volume:
.5;a.fadeIn=a.fadeIn?0<=a.fadeIn?a.fadeIn:.5:.5;a.gainNode.gain.setValueAtTime(0,b.currentTime);a.gainNode.gain.linearRampToValueAtTime(a.volume/4,b.currentTime+a.fadeIn/2);a.gainNode.gain.linearRampToValueAtTime(a.volume,b.currentTime+a.fadeIn);a.audioSource.loop=!0;a.audioSource.start()},stop:d,fade:function(a){a.fadeOut=a.fadeOut?0<=a.fadeOut?a.fadeOut:.5:.5;a.canFade?(a.gainNode.gain.linearRampToValueAtTime(0,b.currentTime+a.fadeOut),a.canFade=!1,c=setTimeout(function(){d(a)},1E3*a.fadeOut)):
d(a)}}}}();
//*/
var noise = {
  volume: 0.05, // 0 - 1
  fadeIn: 2.5, // time in seconds
  fadeOut: 1.3, // time in seconds
}

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console