Skip to content

fix(midi): always export SMF as Type 1 (Parallel) - #61

Merged
PhysShell merged 1 commit into
mainfrom
claude/midi-format-type1
Jun 16, 2026
Merged

fix(midi): always export SMF as Type 1 (Parallel)#61
PhysShell merged 1 commit into
mainfrom
claude/midi-format-type1

Conversation

@PhysShell

@PhysShell PhysShell commented Jun 16, 2026

Copy link
Copy Markdown
Owner

Problem

A generated single-track riff (griff generate …) produced a MIDI file that
strict readers reject with "a type 0 file must contain exactly one track".
Multi-track output (import, complement) opened fine.

Root cause

export_score (core/src/midi.rs) chose the SMF header format from the note-track
count:

let format = if score.tracks.len() == 1 { Format::SingleTrack } else { Format::Parallel };

But a dedicated meta/conductor chunk is always emitted (ADR-0003). So a
single-track score serialises to two chunks (meta + 1 note) while declaring
Type 0 — and SMF Type 0 must contain exactly one track chunk. The file is
therefore spec-invalid. Multi-track scores avoided this only by accident of
already having ≥2 note tracks.

Confirmed on a freshly generated riff: header was format=0, ntracks=2 (invalid);
after the fix it is format=1, ntracks=2 with two real MTrk chunks.

Fix

Always declare Format::Parallel. One line, plus a comment explaining the
invariant.

Test (TDD, red → green)

export_score_single_track_is_type1_parallel: a single-track score must export
as Format::Parallel with ≥2 chunks. Red before the fix (was Type 0), green
after. Full griff-core suite (87 tests) and clippy --all-targets are clean.

This is the first of two fixes for the reported import/export issues; the GP6
string-indexing pitch fix follows in a separate PR.

https://claude.ai/code/session_01TTUbGjzD8ysnVnCJnZJE95


Generated by Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Fixed MIDI file export to consistently use the proper header format for all score configurations, ensuring accurate metadata inclusion and correct file structure compatibility.

export_score declared Type 0 (SingleTrack) for single-track scores, but a
dedicated meta/conductor chunk is always emitted (ADR-0003), so the file
carried two chunks — spec-invalid for Type 0, which must contain exactly
one track. Strict readers rejected such files (notably single-track
`generate` output), while multi-track import/complement output stayed
valid only by accident of having ≥2 note tracks. Always declare Type 1.

Adds a regression test: a single-track export must parse as
Format::Parallel with ≥2 chunks.

https://claude.ai/code/session_01TTUbGjzD8ysnVnCJnZJE95
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: ce3ba47e-6f6c-4c3e-9cf1-c7cb34eb6cd8

📥 Commits

Reviewing files that changed from the base of the PR and between 44c3cc7 and 8047a0f.

📒 Files selected for processing (1)
  • core/src/midi.rs

📝 Walkthrough

Walkthrough

export_score in core/src/midi.rs is changed to unconditionally set the SMF header format to Format::Parallel instead of conditionally using Format::SingleTrack for single-track scores. A regression test is added that exports a single-track fixture and asserts the resulting SMF has Format::Parallel and at least two tracks.

Changes

SMF Header Format Fix

Layer / File(s) Summary
Unconditional Parallel format + regression test
core/src/midi.rs
export_score removes the conditional Format::SingleTrack branch and always emits Format::Parallel. Test imports gain midly::Format, and a new regression test exports a single-track fixture, then asserts the parsed SMF reports Format::Parallel and contains at least two chunks.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐇 A track once lonely, declared too slim,
Now marches in parallel, row by row!
The meta chunk leads, the notes follow trim,
Type-1 format puts on quite the show.
No more single-file fib — the header's true,
With two chunks at least, the spec shines through! 🎵

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically summarizes the main change: fixing MIDI export to always use SMF Type 1 (Parallel) format instead of conditionally selecting Type 0 based on track count.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/midi-format-type1

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

2 participants