Skip to content

Commit

Permalink
Scale: Improved comments/validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ideoforms committed Sep 19, 2024
1 parent 4c41b27 commit 70b8cde
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion isobar/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ class Scale(object):
dict = {}

def __init__(self, semitones=[0, 2, 4, 5, 7, 9, 11], name="unnamed scale", octave_size=12):
for semitone in semitones:
if not isinstance(semitone, int):
raise ValueError("semitones must be a list of integers")
self.semitones = semitones
""" For polymorphism with WeightedScale -- assume all notes equally weighted. """
""" List of semitone making up the scale """

self.weights = [1.0 / len(self.semitones) for _ in range(len(self.semitones))]
""" For polymorphism with WeightedScale -- assume all notes equally weighted. """

self.name = name
self.octave_size = octave_size
if name not in Scale.dict:
Expand Down Expand Up @@ -94,6 +100,10 @@ def byname(name):
def random():
key = random.choice(list(Scale.dict.keys()))
return Scale.dict[key]

def random_note(self):
note = random.choices(self.semitones, self.weights)
return note[0]

Scale.chromatic = Scale([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], "chromatic")
Scale.major = Scale([0, 2, 4, 5, 7, 9, 11], "major")
Expand Down

0 comments on commit 70b8cde

Please sign in to comment.