Generating Sound with the Web Audio API
19 March 2011
The nascent Web Audio API is a neat addition to the HTML5 (6?) landscape. It opens the door to realtime audio synthesis directly in JavaScript, something that is currently only possible using Flash.
It’s easy to use. The following code generates a simple sine wave:
(When originally created, this used
webkitAudioContext
and createJavaScriptNode
. The API has changed, so I updated it in February 2023 to use createScriptProcessor
, although that API is also deprecated. There's also a newer version that lets you enter a formula to generate the sound.)
If you're running a build of Safari or Chromium with support for the API (available here), running the example and clicking “play” will produce a lovely tone.