Skip to content

Faster Pitch(80) or p.midi = 20 or p.ps = 60 creation - #2071

Merged
mscuthbert merged 5 commits into
masterfrom
pitch-midi-ps
Oct 8, 2026
Merged

mscuthbert merged 5 commits into
masterfrom
pitch-midi-ps

Conversation

@mscuthbert

@mscuthbert mscuthbert commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Faster whole-number .ps, .midi, and Pitch(number), part of the #2010 speedup work.

New pitchClassToDefaultStepAlter gives the step and alter for each pitch class. _convertPsToStep uses it for whole numbers and now returns None for their Microtone instead of Microtone(0); Pitch(number) repeats the lookup inline to save a call. The ps setter assigns step, accidental, and octave directly and informs the Note once instead of three times. Results are otherwise unchanged: p.midi = 60 still gives no accidental and Pitch(60) still gives a natural (#2010 will make them agree). A new test pins every whole-number ps and MIDI value; it passes on master too.

before after
p.midi = 60 745 ns 421 ns
p.midi = 61 836 ns 459 ns
p.ps = 61 788 ns 409 ns
p.ps = 61.0 952 ns 546 ns
p.ps = 61.5 1644 ns 1429 ns
Pitch(60) 658 ns 406 ns
Pitch(61) 754 ns 433 ns
Pitch(midi=60) 906 ns 585 ns
Note(60) 1779 ns 1468 ns

Whole-file MIDI imports (k525MIDIMvt1.mid, test03/04/09.mid, CANYON.MID) do not measurably change: pitches are a tiny part of them.

AI-assisted (Claude)

pitchClassToDefaultStepAlter gives the step and alter for each pitch class.
_convertPsToStep, the ps setter, and Pitch(number) use it for whole numbers.
The ps setter no longer builds a natural Accidental only to discard it, or
a Microtone(0) (_microtone stays None, which every reader treats the same),
and it assigns step, accidental, and octave directly, informing the client
once instead of three times.  Results are unchanged: p.midi = 60 still gives
no accidental, and Pitch(60) still gives a natural.

p.midi = 60       745 ns -> 165 ns
p.midi = 61       836 ns -> 366 ns
p.ps = 61.0       952 ns -> 345 ns
Pitch(60)         658 ns -> 406 ns
Pitch(midi=60)    906 ns -> 324 ns
Note(60)         1779 ns -> 1482 ns

Whole-file MIDI imports do not measurably change: pitches are a tiny part.

AI-assisted (Claude)
@mscuthbert mscuthbert changed the title Faster whole-number ps and MIDI pitches Faster Pitch(80) or p.midi = 20 or p.ps = 60 creation Oct 8, 2026
@mscuthbert
mscuthbert marked this pull request as ready for review October 8, 2026 05:17
@coveralls

coveralls commented Oct 8, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 93.371% (+0.002%) from 93.369% — pitch-midi-ps into master

_convertPsToStep returns None instead of Microtone(0) for whole numbers.
The ps setter goes back to a single _convertPsToStep call rather than
repeating the table lookup; Pitch.__init__ keeps an inline copy of the
lookup, which saves a call (about 45 ns), with comments in both places to
keep them in step.  Whole numbers are found by comparing int(ps) with ps,
which is cheaper than the isinstance check plus a second int().

TODOs note that _convertPsToStep should return an alter, so that the ps
setter can change the existing accidental instead of replacing it.

AI-assisted (Claude)
Test each pitch class once plus the octave edges (0, 11, 12, 127, and a
negative ps) instead of every number from -13 to 139, and check that the
Note is told once with a mock of pitchChanged instead of planting an
entry in its private cache.

AI-assisted (Claude)
testPsAndMidiSettersInformNoteOnce checks that the ps and midi setters
tell the Note once.  testWholeNumberPsAndMidi keeps only cases the
doctests do not cover: clearing a quarter-tone accidental and a microtone,
a natural as None, a negative ps, the midi setter above 127, and
Pitch(number) keeping its natural.

AI-assisted (Claude)
@mscuthbert
mscuthbert merged commit 134cb31 into master Oct 8, 2026
7 checks passed
@mscuthbert
mscuthbert deleted the pitch-midi-ps branch October 8, 2026 09:30
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