-
Notifications
You must be signed in to change notification settings - Fork 49
/
index.html
1532 lines (1528 loc) · 63.8 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 lang="en">
<head>
<title>
Web MIDI API
</title>
<meta charset="utf-8">
<script src='https://www.w3.org/Tools/respec/respec-w3c' class=
'remove'></script>
<script class='remove'>
var respecConfig = {
shortName: "webmidi",
specStatus: "ED",
copyrightStart: "2015",
editors: [
{ name: "Chris Wilson",
url: "mailto:[email protected]",
w3cid: "3742",
company: "Google",
companyURL: "http://google.com"
},
{ name: "Michael Wilson",
url: "mailto:[email protected]",
w3cid: "136815",
company: "Google",
companyURL: "http://google.com"
},
],
formerEditors: [
{ name: "Jussi Kalliokoski",
url: "http://juss.in",
w3cid: "45545"
},
],
github: "WebAudio/web-midi-api",
group: "wg/audio",
otherLinks: [
{
key: "Implementation report:",
data: [
{
value: "No preliminary interoperability or implementation report exists.",
},
],
},
],
previousPublishDate: "2013-11-26",
previousMaturity: "WD",
testSuiteURI: "https://github.com/web-platform-tests/wpt/tree/master/webmidi",
xref: {
profile: "web-platform",
specs: ["hr-time", "permissions", "permissions-policy"],
}
};
</script>
</head>
<body>
<section id="abstract">
<p>
Some user agents have music devices, such as synthesizers, keyboard and
other controllers, and drum machines connected to their host computer
or device. The widely adopted Musical Instrument Digital Interface
(MIDI) protocol enables electronic musical instruments, controllers and
computers to communicate and synchronize with each other. MIDI does not
transmit audio signals: instead, it sends event messages about musical
notes, controller signals for parameters such as volume, vibrato and
panning, cues and clock signals to set the tempo, and system-specific
MIDI communications (e.g. to remotely store synthesizer-specific patch
data). This same protocol has become a standard for non-musical uses,
such as show control, lighting and special effects control.
</p>
<p>
This specification defines an API supporting the MIDI protocol, enabling
web applications to enumerate and select MIDI input and output devices
on the client system and send and receive [=MIDI messages=]. It is
intended to enable non-music MIDI applications as well as music ones, by
providing low-level access to the [=MIDI devices=] available on the
users' systems. The Web MIDI API is not intended to describe music or
controller inputs semantically; it is designed to expose the mechanics
of MIDI input and output interfaces, and the practical aspects of
sending and receiving [=MIDI messages=], without identifying what those
actions might mean semantically (e.g., in terms of "modulate the vibrato
by 20Hz" or "play a G#7 chord", other than in terms of changing a
controller value or sending a set of note-on messages that happen to
represent a G#7 chord).
</p>
<p>
To some users, "MIDI" has become synonymous with Standard MIDI Files
and General MIDI. That is not the intent of this API; the use case of
simply playing back a .SMF file is not within the purview of this
specification (it could be considered a different format to be
supported by the HTML [^audio^] element, for example). The Web MIDI API
is intended to enable direct access to devices that respond to MIDI -
controllers, external synthesizers or lighting systems, for example.
The Web MIDI API is also explicitly designed to enable a new class of
applications on the web that can respond to MIDI controller inputs -
using external hardware controllers with physical buttons, knobs and
sliders (as well as musical controllers like keyboard, guitar or wind
instrument controllers) to control web applications.
</p>
<p>
The Web MIDI API is also expected to be used in conjunction with other
APIs and elements of the web platform, notably the [=Web Audio
API=]. This API is also intended to be familiar to users of MIDI APIs on
other systems, such as Apple's CoreMIDI and Microsoft's Windows MIDI
API.
</p>
</section>
<section id="sotd"></section>
<section class="informative">
<h2>
Introduction
</h2>
<p>
The Web MIDI API specification defines a means for web developers to
enumerate, manipulate and access [=MIDI devices=] - for example,
interfaces that may provide hardware MIDI ports with other devices
plugged in to them and USB devices that support the USB-MIDI
specification. Having a Web API for MIDI enables web applications that
use existing software and hardware synthesizers, hardware music
controllers and light systems and other mechanical apparatus controlled
by MIDI. This API has been defined with this wide variety of use cases
in mind.
</p>
<p>
The approaches taken by this API are similar to those taken in Apple's
CoreMIDI API and Microsoft's Windows MIDI API; that is, the API is
designed to represent the low-level software protocol of MIDI, in order
to enable developers to build powerful MIDI software on top. The API
enables the developer to enumerate input and output interfaces, and send
and receive [=MIDI messages=], but (similar to the aforementioned APIs)
it does not attempt to semantically define or interpret [=MIDI
messages=] beyond what is necessary to robustly support current devices.
</p>
<p>
The Web MIDI API is not intended to directly implement high-level
concepts such as sequencing; it does not directly support Standard MIDI
Files, for example, although a Standard MIDI File player can be built
on top of the Web MIDI API. It is also not intended to semantically
capture patches or controller assignments, as General MIDI does; such
interpretation is outside the scope of the Web MIDI API (though again,
General MIDI can easily be utilized through the Web MIDI API).
</p>
</section>
<section id="conformance">
<p>
This specification defines conformance criteria that apply to a single
product: the [=user agent=] that implements the interfaces that
it contains.
</p>
<p>
Implementations 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>
<h2>
Terminology
</h2>
<p>
The <dfn id="WebAudio">Web Audio API</dfn> and its associated
interfaces and concepts are defined in [[webaudio]].
</p>
<p>
The terms <dfn>MIDI</dfn>, <dfn>MIDI device</dfn>, <dfn>MIDI input
port</dfn>, <dfn>MIDI output port</dfn>, <dfn>MIDI interface</dfn>,
<dfn>MIDI message</dfn>, <dfn>System Real Time</dfn> and
<dfn>System Exclusive</dfn> are defined in [[MIDI]].
</p>
<div class="note">
<dfn>Valid MIDI message</dfn> is defined in [[MIDI]]. The
following may be used as a non-normative guide:
<ul>
<li>The first byte (the status byte) should have the high bit
set, any following bytes should not unless they are part of a
[=System Exclusive=] message</li>
<li>If the high nibble of the status byte in hex is `8`, `9`,
`A`, `B`, or `E` then the total message length should be 3
bytes
<li>If the high nibble of the status byte is `C` or `D` then
the total message length should be 2 bytes</li>
<li>If the status byte is `F1` or `F3` then the total message
length should be 2 bytes</li>
<li>If the status byte is `F2` then the total message length
should be 3 bytes</li>
<li>If the status byte is `F6`, `F8`, `FA`, `FB`, `FC`, `FE`,
or `FF` then the total message length should be 1 byte (only
the status byte)</li>
<li>If the status byte is `F0` then this is a [=System
Exclusive=] message with no length restriction, and the last
byte should be `F7`</li>
<li>If the status byte is `F4`, `F5`, `F7`, `F9`, or `FD` then
the message is not valid</li>
</ul>
</div>
</section>
<section>
<h2>
Obtaining Access to [=MIDI Devices=]
</h2>
<section data-link-for="Navigator">
<h2>
Permissions Integration
</h2>
<p>
The Web Midi API is a [=powerful feature=] that is identified
by the [=powerful feature/name=] <a>"midi"</a>. It integrates with
[[[Permissions]]] by defining the following permission-related flags:
</p>
<dl>
<dt>
[=powerful feature/permission descriptor type=]
</dt>
<dd>
<pre class="idl">
dictionary MidiPermissionDescriptor : PermissionDescriptor {
boolean sysex = false;
};
</pre>
<p>
`{name: "midi", sysex: true}` is [=PermissionDescriptor/stronger than=] `{name:
"midi", sysex: false}`.
</p>
</dd>
</dl>
</section>
<section data-link-for="Navigator">
<h2>
Permissions Policy Integration
</h2>
<p>
The Web Midi API defines a [=policy-controlled feature=] named
<dfn class="permission">"midi"</dfn> which has a
[=policy-controlled feature/default allowlist=] of `'self'`.
</p>
</section>
<section data-dfn-for="Navigator">
<h2>
Extensions to the <dfn>Navigator</dfn> interface
</h2>
<pre class="idl">
partial interface Navigator {
[SecureContext]
Promise <MIDIAccess> requestMIDIAccess(optional MIDIOptions options = {});
};
</pre>
<dl>
<dt>
<dfn>requestMIDIAccess()</dfn> method
</dt>
<dd>
<p>
When invoked, returns a Promise object representing a request for
access to [=MIDI devices=] on the user's system.
</p>
<p>
Requesting MIDI access SHOULD prompt the user for access to MIDI
devices, particularly if [=System Exclusive=] access is
requested. In some scenarios, this permission may have already
been implicitly or explicitly granted, in which case this prompt
may not appear. If the user gives express permission or the call
is otherwise approved, the vended Promise is resolved. The
underlying system may choose to allow the user to select specific
[=MIDI interfaces=] to expose to this API (i.e. pick and choose
interfaces on an individual basis), although this is not
required. The system may also choose to prompt (or not) based on
whether [=System Exclusive=] support is requested, as [=System
Exclusive=] access has greater privacy and security implications.
</p>
<p>
If the user declines or the call is denied for any other reason,
the Promise is rejected with a {{DOMException}} parameter.
</p>
<p data-link-for="Navigator">
When the {{requestMIDIAccess()}} method is called, the user agent
MUST run the following steps:
</p>
<ol>
<li>
<p>
Let <var>promise</var> be a new Promise object and
<var>resolver</var> be its associated resolver.
</p>
</li>
<li>
<p>
Return <var>promise</var> and run the following steps
asynchronously.
</p>
</li>
<li>
<p>
Let <var>document</var> be the calling context's
[=Document=].
</p>
</li>
<li>
<p>
If <var>document</var> is not [=allowed to use=] the
[=policy-controlled feature=] named <a>midi</a>, jump to
the step labeled <em>failure</em> below.
</p>
</li>
<li>
<p>
Optionally, e.g. based on a previously-established user
preference, for security reasons, or due to platform
limitations, jump to the step labeled <em>failure</em> below.
</p>
</li>
<li>
<p>
Optionally, e.g. based on a previously-established user
preference, jump to the step labeled <em>success</em> below.
</p>
</li>
<li>
<p>
Prompt the user in a user-agent-specific manner for permission
to provide the entry script's origin with a {{MIDIAccess}}
object representing control over user's [=MIDI devices=]. This
prompt may be contingent upon whether [=System Exclusive=]
support was requested, and may allow the user to enable or
disable that access.
</p>
<p>
If permission is denied, jump to the step labeled
<em>failure</em> below. If the user never responds, this
algorithm will never progress beyond this step. If permission
is granted, continue the following steps.
</p>
</li>
<li>
<p>
<em><b>success</b></em>: Let <var>access</var> be a new
{{MIDIAccess}} object. (It is possible to call
requestMIDIAccess() multiple times; this may prompt the user
multiple times, so it may not be best practice, and the same
instance of MIDIAccess will not be returned each time.)
</p>
</li>
<li>
<p>
Call <var>resolver</var>'s <code>accept(value)</code> method
with <var>access</var> as value argument.
</p>
</li>
<li>
<p>
Terminate these steps.
</p>
</li>
<li>
<p>
<em><b>failure</b></em>: Let <var>error</var> be a new
{{DOMException}}. This exception's .name should be
{{"NotAllowedError"}} if the user or their security settings
denied the application from creating a MIDIAccess instance
with the requested options, or if the error is the result of
<var>document</var> not being [=allowed to use=] the feature,
{{"AbortError"}} if the page is going to be closed for a user
navigation, {{"InvalidStateError"}} if the underlying systems
raise any errors, or otherwise it should be
{{"NotSupportedError"}}.
</p>
</li>
<li>
<p>
Call <var>resolver</var>'s <code>reject(value)</code> method
with <var>error</var> as value argument.
</p>
</li>
</ol>
</dd>
</dl>
<section data-dfn-for="MIDIOptions" data-link-for="Navigator">
<h2 id="MIDIOptions">
<dfn>MIDIOptions</dfn> Dictionary
</h2>
<p>
This dictionary contains optional settings that may be provided to
the {{requestMIDIAccess()}} request.
</p>
<pre class="idl">
dictionary MIDIOptions {
boolean sysex;
boolean software;
};
</pre>
<dl>
<dt>
<dfn>sysex</dfn>
</dt>
<dd>
<p>
This member informs the system whether the ability to send and
receive [=System Exclusive=] messages is requested or allowed on
a given {{MIDIAccess}} object. On the option passed to
{{requestMIDIAccess()}}, if this member is set to true, but
[=System Exclusive=] support is denied (either by policy or by
user action), the access request will fail with a
{{"NotAllowedError"}} error. If this support is not requested (and
allowed), the system will throw exceptions if the user tries to
send [=System Exclusive=] messages, and will silently mask out
any [=System Exclusive=] messages received on the port.
</p>
</dd>
<dt>
<dfn>software</dfn>
</dt>
<dd>
<p>
This member informs the system whether the ability to utilize any
software synthesizers installed in the host system is requested
or allowed on a given {{MIDIAccess}} object. On
{{requestMIDIAccess()}}, if this member is set to true, but
software synthesizer support is denied (either by policy or by
user action), the access request will fail with a
{{"NotAllowedError"}} error. If this support is not requested, the
system should not include any software synthesizers in the
{{MIDIAccess}} exposure of available ports.
</p>
<p>
Note that may result in a two-step request procedure if software
synthesizer support is desired but not required - software
synthesizers may be disabled when MIDI hardware device access is
allowed.
</p>
</dd>
</dl>
</section>
</section>
</section>
<section>
<h2>
The MIDI API
</h2>
<section data-dfn-for="MIDIInputMap">
<h3 id="MIDIInputMap">
<dfn>MIDIInputMap</dfn> Interface
</h3>
<pre class="idl">
[SecureContext, Exposed=(Window,Worker)] interface MIDIInputMap {
readonly maplike <DOMString, MIDIInput>;
};
</pre>
<p>
The {{MIDIInputMap}} is a maplike interface whose value is a
{{MIDIInput}} instance and key is its ID.
</p>
<p>
This type is used to represent all the currently available MIDI input
ports. This enables:
</p>
<pre class="example"> // to tell how many entries there are:
let numberOfMIDIInputs = inputs.size;
// add each of the ports to a <select> box
inputs.forEach( function( port, key ) {
let opt = document.createElement("option");
opt.text = port.name;
document.getElementById("inputportselector").add(opt);
});
// or you could express in ECMAScript 6 as:
for (let input of inputs.values()) {
let opt = document.createElement("option");
opt.text = input.name;
document.getElementById("inputportselector").add(opt);
}</pre>
</section>
<section data-dfn-for="MIDIOutputMap">
<h3 id="MIDIOutputMap">
<dfn>MIDIOutputMap</dfn> Interface
</h3>
<pre class="idl">
[SecureContext, Exposed=(Window,Worker)] interface MIDIOutputMap {
readonly maplike <DOMString, MIDIOutput>;
};
</pre>
<p>
The {{MIDIOutputMap}} is a maplike interface whose value is a
{{MIDIOutput}} instance and key is its ID.
</p>
<p>
This type is used to represent all the currently available [=MIDI
output ports=]. This enables:
</p>
<pre class="example"> // to tell how many entries there are:
let numberOfMIDIOutputs = outputs.size;
// add each of the ports to a <select> box
outputs.forEach( function( port, key ) {
let opt = document.createElement("option");
opt.text = port.name;
document.getElementById("outputportselector").add(opt);
});
// or you could express in ECMAScript 6 as:
for (let output of outputs.values()) {
let opt = document.createElement("option");
opt.text = output.name;
document.getElementById("outputportselector").add(opt);
}</pre>
</section>
<section data-dfn-for="MIDIAccess" data-link-for="MIDIAccess">
<h2 id="MIDIAccess">
<dfn>MIDIAccess</dfn> Interface
</h2>
<p>
This interface provides the methods to list MIDI input and output
devices, and obtain access to an individual device.
</p>
<pre class="idl">
[SecureContext, Exposed=(Window,Worker), Transferable] interface MIDIAccess: EventTarget {
readonly attribute MIDIInputMap inputs;
readonly attribute MIDIOutputMap outputs;
attribute EventHandler onstatechange;
readonly attribute boolean sysexEnabled;
};
</pre>
<dl>
<dt>
<dfn>inputs</dfn>
</dt>
<dd>
The [=MIDI input ports=] available to the system.
</dd>
<dt>
<dfn>outputs</dfn>
</dt>
<dd>
The [=MIDI output ports=] available to the system.
</dd>
<dt>
<dfn>onstatechange</dfn>
</dt>
<dd>
<p>
The handler called when a new port is connected or an existing
port changes the state attribute.
</p>
<p>
This [=event handler=], of type {{MIDIConnectionEvent}}, MUST be
supported by all objects implementing the {{MIDIAccess}}
interface.
</p>
<p class="note">
It is important to understand that leaving an {{EventHandler}}
attached to this object will prevent it from being
garbage-collected; when finished using the {{MIDIAccess}}, you
should remove any {{onstatechange}} listeners.
</p>
<p id="event-midiaccess-statechange">
Whenever a previously unavailable MIDI port becomes available for
use, or an existing port changes the state attribute, the user
agent SHOULD run the following steps:
</p>
<ol>
<li>Let |port:MIDIPort| be the {{MIDIPort}} corresponding to the
newly-available, or the existing port.
</li>
<li>
[=Fire an event=] named "statechange" at the {{MIDIAccess}},
using {{MIDIConnectionEvent}} with {{MIDIConnectionEvent/port}} set to |port|.
</li>
</ol>
</dd>
<dt>
<dfn>sysexEnabled</dfn>
</dt>
<dd>
This attribute informs the user whether [=System Exclusive=]
support is enabled on this MIDIAccess.
</dd>
</dl>
</section>
<section data-dfn-for="MIDIPort">
<h2 id="MIDIPort">
<dfn>MIDIPort</dfn> Interface
</h2>
<p>
This interface represents a MIDI input or output port.
</p>
<pre class="idl">
[SecureContext, Exposed=(Window,Worker)] interface MIDIPort: EventTarget {
readonly attribute DOMString id;
readonly attribute DOMString? manufacturer;
readonly attribute DOMString? name;
readonly attribute MIDIPortType type;
readonly attribute DOMString? version;
readonly attribute MIDIPortDeviceState state;
readonly attribute MIDIPortConnectionState connection;
attribute EventHandler onstatechange;
Promise <MIDIPort> open();
Promise <MIDIPort> close();
};
</pre>
<dl>
<dt>
<dfn>id</dfn>
</dt>
<dd>
<p>
A unique ID of the port. This can be used by developers to
remember ports the user has chosen for their application. The
User Agent MUST ensure that the {{MIDIPort/id}} is unique to only
that port. The User Agent SHOULD ensure that the id is maintained
across instances of the application - e.g., when the system is
rebooted - and when a device is removed from the system.
Applications may want to cache these ids locally to re-create a
MIDI setup. Some systems may not support completely unique
persistent identifiers; in such cases, it will be more
challenging to maintain identifiers when another interface is
added or removed from the system. (This might throw off the index
of the requested port.) It is expected that the system will do
the best it can to match a port across instances of the MIDI API:
for example, an implementation may opaquely use some form of hash
of the port interface manufacturer, name and index as the id, so
that a reference to that port id is likely to match the port when
plugged in. Applications may use the comparison of id of
MIDIPorts to test for equality.
</p>
</dd>
<dt>
<dfn>manufacturer</dfn>
</dt>
<dd>
<p>
The manufacturer of the port.
</p>
</dd>
<dt>
<dfn>name</dfn>
</dt>
<dd>
<p>
The system name of the port.
</p>
</dd>
<dt>
<dfn>type</dfn>
</dt>
<dd>
<p>
A descriptor property to distinguish whether the port is an input
or an output port. For {{MIDIOutput}}, this MUST be
<code>"output"</code>. For {{MIDIInput}}, this MUST be
<code>"input"</code>.
</p>
</dd>
<dt>
<dfn>version</dfn>
</dt>
<dd>
<p>
The version of the port.
</p>
</dd>
<dt>
<dfn>state</dfn>
</dt>
<dd>
The state of the device.
</dd>
<dt>
<dfn>connection</dfn>
</dt>
<dd>
The state of the connection to the device.
</dd>
<dt>
<dfn>onstatechange</dfn>
</dt>
<dd>
<p>
The handler called when an existing port changes its state or
connection attributes.
</p>
<p>
This [=event handler=], of type "statechange", MUST be
supported by all objects implementing {{MIDIPort}} interface.
</p>
<p class="note">
It is important to understand that leaving an {{EventHandler}}
attached to this object will prevent it from being
garbage-collected; when finished using the {{MIDIPort}}, you
should remove any {{MIDIPort/onstatechange}} listeners.
</p>
</dd>
<dt>
<dfn>open</dfn>
</dt>
<dd>
<p>
Makes the [=MIDI device=] corresponding to the {{MIDIPort}}
explicitly available. Note that this call is NOT required in order
to use the {{MIDIPort}}- calling <code>send()</code> on a
{{MIDIOutput}} or attaching a MIDIMessageEvent handler on a
{{MIDIInput}} will cause an implicit open(). The underlying
implementation may not need to do anything in response to this
call. However, some underlying implementations may not be able to
support shared access to [=MIDI devices=], so using explicit
open() and close() calls will enable MIDI applications to
predictably control this exclusive access to devices.
</p>
<p>
When invoked, this method returns a Promise object representing a
request for access to the given MIDI port on the user's system.
</p>
<p>
If the port device has a state of <a data-lt=
"MIDIPortDeviceState.connected">"connected"</a>, when access to
the port has been obtained (and the port is ready for input or
output), the vended Promise is resolved.
</p>
<p>
If access to a connected port is not available (for example, the
port is already in use in an exclusive-access-only platform), the
Promise is rejected (if any) is invoked.
</p>
<p>
If <code>open()</code> is called on a port that is <a data-lt=
"MIDIPortDeviceState.disconnected">"disconnected"</a>, the port's
<a data-lt="MIDIPort.connection">.connection</a> will transition
to <a data-lt="MIDIPortConnectionState.pending">"pending"</a>,
until the port becomes <a data-lt=
"MIDIPortDeviceState.connected">"connected"</a> or all references
to it are dropped.
</p>
<p>
When this method is called, the user agent MUST run the
<dfn data-lt="open the port">algorithm to open a MIDIPort</dfn>:
</p>
<ol>
<li>
<p>
Let <var>promise</var> be a new Promise object and
<var>resolver</var> be its associated resolver.
</p>
</li>
<li>
<p>
Return <var>promise</var> and run the following steps
asynchronously.
</p>
</li>
<li>
<p>
Let <var>port</var> be the given {{MIDIPort}} object.
</p>
</li>
<li>
<p>
If the device's connection is already <a data-lt=
"MIDIPortConnectionState.open">"open"</a> (e.g. open() has
already been called on this {{MIDIPort}}, or the port has been
implicitly opened), jump to the step labeled <em>success</em>
below.
</p>
</li>
<li>
<p>
If the device's connection is <a data-lt=
"MIDIPortConnectionState.pending">"pending"</a> (i.e. the
connection had been opened and the device was subsequently
disconnected), jump to the step labeled <em>success</em>
below.
</p>
</li>
<li>
<p>
If the device's state is <a data-lt=
"MIDIPortDeviceState.disconnected">"disconnected"</a>, change
the <a data-link-for="MIDIPort">connection</a> attribute of
the {{MIDIPort}} to <a data-lt=
"MIDIPortConnectionState.pending">"pending"</a>, and enqueue
a new <a data-lt=
"MIDIConnectionEvent">MIDIConnectionEvent</a> to the
<a data-lt="MIDIAccess.onstatechange">statechange</a> handler
of the {{MIDIAccess}} and to the <a data-lt=
"MIDIPort.onstatechange">statechange</a> handler of the
{{MIDIPort}} and jump to the step labeled <em>success</em>
below.
</p>
</li>
<li>
<p>
Attempt to obtain access to the given [=MIDI device=] in the
system. If the device is unavailable (e.g. is already in use
by another process and cannot be opened, or is disconnected),
jump to the step labeled <em>failure</em> below. If the device
is available and access is obtained, continue the following
steps.
</p>
</li>
<li>
<p>
Change the <code>connection</code> attribute of the MIDIPort
to <code>"open"</code>, and enqueue a new
{{MIDIConnectionEvent}} to the <a data-lt=
"MIDIAccess.onstatechange">statechange</a> handler of the
{{MIDIAccess}} and to the <a data-lt=
"MIDIPort.onstatechange">statechange</a> handler of the
{{MIDIPort}}.
</p>
</li>
<li>
<p>
If this port is an output port and has any pending data that
is waiting to be sent, asynchronously begin sending that
data.
</p>
</li>
<li>
<p>
<em><b>success</b></em>: Call <var>resolver</var>'s
<code>accept(value)</code> method with <var>port</var> as
value argument.
</p>
</li>
<li>
<p>
Terminate these steps.
</p>
</li>
<li>
<p>
<em><b>failure</b></em>: Let <var>error</var> be a new
{{DOMException}}. This exception's .name should be
<code>"InvalidAccessError"</code> if the port is unavailable.
</p>
</li>
<li>
<p>
Call <var>resolver</var>'s <code>reject(value)</code> method
with <var>error</var> as value argument.
</p>
</li>
</ol>
</dd>
<dt>
<dfn>close</dfn>
</dt>
<dd>
<p>
Makes the [=MIDI device=] corresponding to the {{MIDIPort}}
explicitly unavailable (subsequently changing the state from
"open" to "closed"). Note that successful invocation of this
method will result in [=MIDI messages=] no longer being delivered
to MIDIMessageEvent handlers on a {{MIDIInput}} (although setting
a new handler will cause an implicit open()).
</p>
<p>
The underlying implementation may not need to do anything in
response to this call. However, some underlying implementations
may not be able to support shared access to [=MIDI devices=], and
the explicit close() call enables MIDI applications to ensure
other applications can gain access to devices.
</p>
<p>
When invoked, this method returns a Promise object representing a
request for access to the given MIDI port on the user's system.
When the port has been closed (and therefore, in exclusive access
systems, the port is available to other applications), the vended
Promise is resolved. If the port is disconnected, the Promise is
rejected.
</p>
<p>
When the <code>close()</code> method is called, the user agent
MUST run the following steps:
</p>
<ol>
<li>
<p>
Let <var>promise</var> be a new Promise object and
<var>resolver</var> be its associated resolver.
</p>
</li>
<li>
<p>
Return <var>promise</var> and run the following steps
asynchronously.
</p>
</li>
<li>
<p>
Let <var>port</var> be the given {{MIDIPort}} object.
</p>
</li>
<li>
<p>
If the port is already closed (its <a data-lt=
"MIDIPort.connection">.connection</a> is <a data-lt=
"MIDIPortConnectionState.closed">"closed"</a> - e.g. the port
has not yet been implicitly or explicitly opened, or
<a data-lt="MIDIPort.close">close()</a> has already been
called on this {{MIDIPort}}), jump to the step labeled
<em><b>closed</b></em> below.
</p>
</li>
<li>
<p>
If the port is an input port, skip to the next step. If the
output port's <a data-lt="MIDIPort.state">.state</a> is not
<a data-lt="MIDIPortDeviceState.connected">"connected"</a>,
clear all pending send data and skip to the next step. Clear
any pending send data in the system with timestamps in the
future, then finish sending any send messages with no
timestamp or with a timestamp in the past or present, prior
to proceeding to the next step.
</p>
</li>
<li>
<p>
Close access to the port in the underlying system if open,
and release any blocking resources in the underlying system.
</p>
</li>
<li>
<p>
Change the <code>connection</code> attribute of the MIDIPort
to <code>"closed"</code>, and enqueue a new
{{MIDIConnectionEvent}} to the <a data-lt=
"MIDIAccess.onstatechange">statechange</a> handler of the
{{MIDIAccess}} and to the <a data-lt=
"MIDIPort.onstatechange">statechange</a> handler of the
{{MIDIPort}}.
</p>
</li>
<li>
<p>
<em><b>closed</b></em>: Call <var>resolver</var>'s
<code>accept(value)</code> method with <var>port</var> as
value argument.
</p>
</li>
<li>
<p>
Terminate these steps.
</p>
</li>
</ol>
</dd>
</dl>
<p id="event-midiport-statechange">
Whenever the MIDI port corresponding to the {{MIDIPort}} changes the
state attribute, the user agent SHOULD run the following steps:
</p>
<ol>
<li>
<p>
Let |port:MIDIPort| be the {{MIDIPort}}.
</p>
</li>
<li>
<p>
[=Fire an event=] named <a data-lt=
"MIDIPort.onstatechange">statechange</a> at the {{MIDIPort}}, and
<a data-lt="MIDIAccess.onstatechange">statechange</a> at the
{{MIDIAccess}}, using {{MIDIConnectionEvent}} with the {{MIDIConnectionEvent/port}} attribute set
to |port|.
</p>
</li>
</ol>
<section data-dfn-for="MIDIInput">
<h3 id="MIDIInput">
<dfn>MIDIInput</dfn> Interface
</h3>
<pre class="idl">
[SecureContext, Exposed=(Window,Worker)] interface MIDIInput: MIDIPort {
attribute EventHandler onmidimessage;
};
</pre>
<dl>
<dt>
<dfn>onmidimessage</dfn>
</dt>
<dd>
<p>
This [=event handler=], of type "midimessage", MUST be
supported by all objects implementing {{MIDIInput}} interface.
</p>
<p>
If the handler is set and the state attribute is not
<code>"opened"</code>, underlying implementation tries to make
the port available, and change the state attribute to
<code>"opened"</code>. If succeeded, {{MIDIConnectionEvent}} is
delivered to the corresponding <code>MIDIPort</code> and
{{MIDIAccess}}.
</p>
</dd>
</dl>
<p id="event-midiinput-message">
Whenever the MIDI port corresponding to the {{MIDIInput}} finishes
receiving one or more [=MIDI messages=], the user agent MUST run the
following steps:
</p>