How to use web audio API using JavaScript
What is the web audio API?
The Web Audio API provides a powerful and versatile system to control audio on the web. This
It can be used to add effects and filters to an audio source on the web. The audio source can be from the <audio>, video/audio source files, or an audio network stream.
Code
Let’s look at an example.
Explanation
-
This example channels the audio from an
<audio>element to anAudioContext. -
Sound effects (like panning) are added to the audio source before being channeled to the speakers’ audio output.
-
The
Initbutton calls theinitfunction when clicked. This will create anAudioContextinstance and set it toaudioContext. -
Next, it creates a media source,
createMediaElementSource(audio), and passes the audio element as the audio source.
-
The volume node
volNodeis created usingcreateGain. Here, we adjust the volume of the audio. -
Next, the panning effect is set using the
StereoPannerNode. -
Finally, the nodes are connected to the media source.
-
The buttons (
Play,Pause,Stop) play, pause and stop the audio. -
We have a volume and panning range slider. If these are changed, the volume and the panning effect of the audio are affected.
Free Resources
- undefined by undefined