This method generates a raw binary wave (wav) file with a simple frequency (sine wave) and writes it directly to the audio tag output. It is compatible with all modern browsers (tested in Chrome, Safari and Firefox). Not working with Internet Explorer.

To create the binary wave file it uses "riffwave.js" library from the page "HTML5 sound synthesis via PCM encoding": http://www.codebase.es/riffwave/

<script src="riffwave.js"></script>
<script>

function play_tone(freq) {

  var samples = [];
  var samples_length = 44100;            // 1 second (44.1 KHz)
  for (var i=0; i<samples_length; i++) { // fills array with samples
    var t = i/samples_length;            // time from 0 to 1
    // Generate samples using sine wave equation (between 0 and 255)
    samples[i] = 128+Math.round(127*Math.sin(freq*2*Math.PI*t));
  }

  var wave = new RIFFWAVE();          // Create raw wav file
      wave.header.sampleRate = 44100; // 44.1Khz (1 second)
      wave.header.numChannels = 1;    // 1 channel (mono)
      wave.Make(samples);
      

  var audio = new Audio();    // Create <audio> tag
      audio.src=wave.dataURI;
      audio.play();

}

play_tone(440); // Frequency 440 Hz = "A" note

</script>
</pre>
Change frequency number to hear other notes: