Chapter 7. Integrating with Other Technologies
The Web Audio API makes audio processing and analysis a fundamental part of the web platform. As a core building block for web developers, it is designed to play well with other technologies.
Setting Up Background Music with the <audio> Tag
As I mentioned at the very start of the book, the <audio> tag has many limitations that make
it undesirable for games and interactive applications. One advantage of
this HTML5 feature, however, is that it has built-in buffering and
streaming support, making it ideal for long-form playback. Loading a large
buffer is slow from a network perspective, and expensive from a
memory-management perspective. The <audio> tag setup is ideal for music
playback or for a game soundtrack.
Rather than going the usual path of loading a sound directly by
issuing an XMLHttpRequest and then
decoding the buffer, you can use the media stream audio source node
(MediaElementAudioSourceNode) to create
nodes that behave much like audio source nodes (AudioSourceNode), but wrap an existing
<audio> tag. Once we have this node connected to
our audio graph, we can use our knowledge of the Web Audio API to do great
things. This small example applies a low-pass filter to the <audio> tag:
window.addEventListener('load',onLoad,false);functiononLoad(){varaudio=newAudio();source=context.createMediaElementSource(audio);varfilter=context.createBiquadFilter();filter.type=filter.LOWPASS;filter.frequency.value=440;source ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access