Skip to content

Make written MIDI tempo rounding opt-in - #266

Merged
craffel merged 2 commits into
craffel:mainfrom
snowyukitty:fix/round-written-midi-tempo
Oct 27, 2026
Merged

craffel merged 2 commits into
craffel:mainfrom
snowyukitty:fix/round-written-midi-tempo

Conversation

@snowyukitty

@snowyukitty snowyukitty commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Addresses #103 with opt-in rounding while preserving existing writes.

PrettyMIDI.write(filename, round_tempo=True) uses mido.bpm2tempo() to round MIDI tempos to the nearest integer microsecond per quarter note. For example, 123 BPM becomes 487805 instead of 487804. Omitting the argument, or passing round_tempo=False, retains the previous truncation behavior.

The method documents the option. Regression tests decode written MIDI bytes and cover the default and explicit modes, integer tempo changes across four resolutions and three read/write cycles when rounding is enabled, boundary tempo values, unchanged note ticks, and unchanged in-memory timing data.

Validation on Linux with Python 3.12, NumPy 2.5.4 and mido 1.3.3:

  • python -m pytest tests -q: 29 passed, with the existing adjust_times warning.
  • Restoring the previous PR source makes all four tempo tests fail: the compatibility test detects the changed default, and the three rounding tests detect the missing option.
  • A separate 32-case comparison of complete serialized MIDI files matches upstream bytes for both the omitted and explicit-false modes, and matches the previous rounded PR bytes for the explicit-true mode. Each case includes tempo changes and notes.
  • python -m flake8 tests/test_write_tempo.py and git diff --check: passed.

The legacy nosetests -v --with-coverage --cover-package=pretty_midi command cannot start on Python 3.12 because nose imports the removed imp module. Python 2.7/3.5 and DAW playback were not tested.

@craffel

craffel commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Thanks, but this needs some kind of argument that allows for backwards-compatible behavior.

@snowyukitty snowyukitty changed the title Round written MIDI tempo to the nearest microsecond Make written MIDI tempo rounding opt-in Oct 26, 2026
@craffel

craffel commented Oct 27, 2026

Copy link
Copy Markdown
Owner

ThankS!

@craffel
craffel merged commit bc3a1c5 into craffel:main Oct 27, 2026
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