T-049 — MTS-ESP and Scala: the piano follows an MTS-ESP master while it plays, and loads .scl/.kbm tunings

Status: done · Implementation: Claude · Area: rust · Depends on: T-018

Read for that: T-018 (D1, D3–D5, Outcome), T-016 (the recompute path: 0.37 ms per CoeffSet, 5 ms polling, a change lands ~10 ms later), T-019 (state v4 sections, preset TOML, the user preset directory), Piano notes (Parameters, State and presets), Plugin formats (audio-thread rules); code: crates/dsp/src/piano/{tuning,coeffs,exchange}.rs, crates/piano/src/{params,audio,gui,presets}.rs, crates/params/src/{state,preset}.rs; ODDSound’s client, github.com/ODDSound/MTS-ESP Client/libMTSClient.{h,cpp} (0BSD).

Why: T-018 gave the piano a recorded or B-stretched base with seven historical temperaments, and deferred the two ways microtonal musicians tune plugins: MTS-ESP (one master plugin in the session retunes every connected client, also while notes sound) and Scala files (.scl scale, .kbm keyboard mapping), the common exchange format of tunings.

How MTS-ESP works (from the client source, 2026-12-04)

  • The client is a small C++ file that dlopens libMTS from a fixed place (macOS /Library/Application Support/MTS-ESP/libMTS.dylib, Linux /usr/local/lib/libMTS.so, Windows %CommonProgramFiles%\MTS-ESP\LIBMTS.dll); without it the plugin simply has no master. All licences are 0BSD.
  • libMTS exports C functions: MTS_RegisterClient/MTS_DeregisterClient (once per plugin instance), MTS_HasMaster, MTS_GetTuningTable (a pointer to 128 doubles in shared memory, the frequency of each MIDI note, written by the master at any time), MTS_ShouldFilterNote(note, channel) (the master’s map leaves a key unmapped), MTS_GetScaleName, plus multi-channel variants.
  • A client is expected to read the table “as often as possible whilst a note is playing”, check note filtering at note-on, and show the connection on its UI. The table read is a plain memory read; nothing blocks.

What

  1. MTS-ESP client (D2): find and load libMTS once per process, register each plugin instance, read the single tuning table and the note filter, report connection and scale name.
  2. Retuning while playing (D3): with a master connected, the first partial of key (MIDI ) is moved to the master’s frequency for , by the same one-ratio-per-key shift as T-018 (inharmonicity, unison beats, layout unchanged; modes crossing Nyquist muted as now).
  3. Scala import (D5, D6): .scl and optional .kbm parsed into the same per-key first-partial targets; unmapped .kbm keys are not played.
  4. Controls and display (D4): an MTS-ESP switch, a third Tuning entry Scala, the controls that do not apply greyed out, connection status and scale name in the editor.

The engine gets one new input beside Settings: an optional per-key target table (88 first-partial frequencies in Hz, None = T-018’s tuning) that the worker reads when it builds a CoeffSet; key_cents takes it. MTS-ESP and Scala are two producers of that table; crates/dsp stays free of FFI and file I/O.

Decisions (accepted by the maintainer, 2026-12-04)

