npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@react-midi/hooks

v2.0.1

Published

This package provides hooks for building MIDI-capable React applications and components.

Downloads

300

Readme

@react-midi/hooks

This package provides hooks for building MIDI-capable React applications and components.

npm install --save @react-midi/hooks

<MIDIProvider>

The MIDIProvider component serves as the cornerstone of our MIDI integration, enabling seamless interaction with MIDI devices directly within your React application. By leveraging the Web MIDI API, MIDIProvider offers a comprehensive solution for managing MIDI connections, inputs, outputs, and real-time MIDI events, all within a reactively designed context.

MIDIProvider is a prerequisite for utilizing any of the other MIDI-related hooks within our library, as they depend on the context it establishes to function correctly.

Getting Started:

To begin working with @react-midi/hooks, wrap your component tree with MIDIProvider:

import { MIDIProvider } from '@react-midi/hooks'

function App() {
  return (
    <MIDIProvider>
      <YourMIDIEnabledComponent />
    </MIDIProvider>
  )
}

useMIDIInputs():

The useMIDIInputs hook is designed to simplify the process of accessing and managing MIDI input devices. It provides an interface for obtaining the list of available MIDI inputs, identifying the currently selected input device, and selecting an input device by its ID.

const { input, inputs, selectInput, selectedInputId } = useMIDIInputs()

// Example: Select a MIDI input by ID
selectInput('desired-input-device-id')

useMIDIOutputs():

The useMIDIOutputs hook is almost the exact same as useMIDIInputs, but handles MIDI outputs instead. It provides an interface for obtaining the list of available MIDI outputs, identifying the currently selected output device, and selecting an output device by its ID.

const { output, outputs, selectOutput, selectedOutputId } = useMIDIOutputs()

// Example: Select a MIDI output by ID
selectInput('desired-output-device-id')

useMIDIOutput():

The useMIDIOutput hook provides methods to send MIDI messages (note on, note off, and control change) to the selected MIDI output. It simplifies the process of sending MIDI messages by abstracting the details of constructing MIDI message arrays.

// To use this hook in a component to send a 'note on' and 'note off' message
const { noteOn, noteOff, cc } = useMIDIOutput()
noteOn(60, { velocity: 127, channel: 1 }) // Send 'note on' for note 60 with velocity 127 on channel 1
noteOff(60, { channel: 1 }) // Send 'note off' for note 60 on channel 1
cc(64, 127, 1) // Send control change message with control number 64, value 127, on channel 1

useMIDINote():

The useMIDINote hook allows you to listen for MIDI note messages and updates its state with the message details if the message matches the specified note and channel filters. If no filters are provided, it will update the state with every note message received.

// To use this hook to listen for note C3 (MIDI note number 60) on channel 1
const midiNote = useMIDINote({ note: 60, channel: 1 })
if (midiNote) {
  console.log(
    `Note ${midiNote.note} with velocity ${midiNote.velocity} received on channel ${midiNote.channel}`
  )
}

useMIDINotes():

The useMIDINotes hook subscribes to MIDI note messages that satisfy specified filtering criteria (such as note number and channel). It maintains an array of active (i.e., currently pressed) MIDI notes. When a note-on message is received, the note is added to the array. When a note-off message for an active note is received, that note is removed from the array. This allows for tracking of currently active MIDI notes that match the filter.

// To use this hook to monitor active notes for MIDI channel 1
const activeNotes = useMIDINotes({ channel: 1 })
// activeNotes will be an array of objects, each representing an active MIDI note on channel 1.

useMIDIControl():

The useMIDIControl hook listens for MIDI control change (CC) messages and updates its state with the details of the message if it matches the specified control number and channel filters. If no filters are provided, it will update the state with every control change message received. This can be useful for components that need to react to specific control change messages.

// To use this hook to listen for control change number 7 on channel 1
const midiControl = useMIDIControl({ cc: 7, channel: 1 })
if (midiControl) {
  console.log(
    `Control change ${midiControl.control} with value ${midiControl.value} received on channel ${midiControl.channel}`
  )
}

useMIDIControls():

The useMIDIControls hook listens for MIDI control change messages that match a provided filter, and maintains an array of values corresponding to a predefined set of control numbers (controls). Each control's value in the array is updated when a matching control change message is received. This is useful for components that need to keep track of and react to changes in multiple MIDI control values simultaneously.

// To use this hook to monitor control changes for MIDI controls 7 and 10 on channel 1
const controlValues = useMIDIControls([7, 10], { channel: 1 })
// controlValues will be an array of two numbers, initially [0, 0], updated with the latest values for controls 7 and 10.

useMIDIMessage():

The useMIDIMessage hook listens for any MIDI messages and updates its state with the latest message received. It is useful for components that need to react to or display information based on incoming MIDI messages.

// To use this hook in a component to display the latest MIDI message
const midiMessage = useMIDIMessage()
if (midiMessage) {
  console.log(`MIDI message received:`, midiMessage)
}