Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. Web Development

The Web Audio API: Adding Sound to Your Web App

This post is part of a series called The Web Audio API.
The Web Audio API: What Is It?
The Web Audio API: Make Your Own Web Synthesizer

What happened to audio on the web? For a time the web was the platform to show off your taste in tunes. From MIDI versions of The Final Countdown bubbling in the background, to autoplaying MySpace mp3s being thrown in your face, sound was everywhere. 

Not anymore. Having been burned by these user experience atrocities, web developers stay well away from it. Sadly sound has taken a backseat on the web, while native apps flourish. 

Think of the notification sound your hear when receiving an email, or the little pop when you pull to refresh the Twitter app. Such apps show how sound can be integral to a great user experience. 

In this tutorial I'll show you how to bring sound back to the web, in a good way!

The Web Audio API

The Web Audio API is a powerful, high-performance way of manipulating sound in the browser. Before continuing this tutorial, you might want to check out the previous tutorial in this series where I covered how to make a basic sound using JavaScript as well as play an mp3 file.

For this tutorial we're going to mock up a payment page that gives us audio feedback that our payment is successful. I'll use Bootstrap to make things look nicer quicker.

You'll notice at the very bottom I've included a file called "success-sound.js". This is where we'll write our code to provide audio feedback to the user when their payment is successful. Once you've created this file, the first thing we want to do is create an AudioContext. You may remember from the last tutorial that an AudioContext is how we access the Web Audio API's various functions.

var context = new AudioContext();


One of the best things about the Web Audio API is that it allows us to create sounds from scratch without even looking at an audio file. We do this using oscillators. 

Oscillators are a way of creating a tone we can hear. They do this by generating a periodic wave at a certain frequency. The shape of this wave varies, but the most common types are sine, square, triangle and sawtooth waves. These types of waves all sound different. Let's create two triangle wave oscillators.

Oscillators are quite loud by default, so unless we want to give our users the fright of their lives, we should turn down the volume a little. Because the Web Audio API works by chaining nodes together to pipe sound around, we create and connect our oscillators to a GainNode.

Gain nodes multiply the volume of the sound coming in by the number you specify. So in this instance the volume will be one tenth of the signal being passed to it.

Let's connect everything up.

Then check we've done it right by playing the oscillators for two seconds.

At this point you should be hearing a tone when you reload your page. Not the most amazing sound, I'm sure you'll agree, but it's a start!

The oscillators we've created are playing at a default frequency. By changing this frequency, we can change the musical note you hear when it's played. This is what will make our tone a little more pleasing and is key to the feeling you want to invoke when your user hears it. Let's change our oscillator to play at note "B4", which is 493.883Hz.

Now if we reload the page, you'll hear the tone at a different pitch. You may be thinking at this point, "Why are we playing two oscillators with the exact same pitch?" Well, this leads us to a little trick we can do to make our tone sound a bit nicer.

If we detune our oscillators to make their frequencies slightly different, we end up with a nice chorus effect, making our tone sound much richer.

While our little sound sounds a lot better, it ends quite abruptly. To make this less jarring, we should quickly turn down the volume at the end of the sound; this is also known as "fading out". This is done via AudioParams which are used to automate the values of audio nodes, such as gain and frequency. We'll go into AudioParams in much more detail in the next tutorial in this series.

What we're saying here is make sure that the volume is at 0.1, 0.05 seconds before our tone finishes. Then keep turning the volume down until it reaches zero at the same time our tone finishes.

Let's wrap up our code so far into a single function and see what we've got.

To make this function a bit more powerful, I've removed some of the variables and allowed these values to be passed in. This permits us to play different notes at different frequencies. Now it's time to get creative!


Think about what you want your users to feel when they've just bought something from your online store. It's a positive experience—someone's bought something they wanted in order to make their life better somehow, no errors occurred and the transaction was processed successfully. 

Audio-wise, indication of success is actually quite simple. A musical motif that goes up in pitch at the end always sounds much more joyous than one that goes down. You don't even need to have a tune or a whole bunch of notes to convey this. To prove this theory, let's just use two single notes for our success motif.

Ahh, the sweet sound of success.

Remember, if playing around with oscillators isn't your idea of fun, you can use an mp3 file instead. Have a read of the previous tutorial to see how.

It's best to wrap these two playNote calls into a single function call so we've got an easy hook into playing our sound.

It's now up to you how you want to trigger this sound and what event you wish to play it in reaction to. For the purposes of this tutorial. let's fake an Ajax call that takes three seconds. We'll use this to pretend that some server-side transaction is happening.

All we need to do now is to add an event listener to our buy now button.

Click the button, wait three seconds, and then dance with glee as you hear the audio confirmation that your transaction was successful.

In order to change the text on the button to indicate visually that something's happened, Bootstrap provides some button helper functions to swap in the text provided in a data attribute. How this works is outside the scope of this article, but here's the code for completeness.

I hope you've found this tutorial useful and that it's encouraged you to add sound (responsibly!) to your web app. The code for this tutorial is on GitHub, as well as a demo of our final sound. The next tutorial in this series is for those of you who've caught the oscillator bug; we'll be building a web audio synthesizer.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.