Skip to content

Translate a live MIDI 1 stream to MIDI 2 without losing what follows a pending RPN - #295

Open
jcelerier wants to merge 1 commit into
masterfrom
fix/midi1-stream-controller-translation
Open

jcelerier wants to merge 1 commit into
masterfrom
fix/midi1-stream-controller-translation

Conversation

@jcelerier

Copy link
Copy Markdown
Member

Problem

midi1_to_midi2 is used one message at a time on live input, but it drove cmidi2_convert_midi1_to_ump, which is a whole-buffer converter:

  • It keeps one RPN / NRPN / data entry state for all channels.
  • It emits an RPN only on CC 38.
  • At the end of every call it returns INVALID_DTE_SEQUENCE while an RPN is pending, and the wrapper then drops the message it had just converted.

A keyboard sending CC 101, CC 100, CC 6 without CC 38 (how most send their pitch bend range) therefore made every later message of the device disappear, notes included, until a CC 38 arrived. This was reproduced on the ALSA sequencer with an M-Audio Keystation's exact sequence.

Change

midi1_to_midi2 becomes a stream translator, with libremidi::controller_translation choosing how controllers are handled:

  • raw (default): every channel voice message is translated on its own. CC 0, 6, 32, 38 and 96 to 101 stay controllers, and a Program Change carries no bank. Converting back to MIDI 1 gives the bytes that were sent, in order.
  • semantic: RPN, NRPN and bank select are combined following the UMP specification's default translation (Appendix D.3.3 / D.3.4):
    • state is kept per channel;
    • data entry is sent at once on CC 6 with LSB 0, then again on CC 38;
    • the selection is kept after data entry;
    • the null RPN / NRPN deselects;
    • CC 6 / 38 with nothing selected are dropped;
    • CC 0 / 32 are folded into the next Program Change.

Sending on CC 6 is the specification's optional timeout taken to zero, and matches what CoreMIDI was reported to do. Of the translators surveyed (Linux kernel, AM_MIDI2.0Lib, JUCE, CoreMIDI, cmidi2 / ktmidi), cmidi2 and ktmidi are the only ones that keep global state or fail on a pending RPN.

In both modes:

  • no error depends on earlier input;
  • a swallowed message is success with no output;
  • UMPs produced before an input error are still delivered;
  • running status works within a call;
  • nothing allocates.

SysEx still goes through cmidi2. midi2_to_midi1 now delivers what it produced before an error, and reports a truncated last packet instead of reading past it. ump_input_configuration::midi1_controllers picks the mode where MIDI 1 input is converted for a UMP client.

Tests

tests/unit/controller_translation.cpp covers:

  • 25 byte-sequence cases in semantic mode, and their raw-mode expectations;
  • 10,000 random RPN / NRPN / bank fragments mixed with notes in both modes: every note arrives, nothing errors, and raw mode round-trips byte for byte;
  • round trips in both directions;
  • error delivery, and output larger than the conversion buffer.

All 14 non-PipeWire unit tests pass in a header-only build with clang.

🤖 Generated with Claude Code

https://claude.ai/code/session_01KdPgugz48pdcaqHJAg2vid

…a pending RPN

midi1_to_midi2 is used one message at a time on live input, but drove
cmidi2_convert_midi1_to_ump, a whole-buffer converter: it kept one RPN /
NRPN / data entry state for all channels, emitted an RPN only on CC 38, and
returned INVALID_DTE_SEQUENCE at the end of every call while one was
pending, upon which the wrapper dropped the message it had just converted.
A keyboard sending "CC 101, CC 100, CC 6" without CC 38, which is how most
of them send their pitch bend range, made every later message of the
device disappear.

midi1_to_midi2 is now a stream translator with a choice of how controllers
are handled, libremidi::controller_translation:
- raw (the default): every channel voice message is translated on its own.
  CC 0, 6, 32, 38 and 96 to 101 stay controllers, a Program Change carries
  no bank, and a MIDI 1 consumer converting back gets the bytes that were
  sent, in order.
- semantic: RPN, NRPN and bank select are combined into MIDI 2 messages as
  the UMP specification's default translation describes (Appendix D.3.3,
  D.3.4), with state per channel. Data entry is sent at once on CC 6 with
  LSB 0 and again on CC 38, the selection is kept after it, the null RPN /
  NRPN deselects, CC 6 / 38 with nothing selected are dropped, and CC 0 /
  32 are folded into the next Program Change.

In both, no error depends on earlier input, a swallowed message is a
success with no output, UMPs produced before an input error are delivered,
running status works within a call, and nothing allocates. SysEx still goes
through cmidi2. midi2_to_midi1 delivers what it produced before an error,
and reports a truncated last packet instead of reading past it.

ump_input_configuration::midi1_controllers chooses the mode where MIDI 1
input is converted for a UMP client.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KdPgugz48pdcaqHJAg2vid

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant