MIDIEngine is a singleton object that provides an interface for interacting with the Web MIDI API. It allows initialization of MIDI access, enumeration of input/output ports, opening/closing ports, sending MIDI messages, and handling incoming MIDI messages via a callback.
Quick Navigation
Initialization
- MIDIEngine.init - Initialize MIDI access
- MIDIEngine.deinit - Cleanup and release resources
- MIDIEngine.refresh_devices - Refresh device list
Input Methods
- MIDIEngine.get_input_port_count - Get number of input ports
- MIDIEngine.get_input_port_name - Get input port name by index
- MIDIEngine.get_input_port_names - Get all input port names
- MIDIEngine.open_input_port - Open input port for listening
- MIDIEngine.close_input_port - Close input port
- MIDIEngine.is_input_port_open - Check if input port is open
Output Methods
- MIDIEngine.get_output_port_count - Get number of output ports
- MIDIEngine.get_output_port_name - Get output port name by index
- MIDIEngine.get_output_port_names - Get all output port names
- MIDIEngine.open_output_port - Open output port for sending
- MIDIEngine.close_output_port - Close output port
- MIDIEngine.is_output_port_open - Check if output port is open
- MIDIEngine.send_message - Send MIDI message
Examples
// Listen to MIDI messages
window._midi_callback = function(messageJson) {
const message = JSON.parse(messageJson);
console.log("MIDI received:", message.data);
};
await MIDIEngine.init();
MIDIEngine.open_input_port(0);
// Send MIDI messages
await MIDIEngine.init();
MIDIEngine.open_output_port(0);
MIDIEngine.send_message([0x90, 60, 127]); // Note ON
Methods
(static) close_input_port() → {void}
Closes the currently open MIDI input port.
Returns:
- Type
- void
(static) close_output_port() → {void}
Closes the currently open MIDI output port.
Returns:
- Type
- void
(static) deinit() → {void}
Deinitializes MIDI, closes ports, and releases device references.
Returns:
- Type
- void
(static) get_input_port_count() → {number}
Returns the number of available MIDI input ports.
Returns:
The number of available MIDI input ports.
- Type
- number
(static) get_input_port_name(index) → {string}
Returns the name of the MIDI input port at the specified index.
Parameters:
Name | Type | Description |
---|---|---|
index |
number | Index of the input port. |
Returns:
Name of the MIDI input port.
- Type
- string
(static) get_input_port_names() → {string}
Returns a JSON string array of all MIDI input port names.
Returns:
JSON string array of all MIDI input port names.
- Type
- string
(static) get_output_port_count() → {number}
Returns the number of available MIDI output ports.
Returns:
The number of available MIDI output ports.
- Type
- number
(static) get_output_port_name(index) → {string}
Returns the name of the MIDI output port at the specified index.
Parameters:
Name | Type | Description |
---|---|---|
index |
number | Index of the output port. |
Returns:
Name of the MIDI output port.
- Type
- string
(static) get_output_port_names() → {string}
Returns a JSON string array of all MIDI output port names.
Returns:
JSON string array of all MIDI output port names.
- Type
- string
(static) init() → {Promise.<void>}
Initializes MIDI access and populates input/output port lists.
Returns:
Resolves when MIDI access is granted or logs error if denied.
- Type
- Promise.<void>
(static) is_input_port_open(index) → {boolean}
Checks if the MIDI input port at the specified index is connected.
Parameters:
Name | Type | Description |
---|---|---|
index |
number | Index of the input port. |
Returns:
True if the MIDI input port is connected.
- Type
- boolean
(static) is_output_port_open(index) → {boolean}
Checks if the MIDI output port at the specified index is connected.
Parameters:
Name | Type | Description |
---|---|---|
index |
number | Index of the output port. |
Returns:
True if the MIDI output port is connected.
- Type
- boolean
(static) open_input_port(index) → {void}
Opens the MIDI input port at the specified index and assigns a message callback.
Parameters:
Name | Type | Description |
---|---|---|
index |
number | Index of the input port to open. |
Returns:
- Type
- void
(static) open_output_port(index) → {void}
Opens the MIDI output port at the specified index.
Parameters:
Name | Type | Description |
---|---|---|
index |
number | Index of the output port to open. |
Returns:
- Type
- void
(static) refresh_devices() → {void}
Refreshes the list of available MIDI input and output devices.
Returns:
- Type
- void
(static) send_message(dataArray) → {void}
Sends a MIDI message (array of bytes) to the currently open output port.
Parameters:
Name | Type | Description |
---|---|---|
dataArray |
Array.<number> | MIDI message bytes to send. |
Returns:
- Type
- void