Fix cent_diffs to store signed values

- harmonic_space.py: remove abs() to store signed cent differences
- graph.py: melodic threshold now uses abs(cents) for magnitude
- Change melodic power from 2 to 3 for sharper penalization

This fixes contrary_motion factor which was broken (always 0 due to absolute values)
This commit is contained in:
Michael Winter 2026-03-16 14:53:13 +01:00
parent 1eb95ffad7
commit 6d3cbd83e9
2 changed files with 9 additions and 8 deletions

View file

@ -277,9 +277,9 @@ class PathFinder:
"""Returns continuous score based on melodic threshold. """Returns continuous score based on melodic threshold.
- cents == 0: score = 1.0 (no movement is always ideal) - cents == 0: score = 1.0 (no movement is always ideal)
- Below min (0 < cents < min): score = (cents / min)^2 - Below min (0 < cents < min): score = (cents / min)^3
- Within range (min <= cents <= max): score = 1.0 - Within range (min <= cents <= max): score = 1.0
- Above max (cents > max): score = ((1200 - cents) / (1200 - max))^2 - Above max (cents > max): score = ((1200 - cents) / (1200 - max))^3
Returns product of all voice scores. Returns product of all voice scores.
""" """
@ -293,12 +293,13 @@ class PathFinder:
product = 1.0 product = 1.0
for cents in cent_diffs: for cents in cent_diffs:
if cents == 0: abs_cents = abs(cents)
if abs_cents == 0:
score = 1.0 score = 1.0
elif cents < melodic_min: elif abs_cents < melodic_min:
score = (cents / melodic_min) ** 2 score = (abs_cents / melodic_min) ** 3
elif cents > melodic_max: elif abs_cents > melodic_max:
score = ((1200 - cents) / (1200 - melodic_max)) ** 2 score = ((1200 - abs_cents) / (1200 - melodic_max)) ** 3
else: else:
score = 1.0 score = 1.0
product *= score product *= score

View file

@ -258,7 +258,7 @@ class HarmonicSpace:
for src_idx, dest_idx in movements.items(): for src_idx, dest_idx in movements.items():
src_pitch = c1.pitches[src_idx] src_pitch = c1.pitches[src_idx]
dst_pitch = c2_transposed.pitches[dest_idx] dst_pitch = c2_transposed.pitches[dest_idx]
cents = abs(src_pitch.to_cents() - dst_pitch.to_cents()) cents = src_pitch.to_cents() - dst_pitch.to_cents()
cent_diffs.append(cents) cent_diffs.append(cents)
source = list(c1.pitches) source = list(c1.pitches)