Gracias, Wikipedia.
ShareCode
Permalink: http://www.treeweb.es/u/974/ 01/02/2011

ShareCode

1  <input type="text" size="4" id="freq" value="440"><label for="hz">Hz</label>2  <button onclick="start()">play</button>3  <button onclick="stop()">stop</button>4  5  <script type="text/javascript"> 6  function AudioDataDestination(sampleRate, readFn) {7  // Initialize the audio output.8  var audio = new Audio();9  audio.mozSetup(1, sampleRate);10  11  var currentWritePosition = 0;12  var prebufferSize = sampleRate / 2; // buffer 500ms13  var tail = null;14  15  // The function called with regular interval to populate 16  // the audio output buffer.17  setInterval(function() {18  var written;19  // Check if some data was not written in previous attempts.20  if(tail) { 21  written = audio.mozWriteAudio(tail);22  currentWritePosition += written;23  if(written < tail.length) {24  // Not all the data was written, saving the tail...25  tail = tail.subarray(written);26  return; // ... and exit the function.27  }28  tail = null;29  }30  31  // Check if we need add some data to the audio output.32  var currentPosition = audio.mozCurrentSampleOffset();33  var available = currentPosition + prebufferSize - currentWritePosition;34  if(available > 0) {35  // Request some sound data from the callback function.36  var soundData = new Float32Array(parseFloat(available));37  readFn(soundData);38  39  // Writing the data.40  written = audio.mozWriteAudio(soundData);41  if(written < soundData.length) {42  // Not all the data was written, saving the tail.43  tail = soundData.slice(written);44  }45  currentWritePosition += written;46  }47  }, 100);48  }49  50  // Control and generate the sound.51  52  var frequency = 0, currentSoundSample;53  var sampleRate = 44100;54  55  function requestSoundData(soundData) {56  if (!frequency) { 57  return; // no sound selected58  }59  60  var k = 2* Math.PI * frequency / sampleRate;61  for (var i=0, size=soundData.length; i<size; i++) {62  soundData[i] = Math.sin(k * currentSoundSample++);63  } 64  }65  66  var audioDestination = new AudioDataDestination(sampleRate, requestSoundData);67  68  function start() {69  currentSoundSample = 0;70  frequency = parseFloat(document.getElementById("freq").value);71  }72  73  function stop() {74  frequency = 0;75  }76  </script>77 


Este ShareCode tiene versiones:
  1. Hz play stop ... (24/04/2013)
  2. Hz play stop ... (24/04/2013)
  3. Hz play stop ... (24/04/2013)
  4. Hz play stop ... (24/04/2013)
Enlace
El enlace para compartir es:

Powered by TreeWeb
© TreeWeb 2010. Todos los derechos reservados