cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-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.

            
              <div class="wrapper">
  <div class='button' id='js-trigger-sound'>Click for night vision sound</div>
</div>
            
          
!
            
              @import url(https://fonts.googleapis.com/css?family=Karla:400);

body{
  background: #f8f8f8;
  font-family:Karla, sans-serif;
}

.wrapper{
	width:400px;
	height:100px;
	position:absolute;
	left:50%;
	top:50%;
	margin:-50px 0 0 -200px;
  text-align:center;
}

.button{
  font-size: 24px;
  border: solid 1px #333;
  width:200px;
  border-radius:3px;
  padding:15px;
  transition: all 0.5s;
  margin:0 auto 40px;
}

.button:hover{
  color: #55D43F;
  border-color:#55D43F;
  transition-duration:0s;
  cursor:pointer;
}
            
          
!
            
              // polyfill
var AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext;

function NVD(context) {
  this.context = context; // the audio context
  this.downSweepTime = 0.05; // the time taken for the initial sweep 
  this.upSweepTime = 1.525; // the time taken for the second sweep 
  this.initialFrequency = 5000;
  this.lowestFrequency = 1500;
  this.finalFrequency = 2500;
  this.maxGainValue = 0.2;

  this.gainNode = context.createGain();
  this.gainNode.connect(this.context.destination);
}


NVD.prototype.play = function(time) {
  this.osc = context.createOscillator();
	this.osc.connect(this.gainNode);
  this.setOscillatorWaveform();
  this.osc.start(time)
  this.osc.frequency.setValueAtTime(this.initialFrequency, time);
  this.osc.frequency.linearRampToValueAtTime(this.lowestFrequency, time + this.downSweepTime);
  this.osc.frequency.linearRampToValueAtTime(this.finalFrequency, time + this.downSweepTime + this.upSweepTime);

  this.gainNode.gain.setValueAtTime(this.maxGainValue, time);
  this.gainNode.gain.linearRampToValueAtTime(0, time + this.downSweepTime + this.upSweepTime);

  this.osc.stop(time + this.downSweepTime + this.upSweepTime);
}

NVD.prototype.setOscillatorWaveform = function() {

  var coeffs = [
    0,
    0.010,
    0.022,
    0.007,
    0.004,
    0.004
  ];
  var real = new Float32Array(coeffs);
  var imag = new Float32Array(real.length); // creates an array of zeros.
  var wave = context.createPeriodicWave(real, imag);
  this.osc.setPeriodicWave(wave);
}

var context = new AudioContext();

$("#js-trigger-sound").click(function(e) {
  e.preventDefault();
  var nvd = new NVD(context);
  nvd.play(context.currentTime);
});
            
          
!
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