Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <p>This is a web audio test and a proof of concept of a binaural beats generator.  Needs to be listen to through headphones.</p>

<p>
  Carrier Frequency: <span id="carrierLabel">250</span><br>
  <input class="slider" id="carrier" type="range" value="250" min="100" max="1000" step="1" oninput="handleCarrierChange(value)" />
</p>
<p>
  Binaural Beat Frequency: <span id="beatLabel">10</span><br>
  <input class="slider" id="beat" type="range" value="10" min="0" max="40" step="1" oninput="handleBeatChange(value)" />
</p>
<p>
  Volume: <span id="volumeLabel">50</span><br>
  <input class="slider" id="beat" type="range" value="50" min="0" max="100" step="1" oninput="handleVolumeChange(value)" />
</p>

<p>
  <button name="toggleButton" onclick="toggleSound()">Start / Stop</button>
</p>

<p>If you don't know what a binaural beat is <a href="http://deadlygeek.com/binaural-beats/binaural-beats-music-for-the-mind/">check out my article</a> or this <a href="http://en.wikipedia.org/wiki/Binaural_beats">wiki article</a>.</p>
              
            
!

CSS

              
                input.slider {
  width: 300px;
}

label {
	font-size: 13px;
	width: 110px;
}
              
            
!

JS

              
                var carrier_freq = 250;
var beat_freq = 10;
var isPlaying = false;
var left_osc, right_osc;
var volume = .5;
var AudioContext = window.AudioContext || window.webkitAudioContext;

var cntx = new AudioContext();

var merger = cntx.createChannelMerger(2);

var volumeNode = cntx.createGain();
volumeNode.gain.value = volume;

merger.connect(volumeNode);
volumeNode.connect(cntx.destination);

function toggleSound() {
  if (isPlaying) {
    isPlaying = false;
    right_osc.stop();
    left_osc.stop();
  } else {
    //the oscillators are created here because once they are stopped they can not 
    //be started again.
    left_osc = cntx.createOscillator();
    right_osc = cntx.createOscillator();
    setFreqs();
    right_osc.connect(merger, 0, 0);
    left_osc.connect(merger, 0, 1);
    right_osc.start();
    left_osc.start();
    isPlaying = true;
  }
}

function setFreqs() {
   if (typeof right_osc != "undefined") {
     right_osc.frequency.value = carrier_freq;
     left_osc.frequency.value = carrier_freq + beat_freq;
   }
}

function handleVolumeChange(val) {
  var element = document.getElementById("volumeLabel");
  element.innerHTML = val;
  volume = Number(val)/100;
  volumeNode.gain.value = volume;
}

function handleCarrierChange(val) {
  var element = document.getElementById("carrierLabel");
  element.innerHTML = val;
  carrier_freq = Number(val);
  setFreqs();
}

function handleBeatChange(val) {
  var element = document.getElementById("beatLabel");
  element.innerHTML = val;
  beat_freq = Number(val);
  setFreqs();
}
              
            
!
999px

Console