Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a beeping sound using Mozilla audio API
    primarykey
    data
    text
    <p>I'm investigating using the mozilla audio API tone generator:</p> <pre><code> function AudioDataDestination(sampleRate, readFn) { // Initialize the audio output. var audio = new Audio(); audio.mozSetup(1, sampleRate); var currentWritePosition = 0; var prebufferSize = sampleRate / 2; // buffer 500ms var tail = null, tailPosition; // The function called with regular interval to populate // the audio output buffer. setInterval(function() { var written; // Check if some data was not written in previous attempts. if(tail) { written = audio.mozWriteAudio(tail.subarray(tailPosition)); currentWritePosition += written; tailPosition += written; if(tailPosition &lt; tail.length) { // Not all the data was written, saving the tail... return; // ... and exit the function. } tail = null; } // Check if we need add some data to the audio output. var currentPosition = audio.mozCurrentSampleOffset(); var available = currentPosition + prebufferSize - currentWritePosition; if(available &gt; 0) { // Request some sound data from the callback function. var soundData = new Float32Array(available); readFn(soundData); // Writting the data. written = audio.mozWriteAudio(soundData); if(written &lt; soundData.length) { // Not all the data was written, saving the tail. tail = soundData; tailPosition = written; } currentWritePosition += written; } }, 100); } // Control and generate the sound. var frequency = 0, currentSoundSample; var sampleRate = 44100; function requestSoundData(soundData) { if (!frequency) { return; // no sound selected } var k = 2* Math.PI * frequency / sampleRate; for (var i=0, size=soundData.length; i&lt;size; i++) { soundData[i] = Math.sin(k * currentSoundSample++); } } var audioDestination = new AudioDataDestination(sampleRate, requestSoundData); function start() { currentSoundSample = 0; frequency = parseFloat(document.getElementById("freq").value); } function stop() { frequency = 0; } </code></pre> <p>I'd like this tone to be on for 500ms then off for 500ms then repeat until a stop button is pressed. I thought I could just do this by setting the sample numbers 22,050 to 44,100 to zero. However this method does not seem to work. I think it's because the function to repopulate the buffer occurs every 100ms, but this is now beyond the limit of my knowledge. Any help would be very much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload