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 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-midi-context

v1.0.20

Published

A library to provide React developers with easy access to the WebMIDI API

Downloads

31

Readme

react-midi-context

A React component for working with MIDI devices and sending MIDI messages.

Installation

npm install react-midi-context

Usage

Import the MIDIProvider component and wrap it around your app to provide MIDI functionality to your components:

import { MIDIProvider } from 'react-midi-context';

function App() {
  return (
    <MIDIProvider>
      {/* Your app components */}
    </MIDIProvider>
  );
}

MIDIProvider API

The MIDIProvider component provides the following API through the MIDIContext context:

  • initializeMIDI: Initializes the MIDI functionality and returns an object with MIDI inputs and outputs.
  • openMIDIInput(input, callback): Opens a MIDI input for receiving MIDI messages.
  • onMIDIMessage(event): Handles a MIDI input event and returns an object with the MIDI data.
  • sendMIDIMessage(props): Sends a MIDI message with the specified properties.
  • sendMIDICC(channel, cc, value, device): Sends a MIDI Control Change (CC) message.
  • sendMIDINoteOn(channel, pitch, value/velocity, device): Sends a MIDI Note On message.
  • sendMIDINoteOff(channel, pitch, value/velocity, device): Sends a MIDI Note Off message.
  • getMIDIValue({channel, cc, device}): Retrieves the stored value for a MIDI channel and control change (CC) number.
  • midiAccess: The MIDI access object.
  • midiInputs: An array of available MIDI inputs.
  • midiOutputs: An array of available MIDI outputs.
  • connectedMIDIInputs: An array of currently connected MIDI inputs.
  • addMIDIInput(input, callback): Adds a MIDI input and opens it for receiving MIDI messages.
  • removeMIDIInput(input): Removes a MIDI input and closes it.
  • connectedMIDIOutputs: An array of currently connected MIDI outputs.
  • addMIDIOutput(output): Adds a MIDI output.
  • removeMIDIOutput(output): Removes a MIDI output.
  • subscribe(callback): Subscribes to changes in the MIDI context.

Examples

Send a MIDI Control Change (CC) message:

import { useMIDIContext } from 'react-midi-context';

function MyComponent() {
  const { sendMIDICC } = useMIDIContext();

  const handleButtonClick = () => {
    sendMIDICC(1, 64, 127, outputDevice);
  };

  return (
    <button onClick={handleButtonClick}>Send MIDI CC</button>
  );
}

Open a MIDI input and handle incoming MIDI messages:

import { useMIDIContext } from 'react-midi-context';

function MyComponent() {
  const { openMIDIInput, onMIDIMessage } = useMIDIContext();

  useEffect(() => {
    const handleMIDIMessage = (event) => {
      const { data, timeStamp, str } = onMIDIMessage(event);
      // Handle the MIDI message
    };

    const openInput = async () => {
      const input = await openMIDIInput(inputDevice);
      if (input) {
        input.onmidimessage = handleMIDIMessage;
      }
    };

    openInput();

    return () => {
      // Clean up the input
    };
  }, []);

  return (
    // Your component JSX
  );
}

License

This project is licensed under the ISC License.