Generating sound with JavaScript in Firefox4 is very easy
with Mozilla's Audio Data API. No plug-ins are required! In seven
steps, and with only eight lines of code, this quick tutorial will show
you how to create and play your own tones with JavaScript in Firefox 4.
2 Min. Tutorial: Generate sound in Firefox 4 with JavaScript
This tutorial will include:
Create a basic audio setup in JavaScript
Create and fill a buffer with audio samples
Push the samples to the browser's sound engine
Things you will need:
A copy of Firefox4, or the Firefox4 Beta .
A blank document named "audio.html".
A text editor.
Step 1: Create an HTML document
<script type ="text/javascript" >
//write your code here
</script>
<button> Play</button>
Create your HTML document and add script-tags. Between the script
tags, you can begin writting your JavaScript code. Then create a button
element which we will use to play the sound.
Step 2: Create an Audio interface
Between your script tags, add the following line:
var output = new Audio( ) ;
This first JavaScript command creates a new Audio interface in
JavaScript called "output". We will use this interface to set up and
play sound in the browser.
Step 3: Set up your audio element
var output = new Audio( ) ;
output.mozSetup ( 1 , 44100 ) ;
This step uses the .mozSetup( channels, sampleRate ) method
to switch the mode of the audio interface. Here we tell the browser
that "output" should have 2 channels at 44.1 Khz. The two channels are
left & right, and 44.1Khz is the same sample rate used by music CDs.
Step 4: Create a sample buffer
var output = new Audio( ) ;
output.mozSetup ( 1 , 44100 ) ;
var samples = new Float32Array( 22050 ) ;
In Step 4 we create a sample buffer by adding a new WebGL Float32Array
in JavaScript. The length of this array is the same as the audio sample
rate. A "second's-worth" of mono audio at 44.1Khz, requires an array of
22050 indices.
Step 5: Filling the sample buffer
var output = new Audio( ) ;
output.mozSetup ( 1 , 44100 ) ;
var samples = new Float32Array( 22050 ) ;
for ( var i= 0 , l= samples.length ; i< l; i++ ) {
samples[ i] = Math.sin ( i / 20 ) ;
}
Here we create a basic for-loop and fill the sample buffer array
with a sine wave using the JavaScript Math object. The actual sound, or
"waveform" is being generated in this loop with the Sin method, filling
the buffer array with samples ranging from 1 to -1.
Step 6: Write the sample buffer out
<script type ="text/javascript" >
var output = new Audio();
output.mozSetup( 1, 44100 );
var samples = new Float32Array( 22050 );
for(var i=0, l=samples.length; i< l; i++) {
samples[ i] = Math.sin( i / 20 ) ;
}
</script>
<button onclick ="output.mozWriteAudio( samples );" > Play</button>
In this final step, we use the .mozWriteAudio( sample_array )
method to play the audio. The mozWriteAudio method takes an array of
Numbers as an argument and writes adds them to the sound engine of the
browser. The browser then uses the operating system's drivers to play
the sound over your computer speakers.
Normally you would attach this at an event listener, but for the sake of simplicity, we will add the code inline to the "onclick" event of the button element.
Step 7: Run the code
Your final code should look something like this:
<script type= "text/javascript" >
// Create an Audio interface
var output = new Audio ();
// Set up a mono channel at 44.1Khz
output . mozSetup ( 1 , 44100 );
// Create a sample buffer array
var samples = new Float32Array ( 22050 );
// Fill the sample buffer array with values
for ( var i = 0 , l = samples . length ; i < l ; i ++ ){
samples [ i ] = Math . sin ( i / 20 );
}
</script>
<!-- Play the audio out -->
<button onclick= "output.mozWriteAudio( samples );" > Play</button>