Skip to main content

MIDI Input Overview

Overview

MIDIValInput class provides many useful methods for interacting with incomming messages. The document above describes the most popular ones, for most comprehensive list please refer to @midival/core API documentation.

All of the methods presented below return unsubscribe callback which can be called to unsubscribe from the event. Example:

const unsubscribe = input.onAllNoteOn((msg) => console.log(msg))

setTimeout(unsubscribe, 10000); // unsubscrbe after 10s.

Recieving Notes On

Once you have instantiated MIDIValInput class, you can start recieving Note on messages.

input.onAllNoteOn(message => {
console.log(`[Note On] Note: ${mesage.note} Velocity: ${message.velocity} Channel: ${message.channel}`)
})

You can easily extract MIDI note value (for example 60 for C4), Velocity (value between 0-127) and channel (1-16).

Recieving Specific Note On

You can also subscribe to recieve only specific note on. This might be helpful when building drum machine where each note corresponds to separate sample and is generated separately.

input.onNoteOn(60, triggerHighHat)
input.onNoteOn(62, triggerSnareDrum)
input.onNoteOn(64, triggerBassDrum)
// etc.

Recieving Notes Off

Similarly to recieving notes on, you can recieve note off message for every note:

input.onAllNoteOff(message => {
console.log(`[Note Off] Note: ${message.note} Velocity: ${message.velocity} Channel: ${message.channel}`)
})

You can extract MIDI note value, channel (1-16) and velocity (in most cases equal to 0 although some of the synthesisers can assign an extra information, for example Roli products assign Lift as part of their 5D touch model)

Recieving Specific Note Off

Like with Note On message, you can subscribe only to specific note off messages which might be helpful for drum machines

input.onNoteOff(60, stopHighHat)
input.onNoteOff(62, stopSnareDrum)
input.onNoteOff(64, stopBassDrum)

Recieving CC (Control Change) messages

You can subscribe to specific CC messages. Those are usually send when user changes value on their MIDI keyboard using control knob, slider or digital screen. Similarly to note on / off messages, you can subscribe to all of them or only specific commands

Recieving all CC messages

To recieve all CC messages:

input.onAllControlChangeMessage(msg => {
console.log(`[CC] Control: ${msg.control} Value: ${msg.value} Channel: ${msg.channel}`)
})

Recieving specific CC messages

To recieve specific CC messages:

input.onControlChangeMessage(1, msg => {
console.log(`Modulation wheel value: ${msg.value}`)
})

Using named messages

Using control numbers to select or filter CC messages might be time consuming, unreadable and prone to errors. To prevent that, you can use @midival/constants package to provide nicer interface:

import { ControlChange } from '@midival/constants'

input.onControlChangeMessage(ControlChange.ModulationWheel, msg => {
console.log(`Modulation wheel value: ${msg.value}`)
})

Transforming CC to human readable values

Sometimes you want to use human readable values for your controls, either in the interface on in your debug logs. To help with that, you can use ControlChangeToReadableName map from @midival/constants package. This is especially helpful when dealing with onAllControlChangeMessage

import { ControlChangeToReadableName } from '@midival/constants'

input.onAllControlChangeMessage(({ control, value }) => {
console.log(`[CC] ${ControlChangeToReadableName[control]}: ${value}`)
})