-
Notifications
You must be signed in to change notification settings - Fork 0
/
music_notes.py
71 lines (45 loc) · 1.56 KB
/
music_notes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import collections
from fractions import Fraction
import json
from constants import NOTES
class Note:
def __init__(self, pitch, octave, duration, other = None):
self.pitch = pitch
self.octave = octave
self.duration = Fraction(duration)
self.abs_value = NOTES[self.pitch] + (self.octave * 12)
self.other_attributes = other
def __repr__(self):
return "Note: " + self.pitch + str(self.octave)
class MusicSequence(list):
'''
A list of Notes with some helper functions
'''
def __init__(self, file):
with open(file) as fp:
piece = json.load(fp)
for note in piece["notes"]:
self.append(Note(note['pitch'], note['octave'], note['duration'], note['other']))
def to_string(self):
str = ""
for note in self:
str += note.pitch
return str
def relative(self):
lst = []
previous = self[0]
lst.append(0)
for note in self[1:]:
lst.append(note.abs_value - previous.abs_value)
previous = note
return lst
def to_relative_string(self):
str = ""
for val in self.relative():
str += chr(val + 75) # 75, or 'K', is the 0 point
return str
def to_abs_value_string(self):
string = ""
for note in self:
string += chr(note.abs_value+50)
return string