From 6d3cbd83e9c5e49915e84003a9fc0597eafbe14b Mon Sep 17 00:00:00 2001 From: Michael Winter Date: Mon, 16 Mar 2026 14:53:13 +0100 Subject: [PATCH] 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) --- src/graph.py | 15 ++++++++------- src/harmonic_space.py | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/graph.py b/src/graph.py index 124be70..be0cc1e 100644 --- a/src/graph.py +++ b/src/graph.py @@ -277,9 +277,9 @@ class PathFinder: """Returns continuous score based on melodic threshold. - 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 - - 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. """ @@ -293,12 +293,13 @@ class PathFinder: product = 1.0 for cents in cent_diffs: - if cents == 0: + abs_cents = abs(cents) + if abs_cents == 0: score = 1.0 - elif cents < melodic_min: - score = (cents / melodic_min) ** 2 - elif cents > melodic_max: - score = ((1200 - cents) / (1200 - melodic_max)) ** 2 + elif abs_cents < melodic_min: + score = (abs_cents / melodic_min) ** 3 + elif abs_cents > melodic_max: + score = ((1200 - abs_cents) / (1200 - melodic_max)) ** 3 else: score = 1.0 product *= score diff --git a/src/harmonic_space.py b/src/harmonic_space.py index 6563a87..4ac4837 100644 --- a/src/harmonic_space.py +++ b/src/harmonic_space.py @@ -258,7 +258,7 @@ class HarmonicSpace: for src_idx, dest_idx in movements.items(): src_pitch = c1.pitches[src_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) source = list(c1.pitches)