Class: MIDIConnection

MIDIConnection(options)

Low-level Web MIDI API connection handler. Manages: - MIDI device access/request - Device enumeration (inputs/outputs) - Device connection/disconnection - Hotplug detection and events - Raw MIDI message sending/receiving - SysEx message handling - Connection state tracking NOTE: Typically used internally by MIDIController. Most applications should use MIDIController instead for higher-level APIs.

Constructor

new MIDIConnection(options)

Parameters:
Name Type Description
options Object
Properties
Name Type Attributes Default Description
sysex boolean <optional>
false Request SysEx access
Source:
Examples
// Basic usage
const connection = new MIDIConnection({ sysex: true });
await connection.requestAccess();
await connection.connect("My MIDI Device");
connection.send([0x90, 60, 100]); // Note on
// Listening for device changes
connection.on(CONNECTION_EVENTS.DEVICE_CHANGE, ({ device, state }) => {
  console.log(`${device.name} is now ${state}`);
});

Extends

Classes

MIDIConnection

Methods

(async) connect(deviceopt) → {Promise.<void>}

Connect to a MIDI output device. Can connect by index, name, or ID.
Parameters:
Name Type Attributes Description
device string | number <optional>
Device name, ID, or index (defaults to first available)
Source:
Throws:
Returns:
Type
Promise.<void>
Examples
// Connect to first available device
await connection.connect();
// Connect by index
await connection.connect(0);
// Connect by name
await connection.connect("My MIDI Keyboard");
// Connect by device ID
await connection.connect("input-12345");

(async) connectInput(deviceopt, onMessage) → {Promise.<void>}

Connect to a MIDI input device for receiving messages
Parameters:
Name Type Attributes Description
device string | number <optional>
Device name, ID, or index (defaults to first available)
onMessage function Callback for incoming MIDI messages
Source:
Throws:
Returns:
Type
Promise.<void>

disconnect()

Disconnect from both output and input
Source:

disconnectInput()

Disconnect from current input
Source:

disconnectOutput()

Disconnect from current output
Source:

emit(event, data)

Emit an event
Parameters:
Name Type Description
event string Event name
data * Event data
Overrides:
Source:

getCurrentInput() → {Object|null}

Get current input device info
Source:
Returns:
Type
Object | null

getCurrentOutput() → {Object|null}

Get current output device info
Source:
Returns:
Type
Object | null

getInputs() → {Array.<{id: string, name: string, manufacturer: string}>}

Get all available MIDI inputs
Source:
Returns:
Type
Array.<{id: string, name: string, manufacturer: string}>

getOutputs() → {Array.<{id: string, name: string, manufacturer: string}>}

Get all available MIDI outputs
Source:
Returns:
Type
Array.<{id: string, name: string, manufacturer: string}>

isConnected() → {boolean}

Check if currently connected to an output
Source:
Returns:
Type
boolean

off(event, handler)

Remove an event listener
Parameters:
Name Type Description
event string Event name
handler function Event handler function
Overrides:
Source:

on(event, handler) → {function}

Register an event listener
Parameters:
Name Type Description
event string Event name
handler function Event handler function
Overrides:
Source:
Returns:
Unsubscribe function
Type
function

once(event, handler)

Register a one-time event listener
Parameters:
Name Type Description
event string Event name
handler function Event handler function
Overrides:
Source:

removeAllListeners(eventopt)

Remove all event listeners
Parameters:
Name Type Attributes Description
event string <optional>
Optional event name to clear specific event
Overrides:
Source:

(async) requestAccess() → {Promise.<void>}

Request MIDI access from the browser. Sets up hotplug detection and emits events when devices connect/disconnect.
Source:
Fires:
  • CONNECTION_EVENTS.event:DEVICE_CHANGE - When any MIDI device state changes
  • CONNECTION_EVENTS.event:IN_DEV_CONNECTED - When an input device connects
  • CONNECTION_EVENTS.event:IN_DEV_DISCONNECTED - When an input device disconnects
  • CONNECTION_EVENTS.event:OUT_DEV_CONNECTED - When an output device connects
  • CONNECTION_EVENTS.event:OUT_DEV_DISCONNECTED - When an output device disconnects
Throws:
If MIDI is not supported or access is denied
Type
MIDIAccessError
Returns:
Type
Promise.<void>
Examples
// Request basic MIDI access
await connection.requestAccess();
// Request with SysEx support
const connection = new MIDIConnection({ sysex: true });
await connection.requestAccess();

send(message, timestampopt) → {void}

Send a MIDI message to the connected output. Automatically converts Arrays to Uint8Array for Web MIDI API compatibility.
Parameters:
Name Type Attributes Default Description
message Uint8Array | Array.<number> MIDI message bytes (e.g., [0x90, 60, 100])
timestamp number <optional>
performance.now() Optional timestamp for scheduled sending
Source:
Returns:
Type
void
Examples
// Send a note on message (channel 1, note C4, velocity 100)
connection.send([0x90, 60, 100]);
// Send a note off message
connection.send([0x80, 60, 0]);
// Send a control change
connection.send([0xB0, 7, 64]); // Volume to 64

sendSysEx(data, includeWrapperopt) → {void}

Send a System Exclusive (SysEx) message. Requires sysex: true in constructor options.
Parameters:
Name Type Attributes Default Description
data Array.<number> SysEx data bytes (without F0/F7 wrapper)
includeWrapper boolean <optional>
false If true, wraps data with F0/F7 bytes
Source:
Returns:
Type
void
Examples
// Send a basic SysEx message (will be wrapped with F0/F7)
connection.sendSysEx([0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7F]);
// Send pre-wrapped SysEx message
connection.sendSysEx([0xF0, 0x41, 0x10, 0x42, 0xF7], false);