QuestionOptionsProposal
D1Scope(a) MTS-ESP with the single tuning table and note filtering, plus Scala .scl + .kbm; out: multi-channel tables, MTS SysEx (MIDI tuning messages without a master), .tun files, CLAP’s draft tuning extension; (b) MTS-ESP only, Scala a new task; (c) (a) plus MTS SysEx(a): both produce the same per-key table, so the second costs little once the first is there. Multi-channel tables do not fit an engine of 88 key states (one string set per key: two channels cannot tune the same key differently); SysEx needs CLAP’s MIDI-SysEx dialect and a parser for a rarely used path; clack 0.2 has no binding for the draft tuning extension
D2MTS-ESP client code(a) a Rust port of the client’s loader in a new platform crate crates/tuning (module mts, libloading for dlopen/LoadLibraryW, the same paths and symbol names, only the functions of D1; this module is the workspace’s second unsafe island, beside dsp::ftz); (b) compile ODDSound’s libMTSClient.cpp with cc and bind it (needs a C++ compiler in the Nix shell and CI, plus a C shim since the header is C++); (c) the crate mts-client-rs 0.2.0 (Sept 2026, 39 downloads)(a): about 150 lines; the client is a symbol table and a pointer read, and the port lets the table read be explicit (read_volatile of 88 doubles, as the C++ does by plain reads). (b) adds a C++ toolchain to every build for that; (c) is too young to depend on. crates/tuning also holds the Scala parser (safe code), so every later instrument reuses both
D3How a master’s change reaches the sound(a) the coefficient worker polls: every 5 ms (T-016’s interval) it reads the table for MIDI 21–108 and rebuilds the CoeffSet when any key moved by more than 0.01 cent or the master connected or left; a change lands ~10 ms later, phase-continuous (the mode states stay, as for every recompute); the audio thread only calls MTS_ShouldFilterNote at note-on; (b) the audio thread re-rotates the changed keys’ coefficients itself each block (one sincos per mode: a held chord of 10 keys is ~1,000 per block); (c) tuning read only at note-on(a): a recompute costs 0.37 ms off the audio thread and nothing on it; a master’s glide is followed in steps of 5–10 ms, finer than an audible pitch step for a gliding tuning. (b) costs audio-thread time for no audible gain; (c) is what ODDSound asks clients not to do
D4Controls and precedence(a) new parameter MTS-ESP (id 10, stepped Off/On, default On, recompute class): when On and a master is connected, the master’s frequencies are the targets and A4, Tuning, Stretch, Temperament, Root are inactive (greyed, their values kept); a master leaving returns to them within one poll; a filtered note’s note-on is ignored (its note-off still processed). Tuning gains a third label Scala (index 2, appended, so saved projects keep their meaning). Editor: a status line MTS-ESP: connected — ⟨scale name⟩ / no master; (b) the same with default Off(a): a master exists only when the user inserted one in the session, and following it automatically is what MTS-ESP clients do; with no master installed nothing changes (golden test, demos, every existing project)
D5Scala mapping(a) as Scala defines it: .scl degrees in cents or ratios, the last one the period; without .kbm a linear map with degree 0 on MIDI 60 and MIDI 60 at (A4 from the parameter: C4 = 261.63 Hz at 440, as MTS-ESP Mini and Surge default); with .kbm its map size, key range, middle note, reference note and frequency, octave degree and map (x = unmapped ⇒ the key is not played), and A4 inactive; Stretch, Temperament, Root inactive under Scala; Scala chosen with no scale loaded plays as Recorded; (b) (a) plus T-018’s stretch from B applied on top (period × per octave)(a): a Scala user expects the file’s frequencies exactly, and MTS-ESP masters play .scl the same way, so the two sources agree. (b) is worth trying for 12-note scales on this piano (pure 2:1 first partials make its octaves beat) but changes what the file says; a later option if wanted
D6How a Scala file gets in, and where it is kept(a) a user tunings directory shared by all instruments ($XDG_DATA_HOME/Instruments/Tunings, macOS ~/Library/Application Support/Instruments/Tunings, Windows %APPDATA%\Instruments\Tunings), scanned on the main thread when the editor opens or Rescan is pressed; the editor lists .scl files in one combo box and .kbm files (plus Default) in another; the chosen files’ text (not path) goes into state section SCAL (name, scl, kbm; ≤ 16 KiB each) and into presets as [tuning] with scl_name, scl, kbm_name, kbm, so a project or preset plays the same on another machine; a parse error shows next to the combo box and the last valid scale stays; (b) a native file dialog (rfd: a new dependency, and a modal dialog from a plugin’s GUI thread behaves differently per host and OS); (c) presets only (hand-edited [tuning])(a): no dialog and no new dependency, the same pattern as T-019’s user presets; T-030 (export/import from the GUI) can add a file dialog for both later

