Lab

useMidiInput

The notes of the page's MIDI input, with velocity and pitch bend: every component that calls it hears the same devices.

pnpm dlx shadcn@latest add @audio-ui/use-midi-input
import { useMidiInput } from "@/hooks/use-midi-input";
Fileshooks/use-midi-input.ts
Depends onNothing
npm@audio-ui/engine
Used byblock-drum-synth, block-electric-piano, block-fm-synth, block-guitar, block-mono-synth, block-organ, block-piano-roll, block-poly-synth, block-sampler, block-string-machine, block-tape-keys, midi-connect-control, piano-size-default-demo, spectrum-analyzer-demo
ExportKindWhat it is
UseMidiInputOptionsinterface
UseMidiInputReturninterface
useMidiInputfunctionThe notes of the page's MIDI input.

Usage

useMidiInput hears the page's MIDI input: every component that calls it hears the same devices, and connecting from one connects them all. Browsers ask for permission, so connect from a click, with requestAccess or with the ready-made button (npx shadcn@latest add @audio-ui/midi-connect-control).

import { useMidiInput } from "@/hooks/use-midi-input";

const midi = useMidiInput({
  onNoteOff: (pitch) => voices.releaseNote(pitch, envelope),
  onNoteOn: (pitch, velocity) => voices.triggerNote(pitch, envelope, velocity),
  onPitchBend: voices.setPitchBend,
});

<button onClick={midi.requestAccess} type="button">Connect MIDI</button>
<Piano activeNotes={midi.activeNotes} maxPitch={72} minPitch={48} />

activeNotes lights the keys held on the device, on a Piano or a Piano roll.

Options · 3

Returns · 8

Without React, @audio-ui/engine/midi reads the messages themselves and the MIDI files.

Built by Ouest Labs, the source code is available on GitHub.
RSS