# -*- coding: ISO-8859-1 -*- """ capellaScript -- Copyright (c) 2008,2009 Peter Becker >>> Ziach_nach_Noten_B : Umsetzen von Ziach Griffschrift in Notenschrift Teil 2|| Übersetzen|| Bei Problemen bitte eine Mail an peter_becker@freenet.de <<< """ # 01.09.2008 Erste Version # 23.10.2008 Fehler in Positions Normalisierung. Als Umgehung erst mal auskommentiert # 25.10.2008 Positionsnormalisierung korrigiert # 31.03.2009 Erweiterung auf Kreuzerkennung lt. Capella Scan 7.x # 12.05.2009 Laufzeithalbierung, Fehler bei crossCircle # 26.07.2009 Köpfe mit shape=none entfernen. Sie dienen nur als Platzhalter, Triangel als Doppelkreuz akzeptieren # 20.11.2009 Absturz bei nicht eingefärbten Pausen korrigiert # 24.11.2009 fehlerhaftes Entfernen von Kreuzköpfen korrigiert ( Verschlimmbesserung vom 26.7.09 ) import xml.dom, tempfile, math #from caplib.capDOM import ScoreChange, firstChildElement, * from caplib.capDOM import * #from xml.dom.minidom import NodeList, Node, Element, * from xml.dom.minidom import * global score doc = [] # parentNode von score # Druck - Darstellung mit Kreuz DruckKreuz = dict( G4 = dict(wert='G4' , kreuz='0' ), A4 = dict(wert='C5' , kreuz='0' ), B4 = dict(wert='B4' , kreuz='0' ), C5 = dict(wert='E5' , kreuz='0' ), D5 = dict(wert='D5' , kreuz='0' ), E5 = dict(wert='G5' , kreuz='0' ), F5 = dict(wert='G5' , kreuz='0' ), G5 = dict(wert='C6' , kreuz='0' ), A5 = dict(wert='B5' , kreuz='0' ), B5 = dict(wert='E6' , kreuz='0' ), C6 = dict(wert='D6' , kreuz='0' ), D6 = dict(wert='G6' , kreuz='0' ), E6 = dict(wert='G6' , kreuz='0' ), F6 = dict(wert='C7' , kreuz='0' ), G6 = dict(wert='B6' , kreuz='0' ), A6 = dict(wert='E7' , kreuz='0' ), B6 = dict(wert='D7' , kreuz='0' ), C7 = dict(wert='G7' , kreuz='0' ), D7 = dict(wert='G7' , kreuz='0' ), E7 = dict(wert='C8' , kreuz='0' ), F7 = dict(wert='B7' , kreuz='0' ) ) # Druck - Darstellung nur Kopf DruckKopf = dict( E4 = dict(wert='A3' , kreuz='0' ), F4 = dict(wert='D4' , kreuz='0' ), G4 = dict(wert='C4' , kreuz='1' ), A4 = dict(wert='F4' , kreuz='1' ), B4 = dict(wert='E4' , kreuz='0' ), C5 = dict(wert='A4' , kreuz='0' ), D5 = dict(wert='A4' , kreuz='0' ), E5 = dict(wert='D5' , kreuz='0' ), F5 = dict(wert='C5' , kreuz='1' ), G5 = dict(wert='F5' , kreuz='1' ), A5 = dict(wert='E5' , kreuz='0' ), B5 = dict(wert='A5' , kreuz='0' ), C6 = dict(wert='A5' , kreuz='0' ), D6 = dict(wert='D6' , kreuz='0' ), E6 = dict(wert='C6' , kreuz='1' ), F6 = dict(wert='F6' , kreuz='1' ), G6 = dict(wert='E6' , kreuz='0' ), A6 = dict(wert='A6' , kreuz='0' ), B6 = dict(wert='A6' , kreuz='0' ), C7 = dict(wert='D7' , kreuz='0' ), D7 = dict(wert='C7' , kreuz='1' ), E7 = dict(wert='F7' , kreuz='1' ), F7 = dict(wert='E7' , kreuz='0' ), G7 = dict(wert='A7' , kreuz='0' ), A7 = dict(wert='A7' , kreuz='0' ) ) # Zug - Darstellung mit Kreuz ZugKreuz = dict( G4 = dict(wert='C5' , kreuz='0' ), A4 = dict(wert='F5' , kreuz='0' ), B4 = dict(wert='D5' , kreuz='0' ), C5 = dict(wert='G5' , kreuz='0' ), D5 = dict(wert='F5' , kreuz='1' ), E5 = dict(wert='B5' , kreuz='0' ), F5 = dict(wert='A5' , kreuz='0' ), G5 = dict(wert='D6' , kreuz='0' ), A5 = dict(wert='C6' , kreuz='0' ), B5 = dict(wert='F6' , kreuz='0' ), C6 = dict(wert='D6' , kreuz='0' ), D6 = dict(wert='G6' , kreuz='0' ), E6 = dict(wert='F6' , kreuz='1' ), F6 = dict(wert='B6' , kreuz='0' ), G6 = dict(wert='A6' , kreuz='0' ), A6 = dict(wert='D7' , kreuz='0' ), B6 = dict(wert='C7' , kreuz='0' ), C7 = dict(wert='F7' , kreuz='0' ), D7 = dict(wert='E7' , kreuz='0' ), E7 = dict(wert='A7' , kreuz='0' ), F7 = dict(wert='F7' , kreuz='1' ) ) # Zug - Darstellung nur Kopf ZugKopf = dict( E4 = dict(wert='D4' , kreuz='0' ), F4 = dict(wert='G4' , kreuz='0' ), G4 = dict(wert='E4' , kreuz='0' ), A4 = dict(wert='A4' , kreuz='0' ), B4 = dict(wert='G4' , kreuz='1' ), C5 = dict(wert='C5' , kreuz='1' ), D5 = dict(wert='B4' , kreuz='0' ), E5 = dict(wert='E5' , kreuz='0' ), F5 = dict(wert='D5' , kreuz='0' ), G5 = dict(wert='G5' , kreuz='0' ), A5 = dict(wert='F5' , kreuz='1' ), B5 = dict(wert='A5' , kreuz='0' ), C6 = dict(wert='G5' , kreuz='1' ), D6 = dict(wert='C6' , kreuz='1' ), E6 = dict(wert='B5' , kreuz='0' ), F6 = dict(wert='E6' , kreuz='0' ), G6 = dict(wert='D6' , kreuz='0' ), A6 = dict(wert='G6' , kreuz='0' ), B6 = dict(wert='F6' , kreuz='1' ), C7 = dict(wert='B6' , kreuz='0' ), D7 = dict(wert='G6' , kreuz='1' ), E7 = dict(wert='C7' , kreuz='1' ), F7 = dict(wert='B6' , kreuz='0' ), G7 = dict(wert='E7' , kreuz='0' ), A7 = dict(wert='D7' , kreuz='0' ) ) # y Offset der Notenköpfe KopfOffset = dict(E4 = '5.5',F4 = '5',G4 = '4.5',A4 = '4', B4 = '3.5', C5 = '3',D5 = '2.5',E5 ='2',F5 = '1.5',G5 = '1',A5 = '0.5',B5 ='0', C6 = '-0.5',D6 = '-1',E6 = '-1.5',F6 = '-2',G6 = '-2.5',A6 = '-3',B6 = '-3.5', C7 = '-4',D7 = '-4.5',E7 = '-5',F7 = '-5.5',G7 = '-6',A7 ='-6.5') def latin1_d(u): return u.decode('Latin-1') def addElementNode(el,tagName,score): # Neue Node zu "el" hinzufügen wenn Node "tagName" nicht existiert # ansonsten existierende Node zurückmelden childs = el.childNodes for n in range(childs.length): if childs[n].nodeType ==childs[n].ELEMENT_NODE and childs[n].tagName == tagName: return childs[n] newChild = score.parentNode.createElement(tagName) el.appendChild(newChild) return newChild class ScoreChange(ScoreChange): def changeScore(self, score): for system in score.getElementsByTagName('system'): # nur 1 Zeile stehen lassen staffs = 0 for staff in system.getElementsByTagName('staff'): if staffs <> 0: staff.parentNode.removeChild(staff) staffs = staffs + 1 for nOs in system.getElementsByTagName('noteObjects'): # alles ausser 'chords' schwarz färben childs = nOs.childNodes #messageBox('CHILDS',str(childs)) clef = 0 for n in range(childs.length): if childs[n].nodeType ==childs[n].ELEMENT_NODE and childs[n].tagName == 'clefSign': clef = 1 if childs[n].nodeType ==childs[n].ELEMENT_NODE and childs[n].tagName == 'barline': if childs[n].hasAttribute('color'): childs[n].removeAttribute('color') if childs[n].nodeType ==childs[n].ELEMENT_NODE and childs[n].tagName <> 'chord': display = childs[n].getElementsByTagName('display') if display: if display[0].hasAttribute('color'): display[0].removeAttribute('color') if clef == 0: # falls kein Schlüssel in der Zeile newChild = score.parentNode.createElement('clefSign') # Violinschlüssel einfügen for n in range(childs.length): if childs[n].nodeType ==childs[n].ELEMENT_NODE: fc = childs[n] nOs.insertBefore(newChild,fc) break for cl in nOs.getElementsByTagName('clefSign'): cl.setAttribute('clef','treble') for rs in nOs.getElementsByTagName('rest'): for do in rs.getElementsByTagName('drawObj'): # Druckbalken an Pausen entfernen for ln in do.getElementsByTagName('line'): do.parentNode.removeChild(do) for no in nOs.getElementsByTagName('chord'): #messageBox('NO',str(no)) display = no.getElementsByTagName('display') color ='000000' if display: color = display[0].getAttribute('color') # Balgrichtung extrahieren #if color == '0000FF' or color == 'FF0000': pitch = [] # Tonhöhe der Köpfe am Notenhals pos = [] # y-Position vorhandener DrawObjects am Notenhals heads = [] # Kopf, Elemente am Notenhals kreuz = [] # Kopf hat Kreuz, yes/no form = [] # Kopfform aus Original alter = [] # Doppelkreuz als Vorzeichen if color == '0000FF': richtung = 'D' else: richtung = 'Z' for hd in no.getElementsByTagName('head'): pitch1 = hd.getAttribute('pitch') # Tonhöhe extrahieren pitch.append(str(pitch1)) heads.append(hd) # und merken form1 = "no" if hd.hasAttribute('shape'): form1 = hd.getAttribute('shape') if form1 <> "crossCircle": form1 = "no" hd.removeAttribute('shape') form.append(str(form1)) # falls Kopfform crossCircle, dies merken alt = hd.getElementsByTagName('alter') alter1 = "no" if alt: alter1 = alt[0].getAttribute('step') if alter1 <> "2": alter1 = "no" alter.append(str(alter1)) # falls Doppelkreuz als Vorzeichen, dies merken for do in no.getElementsByTagName('drawObj'): # Kreuze feststellen und Position merken for ln in do.getElementsByTagName('line'): do.parentNode.removeChild(do) # Druckbalken weg for tx in do.getElementsByTagName('text'): if tx: y = tx.getAttribute('y') font = tx.getElementsByTagName('font') face = font[0].getAttribute('face') if face == 'Ziach': do.parentNode.removeChild(do) # weg mit TAB Schlüssel if face == 'capella3': content = tx.getElementsByTagName('content') data = content[0].firstChild.data if data == 'T' or data == u'\xee': # Position normalisiere n.5 oder n.0 y1 = math.modf(float(y)) z = y1[1] n = y1[0] y = float(y) #messageBox('X1',str(y) + '\n' + str(y1) + '\n' + str(z) + '\n' + str(n)) if y >= 0: # positives offset #messageBox('Y+',str(y)) if n <= 0.25: n = 0 elif n > 0.25 and n <= 0.5: n = 0.5 elif n <= 0.75 and n >= 0.5: n = 0.5 elif n > 0.75: n = 0 z = z + 1 y2 = n + z else: # negatives offset #messageBox('Y-',str(y)) if n >= -0.25: n = 0 #messageBox('Y1',str(n)) elif n < -0.25 and n >= -0.5: n = -0.5 #messageBox('Y2',str(n)) elif n >= -0.75 and n <= -0.5: n = -0.5 #messageBox('Y3',str(n)) elif n < -0.75: n = 0 z = z - 1 #messageBox('Y4',str(n)) y2 = n + z #messageBox('X2',str(y) + '\n' + str(y1) + '\n' + str(z) + '\n' + str(n) + '\n' + str(y2)) pos.append(str(y2)) # y-Position merken do.parentNode.removeChild(do) # weg mit den Kreuzen oder Triangeln if pitch: #messageBox('NOs',str(pitch) + '\n' + str(pos) + '\n' + str(heads)) for ix1 in range(len(pitch)): # testen ob Ton ein Kreuz hat off = str(float(KopfOffset[pitch[ix1]])) kr = 'no' if off in pos: kr = 'yes' #messageBox('Step1','Step 1') else: fo = form[ix1] al = alter[ix1] #messageBox('Step2','Step 2') if fo == 'crossCircle': kr = 'yes' #messageBox('Step3','Step 3') elif al == '2': #messageBox('Step4','Step 4') kr = 'yes' else: #messageBox('Step5','Step 5') kr = 'no' kreuz.append(kr) # merken welcher Kopf ein Kreuz hat und welcher nicht #messageBox('Kreuz',str(kreuz)) #messageBox('NOs',str(off) + '\n' + str(pos) + '\n' + str(kr) + '\n' + str(richtung)) for ix3 in range(len(pitch)): # übersetzen if richtung == 'D' and kreuz[ix3] == 'yes': #messageBox('NOs',str(off) + '\n' + str(pos) + '\n' + str(kr) + '\n' + str(richtung) + '\n' + str(pitch[ix3])) headneu = DruckKreuz[pitch[ix3]]['wert'] vorzneu = DruckKreuz[pitch[ix3]]['kreuz'] #messageBox('Übersetzt bei Druck und Kreuz',str(headneu) + ' ' + str(vorzneu)) elif richtung == 'Z' and kreuz[ix3] == 'yes': headneu = ZugKreuz[pitch[ix3]]['wert'] vorzneu = ZugKreuz[pitch[ix3]]['kreuz'] elif richtung == 'D' and kreuz[ix3] == 'no': headneu = DruckKopf[pitch[ix3]]['wert'] vorzneu = DruckKopf[pitch[ix3]]['kreuz'] else: headneu = ZugKopf[pitch[ix3]]['wert'] vorzneu = ZugKopf[pitch[ix3]]['kreuz'] #messageBox('ÜÜ',str(pitch[ix3]) + '\n' + str(headneu)+ '\n' + str(vorzneu)) heads[ix3].setAttribute('pitch',str(headneu)) alter = addElementNode(heads[ix3],'alter',score) #messageBox('yy',str(alter) + '\nvorzneu=' + str(vorzneu)) for dp in no.getElementsByTagName('display'): # wieder schwarz färben if dp.hasAttribute('color'): dp.removeAttribute('color') if alter.hasAttribute('display'): # display=suppress löschen um Vorzeichen anzuzeigen alter.removeAttribute('display') if vorzneu == '1': alter.setAttribute('step','1') else: alter.parentNode.removeChild(alter) # weg mit 'alter', wird nicht benötigt heads = [] pos = [] pitch = [] kreuz = [] # Hauptprogramm: if activeScore(): #if pqDialog(): activeScore().registerUndo("Ziach_nach_Noten_B") tempInput = tempfile.mktemp('.capx') tempOutput = tempfile.mktemp('.capx') activeScore().write(tempInput) ScoreChange(tempInput, tempOutput) #messageBox('SAVE',str(tempOutput)) activeScore().read(tempOutput) os.remove(tempInput) os.remove(tempOutput)