From 65da66ba26b6a797c7f9c929416c4044917306e8 Mon Sep 17 00:00:00 2001 From: Miika Alonen Date: Sun, 5 Feb 2023 18:25:01 +0200 Subject: [PATCH 1/2] Fixed note resolver Fixed pitch class method and a typo --- ziffers/common.py | 2 +- ziffers/scale.py | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/ziffers/common.py b/ziffers/common.py index d3deead..729306b 100644 --- a/ziffers/common.py +++ b/ziffers/common.py @@ -10,7 +10,7 @@ def sum_dict(arr: list[dict]) -> dict: """Sums a list of dicts: [{a:3,b:3},{b:1}] -> {a:3,b:4}""" result = arr[0] for element in arr[1:]: - for key in hash.keys(): + for key in element.keys(): if key in result: result[key] = result[key] + element[key] else: diff --git a/ziffers/scale.py b/ziffers/scale.py index 206a9a0..9127f79 100644 --- a/ziffers/scale.py +++ b/ziffers/scale.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import re +from math import floor SCALES = { "Minoric": 444, @@ -1536,9 +1537,17 @@ def note_from_pc( root = note_to_midi(root) if isinstance(root, str) else root intervals = get_scale(intervals) if isinstance(intervals, str) else intervals intervals = list(map(lambda x: x / 100), intervals) if cents else intervals + scale_length = len(intervals) + + # Resolve pitch classes to the scale and calculate octave + if pitch_class>=scale_length or pitch_class<0: + octave += floor(pitch_class/scale_length) + pitch_class = scale_length-(abs(pitch_class)%scale_length) if pitch_class<0 else pitch_class%scale_length + if pitch_class == scale_length: + pitch_class = 0 # Computing the result - interval_sum = sum(intervals[0 : pitch_class % len(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) + modifier + return note + (octave * sum(intervals)) + modifier From d06999a11c17c42918fd797b9ab82503effa921a Mon Sep 17 00:00:00 2001 From: Miika Alonen Date: Sun, 5 Feb 2023 18:54:51 +0200 Subject: [PATCH 2/2] Simplifying --- ziffers/scale.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ziffers/scale.py b/ziffers/scale.py index 9127f79..418ae0b 100644 --- a/ziffers/scale.py +++ b/ziffers/scale.py @@ -1547,7 +1547,6 @@ def note_from_pc( pitch_class = 0 # Computing the result - interval_sum = sum(intervals[0 : pitch_class]) - - note = root + interval_sum if pitch_class >= 0 else root - interval_sum + note = root + sum(intervals[0 : pitch_class]) + return note + (octave * sum(intervals)) + modifier