def randomize_time(tree): measure = tt.first_or_none(tree, "measure") n = tt.first_or_none(tree, "staff").getAttribute("n") scoreDef = tt.first_or_none(tree, "scoreDef") if not scoreDef: scoreDef = tt.create_element_node("scoreDef") tree.insertBefore(scoreDef, measure) staffGrp = tt.first_or_none(scoreDef, "staffGrp") if not staffGrp: staffGrp = tt.create_element_node("staffGrp") scoreDef.appendChild(staffGrp) staffDef = tt.first_or_none(staffGrp, "staffDef", lambda e: e.getAttribute("n") == n) if not staffDef: staffDef = tt.create_element_node("staffDef", {"n": n}) staffGrp.appendChild(staffDef) choice = rnd.choice([{ "meter.sym": "true" }, { "meter.count": f"{1 + rnd.randint(4)}", "meter.unit": f"{1 + rnd.randint(4)}" }, {}]) set_attributes(staffDef, choice) return tree.toxml()
def randomize_key(tree): measure = tt.first_or_none(tree, "measure") n = tt.first_or_none(tree, "staff").getAttribute("n") scoreDef = tt.first_or_none(tree, "scoreDef") if not scoreDef: scoreDef = tt.create_element_node("scoreDef") tree.insertBefore(scoreDef, measure) staffGrp = tt.first_or_none(scoreDef, "staffGrp") if not staffGrp: staffGrp = tt.create_element_node("staffGrp") scoreDef.appendChild(staffGrp) staffDef = tt.first_or_none(staffGrp, "staffDef", lambda e: e.getAttribute("n") == n) if not staffDef: staffDef = tt.create_element_node("staffDef", {"n": n}) staffGrp.appendChild(staffDef) choice = rnd.choice([{ "key.sig.show": "true", "key.sig": f"{rnd.randint(5)}f" }, { "key.sig.show": "true", "key.sig": f"{rnd.randint(5)}s" }, { "key.sig.show": "true", "key.sig": f"{rnd.randint(5)}0" }, {}]) set_attributes(staffDef, choice) return tree.toxml()
def randomize_clef(tree): clef = tt.create_element_node("clef") choice = rnd.choice([{ "shape": "G", "line": "2" }, { "shape": "F", "line": "4" }, { "shape": "C", "line": "3" }, {}]) set_attributes(clef, choice) tt.first_or_none(tree, "layer").appendChild(clef) return tree.toxml()
def randomize_note(tree): note_count = rnd.randint(8) layer = tt.first_or_none(tree, "layer") for i in range(note_count): element = tt.create_element_node( rnd.choice(["note", "rest"]), {"dur": rnd.choice(['1/32', '1/16', '1/8', '1/4', '1/2', '1'])}) layer.appendChild(element) return tree.toxml()
def callback(ch, method, properties, body): data = json.loads(body) sheet_name = data['name'] post_processing_steps = data["steps"] task_id = data['task_id'] # Get MEI file mei_path = fsm.get_sheet_whole_directory(sheet_name) / "aligned.mei" mei_xml_tree = tt.purge_non_element_nodes(xml.parse(str(mei_path))) mei_section = mei_xml_tree.getElementsByTagName("section")[0] if "clef" in post_processing_steps: print(f"Performing clef post-processing for sheet {sheet_name}") for layer in mei_xml_tree.getElementsByTagName("layer"): element = layer.firstChild if element != None and element.tagName=="clef": staff = layer.parentNode measure = staff.parentNode clef_line = element.getAttribute("line") clef_shape = element.getAttribute("shape") layer.removeChild(element) prev = measure.previousSibling scoreDef = None while prev: if prev.tagName == "measure": break if prev.tagName == "scoreDef": scoreDef = prev break prev = prev.previousSibling # TODO: actually generalize this code if not scoreDef: scoreDef = tt.create_element_node("scoreDef") mei_section.insertBefore(scoreDef, measure) staffGrp = tt.first_or_none(scoreDef, "staffGrp") if not staffGrp: staffGrp = tt.create_element_node("staffGrp") scoreDef.appendChild(staffGrp) staffDef = tt.first_or_none(staffGrp, "staffDef", lambda e: e.getAttribute("n") == staff.getAttribute("n")) if not staffDef: staffDef = tt.create_element_node("staffDef", {"n": staff.getAttribute("n")}) staffGrp.appendChild(staffDef) staffDef.setAttribute("clef.line", clef_line) staffDef.setAttribute("clef.shape", clef_shape) # Write MEI file if there were changes if post_processing_steps: with open(str(mei_path), 'w') as mei_file: mei_file.write(tt.purge_non_element_nodes(mei_xml_tree.documentElement).toprettyxml()) status_update_msg = { '_id': task_id, 'module': 'post_processing', 'status': 'complete' } global channel channel.queue_declare(queue=cfg.mq_task_scheduler_status) channel.basic_publish(exchange="", routing_key=cfg.mq_task_scheduler_status, body=json.dumps(status_update_msg))