-
Notifications
You must be signed in to change notification settings - Fork 0
/
abjad-osc-server.py
executable file
·454 lines (400 loc) · 16.7 KB
/
abjad-osc-server.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#!/usr/bin/env python
"""
Abjad & OSC server
Requiere:
Lilypond >= 2.19??
Abjad
Lilypond custom stylesheets from Abjad '/docs/source/_stylesheets/default.ily'
Default ip: 127.0.0.1
Default port: 5005
Default output path = './output'
"""
from abjad import *
import subprocess
import argparse
from pythonosc.dispatcher import Dispatcher
from pythonosc import osc_server
import os
DEFAULT_INCLUDES = [os.path.join(os.path.dirname(os.path.realpath(__file__)), 'styles', 'default.ily')]
includes = []
notes = {} #store {'id' : [generated leaves]} //reemplazar notes[id] por LeafGenerator.container[id] ?
clef = Clef('bass')
class LeafGenerator:
container = {}
voices = {}
#voices = { 'id1000' : {'upper' : Voice() ,'lower' : Voice()}, 'id1001' : {'upper' : Voice()} }
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
if self.id not in self.container.keys():
self.container[self.id] = Measure()
#Voice(components=None, lilypond_type='Voice', is_simultaneous=None, name=None)
if self.id not in self.voices.keys():
self.voices[self.id] = { self.voice : Voice(name = self.voice) }
if self.voice not in self.voices[self.id].keys():
self.voices[self.id]= { self.voice : Voice(name = self.voice) }
def add_leaf_to_voice(self, leaf, voice):
if len(voice) > 0:
lastLeaf = voice[-1]
if type(leaf[0]) is type(Tuplet()):
if type(lastLeaf) is type(leaf[0]):
if leaf[0].multiplier == lastLeaf.multiplier:
lastLeaf.extend(leaf[0])
else:
voice.append(leaf)
else:
voice.append(leaf)
else:
voice.append(leaf)
else:
voice.append(leaf)
def make(self):
if self.rest:
fraction = Fraction(self.dur).limit_denominator(40)
duration = Duration(fraction)
pitch = None
leaves = LeafMaker()(pitch, duration)
else:
pitch = NumberedPitch.from_hertz(self.freq)
fraction = Fraction(self.dur).limit_denominator(40)
duration = Duration(fraction)
leaves = LeafMaker()(pitch, duration)
#LeafMaker(decrease_monotonic=True, forbidden_duration=None, metrical_hierarchy=None, skips_instead_of_rests=False, repeat_ties=False, use_multimeasure_rests=False)
try:
if len(self.articulation) > 0:
articulation = Articulation(
self.articulation,
direction=self.articDirection
)
for leaf in abjad.iterate(leaves).leaves():
attach(articulation, leaf)
except AttributeError:
#print("Note has no Articulation attribute")
None
try:
if len(self.dynamic) > 0:
dynamic = Dynamic(
self.dynamic,
direction=self.dynamicDirection
)
for leaf in abjad.iterate(leaves).leaves():
attach(dynamic, leaf)
except AttributeError:
#print("Note has no Dynamic attribute")
None
try:
if len(self.fermata) > 0:
fermata = Fermata(command = self.fermata)
for leaf in abjad.iterate(leaves).leaves():
attach(fermata, leaf)
except AttributeError:
None
#print("Note has no Fermata attribute")
try:
markup = Markup(
self.markup,
direction=self.markupDirection)
if len(self.markupCommand) > 0:
for command in self.markupCommand:
string = "markup."+command+"()"
markup = eval(string)
for leaf in abjad.iterate(leaves).leaves():
attach(markup, leaf)
except AttributeError:
#print("Note has no Markup attribute")
None
try:
if len(self.notehead) > 0:
if self.notehead == 'transparent':
for leaf in abjad.iterate(leaves).leaves():
override(leaf).note_head.transparent = True
else:
for leaf in abjad.iterate(leaves).leaves():
override(leaf).note_head.style = self.notehead
except AttributeError:
None
#print("Note has no Notehead attribute")
#self.container[self.id].automatically_adjust_time_signature = True #Ajusta el Measure a la métrica de compás
self.add_leaf_to_voice(leaves, self.voices[self.id][self.voice])
#self.voices[self.id][self.voice].append(leaves) #Agregar las notas al Voice
self.container[self.id].append(self.voices[self.id][self.voice]) #Agregar el Voice al Measure
attach(clef, select(self.container[self.id]).leaves()[0]) #Agrega el Clef al Measure
def display(self, id, preview):
music = select(LeafGenerator.container[self.id]).leaves()
output_path = args.output
if preview == True:
output_path = args.output + '_preview'
for i in range(len(music)):
markup = Markup(i).tiny().with_color('blue')
attach(markup, music[i], tag='PREVIEW')
id_markup = Markup('ID: ' + id).box().with_color(SchemeColor('purple'))
attach(id_markup, music[0], tag='PREVIEW')
else:
for i in range(len(music)):
wrapper = inspect(music[i]).wrappers(Markup)
print(wrapper)
try:
tag = wrapper[0].tag
if tag == Tag('PREVIEW'):
detach(Markup, music[i])
except:
None
lilypond_file = LilyPondFile.new(
music = LeafGenerator.container[self.id],
includes = includes
)
make_ly = persist(lilypond_file).as_ly()
ly_path = make_ly[0]
cmd = ['lilypond',
'-dcrop',
'-dno-point-and-click',
'-ddelete-intermediate-files',
'-dbackend=svg',
'-o' + output_path,
ly_path]
subprocess.run(cmd)
def clear(id):
if id in LeafGenerator.container:
del LeafGenerator.container[id]
if id in LeafGenerator.voices:
del LeafGenerator.voices[id]
## Handlers ##
### Leaves ###
def note_handler(unused_addr, args, eventData):
event = eval("{" + eventData + "}")
if event.get('new'):
id = event['id']
if id in notes:
del notes[id]
LeafGenerator.clear(id)
else:
notes[event['id']] = LeafGenerator(**event)
notes[event['id']].make()
def literal_handler(unused_addr, args, eventData):
event = eval("{ " + eventData + "}") #eval elimina la posibilidad de "\\" para imprimir "\"?
event['literal'] = event['literal'].replace("backlash-", "\\") #hack horrible
id = event['id']
index = event['index']
literal = LilyPondLiteral(
event['literal'],
event['position'],
tweaks=event['tweaks']
)
if index is None:
attach(literal, notes[id].container[id])
else:
attach(literal, notes[id].container[id][index])
### Indicators ###
def dynamic_handler(unused_addr, args, eventData):
#name='f', *, command=None, direction=None, format_hairpin_stop=None, hide=None, leak=None, name_is_textual=None, ordinal=None, sforzando=None, tweaks=None
event = eval("{ " + eventData + "}")
#print(event)
id = event['id']
if event['command'] == 'None':
event['command'] = None
else:
event['command'] = event['command'].replace("backlash-", "\\") #hack horrible
dynamic = Dynamic(
event['dynamic'],
command = event['command'],
direction = event['direction'],
name_is_textual=event['name_is_textual'],
tweaks=event['tweaks']
)
detach( DynamicTrend,
select(notes[id].container[id]).leaves()[event['index']]
)
detach( Dynamic,
select(notes[id].container[id]).leaves()[event['index']]
)
attach( dynamic,
select(notes[id].container[id]).leaves()[event['index']]
)
def dynamicTrend_handler(unused_addr, args, eventData):
# shape='<', *, left_broken=None, tweaks=None
event = eval("{ " + eventData + "}")
id = event['id']
dynamicTrend = DynamicTrend(
shape=event['shape'],
left_broken=event['left_broken'],
tweaks=event['tweaks']
)
#DynamicTrend convive con Dynamic en el mismo leaf, pero puede eliminarlo on 'left_broken'
detach( DynamicTrend,
select(notes[id].container[id]).leaves()[event['index']]
)
attach( dynamicTrend,
select(notes[id].container[id]).leaves()[event['index']]
)
def markup_handler(unused_addr, args, eventData):
event = eval("{ " + eventData + "}")
event['markup'] = event['markup'].replace("backlash-", "\\") #hack horrible
id = event['id']
markup = Markup(
event['markup'],
direction=event['direction'],
tweaks=event['tweaks']
)
#To-Do: handler para MarkupCommand que acepta argumentos
try:
if len(event['markupCommand']) > 0:
for command in event['markupCommand']:
string = "markup."+command+"()"
markup = eval(string)
except AttributeError:
#print("Event has no markup format attribute")
None
attach( markup,
select(notes[id].container[id]).leaves()[event['index']]
)
def articulation_handler(unused_addr, args, eventData):
event = eval("{ " + eventData + "}")
event['articulation'] = event['articulation'].replace("backlash-", "\\") #hack horrible
id = event['id']
articulation = Articulation(
event['articulation'],
direction=event['direction'],
tweaks=event['tweaks']
)
attach( articulation,
select(notes[id].container[id]).leaves()[event['index']]
)
def bar_line_handler(unused_addr, args, eventData):
event = eval("{ " + eventData + "}")
event['bar_line'] = event['bar_line'].replace("backlash-", "\\") #hack horrible
id = event['id']
bar_line = BarLine(event['bar_line'])
detach( BarLine, select(notes[id].container[id]).leaves()[event['index']])
attach( bar_line, select(notes[id].container[id]).leaves()[event['index']])
def notehead_handler(unused_addr, args, eventData):
event = eval("{ " + eventData + "}")
id = event['id']
leaf = select(notes[id].container[id]).leaves()[event['index']]
if event['notehead'] == 'transparent':
override(leaf).note_head.transparent = True
else:
override(leaf).note_head.style = event['notehead']
### Spanners ###
def slur_handler(unused_addr, args, eventData):
event = eval("{ " + eventData + "}")
id = event['id']
slur = Slur(direction = event['direction'])
selection = select(notes[id].container[id]).leaves() #selecciona leaves de Measure. Como hago para seleccionar leaves del Voice dentro de ese Measure?
default_slice = [0, len(selection), 1]
slice_params = event['slice']
for i in range(len(default_slice)):
try:
slice_params[i]
except IndexError:
slice_params.append(default_slice[i])
slice_obj = slice(slice_params[0],slice_params[1],slice_params[2]) #reescribir mas pythonico
detach(Slur, selection[slice_obj])
attach(slur, selection[slice_obj])
def tie_handler(unused_addr, args, eventData):
event = eval("{ " + eventData + "}")
id = event['id']
tie = Tie(direction = event['direction'], left_broken = event['left_broken'], repeat = event['repeat'], right_broken = event['right_broken'])
selection = select(notes[id].container[id]).leaves() #selecciona leaves de Measure. Como hago para seleccionar leaves del Voice dentro de ese Measure?
default_slice = [0, len(selection), 1]
slice_params = event['slice']
for i in range(len(default_slice)):
try:
slice_params[i]
except IndexError:
slice_params.append(default_slice[i])
slice_obj = slice(slice_params[0],slice_params[1],slice_params[2]) #reescribir mas pythonico
detach(Tie, selection[slice_obj])
attach(tie, selection[slice_obj])
def text_spanner_handler(unused_addr, args, eventData):
event = eval("{ " + eventData + "}")
id = event['id']
markup_left = Markup(
event['left_text'],
#direction=event['direction']
#tweaks=event['tweaks']
)
markup_right = Markup(
event['right_text'],
#direction=event['direction']
#tweaks=event['tweaks']
)
"""
try:
if len(event['markupCommand']) > 0:
for command in event['markupCommand']:
string_left = "markup_left."+command+"()"
string_right = "markup_right."+command+"()"
markup_left = eval(string_left)
markup_right = eval(string_right)
except AttributeError:
pass
"""
start_text_span = StartTextSpan(left_text = markup_left, right_text = markup_right, style = event['style'])
selection = select(notes[id].container[id]).leaves()
default_slice = [0, len(selection), 1]
slice_params = event['slice']
for i in range(len(default_slice)):
try:
slice_params[i]
except IndexError:
slice_params.append(default_slice[i])
slice_obj = slice(slice_params[0],slice_params[1],slice_params[2]) #reescribir mas pythonico
#detach(text_spanner(), selection[slice_obj])
#text_spanner(selection[slice_obj], start_text_span=start_text_span)
override(selection[slice_params[0]]).text_spanner.staff_padding = event['staff_padding'] # -dcrop svg de lilypond no tiene en cuenta este override!
text_spanner(selection[slice_obj], start_text_span=start_text_span, stop_text_span = None)
### Removing items ###
def detach_handler(unused_addr, args, eventData):
event = eval("{ " + eventData + "}")
id = event['id']
detach( event['attachment'],
select(notes[id].container[id]).leaves()[event['index']]
)
def remove_handler(unused_addr, args, eventData):
event = eval("{ " + eventData + "}")
id = event['id']
voice = notes[id].container[id][0] #Para arreglar: VOICE HARDCODEADA EN MEASURE[0]
voice.remove(voice[event['index']])
#notes[id].container[id].pop([event['index']])
### Display ###
def display_handler(unused_addr, args, id, preview):
notes[id].display(id, eval(preview))
## OSC Server ##
def main(args):
includes = DEFAULT_INCLUDES + args.include
dispatcher = Dispatcher()
dispatcher.map("/remove", remove_handler, "Remove")
dispatcher.map("/detach", detach_handler, "Detach")
dispatcher.map("/slur_oneshot", slur_handler, "Slur")
dispatcher.map("/tie_oneshot", tie_handler, "Tie")
dispatcher.map("/text_spanner_oneshot", text_spanner_handler, "Text Spanner")
dispatcher.map("/notehead_oneshot", notehead_handler, "Notehead")
dispatcher.map("/bar_line_oneshot", bar_line_handler, "BarLine")
dispatcher.map("/articulation_oneshot", articulation_handler, "Articulation")
dispatcher.map("/dynamic_oneshot", dynamic_handler, "Dynamic")
dispatcher.map("/dynamicTrend_oneshot", dynamicTrend_handler, "DynamicTrend")
dispatcher.map("/literal_oneshot", literal_handler, "Literal")
dispatcher.map("/markup_oneshot", markup_handler, "Markup")
dispatcher.map("/note_event", note_handler, "Note")
dispatcher.map("/display", display_handler, "Display")
server = osc_server.ThreadingOSCUDPServer((args.host, args.port), dispatcher)
print("Serving on {}".format(server.server_address))
server.serve_forever()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-H", "--host",
default="localhost",
help="The ip to listen on")
parser.add_argument("-P", "--port",
type=int,
default=5005,
help="The port to listen on")
parser.add_argument("-O", "--output",
default='./output',
help="Location of compiled .ly")
parser.add_argument("-I", "--include",
default=[],
nargs="+",
help="Include Lilypond file")
args = parser.parse_args()
main(args)