2.0.0 • Published 5 years ago

@mkaradeniz/use-midi v2.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

useMidi

useMidi is a low-level custom React Hook to access the Web MIDI API. It provides access to incoming MIDIMessageEvents and helpers to request and observe the user's permission.

Installation

yarn add @mkaradeniz/use-midi

or

npm install @mkaradeniz/use-midi

Demo

Usage

const [midiMessages, { isSupported, isRequested, isAllowed }, requestMidiAccess, midiAccess] = useMidi()

API

Input

TypeDefault
Options{ automaticallyRequestPermission: false, debug: false, suppressActiveSensing: true, sysex: false }

MidiMessage

PropertyTypeDescription
commandCodenumberRecieved command code.
notenumber | undefinedIf recieved, the given note.
timestampDOMHighResTimeStampHigh-resolution time of when the event was received.
rawEventWebMidi.MIDIMessageEventThe raw WebMidi.MIDIMessageEvent.
velocitynumber | undefinedIf recieved, the given velocity.

Options

OptionTypeDescriptionDefault
automaticallyRequestPermissionbooleanIf set to true, useMidi will automatically ask for the user's permission to access the MIDI devices, instead of waiting for the consumer to call requestAccess.false
callback(midiMessage: MidiMessage) => voidIf provided, the callback will be called on every received MidiMessage.undefined
messagesHistoryCountnumbermidiMessages will always contain the last messagesHistoryCount elements.256
debugbooleanIf set to true, useMidi will log every event to the console.false
suppressActiveSensingbooleanIf set to true, useMidi will ignore the Active Sensing event.true
sysexbooleanIf set to true, useMidi use the sysex option when requesting MIDI access.false

Returns

IndexVariableTypeDescriptionDefault
0midiMessagesMidiMessage[]An array of all MidiMessages, from oldest (index 0) to newest.[]
1statusStatusObject with all statuses.{isAllowed: false, isRequested: False, isSupported: false}
2requestAccess() => voidFunction to request the user's permission to access MIDI devices.() => void
3midiAccessMIDIAccessThe MIDIAccess interface.undefined

Status

StatusTypeDescriptionDefault
isAllowedbooleanWhether the user gave permission to access MIDI devices.false
isRequestedbooleanWhether we requested the user to grant permission to access MIDI devices.false
isSupportedbooleanWhether the Web MIDI API is supported by the browser.false

Development

This project is written in TypeScript and setup as a monorepo managed by Yarn-Workspaces & Lerna.

⚠️ Only release through yarn release in root. Do not npm publish individual packages.