Repository navigation
[patch] Stop Pitch octaves wrapping into range, and reject whitespace or '+' in the octave - #402
Merged
Merged
Conversation
… or '+' in the octave Fixes #307 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019RYN9jT6szUiySkywFKCA1
|
This was referenced Oct 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



Fixes #307
What was wrong
Pitch.TryParseandPitch.Create(NoteLetter, Accidental, int octave)computed((octave + 1) * 12) + letter + accidentalin uncheckedint. A huge octave wrapped back into 0..127:"C1073741823","C2147483647"and"C-1073741825"all parsed as C-1, and the typed factory returned C-1 for those octaves instead of throwing.NumberStyles.Integer, which allows leading and trailing whitespace and a leading+. So"C 4 "and"C+4"parsed and then printed back as"C4", which breaks the canonical-text round trip.Change (
Semantics.Music/Pitch.cs)MidiOf(octave, letter, accidental)computes the MIDI number inlong, so no octave can wrap. BothTryParseand the typedCreateuse it before the 0..127 range check. I choselongarithmetic over a hard-coded octave window so the range check stays the only rule.Create(NoteLetter, Accidental, int)now throwsArgumentOutOfRangeExceptionfor an out-of-range result itself, namingoctave. This is the documented exception type; before, it came fromCreate(int)namingmidi.TryParseparses the octave withNumberStyles.AllowLeadingSignonly, and rejects an explicit+. So a leading-(needed for octave -1) is the only thing accepted besides digits, and whitespace is rejected.Tests (
PitchTests)TryParse_RejectsWrappingOrNonCanonicalOctave:C1073741823,C-1073741825,C2147483647,C-2147483648,C 4,C 4,C4,C+4, plusCandC#with no octave.TryParse_AcceptsNegativeOctave:C-1still parses to MIDI 0 and prints asC-1.Create_FromLetter_RejectsOctaveThatWouldWrap:1073741823,int.MaxValueandint.MinValuethrowArgumentOutOfRangeException.With
Pitch.csreverted, 11 of these cases fail. With the fix, the fullSemantics.Testsuite passes: 1494 passed, 0 failed, 8 skipped.Semantics.Musicbuilds for every target framework.TimeSignatureandVelocityalso parse withNumberStyles.Integer. I left them out of this PR to keep it scoped to the issue.🤖 Generated with Claude Code
https://claude.ai/code/session_019RYN9jT6szUiySkywFKCA1
Generated by Claude Code