files/en-us/web/api/midiport/index.md
{{securecontext_header}}{{APIRef("Web MIDI API")}}
The MIDIPort interface of the {{domxref('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.
{{InheritanceDiagram}}
{{domxref("MIDIPort.id")}} {{ReadOnlyInline}}
{{domxref("MIDIPort.manufacturer")}} {{ReadOnlyInline}}
{{domxref("MIDIPort.name")}} {{ReadOnlyInline}}
{{domxref("MIDIPort.type")}} {{ReadOnlyInline}}
"input"
MIDIPort is an input port."output"
MIDIPort is an output port.{{domxref("MIDIPort.version")}} {{ReadOnlyInline}}
{{domxref("MIDIPort.state")}} {{ReadOnlyInline}}
"disconnected"
MIDIPort represents is disconnected from the system."connected"
MIDIPort represents is currently connected.{{domxref("MIDIPort.connection")}} {{ReadOnlyInline}}
"open"
MIDIPort represents has been opened and is available."closed"
MIDIPort represents has not been opened, or has been closed."pending"
MIDIPort represents has been opened but has subsequently disconnected.This interface also inherits methods from {{domxref("EventTarget")}}.
MIDIPort explicitly available, and returns a {{jsxref("Promise")}} which resolves once access to the port has been successful.MIDIPort unavailable, changing the {{domxref("MIDIPort.state","state")}} from "open" to "closed". This returns a {{jsxref("Promise")}} which resolves once the port has been closed.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}'`,
);
}
}
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}}
{{Compat}}