Skip to content

Commit

Permalink
stricter type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
mscuthbert committed Jan 3, 2024
1 parent 8d4af39 commit f33c018
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
7 changes: 2 additions & 5 deletions music21/chord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,10 @@ def volume(self, expr: None|'music21.volume.Volume'|int|float):
note.NotRest._setVolume(self, expr, setClient=False)
elif common.isNum(expr):
vol = self._getVolume()
if t.TYPE_CHECKING:
assert isinstance(expr, (int, float))

if expr < 1: # assume a scalar
vol.velocityScalar = expr
vol.velocityScalar = float(expr)
else: # assume velocity
vol.velocity = expr
vol.velocity = int(expr)
else:
raise ChordException(f'unhandled setting expr: {expr}')

Expand Down
4 changes: 2 additions & 2 deletions music21/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -1269,9 +1269,9 @@ def _setVolume(self, value: None|volume.Volume|int|float, setClient=True):
# call local getVolume will set client appropriately
vol = self._getVolume()
if value < 1: # assume a scalar
vol.velocityScalar = value
vol.velocityScalar = float(value)
else: # assume velocity
vol.velocity = value
vol.velocity = int(value)

else:
raise TypeError(f'this must be a Volume object, not {value}')
Expand Down
25 changes: 15 additions & 10 deletions music21/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ def __init__(
# store a reference to the client, as we use this to do context
# will use property; if None will leave as None
self.client = client
self._velocityScalar = None
self._velocityScalar: float|None = None
if velocity is not None:
self.velocity = velocity
elif velocityScalar is not None:
self.velocityScalar = velocityScalar
self.velocityScalar = float(velocityScalar)
self._cachedRealized = None
self.velocityIsRelative = velocityIsRelative

Expand Down Expand Up @@ -331,7 +331,7 @@ def realized(self):
return self.getRealized()

@property
def velocity(self) -> int:
def velocity(self) -> int|None:
'''
Get or set the velocity value, a numerical value between 0 and 127 and
available setting amplitude on each Note or Pitch in chord.
Expand All @@ -355,18 +355,20 @@ def velocity(self) -> int:
return round(v)

@velocity.setter
def velocity(self, value: int | float):
if not common.isNum(value):
def velocity(self, value: int|float|None):
if value is None:
self._velocityScalar = None
elif not common.isNum(value):
raise VolumeException(f'value provided for velocity must be a number, not {value}')
if value < 0:
elif value <= 0:
self._velocityScalar = 0.0
elif value > 127:
elif value >= 127:
self._velocityScalar = 1.0
else:
self._velocityScalar = value / 127.0

@property
def velocityScalar(self):
def velocityScalar(self) -> float|None:
'''
Get or set the velocityScalar value, a numerical value between 0
and 1 and available setting amplitude on each Note or Pitch in
Expand Down Expand Up @@ -401,7 +403,10 @@ def velocityScalar(self):
return v

@velocityScalar.setter
def velocityScalar(self, value):
def velocityScalar(self, value: int|float|None):
if value is None:
self._velocityScalar = None

if not common.isNum(value):
raise VolumeException('value provided for velocityScalar must be a number, '
+ f'not {value}')
Expand All @@ -411,7 +416,7 @@ def velocityScalar(self, value):
scalar = 1
else:
scalar = value
self._velocityScalar = scalar
self._velocityScalar = float(scalar)


# ------------------------------------------------------------------------------
Expand Down

0 comments on commit f33c018

Please sign in to comment.