-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
208 lines (172 loc) · 6.31 KB
/
main.py
File metadata and controls
208 lines (172 loc) · 6.31 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import midi
from random import random, choice, seed
from time import sleep
from sound import Sound
from random import choice
from audio import Audio
from neoscore.core import neoscore
from neoscore.core.rich_text import RichText
from neoscore.core.text import Text
from neoscore.core.units import ZERO, Mm
from neoscore.western.staff import Staff
from neoscore.western.chordrest import Chordrest
from neoscore.western.clef import Clef
from neoscore.western.duration import Duration
from neoscore.western.barline import Barline
from neoscore.western.pedal_line import PedalLine
class Main:
def __init__(self, source_midi):
# start audio listener
self.listener = Audio()
self.listener.start()
# get all midi note lists for s a t b
self.midilist = midi.get_midi_lists(source_midi)
# start neoscore
neoscore.setup()
# build digital score UI
self.make_UI()
self.beat = 0
# build first bar
self.audiodata = self.listener.read()
self.beat_size = 20
self.build_bar(1)
self.build_bar(2)
# start sound design
self.sound_design = Sound()
self.sound_design.start()
def build_bar(self, bar):
if bar == 1:
start_pos = Mm(10)
self.notes_on_staff_list_1 = []
else:
start_pos = Mm(100)
self.notes_on_staff_list_2 = []
note_duration_sum = 0
breakflag = False
while note_duration_sum < 80:
# position in bar
pos_x = Mm(note_duration_sum) + start_pos
# get a random note from original source list,
event_type, pitches, raw_duration = self.get_note()[0:3]
# double length of original duration
raw_duration *= 2
# calculate duration
if isinstance(raw_duration, float):
raw_duration, neoduration = self.calc_duration(raw_duration)
else:
raw_duration, neoduration = self.calc_duration(1)
length = raw_duration * self.beat_size
# print note on neoscore unless over bar limit
if note_duration_sum + length > 80:
# what is remaining?
raw_rest_gap = (80 - note_duration_sum) / 20
raw_duration, neoduration = self.calc_duration(raw_rest_gap)
pitches = []
breakflag = True
# add the note/rest to the score and to the note list
# if len(event_type) > 4:
# t = Text((pos_x, Mm(0)), self.staff, event_type)
n = Chordrest(pos_x, self.staff, pitches, neoduration)
if bar == 1:
self.notes_on_staff_list_1.append([n])
else:
self.notes_on_staff_list_2.append([n])
# if end of bar length: break
if breakflag:
break
else:
note_duration_sum += length
def calc_duration(self, raw_duration):
if raw_duration < 0.25:
neo_duration = (1, 16)
elif raw_duration == 0.25:
neo_duration = (1, 16)
elif raw_duration == 0.5:
neo_duration = (1, 8)
elif raw_duration == 0.75:
neo_duration = (3, 8)
elif raw_duration == 1:
neo_duration = (1, 4)
elif raw_duration == 1.5:
neo_duration = (3, 4)
elif raw_duration == 2:
neo_duration = (1, 2)
else:
neo_duration = (1, 4)
raw_duration = 1
return raw_duration, Duration(neo_duration[0], neo_duration[1])
def get_note(self) -> list:
# chance of live note or from midilist
if random() > 0.36:
new_note = choice(self.midilist)
print("MIDI", new_note)
else:
new_note = self.listener.read()
print("EAR", new_note)
return new_note
def make_UI(self):
annotation = """
DEMO digital score BLAH BLAH
"""
# add text at top
RichText((Mm(1), Mm(1)), None, annotation, width=Mm(170))
# make 4 2 bar staves
self.staff = Staff((ZERO, Mm(70)), None, Mm(180))
# add barlines
Barline(Mm(90), [self.staff])
Barline(Mm(180), [self.staff])
# add clefs
Clef(ZERO, self.staff, "treble")
# mark conductor points
bar1_origin = Mm(10)
self.conductor_1_1 = Text((bar1_origin, Mm(50)), None, "1")
self.conductor_1_2 = Text((bar1_origin + Mm(40), Mm(50)), None, "2")
self.conductor_2_1 = Text((bar1_origin + Mm(90), Mm(50)), None, "3")
self.conductor_2_2 = Text((bar1_origin + Mm(130), Mm(50)), None, "4")
self.conductor_list = [self.conductor_1_1,
self.conductor_1_2,
self.conductor_2_1,
self.conductor_2_2
]
self.bar_indicator = PedalLine(
(Mm(0), Mm(20)),
self.staff,
Mm(90),
half_lift_positions=[Mm(45)]
)
def change_beat(self, beat):
if beat > 4:
beat -= 4
# flatten all scales
for b in self.conductor_list:
b.scale = 1
# boost the beat
self.conductor_list[beat-1].scale = 3
def refresh_func(self, time):
block = True
# calc which beat and change score
now_beat = (int(time) % 8) + 1 # 8 beats = 2 bars
if now_beat != self.beat:
self.change_beat(now_beat)
self.beat = now_beat
block = False
if now_beat == 1:
if not block:
self.bar_indicator.pos = (Mm(0), Mm(20))
for l in self.notes_on_staff_list_2:
print(l)
for e in l:
e.remove()
self.build_bar(2)
elif now_beat == 5:
if not block:
self.bar_indicator.pos = (Mm(90), Mm(20))
for l in self.notes_on_staff_list_1:
print(l)
for e in l:
e.remove()
self.build_bar(1)
if __name__ == "__main__":
run = Main("data/midi/A_Sleepin_Bee.mid")
neoscore.show(run.refresh_func,
display_page_geometry=False)