Skip to content

Commit

Permalink
fix lint, speed up Pitch.ps = 53.0 etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Myke Cuthbert committed Sep 8, 2021
1 parent e0687be commit 4a8f2da
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion music21/chord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4228,7 +4228,7 @@ def commonName(self):
Changed in v5.5: special cases for checking enharmonics in some cases
Changed in v6.5: better handling of 0-, 1-, and 2-pitchClass and microtonal chords.
Changed in v7: Inversions of augmented triads are used.
Changed in v7: Inversions of augmented sixth-chords are specified.
'''
if any(not p.isTwelveTone() for p in self.pitches):
return 'microtonal chord'
Expand Down
1 change: 0 additions & 1 deletion music21/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import copy
import unittest
import sys
from typing import Optional
from collections import OrderedDict
from typing import Optional

Expand Down
6 changes: 5 additions & 1 deletion music21/musicxml/m21ToXml.py
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,9 @@ def setColor(self, mxObject, m21Object):
'''
Sets mxObject['color'] to a normalized version of m21Object.style.color
'''
# we repeat 'color' rather than just letting setStyleAttributes
# handle it, because otherwise it will run the expensive
# hyphenToCamelCase routine on something called on each note.
self.setStyleAttributes(mxObject, m21Object, 'color', 'color')
if 'color' in mxObject.attrib: # set
mxObject.attrib['color'] = normalizeColor(mxObject.attrib['color'])
Expand Down Expand Up @@ -969,7 +972,6 @@ def setPosition(self, mxObject, m21Object):
set positioning information for an mxObject from
default-x, default-y, relative-x, relative-y from
the .style attribute's absoluteX, relativeX, etc. attributes.
'''
musicXMLNames = ('default-x', 'default-y', 'relative-x', 'relative-y')
m21Names = ('absoluteX', 'absoluteY', 'relativeX', 'relativeY')
Expand Down Expand Up @@ -5785,6 +5787,8 @@ def beamToXml(self, beamObject):

# not to be done: repeater (deprecated)
self.setColor(mxBeam, beamObject)
# again, we pass the name 'fan' twice so we don't have to run
# hyphenToCamelCase on it.
self.setStyleAttributes(mxBeam, beamObject, 'fan', 'fan')

return mxBeam
Expand Down
3 changes: 3 additions & 0 deletions music21/musicxml/xmlToM21.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,9 @@ def setColor(self, mxObject, m21Object):
'''
Sets m21Object.style.color to be the same as color...
'''
# we repeat 'color' rather than just letting setStyleAttributes
# handle it, because otherwise it will run the expensive
# hyphenToCamelCase routine on something called on each note.
self.setStyleAttributes(mxObject, m21Object, 'color', 'color')

def setFont(self, mxObject, m21Object):
Expand Down
28 changes: 18 additions & 10 deletions music21/pitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@
'A': 9,
'B': 11,
}
NATURAL_PCS = (0, 2, 4, 5, 7, 9, 11)
STEPREF_REVERSED = {
0: 'C',
2: 'D',
4: 'E',
5: 'F',
7: 'G',
9: 'A',
11: 'B',
}
STEPNAMES = {'C', 'D', 'E', 'F', 'G', 'A', 'B'} # set
STEP_TO_DNN_OFFSET = {'C': 0, 'D': 1, 'E': 2, 'F': 3, 'G': 4, 'A': 5, 'B': 6}

Expand Down Expand Up @@ -231,12 +241,14 @@ def _convertPsToStep(ps) -> Tuple[str, 'Accidental', 'Microtone', int]:
>>> pitch._convertPsToStep(42.999739)
('G', <music21.pitch.Accidental natural>, <music21.pitch.Microtone (-0c)>, 0)
'''
name = ''

if isinstance(ps, int):
pc = ps % 12
alter = 0
micro = 0
elif ps == int(ps):
pc = int(ps) % 12
alter = 0
micro = 0
else:
# rounding here is essential
ps = round(ps, PITCH_SPACE_SIG_DIGITS)
Expand Down Expand Up @@ -274,18 +286,17 @@ def _convertPsToStep(ps) -> Tuple[str, 'Accidental', 'Microtone', int]:

octShift = 0
# check for unnecessary enharmonics
if pc in (4, 11) and alter == 1:
if alter == 1 and pc in (4, 11):
acc = Accidental(0)
pcName = (pc + 1) % 12
# if a B, we are shifting out of this octave, and need to get
# the above octave, which may not be represented in ps value
if pc == 11:
octShift = 1
# its a natural; nothing to do
elif pc in STEPREF.values():
acc = Accidental(0 + alter)
elif pc in NATURAL_PCS: # 0, 2, 4, 5, 7, 9, 11
acc = Accidental(0 + alter) # alter is usually 0 unless half-sharp.
pcName = pc

elif (pc - 1) in (0, 5, 7) and alter >= 1: # is this going to be a C##, F##, G##?
acc = Accidental(alter - 1)
pcName = pc + 1
Expand All @@ -306,10 +317,7 @@ def _convertPsToStep(ps) -> Tuple[str, 'Accidental', 'Microtone', int]:
else: # pragma: no cover
raise PitchException(f'cannot match condition for pc: {pc}')

for key, value in STEPREF.items():
if pcName == value:
name = key
break
name = STEPREF_REVERSED.get(pcName, '')

# create a micro object always
if micro != 0:
Expand Down
2 changes: 1 addition & 1 deletion music21/vexflow/toMusic21j.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
# Name: vexflow/toM21j.py
# Name: vexflow/toMusic21j.py
# Purpose: music21 classes for converting music21 objects to music21j
#
# Authors: Michael Scott Cuthbert
Expand Down

0 comments on commit 4a8f2da

Please sign in to comment.