From f57bb75dfc958b7cdf0d81c024c779a75e714c11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Sun, 20 Sep 2026 22:34:08 -0400 Subject: [PATCH] webmidi: report input timestamps on the monotonic clock's scale MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MIDIMessageEvent.timeStamp counts milliseconds from the page's time origin, while absolute_timestamp() answers with the monotonic clock, which emscripten reports as timeOrigin + performance.now(). A consumer subtracting the two — placing an incoming message inside the current audio buffer, say — was therefore off by the time origin, some 1.8e12 milliseconds, and every message landed at the start of the buffer. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01SCvayD61okH7PATstw4whi --- include/libremidi/backends/emscripten/midi_access.hpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/include/libremidi/backends/emscripten/midi_access.hpp b/include/libremidi/backends/emscripten/midi_access.hpp index 8f3a9957..6c1c3fc4 100644 --- a/include/libremidi/backends/emscripten/midi_access.hpp +++ b/include/libremidi/backends/emscripten/midi_access.hpp @@ -289,7 +289,10 @@ class midi_access_emscripten input.onmidimessage = (message) => { let bytes = message.data; var heapBytes = _arrayToHeap(bytes); - Module._libremidi_devices_input(port_index, message.timeStamp, bytes.length, heapBytes.byteOffset); + // message.timeStamp counts from the page's time origin, while + // absolute_timestamp() reads the monotonic clock, which emscripten + // reports as timeOrigin + performance.now(): put both on that scale. + Module._libremidi_devices_input(port_index, performance.timeOrigin + message.timeStamp, bytes.length, heapBytes.byteOffset); _freeArray(heapBytes); }; , port_index