From bfef3f5b2c8291951ec0a0caa35b26181aa6d3ad Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 25 Jul 2024 19:27:01 -0400 Subject: [PATCH] refactor: add harmony calculation to Chord class --- pytheory/chords.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pytheory/chords.py b/pytheory/chords.py index c2b5257..a825ca2 100644 --- a/pytheory/chords.py +++ b/pytheory/chords.py @@ -6,9 +6,19 @@ class Chord: l = tuple([tone.full_name for tone in self.tones]) return f"" - # @property - # def harmony(self): - # pass + @property + def harmony(self): + if len(self.tones) < 2: + return 0 # No harmony for a single tone or empty chord + + # Calculate intervals between adjacent tones + intervals = [abs(self.tones[i].pitch() - self.tones[i-1].pitch()) for i in range(1, len(self.tones))] + + # Simple harmony calculation: sum of inverse intervals + # Smaller intervals contribute more to harmony + harmony_value = sum(1 / interval for interval in intervals if interval != 0) + + return harmony_value class NamedChord: def __init__(self, *, name, system):