JavaScript Sound Playing with HTML5

If you are a big fan of new technologies on web development, then HTML should not be strange, because it seems that everybody talk about HTML5 everyday in meetings, conferences, product introductions, ...

Really HTML5 is a big improvement for web developers in tasks. We can do a lot of interesting things in HTML5 without any plug-in installation, such as playing HTML5 music/video, making HTML5 canvas, creating awesome HTML5 games, ...

And today, I would like to help you create a simple HTML5 audio player in this JavaScript article tutorial. With a few JavaScript codelines, we're able to play HTML5 audio JavaScript sounds, that's so cool!


Sampled by © JavaScriptBank.com

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:

  1. Create a basic audio setup in JavaScript
  2. Create and fill a buffer with audio samples
  3. Push the samples to the browser's sound engine

Things you will need:

  1. A copy of Firefox4, or the Firefox4 Beta.
  2. A blank document named "audio.html".
  3. 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>

Language
Translate this page to English Translate this page to French Translate this page to Vietnamese

Recent articles
Insights for Advanced Zooming and Panning in JavaScript Charts
How to open a car sharing service
Vue developer as a vital part of every software team
Vue.js developers: hire them, use them and get ahead of the competition
3 Reasons Why Java is so Popular
Migrate to Angular: why and how you should do it
The Possible Working Methods of Python Ideology
JavaScript Research Paper: 6 Writing Tips to Craft a Masterpiece
Learning How to Make Use of New Marketing Trends
5 Important Elements of an E-commerce Website


Top view articles
Adding JavaScript to WordPress Effectively with JavaScript Localization feature
Top 10 Beautiful Christmas Countdown Timers
Top 10 Best JavaScript eBooks that Beginners should Learn
65 Free JavaScript Photo Gallery Solutions
16 Free Code Syntax Highlighters by Javascript For Better Programming
Best Free Linux Web Programming Editors
Top 50 Most Addictive and Popular Facebook mini games
More 30 Excellent JavaScript/AJAX based Photo Galleries to Boost your Sites
Top 10 Free Web Chat box Plug-ins and Add-ons
The Ultimate JavaScript Tutorial in Web Design


Free JavaScript Tutorials & Articles
at www.JavaScriptBank.com