forked from WebAudio/web-audio-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
8447 lines (8229 loc) · 335 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='https://www.w3.org/Tools/respec/respec-w3c-common' async
class='remove'>
</script>
<script src=
"https://www.w3.org/scripts/MathJax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</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: "Chris Wilson",
company: "Google, Inc.",
companyURL: "https://www.google.com/",
mailto: "[email protected]",
w3cid: "3742" },
],
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,
otherLinks: [
{
key: "Previous editor",
data : [{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);
}
}
}
// This allow wrapping mathjax formulas in <pre> so the LaTeX code is not
// auto wrapped and keeps a nice formatting.
MathJax.Hub.Config({
tex2jax: {
skipTags: ["script","noscript","style","textarea","code"]
}
});
</script>
</head>
<body>
<section id="abstract">
<p>
This specification describes a high-level JavaScript <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="#the-audioworker-interface">direct JavaScript 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 JavaScript 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=
"#MediaStreamAudioSourceNode">MediaStream</a> from getUserMedia().
</li>
<li>Integration with WebRTC
<ul>
<li>Processing audio received from a remote peer using a
<a><code>MediaStreamAudioSourceNode</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=
"#the-audioworker-interface">directly in JavaScript</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>Doppler Shift
</li>
<li>Source / Listener based
</li>
</ul>
</li>
<li>A <a href="#Convolution">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, the most common example
being <a href=
"#AudioDestinationNode"><code>AudioDestinationNode</code></a> the
final destination to the audio hardware. 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">
<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="highlight 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">
<figcaption>
A more complex example of modular routing.
</figcaption>
</figure>
<pre class="highlight 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">
<figcaption>
Modular routing illustrating one Oscillator modulating the
frequency of another.
</figcaption>
</figure>
<pre class="highlight 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 betweens <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>AudioDestinationNode</code></a> interface, an
<a><code>AudioNode</code></a> subclass representing the final
destination for all rendered audio.
</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>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>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>An <a><code>AudioWorker</code></a> interface representing a
factory for creating custom nodes that can process audio directly in
JavaScript.
</li>
<li>An <a><code>AudioWorkerNode</code></a> interface, an
<a><code>AudioNode</code></a> representing a node processed in an
AudioWorker.
</li>
<li>An <a><code>AudioWorkerGlobalScope</code></a> interface, the
context in which AudioWorker processing scripts run.
</li>
<li>An <a><code>AudioWorkerNodeProcessor</code></a> interface,
representing a single node instance inside an audio worker.
</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>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>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>IIRFilterNode</code></a> interface, an
<a><code>AudioNode</code></a> for a general IIR filter.
</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>SpatialPannerNode</code></a> interface, an
<a><code>AudioNode</code></a> for positioning audio in 3D space.
</li>
<li>A <a><code>SpatialListener</code></a> interface, which works with
a <a>SpatialPannerNode</a> for spatialization.
</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>ConvolverNode</code></a> interface, an
<a><code>AudioNode</code></a> for applying a <a href="#Convolution">
real-time linear effect</a> (such as the sound of a concert hall).
</li>
<li>A <a><code>AnalyserNode</code></a> interface, an
<a><code>AudioNode</code></a> for use with music visualizers, or
other visualization applications.
</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>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>DynamicsCompressorNode</code></a> interface, an
<a><code>AudioNode</code></a> for dynamics compression.
</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>
<li>A <a><code>OscillatorNode</code></a> interface, an
<a><code>AudioNode</code></a> for generating a periodic waveform.
</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>PannerNode</code></a> interface, an
<a><code>AudioNode</code></a> for spatializing / positioning audio in
3D space. This has been replaced by
<a><code>SpatialPannerNode</code></a>, and
<a><code>StereoPannerNode</code></a> for simpler scenarios.
</li>
<li>An <a><code>AudioListener</code></a> interface, which works with
a <a>PannerNode</a> for spatialization.
</li>
<li>A <a><code>ScriptProcessorNode</code></a> interface, an <a><code>
AudioNode</code></a> for generating or processing audio directly in
JavaScript.
</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.
Attempts to create new Nodes on this context will throw
InvalidStateError. (AudioBuffers may still be created, through
<a href=
"#widl-BaseAudioContext-createBuffer-AudioBuffer-unsigned-long-numberOfChannels-unsigned-long-length-float-sampleRate">
createBuffer</a> or <a href=
"#widl-BaseAudioContext-decodeAudioData-Promise-AudioBuffer--ArrayBuffer-audioData-DecodeSuccessCallback-successCallback-DecodeErrorCallback-errorCallback">
decodeAudioData</a>.)
</dd>
</dl>
<dl title="enum AudioContextPlaybackCategory" class="idl">
<dt>
balanced
</dt>
<dd>
Balance audio output latency and stability/power consumption.
</dd>
<dt>
interactive
</dt>
<dd>
Provide the lowest audio output latency possible without glitching.
This is the default.
</dd>
<dt>
playback
</dt>
<dd>
Prioritize sustained playback without interruption over audio
output latency. Lowest power consumption.
</dd>
</dl>
<dl title=
"[Constructor(optional AudioContextOptions contextOptions)] interface BaseAudioContext : EventTarget"
class="idl" data-merge=
"DecodeSuccessCallback DecodeErrorCallback AudioContextOptions">
<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.
</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><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 the audio block size of 128 samples. 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>
</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>
Describes the current state of this BaseAudioContext. The context
state MUST begin in "suspended", and transitions to "running" when
system resources are acquired and audio has begun processing. For
OfflineAudioContexts, the state will remain in "suspended" until
<code>startRendering()</code> is called, at which point it will
transition to "running", and then to "closed" once audio processing
has completed and oncomplete has been fired.
<p>
When the state is "suspended", a call to <code>resume()</code>
will cause a transition to "running", or a call to
<code>close()</code> will cause a transition to "closed".
</p>
<p>
When the state is "running", a call to <code>suspend()</code>
will cause a transition to "suspended", or a call to
<code>close()</code> will cause a transition to "closed".
</p>
<p>
When the state is "closed", no further state transitions are
possible.
</p>
</dd>
<dt>
Promise<void> suspend()
</dt>
<dd>
<p>
Suspends the progression of
<a><code>BaseAudioContext</code></a>'s <a href=
"#widl-BaseAudioContext-currentTime">currentTime</a>, allows any
current context processing blocks that are already processed to
be played to the destination, and then allows the system to
release its claim on audio hardware. This is generally useful
when the application knows it will not need the
<a>BaseAudioContext</a> for some time, and wishes to let the
audio hardware power down. The promise resolves when the frame
buffer is empty (has been handed off to the hardware), or
immediately (with no other effect) if the context is already
suspended. The promise is rejected if the context has been
closed.
</p>
<p>
While the system is suspended, MediaStreams will have their
output ignored; that is, data will be lost by the real time
nature of media streams. HTMLMediaElements will similarly have
their output ignored until the system is resumed. Audio Workers
and ScriptProcessorNodes will simply not fire their
onaudioprocess events while suspended, but will resume when
resumed. For the purpose of AnalyserNode window functions, the
data is considered as a continuous stream - i.e. the
resume()/suspend() does not cause silence to appear in the
AnalyserNode's stream of data.
</p>
</dd>
<dt>
Promise<void> resume()
</dt>
<dd>
<p>
Resumes the progression of the
<a><code>BaseAudioContext</code></a>'s <a href=
"#widl-BaseAudioContext-currentTime">currentTime</a> in an audio
context that has been suspended, which may involve re-priming the
frame buffer contents. The promise resolves when the system has
re-acquired (if necessary) access to audio hardware and has begun
streaming to the destination, or immediately (with no other
effect) if the context is already running. The promise is
rejected if the context has been closed. If the context is not
currently suspended, the promise will resolve.
</p>
<p>
Note that until the first block of audio has been rendered
following a call to this method, <a href=
"#widl-BaseAudioContext-currentTime">currentTime</a> remains
unchanged.
</p>
</dd>
<dt>
Promise<void> close()
</dt>
<dd>
Closes the audio context, releasing any system audio resources used
by the <a><code>BaseAudioContext</code></a>. This will not
automatically release all BaseAudioContext-created objects, unless
other references have been released as well; however, it will
forcibly release any system audio resources that might prevent
additional AudioContexts from being created and used, suspend the
progression of the <a><code>BaseAudioContext</code></a>'s <a href=
"#widl-BaseAudioContext-currentTime">currentTime</a>, and stop
processing audio data. The promise resolves when all
AudioContext-creation-blocking resources have been released. If
this is called on <a>OfflineAudioContext</a>, then return a promise
rejected with a <code>DOMException</code> whose name is
<code>InvalidStateError</code>.
</dd>
<dt>
attribute EventHandler onstatechange
</dt>
<dd>
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 "suspended" state, and a
state change event will be fired whenever the state changes to a
different state.
</dd>
<dt>
AudioBuffer createBuffer()
</dt>
<dd>
Creates an AudioBuffer of the given size. The audio data in the
buffer will be zero-initialized (silent). A NotSupportedError
exception MUST be thrown if any of the arguments is negative, zero,
or outside its nominal range.
<dl class="parameters">
<dt>
unsigned long numberOfChannels
</dt>
<dd>
Determines how many channels the buffer will have. An
implementation must support at least 32 channels.
</dd>
<dt>
unsigned long length
</dt>
<dd>
Determines the size of the buffer in sample-frames.
</dd>
<dt>
float sampleRate
</dt>
<dd>
Describes the sample-rate of the linear PCM audio data in the
buffer in sample-frames per second. An implementation must
support sample rates in at least the range 8192 to 96000.
</dd>
</dl>
</dd>
<dt>
Promise<AudioBuffer> decodeAudioData()
</dt>
<dd>
Asynchronously decodes the audio file data contained in the
ArrayBuffer. The ArrayBuffer can, for example, be loaded from an
XMLHttpRequest's <code>response</code> attribute after setting the
<code>responseType</code> to "arraybuffer". Audio file data can be
in any of the formats supported by the <code>audio</code> or
<code>video</code> elements. The buffer passed to <a href=
"#widl-BaseAudioContext-decodeAudioData-Promise-AudioBuffer--ArrayBuffer-audioData-DecodeSuccessCallback-successCallback-DecodeErrorCallback-errorCallback">
decodeAudioData</a> has its content-type determined by sniffing, as
described in [[mimesniff]].
<dl class="parameters">
<dt>
ArrayBuffer audioData
</dt>
<dd>
An ArrayBuffer containing compressed audio data
</dd>
<dt>
optional DecodeSuccessCallback successCallback
</dt>
<dd>
A callback function which will be invoked when the decoding is
finished. The single argument to this callback is an
AudioBuffer representing the decoded PCM audio data.
</dd>
<dt>
optional DecodeErrorCallback errorCallback
</dt>
<dd>
A callback function which will be invoked if there is an error
decoding the audio file.
</dd>
</dl>
<p>
Although the primary method of interfacing with this function is
via its promise return value, the callback parameters are
provided for legacy reasons. The system shall ensure that the
<a>AudioContext</a> is not garbage collected before the promise
is resolved or rejected and any callback function is called and
completes.
</p>
<p>
The following steps must be performed:
</p>
<ol>
<li>Let <var>promise</var> be a new promise.