Skip to content

feat: add MIDI export via audio-to-note transcription - #32

Open
Antojoel wants to merge 2 commits into
danielravina:mainfrom
Antojoel:feat/midi-export
Open

Antojoel wants to merge 2 commits into
danielravina:mainfrom
Antojoel:feat/midi-export

Conversation

@Antojoel

@Antojoel Antojoel commented Sep 29, 2026 •

Copy link
Copy Markdown

Summary

Adds a per-stem Export as MIDI action (new icon button next to the existing WAV export button in each stem lane), using Basic Pitch (Spotify, Apache-2.0) for audio-to-note transcription.

  • Backend choice: Basic Pitch's ONNX inference backend, not TensorFlow — avoids adding a second heavy ML framework alongside the existing torch/demucs stack. The onnx model file itself ships inside the pip package (~230KB); the actual download is Basic Pitch's dependency tree (librosa, onnxruntime, scipy, numba — ~110–160MB depending on platform), installed on demand the first time MIDI export is used, same pattern as the existing optional vocals/fine-tuned engines.
  • Why --no-deps: basic-pitch 0.4.0's own metadata hard-requires full tensorflow on Windows/Linux when Python ≥3.11 (our bundled runtime is 3.11.10) and coremltools on macOS, even with the [onnx] extra. On top of that, ICASSP_2022_MODEL_PATH picks the first importable backend in the order TF > CoreML > TFLite > ONNX, so ONNX would never actually be used. So basic-pitch==0.4.0 is installed with --no-deps, its runtime deps are listed explicitly in env.ts with onnxruntime as the only backend, and midi.py loads the ONNX model explicitly via build_icassp_2022_model_path(FilenameSuffix.onnx).
  • python/midi.py — new script following the same JSON-line progress protocol as separate.py/roformer.py. Note: Basic Pitch prints debug noise straight to stdout via bare print() calls (per-chunk isfinite/shape/dtype, progress banners) — redirected away in the script so only our JSON lines reach the parent process's stdout parser.
  • env.ts — ensureMidiEngine(): installs the deps, then basic-pitch==0.4.0 --no-deps, then runs an import smoke check (onnxruntime, basic_pitch.inference, ONNX model file present) before writing the .midi-ready marker, so a broken install fails at install time rather than on first export.
  • pipeline.ts — exportStemAsMidi() spawns the script and resolves on its "done" event.
  • Best results on melodic/harmonic stems (piano, guitar, bass); noisier on drums or vocals, noted in the button's tooltip.

A real bug caught along the way

While integration-testing against this project's actual venv (not just a scratch one), basic-pitch installed cleanly but failed at import time: resampy (a transitive dependency) still does import pkg_resources, which setuptools removed in version 81. This project's own bootstrap (pip install -U pip wheel setuptools) always installs the latest setuptools, so this reliably breaks. Fixed by pinning setuptools<81 alongside the basic-pitch install — verified the pin resolves it, in the same venv where I reproduced the failure.

Download size (measured, Python 3.11)

Resolved per target platform with uv pip compile --python-platform (plain pip download --platform evaluates markers against the host, which is why an earlier macOS dry run showed no TensorFlow), then downloaded the exact wheels:

Platform basic-pitch[onnx] (before) --no-deps + explicit deps (now)
Windows x64 445MB (tensorflow, keras) 126MB
Linux x64 678MB (tensorflow, keras) 157MB
macOS arm64 114MB (coremltools) 111MB

Test plan

  • Per-platform resolution on Python 3.11 (table above) — no tensorflow or coremltools on any platform.
  • Built a venv reproducing this project's exact bootstrap sequence from scratch and installed the MIDI engine into it — numpy, torch, demucs, and basic_pitch all import together cleanly.
  • Ran python/midi.py through that venv against real separated stems (piano, guitar, bass) from an actual split — valid .mid output each time (verified note counts and duration with pretty_midi), clean single-JSON-line-per-event stdout, no traceback.
  • With the new install (macOS, Python 3.11): only the ONNX backend present, smoke check passes, midi.py transcribes a two-tone test file to the expected 2 notes.
  • Reproduced the pkg_resources failure without the setuptools pin, then confirmed the pin fixes it, in that exact venv.
  • npm run typecheck && npm run build pass; dev app boots cleanly with the new IPC handlers registered.
  • Install + export not yet run on a real Windows/Linux machine (verified by resolution only).

@danielravina

Copy link
Copy Markdown
Owner

Thanks for this @Antojoel — MIDI export is a really nice addition, and the setuptools<81/pkg_resources investigation is exactly the kind of digging I appreciate.