Done when

  • Gates of AGENTS.md pass, including the plugin bundle with clap-validator and the GUI smoke test (new controls and status line).
  • Tests (written from this page):
    • crates/tuning Scala: parses the Scala reference examples (comments, cents and ratio degrees, a degree without decimal point = ratio, trailing text ignored); rejects wrong counts, zero or negative ratios, files over 16 KiB; 12-TET .scl without .kbm gives 440 · 2^((m−69)/12) to 1e-9; a 7-note .kbm with x entries maps and filters as Scala’s documentation examples; non-octave periods (Bohlen–Pierce 3/1).
    • crates/tuning MTS: with no libMTS present the client reports no master and loads nothing; an opt-in test (#[ignore], run by a script) loads ODDSound’s Linux libMTS.so from a given path, registers a master in the same process (MTS_RegisterMaster, MTS_SetNoteTuning) and sees the client read the table, the filter and the scale name.
    • Engine: a target table equal to T-018’s frequencies reproduces today’s coefficients bit for bit; a table moves each key’s first partial to its target within 0.01 cent with all other modes of the key scaled by the same ratio; a non-finite or non-positive entry keeps the key’s T-018 tuning; a table change while a note sounds is heard after one recompute (fake source, the worker’s poll) and the output stays continuous (no step above the note’s own sample-to-sample maximum).
    • Plugin: precedence of D4 and D5 (MTS On + master > Scala > T-018) through the parameters; filtered notes do not sound and their note-offs are harmless; SCAL state and preset [tuning] round-trip, a state without SCAL loads as today, an oversized or unparsable SCAL is refused like other malformed sections; no_alloc passes with a fake MTS source changing its table and filtering notes while notes ring.
  • Golden test unchanged, and cargo xtask render-demos produces identical files (nothing changes without a master or a scale).

Outcome

  • crates/tuning (new, instruments-tuning): scala.rs parses .scl (cents with a period, ratios p/q or whole numbers, comments and trailing text ignored, ≤ 16 KiB) and .kbm (size, range, middle, reference note and frequency, octave degree, map with x; trailing unmapped entries may be left out) and maps them to all 128 MIDI notes as NoteTuning::{Hz, Untuned, Unmapped} (outside the map’s first…last: the instrument’s own tuning; an unmapped reference note is an error). KeyboardMap::linear(a4) is the default without a .kbm. mts.rs ports ODDSound’s client loader with libloading (0.8, already in the lock file): the three default paths (Windows via %CommonProgramFiles%), one process-wide LibMts, an MtsClient per instance (MTS_RegisterClient / MTS_DeregisterClient), has_master, note_frequencies (volatile reads of the shared table), should_filter_note (channel −1), scale_name. The module is the crate’s only unsafe code.
  • Engine (crates/dsp/src/piano/): KeyTargets (88 first-partial frequencies; not finite or ≤ 0 = own tuning; follows_a4 for Scala without a mapping) and CoeffSet::with_targets: a targeted key’s ratio is target ÷ the model’s first partial, so its other modes move by the same ratio. Settings gains mts and TUNING_SCALA (index 2: Recorded without the temperament for keys without a target). SettingsRequests holds the ExternalTuning source (set once) and the Scala targets; the worker polls the source every 5 ms while MTS-ESP is on and rebuilds when a key moved by more than 0.01 cent or gained or lost its target. crates/dsp now depends on instruments-tuning (impl ExternalTuning for MtsClient).
  • Plugin (crates/piano/): parameter id 10 MTS-ESP (Off/On, default On, group Microtuning); Tuning gains Scala; tuning.rs (ScalaSlot: parsed text, unmapped-key flags for the audio thread, the user tunings directory listing and reads); audio.rs skips a note-on the active source filters; state SCAL and preset [tuning] through apply_snapshot, which parses the scale first and refuses the whole state or preset if it does not parse. A state or preset without a scale unloads it (missing → default, as for parameters). Editor: on Play the controls a followed master or Scala makes ineffective are greyed (params::is_inactive); on Edit a Microtuning section with the MTS-ESP switch, its status (no master / following the master — name / connected, not followed), the Scala scale and mapping choosers and Rescan; choosing a scale switches Tuning to Scala and marks the project dirty. The section went to Edit because one more row on Play pushed the meters out of the 720 × 400 view (the editor test that checks it failed).
  • Tests: crates/tuning (Scala’s documentation example, ratios and cents, malformed files, 12-TET = 440 · 2^((m−69)/12) to 1e-12, a white-key map with x, a range and an unmapped reference, Bohlen–Pierce; no libMTS ⇒ no master); scripts/test-libmts.sh (opt-in, ignored in CI) loads ODDSound’s Linux libMTS.so, registers a master in the same process and reads its table, filter and scale name — passed. Engine: targets_equal_to_the_recorded_tuning_keep_the_coefficients_bit_for_bit, targets_move_each_first_partial_and_scale_the_rest_of_its_key (0.01 cent; bad entries keep the key’s tuning; follows_a4), scala_without_a_target_plays_as_recorded_without_the_temperament, targets_from_mts_and_scala_tables_and_their_tolerance, the_worker_follows_an_external_tuning_while_a_note_sounds (connect, retune, off, on, leave; phase-continuous at every swap). Params: SCAL round-trip, malformed or oversized sections refused, presets carry [tuning]. Plugin: tests/tuning.rs (a stand-in master over Scala over Werckmeister III, filtered notes silent with harmless note-offs, state and preset round-trips, bad scales refused with nothing changed), process_does_not_allocate_while_a_master_retunes_and_filters, inactive_tuning_controls_follow_the_active_source, edit_tab_shows_microtuning_and_a_master_greys_the_tuning_controls.
  • Gates (2026-12-04): cargo fmt --check, cargo clippy --all-targets -- -D warnings, cargo nextest run (191 passed, 2 skipped), Julia Pkg.test(), cargo xtask bundle piano --release, clap-validator validate (38 passed, 0 failed, 6 skipped), scripts/gui-smoke-test.sh (passed; the first two runs after building the gui-test shell failed on X start-up, then four runs passed). Default sound unchanged: golden test, and all seven demo WAVs byte-identical to a render of the previous commit, so no demo refresh.
  • Not checked here: a real MTS-ESP master (ODDSound’s MTS-ESP Mini) in a DAW, and the Windows and macOS library paths; the smoke test captures only the Play view.

T-053 extended Scala map parsing to signed degrees.