web api interface
MIDIPort
Secure context
The MIDIPort interface of the Web MIDI API represents a MIDI input or output port.
A MIDIPort instance is created when a new MIDI device is connected. Therefore it has no constructor.
Instance properties
-
idRead only- : Returns a string containing the unique ID of the port.
-
manufacturerRead only- : Returns a string containing the manufacturer of the port.
-
nameRead only- : Returns a string containing the system name of the port.
-
typeRead only- : Returns a string containing the type of the port, one of:
"input"- : The
MIDIPortis an input port.
- : The
"output"- : The
MIDIPortis an output port.
- : The
- : Returns a string containing the type of the port, one of:
-
versionRead only- : Returns a string containing the version of the port.
-
stateRead only- : Returns a string containing the state of the port, one of:
"disconnected"- : The device that this
MIDIPortrepresents is disconnected from the system.
- : The device that this
"connected"- : The device that this
MIDIPortrepresents is currently connected.
- : The device that this
- : Returns a string containing the state of the port, one of:
-
connectionRead only- : Returns a string containing the connection state of the port, one of:
"open"- : The device that this
MIDIPortrepresents has been opened and is available.
- : The device that this
"closed"- : The device that this
MIDIPortrepresents has not been opened, or has been closed.
- : The device that this
"pending"- : The device that this
MIDIPortrepresents has been opened but has subsequently disconnected.
- : The device that this
- : Returns a string containing the connection state of the port, one of:
Instance methods
This interface also inherits methods from EventTarget.
open()- : Makes the MIDI device connected to this
MIDIPortexplicitly available, and returns aPromisewhich resolves once access to the port has been successful.
- : Makes the MIDI device connected to this
close()
Events
statechange- : Called when an existing port changes its state or connection.
Examples
List ports and their information
The following example lists input and output ports, and displays information about them using properties of MIDIPort.
function listInputsAndOutputs(midiAccess) {
for (const entry of midiAccess.inputs) {
const input = entry[1];
console.log(
`Input port [type:'${input.type}'] id:'${input.id}' manufacturer: '${input.manufacturer}' name: '${input.name}' version: '${input.version}'`,
);
}
for (const entry of midiAccess.outputs) {
const output = entry[1];
console.log(
`Output port [type:'${output.type}'] id: '${output.id}' manufacturer: '${output.manufacturer}' name: '${output.name}' version: '${output.version}'`,
);
}
}
Add available ports to a select list
The following example takes the list of input ports and adds them to a select list, in order that a user can choose the device they want to use.
inputs.forEach((port, key) => {
const opt = document.createElement("option");
opt.text = port.name;
document.getElementById("port-selector").add(opt);
});
Specifications
SpecificationsStandards references are available on the canonical MDN page.
Browser compatibility
Browser compatibilityCompatibility data is available on the canonical MDN page.