Added tests for scale

Added option to give intervals as scale name
This commit is contained in:
2023-02-05 16:54:00 +02:00
parent 89183471fd
commit 09553c539b
3 changed files with 31 additions and 6 deletions

View File

@ -1515,14 +1515,13 @@ def note_to_midi(name: str) -> int:
''' Parse note name to midi '''
items = re.match(r"^([a-gA-G])([#bs])?([1-9])?$",name)
if items==None:
raise ValueError("Invalid note name: "+name)
return 60
values = items.groups()
octave = int(values[2]) if values[2] else 4
modifier = modifiers[values[1]] if values[1] else 0
interval = note_to_interval[values[0].capitalize()]
return 12 + octave * 12 + interval + modifier
def get_scale(name: str) -> list:
"""Get a scale from the global scale list"""
scale = SCALES.get(
@ -1534,19 +1533,21 @@ def get_scale(name: str) -> list:
def note_from_pc(
root: int|str,
pitch_class: int,
intervals: list[int|float],
intervals: str | list[int|float],
cents: bool = False,
octave: int = 0,
addition: int = 0
modifier: int = 0
) -> int:
"""Resolve a pitch class into a note from a scale"""
if type(root)==str:
root = note_to_midi(root)
if cents:
intervals = list(map(lambda x: x / 100), intervals)
if type(intervals) == str:
intervals = get_scale(intervals)
interval_sum = sum(intervals[0:pitch_class])
note = (root + interval_sum if pitch_class >= 0 else root - interval_sum)
return note + octave*sum(intervals) + addition
return note + octave*sum(intervals) + modifier
if __name__ == "__main__":