refactor: add harmony calculation to Chord class

This commit is contained in:
2024-07-25 19:27:01 -04:00
parent 99d5a57f77
commit bfef3f5b2c
+13 -3
View File
@@ -6,9 +6,19 @@ class Chord:
l = tuple([tone.full_name for tone in self.tones])
return f"<Chord tones={l!r}>"
# @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):