cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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-toggle-ringback-tone'>Toggle Ringback Tone</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 Tone(context, freq1, freq2) {
  this.context = context;
  this.status = 0;
  this.freq1 = freq1;
  this.freq2 = freq2;
}

Tone.prototype.setup = function() {
	this.osc1 = context.createOscillator();
	this.osc2 = context.createOscillator();
	this.osc1.frequency.value = this.freq1;
	this.osc2.frequency.value = this.freq2;

	this.gainNode = this.context.createGain();
	this.gainNode.gain.value = 0.25;

	this.filter = this.context.createBiquadFilter();
	this.filter.type = "lowpass";
	this.filter.frequency = 8000;

	this.osc1.connect(this.gainNode);
	this.osc2.connect(this.gainNode);

	this.gainNode.connect(this.filter);
	this.filter.connect(context.destination);
}

Tone.prototype.start = function() {
  this.setup();
  this.osc1.start(0);
  this.osc2.start(0);
  this.status = 1;
}

Tone.prototype.stop = function() {
  this.osc1.stop(0);
  this.osc2.stop(0);
  this.status = 0;
}

Tone.prototype.createRingerLFO = function() {
  // Create an empty 3 second mono buffer at the
  // sample rate of the AudioContext
  var channels = 1;
  var sampleRate = this.context.sampleRate;
  var frameCount = sampleRate * 3;
  var myArrayBuffer = this.context.createBuffer(channels, frameCount, sampleRate);

  // getChannelData allows us to access and edit the buffer data and change.
  var bufferData = myArrayBuffer.getChannelData(0);
  for (var i = 0; i < frameCount; i++) {
    // if the sample lies between 0 and 0.4 seconds, or 0.6 and 1 second, we want it to be on.
    if ((i / sampleRate > 0 && i / sampleRate < 0.4) || (i / sampleRate > 0.6 && i / sampleRate < 1.0)) {
      bufferData[i] = 0.25;
    }
  }

  this.ringerLFOBuffer = myArrayBuffer;
}

Tone.prototype.startRinging = function() {
  this.start();
  // set our gain node to 0, because the LFO is callibrated to this level
  this.gainNode.gain.value = 0;
  this.status = 1;

  this.createRingerLFO();

  this.ringerLFOSource = this.context.createBufferSource();
  this.ringerLFOSource.buffer = this.ringerLFOBuffer;
  this.ringerLFOSource.loop = true;
  // connect the ringerLFOSource to the gain Node audio param
  this.ringerLFOSource.connect(this.gainNode.gain);
  this.ringerLFOSource.start(0);
}

Tone.prototype.stopRinging = function() {
  this.stop();
  this.ringerLFOSource.stop(0);
}

var context = new AudioContext();
var ringbackTone = new Tone(context, 400, 450);
$("#js-toggle-ringback-tone").click(function(e) {
  e.preventDefault();
  if (ringbackTone.status === 0) {
    // The tone is currently off, so we need to turn it on.
    ringbackTone.startRinging();
  } else {
    // The tone is currently on, so we need to turn it off.
    ringbackTone.stopRinging();
  }
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console