HuMIDI - v1.0.0
    Preparing search index...

    Class default

    HuMIDI - A human-friendly MIDI library for the Web MIDI API

    Provides a simple event-driven interface for working with MIDI input devices, including automatic device management, note tracking for stuck note prevention, and per-device enable/disable functionality.

    import HuMIDI from 'humidi';

    // Request MIDI access
    await HuMIDI.requestAccess();

    // Listen for note events
    HuMIDI.on('noteon', (event) => {
    console.log(`Note ${event.note} played with velocity ${event.velocity}`);
    });

    HuMIDI.on('noteoff', (event) => {
    console.log(`Note ${event.note} released`);
    });
    // Get all available input devices
    const inputs = HuMIDI.getInputs();

    // Disable a specific device
    const piano = inputs.find(input => input.name.includes('Piano'));
    piano?.disable();

    // Listen for device connections
    HuMIDI.on('inputconnected', (event) => {
    console.log(`Device connected: ${event.input.name}`);
    });
    // Listen to events on channel 1 only
    HuMIDI.on('noteon', (event) => {
    console.log('Note on channel 1:', event.note);
    }, 1);

    // Listen to all channels (default)
    HuMIDI.on('noteon', (event) => {
    console.log('Note on any channel:', event.note);
    });
    Index

    Constructors

    Methods

    • Request access to MIDI devices from the browser. This must be called before using any other MIDI functionality.

      Returns Promise<void>

      When MIDI access is denied by the user or not supported

      try {
      await HuMIDI.requestAccess();
      console.log('MIDI access granted');
      } catch (error) {
      console.error('MIDI access denied:', error.message);
      }
    • Get the current MIDI access status

      Returns AccessStatus

      The current access status

    • Check if MIDI permissions have been granted without requesting access. This uses the Permissions API to query the current permission state.

      Returns Promise<boolean>

      Promise that resolves to true if MIDI permissions are granted, false otherwise

      const hasPermissions = await HuMIDI.hasPermissions();
      if (hasPermissions) {
      console.log('MIDI permissions already granted');
      } else {
      console.log('Need to request MIDI permissions');
      }
    • Enable or disable all MIDI processing. When disabled, all MIDI messages will be ignored.

      Parameters

      • enabled: boolean

        True to enable MIDI processing, false to disable

      Returns void

      // Temporarily disable all MIDI input
      HuMIDI.setEnabled(false);

      // Re-enable MIDI input
      HuMIDI.setEnabled(true);
    • Check if MIDI processing is currently enabled

      Returns boolean

      True if MIDI processing is enabled, false otherwise

    • Get all available MIDI input devices

      Returns MIDIInput[]

      Array of MIDIInput objects representing connected input devices

      const inputs = HuMIDI.getInputs();
      console.log('Available MIDI inputs:');
      inputs.forEach(input => {
      console.log(`- ${input.name} by ${input.manufacturer}`);
      });
    • Reset the HuMIDI library to its initial state. This clears all event handlers, resets access status, and clears device tracking. Useful for testing or when you need to reinitialize the library.

      Returns void

      // Reset everything
      HuMIDI.reset();

      // Now you can request access again
      await HuMIDI.requestAccess();
    • Register an event handler for MIDI events

      Type Parameters

      • T = any

      Parameters

      • event: Event

        The MIDI event type to listen for

      • handler: EventHandler<T>

        Function to call when the event occurs

      • channel: number = -1

        MIDI channel to listen on (-1 for all channels, 0-15 for specific channels)

      Returns void

      // Listen for note events on all channels
      HuMIDI.on('noteon', (event) => {
      console.log(`Note ${event.note} pressed with velocity ${event.velocity}`);
      });

      // Listen for note events on channel 1 only
      HuMIDI.on('noteon', (event) => {
      console.log(`Channel 1 note: ${event.note}`);
      }, 1);

      // Listen for input device connections
      HuMIDI.on('inputconnected', (event) => {
      console.log(`New device: ${event.input.name}`);
      });
    • Remove an event handler

      Parameters

      • event: Event

        The MIDI event type

      • handler: EventHandler

        The handler function to remove

      • channel: number = -1

        MIDI channel the handler was registered for (-1 for all channels)

      Returns void

      const noteHandler = (event) => console.log('Note:', event.note);

      // Add handler
      HuMIDI.on('noteon', noteHandler);

      // Remove handler
      HuMIDI.off('noteon', noteHandler);
    • Remove all event handlers for a specific MIDI channel

      Parameters

      • channel: number

        MIDI channel to unsubscribe from (0-15)

      Returns void

      // Remove all handlers for channel 1
      HuMIDI.unsubscribeToChannel(1);