JavaScript library for MIDI communication that simplifies sending and receiving MIDI messages between browsers/Node.js and MIDI instruments
—
The WebMidi singleton provides the main interface for enabling MIDI access and managing MIDI ports. It must be enabled before any MIDI functionality can be used.
Enable MIDI access with optional SysEx support.
/**
* Enable WebMidi access to MIDI devices
* @param options - Configuration options
* @param options.sysex - Whether to enable SysEx support (default: false)
* @returns Promise that resolves with WebMidi instance
*/
static enable(options?: {sysex?: boolean}): Promise<WebMidi>;
/**
* Disable WebMidi and close all ports
* @returns Promise that resolves when WebMidi is disabled
*/
static disable(): Promise<WebMidi>;Usage Examples:
import { WebMidi } from "webmidi";
// Basic enable
await WebMidi.enable();
// Enable with SysEx support
await WebMidi.enable({ sysex: true });
// Handle errors
try {
await WebMidi.enable();
console.log("MIDI enabled successfully");
} catch (error) {
console.error("Failed to enable MIDI:", error);
}
// Disable WebMidi
await WebMidi.disable();
console.log("MIDI disabled");Access available MIDI input and output devices.
/**
* Get all available MIDI input ports
* @returns Array of Input objects
*/
static get inputs(): Input[];
/**
* Get all available MIDI output ports
* @returns Array of Output objects
*/
static get outputs(): Output[];
/**
* Get input by unique identifier
* @param id - The port's unique identifier
* @param options - Search options
* @param options.disconnected - Include disconnected ports (default: false)
* @returns Input object or false if not found
*/
static getInputById(id: string, options?: {disconnected?: boolean}): Input | false;
/**
* Get input by name
* @param name - The port name to search for
* @param options - Search options
* @param options.disconnected - Include disconnected ports (default: false)
* @returns Input object or false if not found
*/
static getInputByName(name: string, options?: {disconnected?: boolean}): Input | false;
/**
* Get output by unique identifier
* @param id - The port's unique identifier
* @param options - Search options
* @param options.disconnected - Include disconnected ports (default: false)
* @returns Output object or false if not found
*/
static getOutputById(id: string, options?: {disconnected?: boolean}): Output | false;
/**
* Get output by name
* @param name - The port name to search for
* @param options - Search options
* @param options.disconnected - Include disconnected ports (default: false)
* @returns Output object or false if not found
*/
static getOutputByName(name: string, options?: {disconnected?: boolean}): Output | false;Usage Examples:
// List all devices
console.log("Inputs:", WebMidi.inputs.map(input => input.name));
console.log("Outputs:", WebMidi.outputs.map(output => output.name));
// Find specific devices
const myKeyboard = WebMidi.getInputByName("My MIDI Keyboard");
const mySynth = WebMidi.getOutputByName("My Synthesizer");
// Find by ID (more reliable than name)
const input = WebMidi.getInputById("input-1234567890");Check WebMidi status and capabilities.
/**
* Whether WebMidi has been enabled
* @returns True if WebMidi is enabled
*/
static get enabled(): boolean;
/**
* Whether the Web MIDI API is supported in this environment
* @returns True if Web MIDI API is available
*/
static get supported(): boolean;
/**
* Whether SysEx messages are enabled
* @returns True if SysEx is enabled
*/
static get sysexEnabled(): boolean;
/**
* Whether running in Node.js environment
* @returns True if in Node.js
*/
static get isNode(): boolean;
/**
* Whether running in browser environment
* @returns True if in browser
*/
static get isBrowser(): boolean;
/**
* Library version
* @returns Version string
*/
static get version(): string;
/**
* Current high-resolution timestamp
* @returns Timestamp in milliseconds
*/
static get time(): number;
/**
* Whether validation is enabled (for development/debugging)
* @returns True if validation is enabled
*/
static get validation(): boolean;
static set validation(enabled: boolean);
/**
* Native Web MIDI API interface object
* @returns MIDIAccess object or null if not enabled
*/
static get interface(): MIDIAccess | null;Configure global WebMidi behavior.
/**
* Global octave offset applied to all note operations
* @returns Current octave offset (-10 to 10)
*/
static get octaveOffset(): number;
static set octaveOffset(offset: number);Usage Examples:
// Check capabilities before use
if (!WebMidi.supported) {
console.error("Web MIDI API not supported");
return;
}
// Wait for enable
if (!WebMidi.enabled) {
await WebMidi.enable();
}
// Set global octave offset (affects all note operations)
WebMidi.octaveOffset = -1; // All notes will be 1 octave lower
// Get current time for scheduling
const currentTime = WebMidi.time;Configure default values for MIDI operations.
/**
* Global default values for note and other operations
* @returns Object with default settings
*/
static get defaults(): {
note: {
attack: number;
release: number;
duration: number;
};
};WebMidi fires events for device connection changes and errors.
// Available events: "connected", "disconnected", "enabled", "disabled", "error", "portschanged"
static addListener(event: string, listener: Function): WebMidi;
static removeListener(event: string, listener: Function): WebMidi;Usage Examples:
// Listen for device connection changes
WebMidi.addListener("connected", (e) => {
console.log("Device connected:", e.port.name);
});
WebMidi.addListener("disconnected", (e) => {
console.log("Device disconnected:", e.port.name);
});
// Listen for port changes
WebMidi.addListener("portschanged", () => {
console.log("Available ports changed");
updateDeviceList();
});
// Handle errors
WebMidi.addListener("error", (e) => {
console.error("WebMidi error:", e.error);
});interface WebMidiEnableOptions {
sysex?: boolean;
}
interface DeviceSearchOptions {
disconnected?: boolean;
}Install with Tessl CLI
npx tessl i tessl/npm-webmidi