Two things before this can go in:

  1. Rebase needed. Main has moved quite a bit (Intel Mac support, data dir env, number-key mute, pan controls, drag & drop) and GitHub reports conflicts in index.ts, Player.tsx and StemLane.tsx. Looks straightforward, just needs doing.

  2. TensorFlow gets pulled in on Windows/Linux. I checked basic-pitch on PyPI: basic-pitch[onnx] carries a marker that hard-requires full tensorflow on non-Darwin platforms with Python ≥3.11 — and our bundled Python is 3.11. So on Windows and Linux this installs the full TF wheel (475MB+ download), not the ~300MB the PR estimates. Can you verify on Windows/Linux and correct the size message (or at least warn that the download is much heavier there)?

Also two small ones while you're in there:

  • Pin basic-pitch==0.4.0 like the other installs in env.ts.
  • A quick smoke check (python -c "from basic_pitch.inference import predict") before writing the .midi-ready marker would catch broken installs at install time instead of at first use.

Thanks again — looking forward to getting this in!

@Antojoel

Copy link
Copy Markdown
Author

Sure I'll get it sorted.

Adds a per-stem "Export as MIDI" action using Basic Pitch (Spotify,
Apache-2.0) with its ONNX backend — avoids pulling in TensorFlow
alongside the existing torch stack. The onnx model itself ships inside
the pip package (~230KB); installed on demand into the existing venv
the first time it's used (~300MB dependency tree: librosa, onnxruntime,
scipy, numba).

- python/midi.py: runs transcription, emits the same JSON-line
  progress protocol as separate.py/roformer.py. basic-pitch prints
  debug noise straight to stdout via print() (isfinite/shape/dtype
  per chunk, progress banners) — redirected away so only our JSON
  lines reach the parent process.
- env.ts: ensureMidiEngine() installs "basic-pitch[onnx]" into the
  venv, gated behind a marker file once installed. Also pins
  "setuptools<81" alongside it — resampy (a transitive basic-pitch
  dependency) still does `import pkg_resources`, which setuptools
  removed in 81, and this project's own bootstrap already upgrades
  setuptools to latest, so this reliably breaks without the pin.
- pipeline.ts: exportStemAsMidi() spawns midi.py and resolves once it
  reports "done".
- UI: a MIDI icon button per stem in StemLane, wired through Player
  with a busy spinner and an inline error banner on failure.

- Verified via pip dry-run that basic-pitch[onnx] resolves against
  the project's pinned numpy<2 without forcing an upgrade, and pulls
  no tensorflow.
- Installed for real into a venv reproducing this project's exact
  bootstrap sequence (pip install -U pip wheel setuptools, then the
  pinned demucs/torch/numpy install) and confirmed torch, demucs and
  basic_pitch all import together cleanly.
- Ran python/midi.py through that venv against real separated stems
  (piano, guitar, bass) from an actual split — valid .mid output each
  time, clean single-JSON-line-per-event stdout, no traceback.
- Reproduced the pkg_resources failure without the setuptools pin,
  confirmed the pin fixes it, in that exact venv.
- npm run typecheck && npm run build pass; dev app boots with the new
  IPC handlers registered.
basic-pitch 0.4.0 hard-requires full tensorflow on non-Darwin with
python >= 3.11 (our bundled runtime is 3.11) and coremltools on Darwin,
and ICASSP_2022_MODEL_PATH prefers TF > CoreML > TFLite > ONNX, so the
[onnx] extra still pulled TF and never actually ran on onnx.

- install basic-pitch==0.4.0 with --no-deps and list its runtime deps
  explicitly, with onnxruntime as the only backend
- load the onnx model explicitly in midi.py
- verify the install imports before writing the .midi-ready marker
- correct the size estimate (~150MB)

Measured download on py3.11: Windows 445MB -> 126MB, Linux 678MB ->
157MB, macOS ~111MB; no tensorflow on any platform.
@Antojoel

Copy link
Copy Markdown
Author

@danielravina thanks for the review, all addressed in 87ca649:

  • Rebased on main; conflicts in index.ts / Player.tsx / StemLane.tsx resolved (dropped my Space handler in favour of the new unified keydown handler).
  • TensorFlow: good catch, and it was worse than the download size. ICASSP_2022_MODEL_PATH prefers TF > CoreML > TFLite > ONNX, so the PR was actually running inference on TF (Win/Linux) and CoreML (mac), never ONNX. Rather than warn, I now install basic-pitch==0.4.0 --no-deps plus its runtime deps explicitly, with onnxruntime as the only backend, and midi.py loads the ONNX model directly. Measured on py3.11: Windows 445MB → 126MB, Linux 678MB → 157MB, mac ~111MB. Size message updated to ~150MB.
  • Pinned basic-pitch==0.4.0.
  • Smoke check (imports basic_pitch.inference and onnxruntime, confirms the ONNX model file exists) runs before writing .midi-ready; a failure surfaces as an install error.

Tested on macOS: fresh install pulls only onnxruntime (no tensorflow/coremltools), smoke check passes, stems export to valid .mid. Windows/Linux verified by dependency resolution only.

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.

3 participants