Fix SMPTE time divisions not being read/written correctly #275
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Discovered this while testing a purpose-built MIDI parser using DryWetMidi as a storage model/serializer. Probably went a little overboard with that testing lol, but I found a bug as a result and thought I'd fix it.
Take an SMPTE format of 30 FPS and a resolution of 80 (the example provided in the MIDI file standard):
The expected written value is
E2 50
, but the actual value isE1 B0
. This happens because DryWetMidi was negating the wholeshort
value instead of just the upper byte, which mangles the resolution and causes the format to be 1 lower than it should be (since the two's-compliment +1 step is being done to the lower byte instead of the upper). Only the SMPTE format value should be written negated, the resolution value is positive.One concern I have with this change is compatibility with files written by prior versions of DryWetMidi, but I consider that concern mostly irrelevant when these files most likely don't parse correctly in actual use (unless the target application is also using DryWetMidi).
There could be a read/write option for this, but I'm not familiar enough with the architecture to feel confident in implementing that myself currently. Do let me know if that's something you'd want added.