Add dots to notes duration

This commit is contained in:
Bartłomiej Pluta
2019-07-01 09:19:11 +02:00
parent e47c783d27
commit 611317c890
5 changed files with 26 additions and 13 deletions

28
Note.py
View File

@@ -55,16 +55,14 @@ class NotePitch(Enum):
raise SyntaxException(None, f"Note '{string}' does not exist")
class Note:
def __init__(self, note, octave = 4, duration = 4):
def __init__(self, note, octave = 4, duration = 4, dot = False):
if type(note) == str:
self.note = NotePitch.toPitch(note)
else:
self.note = note
self.octave = octave
self.duration = duration
def hash(self):
return f"{self.note.value}{self.octave}{self.duration}"
self.duration = duration
self.dot = dot
def toFrequency(self):
return self.note.toFrequency() * 2 ** self.octave
@@ -72,28 +70,34 @@ class Note:
def transpose(self, interval):
origIntRepr = self._intRepr()
transposedIntRepr = origIntRepr + interval
return Note._fromIntRepr(transposedIntRepr, self.duration)
return Note._fromIntRepr(transposedIntRepr, self.duration, self.dot)
def withDuration(self, duration):
return Note(self.note, self.octave, duration)
return Note(self.note, self.octave, duration, self.dot)
def withOctave(self, octave):
return Note(self.note, octave, self.duration)
return Note(self.note, octave, self.duration, self.dot)
def withDot(self):
return Note(self.note, self.octave, self.duration, True)
def withoutDot(self):
return Note(self.note, self.octave, self.duration, False)
def _intRepr(self):
return self.octave * len(NotePitch) + self.note.value
def __str__(self):
return f"{self.note}({self.octave}')[{self.duration}]"
return f"{self.note}({self.octave}')[{self.duration}{'.' if self.dot else ''}]"
def __repr__(self):
return self.__str__()
@staticmethod
def _fromIntRepr(intRepr, duration=4):
def _fromIntRepr(intRepr, duration = 4, dot = False):
note = NotePitch(intRepr % len(NotePitch))
octave = int(intRepr / len(NotePitch))
return Note(note, octave, duration)
return Note(note, octave, duration, dot)
@staticmethod
@@ -102,7 +106,7 @@ class Note:
@staticmethod
def range(a, b):
return [Note._fromIntRepr(x, 4) for x in range(a._intRepr(), b._intRepr()+1)]
return [Note._fromIntRepr(x) for x in range(a._intRepr(), b._intRepr()+1)]
def intervalToString(interval):
octaveInterval = int(abs(interval) / len(NotePitch))