Dev.to WebDev πŸ›  Dev πŸ‘ 0 πŸ“– 2 min read

Web MIDI jitter: measure input timestamps, not just callbacks

Disclosure: AI-generated for Sonari Studio. The synthetic trace calculations were checked with Node; this is not a hardware benchmark. Your MIDI knob sends four updates, and your web app handles the last three almost to

Disclosure: AI-generated for Sonari Studio. The synthetic trace calculations were checked with Node; this is not a hardware benchmark.

Your MIDI knob sends four updates, and your web app handles the last three almost together. The UI looks like it jumped. Before replacing the controller or adding smoothing, check whether the messages arrived in a burst or whether your JavaScript handled them in a burst. Record both the event timestamp and the time your handler started.

Record both times without logging each event

On a secure page in a Web MIDI-capable browser, ask the user to choose a MIDI input. Call this function from a Start button with that input’s ID. The example captures MIDI 1.0 Control Change messages only. Turn one knob at a time, then call the returned function to stop and retrieve the trace.

async function startTrace(inputId) {
  const access = await navigator.requestMIDIAccess({ sysex: false });
  const input = access.inputs.get(inputId);
  if (!input) throw new Error('Selected MIDI input is unavailable');
  const rows = [];

  function record(event) {
    const handled = performance.now();
    const [status, cc, value] = event.data;
    if ((status & 0xf0) !== 0xb0 || rows.length >= 1024) return;
    rows.push({
      received: event.timeStamp, handled,
      channel: (status & 0x0f) + 1, cc, value
    });
  }

  input.addEventListener('midimessage', record);
  return () => {
    input.removeEventListener('midimessage', record);
    return rows.slice();
  };
}

The W3C Web MIDI specification defines the event timestamp as the time the system received the message. Subtracting it from the handler-entry time estimates receipt-to-callback delay. It does not measure the time from your hand to the controller, the USB transfer time, or the time to audible output. Browser timestamp precision can vary. Compare messages on the same channel and controller number so other controls do not distort the gaps.

A trace that separates the two cases

Here are four synthetic messages, all times in milliseconds:

System receipt Handler entry Receipt to callback
100 101 1
110 141 31
120 142 22
130 143 13

Input spacing is 10, 10, 10 ms. Callback spacing is 40, 1, 1 ms. The incoming events are evenly spaced at the recorded boundary, but their callbacks bunch up. That is consistent with delays in delivery or scheduling; these timestamps alone do not prove what caused the delay. If the receipt timestamps themselves bunch up, the irregularity is already present by the time recorded as system receipt. The trace cannot locate it more precisely upstream.

To reproduce both gap sequences without a MIDI device:

const receipt = [100, 110, 120, 130];
const callback = [101, 141, 142, 143];
const gaps = xs => xs.slice(1).map((x, i) => x - xs[i]);
console.log(gaps(receipt));                       // [10, 10, 10]
console.log(gaps(callback));                      // [40, 1, 1]
console.log(callback.map((x, i) => x - receipt[i])); // [1, 31, 22, 13]

Decide what to change

Repeat the capture with expensive widget updates disconnected from the MIDI handler. If receipt spacing stays about the same and callback gaps shrink, try separating incoming events from rendering. For a knob on screen, displaying only the latest value on the next frame may be enough. For recording a performance, preserve each event with its timestamp and choose a separate policy for displaying them. A smoothing filter cannot recover timing you discarded.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.