Audio waveform visualizations with WebAudio API for Mic Input
loader image
Quick Tips

Audio waveform visualizations with WebAudio API for Mic Input

In this article, we are going to see what is WebAudio API and how to build an audio waveform chart with the WebAudio API for Mic input. The focus of this article is mainly on audio waveforms.
Let’s get into it…
To start with,

What is WebAudio API?

The Web Audio API is a high-level JavaScript API for processing and synthesizing audio in web applications. This API provides a powerful and versatile system for controlling audio on the Web, and it allows developers to choose audio sources, add effects to audio, create audio visualizations, apply spatial effects (such as panning) and much more.

What shall we read in this article?

WebAudio API provides features to handle audio in web. One of the most interesting features of the Web Audio API is the ability to extract frequency, waveform, and other data from your audio source, which can then be used to create visualizations. This article explains how the WebAudio API works, and provides a couple of basic use cases… Much like a tutorial on WebAudio API. We will focus on using the WebAudio API for Mic input.

WebAudio concepts:

The WebAudio API involves handling audio operations inside an audio context, and has been designed to allow modular routing. Basic audio operations are performed with audio nodes, which are linked together to form an audio routing graph. Several sources — with different types of channel layout — are supported even within a single context. This modular design provides the flexibility to create complex audio functions with dynamic effects.

A simple, typical workflow for web audio looks follows:

Simple and Typical Workflow for Web Audio
WebAudio API
The WebAudio API as lot of interfaces to control audio functionality and events of an audio.
For more information you can check this link:
Here we are going to use the AudioContext to connect audio files.

So, what is AudioContext?

The AudioContext interface represents an audio-processing graph built from audio modules linked together, each represented by an AudioNode. An audio context controls both the creation of the nodes it contains and the execution of the audio processing, or decoding. You need to create an AudioContext before you do anything else, as everything happens inside a context.
Let us look at an example:
Before starting to extract data we need to access the user media input (Like a Mic, for example). For that,


getUserMedia uses secure connection so it won’t work on HTTP. Therefore, we should use HTTPS.
To extract audio data, we need to create an AnalyserNode. We are using the AudioContext.createAnalyser() method as:


To connect audio source we need to use createMediaStreamSource method in the AudioContext.


The analyser node will then return audio data using a Fast Fourier Transform (fft) in a certain frequency domain, depending on what you specify as the AnalyserNode.fftSize property value (if no value is specified, the default is 2048.)
For capturing audio data, we can use the collection methods AnalyserNode.getFloatFrequencyData() and AnalyserNode.getByteFrequencyData() to capture audio frequency and AnalyserNode.getByteTimeDomainData() and AnalyserNode.getFloatTimeDomainData() to capture waveform data. These methods will copy the data to specified UnitArray() or FloatArray based on the methods. Here we going to user AnalyserNode.getFloatTimeDomainData() so we using float array to copy data.


To retrieve the data and copy to our array, we have to call the data collection method, with the array passed as it’s argument. For example:


We now have the audio data, and now for visualization, we need a canvas HTML5 element.

Let’s look at the example below:
Creating a waveform with live input:
To create oscilloscope visualisation we need to setup the buffer.


Next, we need to create and clear canvas:


We now define the updatePitch() function:


We use requestAnimationFrame() to keep looping the drawing:

Here the updatePitch() will do the audio analyzing and will draw the wave line.


The Output looks like this below:
And that’s it! We have successfully created the audio waveform visualization in the Web for the audio that we have taken as input from a mic source. You can try this out with the code that I have provided below:
I hope this has been useful. In the next blog, we shall see how to do the same with audio files (input as recorded audio) and display waveform visualisations for such audio.

The following two tabs change content below.
Manikandan Thangaraj

Manikandan Thangaraj

A Passionate full stack developer with 9 years of experience in full stack web application development and expertise in technologies like PHP and Angular. He has huge craze in learning new things about technologies and constantly shares his knowledge with others.

Leave a Reply

Your email address will not be published. Required fields are marked *

Join our 30,000+ subscribers, never miss out anything on our latest blogs, tips, tutorials, updates & more.