Tone Generator

This is a very minimal example of how to programmatically create sounds using the new Web Audio APIs.

There are a bunch of examples on the Chromium site, but they are rather more complex than you want in a hello world.

var ctx = createAudioContext();

if (!ctx) {
  document.write(
      '<p>Web Audio APIs</a> don\'t appear to be ' +
      'supported in your browser.</p>');
} else {
  document.write(
      '<button onclick="playTone()">Play Tone</button>');
}

function playTone() {
  var offset = 0;
  var node = ctx.createJavaScriptNode(1024, 0, 1);
  node.onaudioprocess = function(e) {
    var buffer = e.outputBuffer;
    var left = buffer.getChannelData(0);
    var right = buffer.getChannelData(1);
    for (var i = 0; i < left.length; i++) {
      left[i] = right[i] = Math.sin(2000 * Math.PI * offset++ / 44100);
    }
  };
  node.connect(ctx.destination);

  setTimeout(function() { node.disconnect(); }, 1000);
}

function createAudioContext() {
  if ('AudioContext' in window) {
    return new AudioContext();
  } else if ('webkitAudioContext' in window) {
    return new webkitAudioContext();
  } else {
    return null;
  }
}