Emit MIDI
Read and play notes with the typed MIDI surface.To play notes, give the device a MIDI output and emit typed events. This is how you build an arp, a sequencer, a chord tool.
m.addPort(midiOut("MIDI"));Each helper takes a sample offset (where in the block it happens), then the note and velocity:
ctx.noteOn(0, 60, 100); // middle C, velocity 100
ctx.noteOff(0, 60); // release it
ctx.cc(0, 74, 64); // CC 74 to 64
ctx.pitchBend(0, 0); // centered, no bend (-8192..8191)A velocity-0 noteOn emits a note-off, so you can route both through one call.
Read notes
Declare a midiIn port, then decode each event into a typed MidiMsg:
const count = ctx.midiInCount();
for (let e = 0; e < count; e++) {
const ev = ctx.midiEvent(e);
if (ev.isNoteOn()) {
ctx.noteOn(ev.offset, ev.note + 12, ev.velocity, ev.channel); // up an octave
} else if (ev.isNoteOff()) {
ctx.noteOff(ev.offset, ev.note + 12, ev.channel);
}
}ev is reused on every call (its fields are overwritten), so copy out anything you
need to keep, and don't hold two events at once. It exposes ev.note, ev.velocity,
ev.channel, ev.controller, ev.value, and ev.bendNorm().
Raw bytes
For a message the typed surface doesn't cover, ctx.emitMidi(offset, status, d1, d2)
is the escape hatch: pass the full status byte yourself.
ctx.emitMidi(0, 0x90, 60, 100); // note on, raw status byteFull event list in the MIDI reference.