forked from WebAudio/web-audio-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
12863 lines (12638 loc) · 512 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<title>
Web Audio API
</title>
<meta charset="utf-8">
<script src='respec-w3c-common-3.3.2.js' class='remove'>
</script>
<link rel="preload" href=
"https://www.w3.org/scripts/MathJax/2.6.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"
as="script">
<link rel="preload" href=
"https://www.w3.org/scripts/MathJax/2.6.1/jax/output/HTML-CSS/jax.js?rev=2.6.1"
as="script">
<link rel="preload" href=
"https://www.w3.org/scripts/MathJax/2.6.1/jax/output/HTML-CSS/fonts/STIX/fontdata.js?rev=2.6.1"
as="script">
<link rel="preload" href=
"https://www.w3.org/scripts/MathJax/2.6.1/config/TeX-AMS-MML_HTMLorMML.js?rev=2.6.1"
as="script">
<link rel="preload" href=
"https://www.w3.org/scripts/MathJax/2.6.1/jax/element/mml/optable/BasicLatin.js?rev=2.6.1"
as="script">
<link rel="preload" href=
"https://www.w3.org/scripts/MathJax/2.6.1/jax/output/HTML-CSS/autoload/mtable.js?rev=2.6.1"
as="script">
<script class='remove'>
var respecConfig = {
specStatus: "ED",
shortName: "webaudio",
edDraftURI: "https://webaudio.github.io/web-audio-api/",
editors: [
{ name: "Paul Adenot",
company: "Mozilla",
companyURL: "https://www.mozilla.org/",
mailto: "[email protected]",
w3cid: "62410" },
{
name: "Raymond Toy",
company: "Google, Inc.",
companyURL: "https://www.google.com/",
mailto: "[email protected]",
w3cid: "66910" },
],
license: "w3c-software-doc",
previousMaturity: "WD",
previousPublishDate: "2013-10-10",
previousURI: "http://www.w3.org/TR/2012/WD-webaudio-20121213/",
wg: "Audio Working Group",
wgURI: "http://www.w3.org/2011/audio/",
wgPublicList: "public-audio",
wgPatentURI: "http://www.w3.org/2004/01/pp-impl/46884/status",
tocIntroductory: true,
copyrightStart: 2013,
testSuiteURI: "https://github.com/w3c/web-platform-tests/tree/master/webaudio",
otherLinks: [
{
key: "Previous editors",
data : [{value: "Chris Wilson (Until January 2016)"},
{value: "Chris Rogers (Until August 2013)"}] },
{
key: "Repository",
href: "https://github.com/WebAudio/web-audio-api" },
{
key: "Bug tracker",
href: "https://github.com/WebAudio/web-audio-api/issues?state=open" },
]
};
</script>
<script>
function findBadLink () {
var old = document.querySelectorAll(".badLink");
for (var i = 0 ; i < old.length; i++) {
nodes[i].style.backgroundColor = "";
nodes[i].classList.remove("badLink");
}
var nodes = document.querySelectorAll('a');
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].href) {
var id = nodes[i].href.split("/");
id = id[id.length - 1];
if (id.length != 0) {
if (id[0] == "#") {
if (document.querySelectorAll(id).length == 0) {
nodes[i].style.backgroundColor = "red";
nodes[i].classList.add("badLink");
console.log(nodes[i].textContent);
}
}
}
} else {
nodes[i].style.backgroundColor = "red";
nodes[i].classList.add("badLink");
}
}
}
function findMissingLink() {
var codetags = document.querySelectorAll("code");
for (var i = 0; i < codetags.length; i++) {
if (!(codetags[i].parentNode instanceof HTMLAnchorElement) ||
codetags[i].parentNode.href == "") {
codetags[i].style.backgroundColor = 'hotpink';
codetags[i].style.color = 'yellow';
console.log(codetags[i].innerHTML);
}
}
}
window.addEventListener("DOMContentLoaded", function () {
"use strict";
// Don't run MathJax during spec generation
if (navigator.userAgent === "respec2html") {
return;
}
new Promise(function (resolve, reject) {
var mathjax = document.createElement('script');
var url = "https://www.w3.org/scripts/MathJax/2.6.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
// Safari doesn't (yet) support load event on scripts so we have to poll. So 😢.
var id = setInterval(function () {
if (window.MathJax) {
clearInterval(id);
resolve();
}
}, 100);
mathjax.onload = function () {
clearInterval(id);
resolve();
};
mathjax.onerror = function (err) {
var error = (err instanceof Event) ? new Error(err.message) : err;
reject(error);
};
// Time out waiting after 20 seconds and reject.
setTimeout(function(){
mathjax.onerror(new Error("Loading timed out."));
}, 20000);
mathjax.id = "mathjax";
mathjax.src = url;
document.body.appendChild(mathjax);
}).then(function () {
MathJax.Hub.Config({
tex2jax: {
skipTags: ["script", "noscript", "style", "textarea", "code"]
}
});
}).catch(function (error) {
console.error(error);
});
});
</script>
<style>
.todo {
border: 1px solid red;
background-color: rgba(255, 0, 0, 0.3);
}
.todo:before {
content: "TODO:";
}
.synchronous:before {
content: "⌛ ";
}
.synchronous:hover{
border-bottom: 1px dotted gray;
}
body#respecDocument {
max-width: 60em;
}
.seclist > p {
font-style: italic;
}
</style>
</head>
<body>
<section id="abstract">
<p>
This specification describes a high-level Web <abbr title=
"Application Programming Interface">API</abbr> for processing and
synthesizing audio in web applications. The primary paradigm is of an
audio routing graph, where a number of <a><code>AudioNode</code></a>
objects are connected together to define the overall audio rendering.
The actual processing will primarily take place in the underlying
implementation (typically optimized Assembly / C / C++ code), but
<a href="#AudioWorklet">direct script processing and synthesis</a> is
also supported.
</p>
<p>
The <a href="#introduction">introductory</a> section covers the
motivation behind this specification.
</p>
<p>
This API is designed to be used in conjunction with other APIs and
elements on the web platform, notably: XMLHttpRequest [[XHR]] (using
the <code>responseType</code> and <code>response</code> attributes).
For games and interactive applications, it is anticipated to be used
with the <code>canvas</code> 2D [[2dcontext]] and WebGL [[WEBGL]] 3D
graphics APIs.
</p>
</section>
<section id="sotd"></section>
<section class="introductory">
<h2>
Introduction
</h2>
<section>
<p>
Audio on the web has been fairly primitive up to this point and until
very recently has had to be delivered through plugins such as Flash
and QuickTime. The introduction of the <code>audio</code> element in
HTML5 is very important, allowing for basic streaming audio playback.
But, it is not powerful enough to handle more complex audio
applications. For sophisticated web-based games or interactive
applications, another solution is required. It is a goal of this
specification to include the capabilities found in modern game audio
engines as well as some of the mixing, processing, and filtering
tasks that are found in modern desktop audio production applications.
</p>
<p>
The APIs have been designed with a wide variety of use cases
[[webaudio-usecases]] in mind. Ideally, it should be able to support
<i>any</i> use case which could reasonably be implemented with an
optimized C++ engine controlled via script and run in a browser. That
said, modern desktop audio software can have very advanced
capabilities, some of which would be difficult or impossible to build
with this system. Apple's Logic Audio is one such application which
has support for external MIDI controllers, arbitrary plugin audio
effects and synthesizers, highly optimized direct-to-disk audio file
reading/writing, tightly integrated time-stretching, and so on.
Nevertheless, the proposed system will be quite capable of supporting
a large range of reasonably complex games and interactive
applications, including musical ones. And it can be a very good
complement to the more advanced graphics features offered by WebGL.
The API has been designed so that more advanced capabilities can be
added at a later time.
</p>
</section>
<section>
<h2 id="Features">
Features
</h2>
<p>
The API supports these primary features:
</p>
<ul>
<li>
<a href="#ModularRouting">Modular routing</a> for simple or complex
mixing/effect architectures, including <a href=
"#mixer-gain-structure">multiple sends and submixes</a>.
</li>
<li>High dynamic range, using 32bits floats for internal processing.
</li>
<li>
<a href="#AudioParam">Sample-accurate scheduled sound playback</a>
with low <a href="#latency">latency</a> for musical applications
requiring a very high degree of rhythmic precision such as drum
machines and sequencers. This also includes the possibility of
<a href="#DynamicLifetime">dynamic creation</a> of effects.
</li>
<li>Automation of audio parameters for envelopes, fade-ins /
fade-outs, granular effects, filter sweeps, LFOs etc.
</li>
<li>Flexible handling of channels in an audio stream, allowing them
to be split and merged.
</li>
<li>Processing of audio sources from an <code>audio</code> or <code>
video</code> <a href="#MediaElementAudioSourceNode">media
element</a>.
</li>
<li>Processing live audio input using a <a href=
"#MediaStreamTrackAudioSourceNode">MediaStream</a> from
getUserMedia().
</li>
<li>Integration with WebRTC
<ul>
<li>Processing audio received from a remote peer using a
<a><code>MediaStreamTrackAudioSourceNode</code></a> and
[[!webrtc]].
</li>
<li>Sending a generated or processed audio stream to a remote
peer using a <a><code>MediaStreamAudioDestinationNode</code></a>
and [[!webrtc]].
</li>
</ul>
</li>
<li>Audio stream synthesis and processing <a href=
"#AudioWorklet">directly using scripts</a>.
</li>
<li>
<a href="#Spatialization">Spatialized audio</a> supporting a wide
range of 3D games and immersive environments:
<ul>
<li>Panning models: equalpower, HRTF, pass-through
</li>
<li>Distance Attenuation
</li>
<li>Sound Cones
</li>
<li>Obstruction / Occlusion
</li>
<li>Source / Listener based
</li>
</ul>
</li>
<li>A <a href="convolution.html">convolution engine</a> for a wide
range of linear effects, especially very high-quality room effects.
Here are some examples of possible effects:
<ul>
<li>Small / large room
</li>
<li>Cathedral
</li>
<li>Concert hall
</li>
<li>Cave
</li>
<li>Tunnel
</li>
<li>Hallway
</li>
<li>Forest
</li>
<li>Amphitheater
</li>
<li>Sound of a distant room through a doorway
</li>
<li>Extreme filters
</li>
<li>Strange backwards effects
</li>
<li>Extreme comb filter effects
</li>
</ul>
</li>
<li>Dynamics compression for overall control and sweetening of the
mix
</li>
<li>Efficient <a href="#the-analysernode-interface">real-time
time-domain and frequency analysis / music visualizer support</a>
</li>
<li>Efficient biquad filters for lowpass, highpass, and other common
filters.
</li>
<li>A Waveshaping effect for distortion and other non-linear effects
</li>
<li>Oscillators
</li>
</ul>
<section>
<h2 id="ModularRouting">
Modular Routing
</h2>
<p>
Modular routing allows arbitrary connections between different
<a><code>AudioNode</code></a> objects. Each node can have
<dfn>inputs</dfn> and/or <dfn>outputs</dfn>. A <dfn>source
node</dfn> has no inputs and a single output. A <dfn>destination
node</dfn> has one input and no outputs. Other nodes such as
filters can be placed between the source and destination nodes. The
developer doesn't have to worry about low-level stream format
details when two objects are connected together; <a href=
"#channel-up-mixing-and-down-mixing">the right thing just
happens</a>. For example, if a mono audio stream is connected to a
stereo input it should just mix to left and right channels <a href=
"#channel-up-mixing-and-down-mixing">appropriately</a>.
</p>
<p>
In the simplest case, a single source can be routed directly to the
output. All routing occurs within an <a href=
"#AudioContext"><code>AudioContext</code></a> containing a single
<a href=
"#AudioDestinationNode"><code>AudioDestinationNode</code></a>:
</p>
<figure>
<img alt="modular routing" src="images/modular-routing1.png" width=
"305" height="128">
<figcaption>
A simple example of modular routing.
</figcaption>
</figure>
<p>
Illustrating this simple routing, here's a simple example playing a
single sound:
</p>
<pre class="example">
var context = new AudioContext();
function playSound() {
var source = context.createBufferSource();
source.buffer = dogBarkingBuffer;
source.connect(context.destination);
source.start(0);
}
</pre>
<p>
Here's a more complex example with three sources and a convolution
reverb send with a dynamics compressor at the final output stage:
</p>
<figure>
<img alt="modular routing2" src="images/modular-routing2.png"
width="561" height="411">
<figcaption>
A more complex example of modular routing.
</figcaption>
</figure>
<pre class="example">
var context = 0;
var compressor = 0;
var reverb = 0;
var source1 = 0;
var source2 = 0;
var source3 = 0;
var lowpassFilter = 0;
var waveShaper = 0;
var panner = 0;
var dry1 = 0;
var dry2 = 0;
var dry3 = 0;
var wet1 = 0;
var wet2 = 0;
var wet3 = 0;
var masterDry = 0;
var masterWet = 0;
function setupRoutingGraph () {
context = new AudioContext();
// Create the effects nodes.
lowpassFilter = context.createBiquadFilter();
waveShaper = context.createWaveShaper();
panner = context.createPanner();
compressor = context.createDynamicsCompressor();
reverb = context.createConvolver();
// Create master wet and dry.
masterDry = context.createGain();
masterWet = context.createGain();
// Connect final compressor to final destination.
compressor.connect(context.destination);
// Connect master dry and wet to compressor.
masterDry.connect(compressor);
masterWet.connect(compressor);
// Connect reverb to master wet.
reverb.connect(masterWet);
// Create a few sources.
source1 = context.createBufferSource();
source2 = context.createBufferSource();
source3 = context.createOscillator();
source1.buffer = manTalkingBuffer;
source2.buffer = footstepsBuffer;
source3.frequency.value = 440;
// Connect source1
dry1 = context.createGain();
wet1 = context.createGain();
source1.connect(lowpassFilter);
lowpassFilter.connect(dry1);
lowpassFilter.connect(wet1);
dry1.connect(masterDry);
wet1.connect(reverb);
// Connect source2
dry2 = context.createGain();
wet2 = context.createGain();
source2.connect(waveShaper);
waveShaper.connect(dry2);
waveShaper.connect(wet2);
dry2.connect(masterDry);
wet2.connect(reverb);
// Connect source3
dry3 = context.createGain();
wet3 = context.createGain();
source3.connect(panner);
panner.connect(dry3);
panner.connect(wet3);
dry3.connect(masterDry);
wet3.connect(reverb);
// Start the sources now.
source1.start(0);
source2.start(0);
source3.start(0);
}
</pre>
<p>
Modular routing also permits the output of
<a><code>AudioNode</code></a>s to be routed to an
<a><code>AudioParam</code></a> parameter that controls the behavior
of a different <a><code>AudioNode</code></a>. In this scenario, the
output of a node can act as a modulation signal rather than an
input signal.
</p>
<figure>
<img alt="modular routing3" src="images/modular-routing3.png"
width="346" height="337">
<figcaption>
Modular routing illustrating one Oscillator modulating the
frequency of another.
</figcaption>
</figure>
<pre class="example">
function setupRoutingGraph() {
var context = new AudioContext();
// Create the low frequency oscillator that supplies the modulation signal
var lfo = context.createOscillator();
lfo.frequency.value = 1.0;
// Create the high frequency oscillator to be modulated
var hfo = context.createOscillator();
hfo.frequency.value = 440.0;
// Create a gain node whose gain determines the amplitude of the modulation signal
var modulationGain = context.createGain();
modulationGain.gain.value = 50;
// Configure the graph and start the oscillators
lfo.connect(modulationGain);
modulationGain.connect(hfo.detune);
hfo.connect(context.destination);
hfo.start(0);
lfo.start(0);
}
</pre>
</section>
</section>
<section>
<h2 id="APIOverview">
API Overview
</h2>
<p>
The interfaces defined are:
</p>
<ul>
<li>An <a class="dfnref" href="#AudioContext">AudioContext</a>
interface, which contains an audio signal graph representing
connections between <a><code>AudioNode</code></a>s.
</li>
<li>An <a><code>AudioNode</code></a> interface, which represents
audio sources, audio outputs, and intermediate processing modules.
<a><code>AudioNode</code></a>s can be dynamically connected together
in a <a href="#ModularRouting">modular fashion</a>.
<a><code>AudioNode</code></a>s exist in the context of an
<a><code>AudioContext</code></a>.
</li>
<li>An <a><code>AnalyserNode</code></a> interface, an
<a><code>AudioNode</code></a> for use with music visualizers, or
other visualization applications.
</li>
<li>An <a><code>AudioBuffer</code></a> interface, for working with
memory-resident audio assets. These can represent one-shot sounds, or
longer audio clips.
</li>
<li>An <a><code>AudioBufferSourceNode</code></a> interface, an
<a><code>AudioNode</code></a> which generates audio from an
AudioBuffer.
</li>
<li>An <a><code>AudioDestinationNode</code></a> interface, an
<a><code>AudioNode</code></a> subclass representing the final
destination for all rendered audio.
</li>
<li>An <a><code>AudioParam</code></a> interface, for controlling an
individual aspect of an <a><code>AudioNode</code></a>'s functioning,
such as volume.
</li>
<li>An <a><code>AudioListener</code></a> interface, which works with
a <a>PannerNode</a> for spatialization.
</li>
<li>An <a><code>AudioWorklet</code></a> interface representing a
factory for creating custom nodes that can process audio directly
using scripts.
</li>
<li>An <a><code>AudioWorkletGlobalScope</code></a> interface, the
context in which AudioWorkletProcessor processing scripts run.
</li>
<li>An <a><code>AudioWorkletNode</code></a> interface, an
<a><code>AudioNode</code></a> representing a node processed in an
AudioWorkletProcessor.
</li>
<li>An <a><code>AudioWorkletProcessor</code></a> interface,
representing a single node instance inside an audio worker.
</li>
<li>A <a><code>BiquadFilterNode</code></a> interface, an
<a><code>AudioNode</code></a> for common low-order filters such as:
<ul>
<li>Low Pass
</li>
<li>High Pass
</li>
<li>Band Pass
</li>
<li>Low Shelf
</li>
<li>High Shelf
</li>
<li>Peaking
</li>
<li>Notch
</li>
<li>Allpass
</li>
</ul>
</li>
<li>A <a><code>ChannelMergerNode</code></a> interface, an
<a><code>AudioNode</code></a> for combining channels from multiple
audio streams into a single audio stream.
</li>
<li>A <a><code>ChannelSplitterNode</code></a> interface, an <a><code>
AudioNode</code></a> for accessing the individual channels of an
audio stream in the routing graph.
</li>
<li>A <a><code>ConstantSourceNode</code></a> interface, an
<a>AudioNode</a> for generating a nominally constant output value
with an <a>AudioParam</a> to allow automation of the value.
</li>
<li>A <a><code>ConvolverNode</code></a> interface, an
<a><code>AudioNode</code></a> for applying a <a href=
"convolution.html">real-time linear effect</a> (such as the sound of
a concert hall).
</li>
<li>A <a><code>DelayNode</code></a> interface, an
<a><code>AudioNode</code></a> which applies a dynamically adjustable
variable delay.
</li>
<li>A <a><code>DynamicsCompressorNode</code></a> interface, an
<a><code>AudioNode</code></a> for dynamics compression.
</li>
<li>A <a><code>GainNode</code></a> interface, an
<a><code>AudioNode</code></a> for explicit gain control. Because
inputs to <a><code>AudioNode</code></a>s support multiple connections
(as a unity-gain summing junction), mixers can be <a href=
"#mixer-gain-structure">easily built</a> with GainNodes.
</li>
<li>An <a><code>IIRFilterNode</code></a> interface, an
<a><code>AudioNode</code></a> for a general IIR filter.
</li>
<li>A <a><code>MediaElementAudioSourceNode</code></a> interface, an
<a><code>AudioNode</code></a> which is the audio source from an
<code>audio</code>, <code>video</code>, or other media element.
</li>
<li>A <a><code>MediaStreamAudioSourceNode</code></a> interface, an
<a><code>AudioNode</code></a> which is the audio source from a
MediaStream such as live audio input, or from a remote peer.
</li>
<li>A <a><code>MediaStreamTrackAudioSourceNode</code></a> interface,
an <a><code>AudioNode</code></a> which is the audio source from a
<code>MediaStreamTrack</code>.
</li>
<li>A <a><code>MediaStreamAudioDestinationNode</code></a> interface,
an <a><code>AudioNode</code></a> which is the audio destination to a
MediaStream sent to a remote peer.
</li>
<li>A <a><code>PannerNode</code></a> interface, an
<a><code>AudioNode</code></a> for spatializing / positioning audio in
3D space.
</li>
<li>A <a><code>PeriodicWave</code></a> interface for specifying
custom periodic waveforms for use by the
<a><code>OscillatorNode</code></a>.
</li>
<li>An <a><code>OscillatorNode</code></a> interface, an
<a><code>AudioNode</code></a> for generating a periodic waveform.
</li>
<li>A <a><code>StereoPannerNode</code></a> interface, an
<a><code>AudioNode</code></a> for equal-power positioning of audio
input in a stereo stream.
</li>
<li>A <a><code>WaveShaperNode</code></a> interface, an
<a><code>AudioNode</code></a> which applies a non-linear waveshaping
effect for distortion and other more subtle warming effects.
</li>
</ul>
<p>
There are also several features that have been deprecated from the
Web Audio API but not yet removed, pending implementation experience
of their replacements:
</p>
<ul>
<li>A <a><code>ScriptProcessorNode</code></a> interface, an <a><code>
AudioNode</code></a> for generating or processing audio directly
using scripts.
</li>
<li>An <a><code>AudioProcessingEvent</code></a> interface, which is
an event type used with <a><code>ScriptProcessorNode</code></a>
objects.
</li>
</ul>
</section>
</section>
<section id="conformance">
<p>
The following conformance classes are defined by this specification:
</p>
<dl>
<dt>
<dfn id="dfn-conforming-implementation">conforming
implementation</dfn>
</dt>
<dd>
<p>
A user agent is considered to be a <a class="dfnref" href=
"#dfn-conforming-implementation">conforming implementation</a> if
it satisfies all of the MUST-, REQUIRED- and SHALL-level criteria
in this specification that apply to implementations.
</p>
</dd>
</dl>
<p>
User agents that use ECMAScript to implement the APIs defined in this
specification must implement them in a manner consistent with the
ECMAScript Bindings defined in the Web IDL specification [[!WEBIDL]] as
this specification uses that specification and terminology.
</p>
</section>
<section id="audioapi">
<h2 id="API">
The Audio API
</h2>
<section>
<h2 id="BaseAudioContext">
The BaseAudioContext Interface
</h2>
<p>
This interface represents a set of <a><code>AudioNode</code></a>
objects and their connections. It allows for arbitrary routing of
signals to an <a><code>AudioDestinationNode</code></a>. Nodes are
created from the context and are then <a href=
"#ModularRouting">connected</a> together.
</p>
<p>
<a><code>BaseAudioContext</code></a> is not instantiated directly,
but is instead extended by the concrete interfaces
<a><code>AudioContext</code></a> (for real-time rendering) and
<a><code>OfflineAudioContext</code></a> (for offline rendering).
</p>
<dl title="enum AudioContextState" class="idl">
<dt>
suspended
</dt>
<dd>
This context is currently suspended (context time is not
proceeding, audio hardware may be powered down/released).
</dd>
<dt>
running
</dt>
<dd>
Audio is being processed.
</dd>
<dt>
closed
</dt>
<dd>
This context has been released, and can no longer be used to
process audio. All system audio resources have been released.
<span class="synchronous">Attempts to create new Nodes on the
AudioContext will throw <code>InvalidStateError</code></span>.
(AudioBuffers may still be created, through <a href=
"#widl-BaseAudioContext-createBuffer-AudioBuffer-unsigned-long-numberOfChannels-unsigned-long-length-float-sampleRate">
createBuffer</a>, <a href=
"#widl-BaseAudioContext-decodeAudioData-Promise-AudioBuffer--ArrayBuffer-audioData-DecodeSuccessCallback-successCallback-DecodeErrorCallback-errorCallback">
decodeAudioData</a>, or the <a>AudioBuffer</a> constructor.)
</dd>
</dl>
<dl title="interface BaseAudioContext : EventTarget" class="idl"
data-merge="DecodeSuccessCallback DecodeErrorCallback">
<dt>
readonly attribute AudioDestinationNode destination
</dt>
<dd>
<p>
An <a href=
"#AudioDestinationNode"><code>AudioDestinationNode</code></a>
with a single input representing the final destination for all
audio. Usually this will represent the actual audio hardware. All
<a><code>AudioNode</code></a>s actively rendering audio will
directly or indirectly connect to <a href=
"#widl-BaseAudioContext-destination"><code>destination</code></a>.
</p>
</dd>
<dt>
readonly attribute float sampleRate
</dt>
<dd>
<p>
The sample rate (in sample-frames per second) at which the
<a><code>BaseAudioContext</code></a> handles audio. It is assumed
that all <a><code>AudioNode</code></a>s in the context run at
this rate. In making this assumption, sample-rate converters or
"varispeed" processors are not supported in real-time processing.
The <dfn>Nyquist frequency</dfn> is half this sample-rate value.
</p>
</dd>
<dt>
readonly attribute double currentTime
</dt>
<dd>
<p>
This is the time in seconds of the sample frame immediately
following the last sample-frame in the block of audio most
recently processed by the context's rendering graph. If the
context's rendering graph has not yet processed a block of audio,
then <a href=
"#widl-BaseAudioContext-currentTime"><code>currentTime</code></a>
has a value of zero.
</p>
<p>
In the time coordinate system of <a href=
"#widl-BaseAudioContext-currentTime"><code>currentTime</code></a>,
the value of zero corresponds to the first sample-frame in the
first block processed by the graph. Elapsed time in this system
corresponds to elapsed time in the audio stream generated by the
<a><code>BaseAudioContext</code></a>, which may not be
synchronized with other clocks in the system. (For an
<a><code>OfflineAudioContext</code></a>, since the stream is not
being actively played by any device, there is not even an
approximation to real time.)
</p>
<p>
All scheduled times in the Web Audio API are relative to the
value of <a href=
"#widl-BaseAudioContext-currentTime"><code>currentTime</code></a>.
</p>
<p>
When the <a><code>BaseAudioContext</code></a> is in the <a href=
"#idl-def-AudioContextState.running"><code>running</code></a>
state, the value of this attribute is monotonically increasing
and is updated by the rendering thread in uniform increments,
corresponding to one <a href="#dfn-render-quantum">render
quantum</a>. Thus, for a running context,
<code>currentTime</code> increases steadily as the system
processes audio blocks, and always represents the time of the
start of the next audio block to be processed. It is also the
earliest possible time when any change scheduled in the current
state might take effect.
</p>
<p>
<code>currentTime</code> MUST be read <a data-lt=
"atomic">atomically</a> on the control thread before being
returned.
</p>
</dd>
<dt>
readonly attribute AudioListener listener
</dt>
<dd>
<p>
An <a href="#AudioListener"><code>AudioListener</code></a> which
is used for 3D <a href="#Spatialization">spatialization</a>.
</p>
</dd>
<dt>
readonly attribute AudioContextState state
</dt>
<dd>
<p>
Describes the current state of the <a>AudioContext</a>, on the
<a>control thread</a>.
</p>
</dd>
<dt>
Promise<void> resume()
</dt>
<dd>
<p>
Resumes the progression of the <a>BaseAudioContext</a>'s <a href=
"#widl-AudioContext-currentTime">currentTime</a> when it has been
suspended.
</p>
<p>
<span class="synchronous">When resume is called, execute these
steps:</span>
</p>
<ol>
<li>Let <em>promise</em> be a new Promise.
</li>
<li>If the <em>control thread state</em> flag on the
<a>BaseAudioContext</a> is <code>closed</code> reject the promise
with <code>InvalidStateError</code>, abort these steps, returning
<em>promise</em>.
</li>
<li>If the <a href="#widl-BaseAudioContext-state">state</a>
attribute of the <a>BaseAudioContext</a> is already
<code>running</code>, resolve <em>promise</em>, return it, and
abort these steps.
</li>
<li>If the <a>BaseAudioContext</a> is not <a>allowed to
start</a>, append <em>promise</em> to
<a>pendingResumePromises</a> and abort these steps, returning
<em>promise</em>.
</li>
<li>Set the <em>control thread state</em> flag on the
<a>BaseAudioContext</a> to <code>running</code>.
</li>
<li>
<a href="#queue">Queue a control message</a> to resume the
<a>BaseAudioContext</a>.
</li>
<li>Return <em>promise</em>.
</li>
</ol>
<p>
Running a <a>control message</a> to resume an
<a>BaseAudioContext</a> means running these steps on the
<a>rendering thread</a>:
</p>
<ol>
<li>Attempt to <a href="#acquiring">acquire system resources</a>.
</li>
<li>Set the <a>rendering thread state</a> flag on the
<a>BaseAudioContext</a> to <code>running</code>.
</li>
<li>Start <a href="#rendering-loop">rendering the audio
graph</a>.
</li>
<li>In case of failure, queue a task on the <a>control thread</a>
to execute the following, and abort these steps
<ol>
<li>Reject all promises from <a>pendingResumePromises</a> in
order, then clear <a>pendingResumePromises</a>.
</li>
<li>Reject <em>promise</em>.
</li>
</ol>
</li>
<li>Queue a task on the <a>control thread</a>'s event loop, to
execute these steps:
<ol>
<li>Resolve all promises from <a>pendingResumePromises</a> in
order, then clear <a>pendingResumePromises</a>.
</li>
<li>Resolve <em>promise</em>.
</li>
<li>If the <a href="#widl-BaseAudioContext-state">state</a>
attribute of the <a>BaseAudioContext</a> is not already
<code>running</code>:
<ol>
<li>Set the <a href=
"#widl-BaseAudioContext-state">state</a> attribute of the
<a>BaseAudioContext</a> to <code>running</code>.
</li>
<li>Queue a task to fire a simple event named
<code>statechange</code> at the <a>BaseAudioContext</a>.
</li>
</ol>
</li>
</ol>
</li>
</ol>
</dd>
<dt>
attribute EventHandler onstatechange
</dt>
<dd>
<p>
A property used to set the <code>EventHandler</code> for an event
that is dispatched to <a><code>BaseAudioContext</code></a> when
the state of the AudioContext has changed (i.e. when the
corresponding promise would have resolved). An event of type
<a><code>Event</code></a> will be dispatched to the event
handler, which can query the AudioContext's state directly. A
newly-created AudioContext will always begin in the
<code>suspended</code> state, and a state change event will be
fired whenever the state changes to a different state. This event
is fired before the <a><code>oncomplete</code></a> event is
fired.
</p>
</dd>
<dt>
AudioBuffer createBuffer()
</dt>
<dd>
<p>
Creates an AudioBuffer of the given size. The audio data in the
buffer will be zero-initialized (silent). <span class=
"synchronous">A <code>NotSupportedError</code> exception MUST be
thrown if any of the arguments is negative, zero, or outside its
nominal range.</span>
</p>
<dl class="parameters">
<dt>
unsigned long numberOfChannels