-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.py
165 lines (141 loc) · 4.3 KB
/
project.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import json
from typing import Any, Dict, List, Optional, Tuple, cast
from mopyx import action, computed
from model import model
@model
class Project:
def __init__(self) -> None:
# 40 - 240
self.tempo = 125
# 0 - 24
self.latency = 0
# 1 - 8
self.quantum = 8
# 0 - 24
self.swing = 0
self.tracks: List[Track] = []
for i in range(5):
self.tracks.append(Track(False, i))
for i in range(3):
self.tracks.append(Track(True, i))
@computed
def dict(self) -> Dict[str, Any]:
return {
'tempo': self.tempo,
'latency': self.latency,
'quantum': self.quantum,
'swing': self.swing,
'tracks': [t.dict for t in self.tracks]
}
@action
def from_dict(self, d: Dict[str, Any]) -> None:
self.tempo = d['tempo']
self.latency = d['latency']
self.quantum = d['quantum']
self.swing = d['swing']
for i in range(len(self.tracks)):
self.tracks[i].from_dict(d['tracks'][i])
def dump(self, name: str) -> None:
with open(name, 'w') as f:
json.dump(self.dict, f)
def load(self, name: str) -> None:
try:
with open(name, 'r') as f:
d = json.load(f)
self.from_dict(d)
except IOError:
pass
@model
class Track:
def __init__(self, percussion: bool, instrument: int):
self.muted = False
self.volume = 1.0
self.percussion = percussion
self.instrument = instrument
self.sequence: List[int] = []
self.patterns: List[Pattern] = []
for i in range(8):
self.patterns.append(Pattern())
@computed
def dict(self) -> Dict[str, Any]:
return {
'muted': self.muted,
'volume': self.volume,
'percussion': self.percussion,
'instrument': self.instrument,
'sequence': self.sequence,
'patterns': [p.dict for p in self.patterns]
}
@action
def from_dict(self, d: Dict[str, Any]) -> None:
self.muted = d['muted']
self.volume = d['volume']
self.percussion = d['percussion']
self.instrument = d['instrument']
self.sequence = d['sequence']
for i in range(len(self.patterns)):
self.patterns[i].from_dict(d['patterns'][i])
@model
class Pattern:
def __init__(self) -> None:
self.notes: List[Note] = []
for i in range(32):
self.notes.append(Note())
# 0 - 8
self.octave = 4
@computed
def empty(self) -> bool:
for n in self.notes:
if not n.empty:
return False
return True
@computed
def dict(self) -> Dict[str, Any]:
return {
'octave': self.octave,
'notes': [n.dict for n in self.notes]
}
@action
def from_dict(self, d: Dict[str, Any]) -> None:
self.octave = d['octave']
for i in range(len(self.notes)):
self.notes[i].from_dict(d['notes'][i])
@model
class Note:
def __init__(self) -> None:
# -1: note off
# 0: silence
# 1 - 120: C0 - B9
self.tone = 0
self.chord: Tuple[Optional[int], Optional[int],
Optional[int]] = (None, None, None)
# None | 0.0 - 1.0
self.control: List[Optional[float]] = [None] * 5
# 0: once
# 1: twice (1/32)
# 2: 3 times (1/24, 2/24)
self.trigger = 0
@computed
def empty(self) -> bool:
if self.tone or self.chord != (None, None, None) or self.trigger:
return False
for c in self.control:
if c is not None:
return False
return True
@computed
def dict(self) -> Dict[str, Any]:
return {
'tone': self.tone,
'chord': self.chord,
'control': self.control,
'trigger': self.trigger
}
@action
def from_dict(self, d: Dict[str, Any]) -> None:
self.tone = d['tone']
self.chord = cast(Tuple[Optional[int], Optional[int],
Optional[int]], tuple(d['chord']))
self.control = d['control']
self.trigger = d['trigger']
project = Project()