Copyright © 2012 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark and document use rules apply.
Some user agents have connected music devices, such as synthesizers, keyboard and other controllers, and drum machines. 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.
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. At the same time, the Web MIDI API is not intended to become a semantic controller platform; 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.
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 HTML5 <audio>
element, for example).
The Web MIDI API is intended to enable direct access to devices that
respond to MIDI - external synthesizers or lighting systems, for
example, or even the software synthesizers that are built in to many
common operating systems. 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.
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 and High-Resolution Time. 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.
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.
This document was published by the Audio Working Group as a First Public Working Draft. This document is intended to become a W3C Recommendation. If you wish to make comments regarding this document, please send them to public-audio@w3.org (subscribe, archives). All feedback is welcome.
Publication as a Working Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.
This document was produced by a group operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
Note: On 1 November 2012 we edited the document in place to remove an incorrect statement about expectations for this document.
MIDIAccess
InterfaceMIDIPort
InterfaceMIDIMessage
InterfaceThis section is non-normative.
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 a wide variety of use cases in mind. 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.
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).
The Web MIDI API consists of the following set of interfaces and objects, as well as some supporting interfaces:
As well as sections marked as non-normative, all authoring guidelines, diagrams, examples, and notes in this specification are non-normative. Everything else in this specification is normative.
The key words must, must not, required, should, should not, recommended, may, and optional in this specification are to be interpreted as described in [RFC2119].
This specification defines conformance criteria that apply to a single product: the user agent that implements the interfaces that it contains.
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.
The concepts queue a task and fires a simple event are defined in [HTML5].
The terms event handlers and event handler event types are defined in [HTML5].
The term Uint8Array is defined in [TYPED-ARRAYS].
The term DOMHighResTimeStamp is defined in the High Resolution Time Specification.
There are two primary security and privacy concerns with adding the Web MIDI API to the web platform:
These issues will be further explored prior to this specification becoming a Recommendation. In the meantime, the suggested security model explicitly allows user agents to require the user's approval before giving access to MIDI devices, although it is not currently required to prompt the user for this approval. It is noted that some web systems (e.g. Java) give access to MIDI interfaces without prompting, although this may not be the security model that this specification eventually uses.
MIDIAccess
InterfaceThis interface provides the methods to enumerate MIDI input and output devices, and obtain access to an individual device.
[NoInterfaceObject]
interface MIDIAccess {
sequence<MIDIPort
> enumerateInputs ();
sequence<MIDIPort
> enumerateOutputs ();
MIDIInput
getInput (MIDIPort or DOMString or short target);
MIDIOutput
getOutput (MIDIPort or DOMString or short target);
};
enumerateInputs
sequence<MIDIPort
>
enumerateOutputs
sequence<MIDIPort
>
getInput
MIDIPort
requested
as a MIDIInput
instance.
When the
getInput()
is invoked, the user agent must run the following steps:
Let target be the
argument.
If the target is a DOMString,
target will be a MIDIPort
whose MIDIPort
fingerprint
matches the supplied
DOMString. If target is a short, target
will be a
that matches the supplied
index in the sequence returned by MIDIPort
enumerateInputs()
.
If the target parameter is a DOMString and there is no MIDIPort
that matches the fingerprint, or if the target parameter is a
short outside the bounds of available MIDI inputs, throw a
TYPE_ERROR
exception.
If target is not a
instance, or if the MIDIPort is not a valid MIDI input
port, throw a MIDIPort
TYPE_ERROR
exception.
If target is not available for access, throw a
REFERENCE_ERROR
exception.
If made necessary by the underlying system APIs, reserve the corresponding port for use within the current process.
Return a MIDIInput
object corresponding to the
target argument.
Parameter | Type | Nullable | Optional | Description |
---|---|---|---|---|
target | MIDIPort or DOMString or short | ✘ | ✘ | The input port to be requested. |
MIDIInput
getOutput
MIDIPort
requested
as a MIDIOutput
instance.
When the
getOutput()
is invoked, the user agent must run the following steps:
Let target be the
argument.
If the target is a DOMString,
target will be a MIDIPort
whose MIDIPort
fingerprint
matches the supplied
DOMString. If target is a short, target
will be a
that matches the supplied
index in the sequence returned by MIDIPort
enumerateOutputs()
.
If the target parameter is a DOMString and there is no MIDIPort
that matches the fingerprint, or if the target parameter is a
short outside the bounds of available MIDI inputs, throw a
TYPE_ERROR
exception.
If target is not a
instance, or if the MIDIPort is not a valid MIDI output
port, throw a MIDIPort
TYPE_ERROR
exception.
If target is not available for access, throw a
REFERENCE_ERROR
exception.
If made necessary by the underlying system APIs, reserve the corresponding port for use within the current process.
Return a MIDIOutput
object corresponding to the
target argument.
Parameter | Type | Nullable | Optional | Description |
---|---|---|---|---|
target | MIDIPort or DOMString or short | ✘ | ✘ | The output port to be requested. |
MIDIOutput
MIDIPort
InterfaceThis interface represents a MIDI input or output port.
Whenever the MIDI port corresponding the
is disconnected or becomes unavailable,
if supported by the underlying system the UA should run the following steps:
MIDIPort
Fire a simple event named disconnect
at the object.
Whenever the MIDI port corresponding the
is connected or reconnected,
the UA must run the following steps:
MIDIPort
Fire a simple event named connect
at the object.
The disconnect
and
connect
events may not be applicable to all
MIDI ports or OS APIs. For example, some system APIs do not dynamically
notify applications when MIDI ports become available or unavailable.
Therefore the semantic here is should, rather than must.
It is also important to note again that the MIDIPort represents an input or output interface, not necessarily an endpoint MIDI device; it is not possible, using this system, to be notified when a synthesizer is plugged into a 5-pin DIN MIDI port on a hardware MIDI interface.
enum MIDIPortType {
"input",
"output"
};
Enumeration description | |
---|---|
input | If a MIDIPort is an input port, the type member must be this value. |
output | If a MIDIPort is an output port, the type member must be this value. |
[NoInterfaceObject]
interface MIDIPort : EventTarget {
readonly attribute MIDIPortType
type;
readonly attribute DOMString name;
readonly attribute DOMString manufacturer;
readonly attribute DOMString version;
readonly attribute DOMString fingerprint;
[TreatNonCallableAsNull]
attribute callback? ondisconnect;
[TreatNonCallableAsNull]
attribute callback? onconnect;
};
fingerprint
of type DOMString, readonly
A unique ID of the port. This can be used by developers to
remember ports the user has chosen for different applications.
The User Agent should make sure the
fingerprint
is unique to only that port if possible.
manufacturer
of type DOMString, readonlyThe manufacturer of the port.
name
of type DOMString, readonlyThe system name of the port.
onconnect
of type callback, nullable
This event handler, of type connect
,
should be supported by all objects implementing the
interface.
MIDIPort
ondisconnect
of type callback, nullable
This event handler, of type disconnect
,
should be supported by all objects implementing the
interface.
MIDIPort
type
of type MIDIPortType
, readonly
A descriptor property to distinguish whether the port is an
input or an output port.
For
,
this must be MIDIOutput
"output"
.
For
,
this must be MIDIInput
"input"
.
version
of type DOMString, readonlyThe version of the port.
MIDIInput
InterfaceMIDIInput
implements MIDIPort
;
[NoInterfaceObject]
interface MIDIInput {
[TreatNonCallableAsNull]
attribute callback? onmessage;
};
Whenever the MIDI port corresponding the
sends a MIDI message, the UA must
run the following steps:
MIDIInput
Let port
be the
.
MIDIInput
Let messages
be a newly constructed
sequence<
corresponding the
MIDI messages the port sent.
MIDIMessage
>
Let event
be a newly constructed
, with the MIDIEvent
MIDIMessages
attribute set to messages
.
Fire an event named message
at the object, using the event
as the event object.
MIDIOutput
InterfaceMIDIOutput
implements MIDIPort
;
All instances of the
type are defined to also implement the MIDIOutput
MIDIPort
interface.
[NoInterfaceObject]
interface MIDIOutput {
void sendMIDIMessage (MIDIMessage
message);
void sendMessage (short status, optional short? data1, optional optional short? data2);
};
sendMIDIMessage
Enqueues the message to be sent to the corresponding MIDI port.
Parameter | Type | Nullable | Optional | Description |
---|---|---|---|---|
message |
| ✘ | ✘ |
The message to be enqueued to be sent at the appropriate time (message.timestamp ). If message.timestamp is zero or null, the message is to be sent as soon as possible.
|
void
sendMessage
Enqueues a simple MIDI message of three or fewer bytes to be sent to the corresponding MIDI port. This method of sending does not utilize timestamps - the message will be sent as soon as possible - and also is not to be used for system exclusive messages (i.e. any MIDI messages over three bytes in length).
Parameter | Type | Nullable | Optional | Description |
---|---|---|---|---|
status | short | ✘ | ✘ | The status byte of a simple MIDI message. |
data1 | short | ✔ | ✔ | The first data byte of a simple MIDI message, if requested. |
data2 | optional short | ✔ | ✔ | The second data byte of a simple MIDI message, if requested. |
void
MIDIMessage
Interface[NoInterfaceObject]
dictionary MIDIMessage {
DOMHighResTimeStamp timestamp;
Uint8Array data;
};
MIDIMessage
Membersdata
of type Uint8ArrayA Uint8Array signifying the data the message contains.
timestamp
of type DOMHighResTimeStamp
A DOMHighResTimeStamp
signifying when the event occurred or should occur. For MIDIMessage
s received by a MIDIInput
, this is required to be set; for MIDIMessage
s that are passed to sendMIDIMessage()
on a MIDIOutput
, this may explicitly be set to zero or null to represent "as soon as possible".
MIDIEvent
Interface[NoInterfaceObject]
interface MIDIEvent : Event {
readonly attribute MIDIMessage
[] MIDIMessages;
};
MIDIMessages
of type array of MIDIMessage
, readonly
An array of
s associated
with the event.
MIDIMessage