New features for resolving note
Added octave & addition & note name parsing
This commit is contained in:
@ -1,5 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
SCALES = {
|
SCALES = {
|
||||||
"Minoric": 444,
|
"Minoric": 444,
|
||||||
"Thaptic": 4341,
|
"Thaptic": 4341,
|
||||||
@ -1493,6 +1495,33 @@ SCALES = {
|
|||||||
"Chromatic": 111111111111,
|
"Chromatic": 111111111111,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
note_to_interval = {
|
||||||
|
"C": 0,
|
||||||
|
"D": 2,
|
||||||
|
"E": 4,
|
||||||
|
"F": 5,
|
||||||
|
"G": 7,
|
||||||
|
"A": 9,
|
||||||
|
"B": 11
|
||||||
|
}
|
||||||
|
|
||||||
|
modifiers = {
|
||||||
|
"#": 1,
|
||||||
|
"b": -1,
|
||||||
|
"s": 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
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:
|
def get_scale(name: str) -> list:
|
||||||
"""Get a scale from the global scale list"""
|
"""Get a scale from the global scale list"""
|
||||||
@ -1503,17 +1532,21 @@ def get_scale(name: str) -> list:
|
|||||||
|
|
||||||
|
|
||||||
def note_from_pc(
|
def note_from_pc(
|
||||||
root: int,
|
root: int|str,
|
||||||
pitch_class: int,
|
pitch_class: int,
|
||||||
intervals: list[int|float],
|
intervals: list[int|float],
|
||||||
cents: bool = False
|
cents: bool = False,
|
||||||
|
octave: int = 0,
|
||||||
|
addition: int = 0
|
||||||
) -> int:
|
) -> int:
|
||||||
"""Resolve a pitch class into a note from a scale"""
|
"""Resolve a pitch class into a note from a scale"""
|
||||||
|
if type(root)==str:
|
||||||
|
root = note_to_midi(root)
|
||||||
if cents:
|
if cents:
|
||||||
intervals = list(map(lambda x: x / 100), intervals)
|
intervals = list(map(lambda x: x / 100), intervals)
|
||||||
intervals = sum(intervals[0:pitch_class])
|
interval_sum = sum(intervals[0:pitch_class])
|
||||||
return (root + intervals if pitch_class >= 0 else
|
note = (root + interval_sum if pitch_class >= 0 else root - interval_sum)
|
||||||
root - intervals)
|
return note + octave*sum(intervals) + addition
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Reference in New Issue
Block a user