Skip to main content

hooks/useMIDIInput

Type Aliases

MIDIInputHandlers

type MIDIInputHandlers = object;

Defined in: dom/dist/index.d.ts:169

Every handler is given the channel last, as 0-15. MIDI channels are written 1-16 on hardware, so add one before showing it to anyone.

Properties

onAftertouchEvent?
optional onAftertouchEvent?: (note, pressure, channel) => void;

Defined in: dom/dist/index.d.ts:183

Pressure for one held note (polyphonic aftertouch).

Parameters
ParameterType
notenumber
pressurenumber
channelnumber
Returns

void

onChannelPressureEvent?
optional onChannelPressureEvent?: (pressure, channel) => void;

Defined in: dom/dist/index.d.ts:185

Pressure for the whole channel, sent by keyboards with one sensor.

Parameters
ParameterType
pressurenumber
channelnumber
Returns

void

onControlChangeEvent?
optional onControlChangeEvent?: (controller, value, channel) => void;

Defined in: dom/dist/index.d.ts:180

controller is the CC number, value is 0-127.

Parameters
ParameterType
controllernumber
valuenumber
channelnumber
Returns

void

onNoteOffEvent?
optional onNoteOffEvent?: (note, channel) => void;

Defined in: dom/dist/index.d.ts:171

Parameters
ParameterType
notenumber
channelnumber
Returns

void

onNoteOnEvent?
optional onNoteOnEvent?: (note, velocity, channel) => void;

Defined in: dom/dist/index.d.ts:170

Parameters
ParameterType
notenumber
velocitynumber
channelnumber
Returns

void

onPitchBendEvent?
optional onPitchBendEvent?: (value, channel) => void;

Defined in: dom/dist/index.d.ts:178

The 14-bit bend, 0-16383, centred at PITCH_BEND_CENTER.

The two data bytes are little-endian — the first carries the low 7 bits — which is the other way round from every other message.

Parameters
ParameterType
valuenumber
channelnumber
Returns

void

onProgramChangeEvent?
optional onProgramChangeEvent?: (program, channel) => void;

Defined in: dom/dist/index.d.ts:181

Parameters
ParameterType
programnumber
channelnumber
Returns

void

Variables

PITCH_BEND_CENTER

const PITCH_BEND_CENTER: 8192 = 8192;

Defined in: dom/dist/index.d.ts:164

Centre of the 14-bit pitch bend range: no bend.

The range is not symmetric — 0 is 8192 below centre and 16383 is 8191 above — so a wheel at rest reports exactly this rather than half of the maximum.

Functions

useMIDIInput()

function useMIDIInput(midiAccess, handlers): void;

Defined in: react/src/hooks/useMIDIInput.ts:29

Handle MIDI input events. To be used with useMIDIAccess.

The handlers are read fresh on every event, so writing them inline is fine: the listeners are attached once per midiAccess and stay attached. Devices plugged in later are picked up without anything having to be rebuilt.

Parameters

ParameterType
midiAccessMIDIAccess | null
handlersMIDIInputHandlers

Returns

void

Example

const { midiAccess } = useMIDIAccess()
useMIDIInput(midiAccess, {
onNoteOnEvent: (note, velocity) => play(note, velocity / 127),
onNoteOffEvent: (note) => stop(note),
onControlChangeEvent: (controller, value) => {
if (controller === 1) setModulation(value / 127)
},
})