# -*- coding: ISO-8859-1 -*- """ capellaScript -- Copyright (c) 2009 Peter Becker >>> Noten mitteln Achtung : Derzeit nur für den 4/4 Takt oder einzelne Noten verwenden|| Dieses Skript mittelt den Takt, indem aus einer 4tel Note 2 angebundene Achtel, und aus einer punktierten Viertel eine Viertel mit angebundenem Achtel macht wenn, sie in der entsprechenden Position stehen oder die Note direkt ausgewählt wird.|| Im Fehlerfall bitte eine Mail an peter_becker@freenet.de <<< History: 29.06.09 - Erste Ausgabe 15.08.09 - mehr Sicherheit bei Ermittlung der Zeilen """ import xml.dom, tempfile from caplib.capDOM import * from xml.dom.minidom import * doc = [] # parentNode von score def latin1_e(u): return u.encode('Latin-1') def getCursor(): sel = curSelection() result = None if sel == 0: messageBox('Fehler', 'keine aktive Partitur') return result return sel def getElementObjects(objList): # returns a List newList = NodeList() for n in range(objList.length): if objList[n].nodeType == objList[n].ELEMENT_NODE: newList.append(objList[n]) return newList def addNewElementNode(el,tagName): # add new Node with tagName "tagName" to el global doc newChild = doc.createElement(tagName) el.appendChild(newChild) return newChild def NoteAufteilen(punkt,duration,obj): if punkt == '2' or punkt == '3' or punkt == '6' or punkt == '7': duration[0].setAttribute('base','1/4') elif punkt == '0' or punkt == '1': duration[0].setAttribute('base','1/8') elif punkt == '4': duration[0].setAttribute('base','1/2') heads = obj.getElementsByTagName('head') for n in range(heads.length): if punkt == '1' or punkt == '2' or punkt == '4' or punkt == '6': duration[0].removeAttribute('dots') if punkt == '7': duration[0].setAttribute('dots','1') tie = addNewElementNode(heads[n],'tie') tie.setAttribute('begin','true') el = obj.cloneNode(1) obj.parentNode.insertBefore(el,obj) tie.setAttribute('end','true') tie.removeAttribute('begin') dos = obj.getElementsByTagName('drawObjects') #falls draw Objects an der 2. Note dranhängen if dos: #diese jetzt löschen dos[0].parentNode.removeChild(dos[0]) if punkt == '1': duration[0].setAttribute('base','1/4') elif punkt == '2': duration[0].setAttribute('base','1/8') elif punkt == '4': duration[0].setAttribute('base','1/4') elif punkt == '6': duration[0].setAttribute('base','1/2') elif punkt == '7': duration[0].setAttribute('base','1/8') duration[0].setAttribute('dots','0') def getBaseData(score): global sel, system, staff,voice, noteObject, objList, selvon selvon = sel[0] system = score.getElementsByTagName('system')[selvon[0]] staff = system.getElementsByTagName('staff')[selvon[1]] voice = staff.getElementsByTagName('voice')[selvon[2]] noteObject = voice.getElementsByTagName('noteObjects')[0] objList = getElementObjects(noteObject.childNodes) def mitteln(score): global sel, system, staff,voice, noteObject, objList, selvon, vo, takt global cas1, cas2, cas3, cas4, cas5, cas6, cas7 for obj in range(objList.length): du = 0 no = vo.noteObj(obj) if objList[obj].tagName == 'timeSign': time = no.meter() takt = 0 elif no.isChord(): duration = objList[obj].getElementsByTagName('duration') if duration[0].hasAttribute('dots'): dot = duration[0].getAttribute('dots') else: dot = '0' lng = duration[0].getAttribute('base') du = no.duration() takt = takt + du #messageBox('TAKT',str(du) + '\n' + str(takt) + '\n' + str(dot) + '\n' + str(cas6)) lng = duration[0].getAttribute('base') if str(takt) == '5/8' and str(lng) == '1/4' and dot == '0' and cas1 == 1: NoteAufteilen('0',duration,objList[obj]) elif str(takt) == '3/4' and str(lng) == '1/4' and dot <> '0' and cas2 == 1: NoteAufteilen('1',duration,objList[obj]) elif str(takt) == '5/8' and str(lng) == '1/2' and dot == '0' and cas3 == 1: NoteAufteilen('7',duration,objList[obj]) elif str(takt) == '5/8' and str(lng) == '1/4' and dot <> '0' and cas4 == 1: NoteAufteilen('2',duration,objList[obj]) elif str(takt) == '3/4' and str(lng) == '1/2' and dot == '0' and cas5 == 1: NoteAufteilen('3',duration,objList[obj]) elif str(takt) == '1' and str(lng) == '1/2' and dot == '1' and cas6 == 1: NoteAufteilen('6',duration,objList[obj]) elif str(takt) == '3/4' and str(lng) == '1/2' and dot == '1' and cas7 == 1: NoteAufteilen('4',duration,objList[obj]) if no.implBarline(): takt = 0 elif no.isRest(): du = no.duration() takt = takt + du if no.implBarline(): takt = 0 elif no.isBarline(): takt = 0 # Hier kommt die Klasse die vom Ende her aufgerufen wird ------------------------------------------------- class ScoreChange(ScoreChange): def changeScore(self, score): global doc, sel, system, staff,voice, noteObject, objList, selvon, vo, takt global auswahl, cas1, cas2, cas3, cas4, cas5, cas6, cas7 doc = score.parentNode sel = getCursor() if sel == None: return else: if auswahl == 0: # nur Note getBaseData(score) obj = objList[selvon[3]] if obj.tagName == 'chord': dot = '0' duration = obj.getElementsByTagName('duration') lng = duration[0].getAttribute('base') if duration[0].hasAttribute('dots'): dot = duration[0].getAttribute('dots') if lng == '1/4' and dot == '0': NoteAufteilen('0',duration,obj) elif lng =='1/4' and dot == '1': NoteAufteilen('1',duration,obj) elif auswahl == 1: # eine Stimme in einer Zeile takt = 0 getBaseData(score) vo=activeScore().system(selvon[0]).staff(selvon[1]).voice(selvon[2]) mitteln(score) elif auswahl == 2: # alle Stimmen in einer Zeile getBaseData(score) obj = objList[selvon[3]] voiceList = getElementObjects(obj.parentNode.parentNode.parentNode.childNodes) for voIx in range(voiceList.length): takt = 0 noteObject = voiceList[voIx].getElementsByTagName('noteObjects')[0] objList = getElementObjects(noteObject.childNodes) vo=activeScore().system(selvon[0]).staff(selvon[1]).voice(voIx) voice=voiceList[voIx] for obj in range(objList.length): no = vo.noteObj(obj) mitteln(score) elif auswahl == 3: # System getBaseData(score) staves = staff.parentNode staffList = getElementObjects(staves.childNodes) for stIx in range(staffList.length): voicesList = getElementObjects(staffList[stIx].childNodes)# for vsIx in range(voicesList.length): voiceList = getElementObjects(voicesList[vsIx].childNodes) for voIx in range(voiceList.length): vo=activeScore().system(selvon[0]).staff(stIx).voice(voIx) voice=voiceList[voIx] noteObject = voiceList[voIx].getElementsByTagName('noteObjects')[0] objList = getElementObjects(noteObject.childNodes) takt = 0 for obj in range(objList.length): no = vo.noteObj(obj) mitteln(score) elif auswahl == 4: # Partitur getBaseData(score) systems = system.parentNode systemList = getElementObjects(systems.childNodes) for syIx in range(systemList.length): staves = getElementObjects(systemList[syIx].childNodes) for svIx in range(staves.length): staffList = getElementObjects(staves[svIx].childNodes) for stIx in range(staffList.length): voicesList = getElementObjects(staffList[stIx].childNodes) for vsIx in range(voicesList.length): voiceList = getElementObjects(voicesList[vsIx].childNodes) for voIx in range(voiceList.length): vo=activeScore().system(syIx).staff(stIx).voice(voIx) voice=voiceList[voIx] noteObject = voiceList[voIx].getElementsByTagName('noteObjects')[0] objList = getElementObjects(noteObject.childNodes) takt = 0 for obj in range(objList.length): no = vo.noteObj(obj) mitteln(score) return # Jetzt kommt der Code für den Dialog---------------------------------------------------------------------- def scriptDialog(): global auswahl, cas1, cas2, cas3, cas4, cas5, cas6, cas7 radAuswahl = Radio(['markierte Note','eine Stimme in der Zeile','alle Stimmen in einer Zeile','System','Partitur'],text='Note mitteln',value=0) lblVor = Label('Vorlaufnote ',width=6) lblUeb = Label('Mittenüberschreitung',width=6) lblLeer= Label(' ') lblAbs = Label(' ',with=6) lblAuf = Label(' Auflösung',width=6) lblVor1 = Label(' 1/8 ',width=6) lblVor2 = Label(' 1/4 ',width=6) lblVor3 = Label(' ',width=6) lblUeb1 = Label(' 1/4 ',width=6) lblUeb2 = Label(' 1/4 . ',width=6) lblUeb3 = Label(' 1/2 ',width=6) lblUeb4 = Label(' 1/2 . ',width=6) lblAbs1 = Label(' -> ',with=6) lblAuf1 = Label(' 1/8 ._.1/8 ',width=6) lblAuf2 = Label(' 1/8 ._.1/4 ',width=6) lblAuf3 = Label(' 1/4 . ._.1/8 ',width=6) lblAuf4 = Label(' 1/4 ._.1/8 ',width=6) lblAuf5 = Label(' 1/4 ._.1/4 . ',width=6) lblAuf6 = Label(' 1/4 ._.1/4 ',width=6) lblAuf7 = Label(' 1/4 ._.1/2 ',width=6) lblAuf8 = Label(' 1/2 ._.1/4 ',width=6) chkCas1 = CheckBox(' ',value=1) chkCas2 = CheckBox(' ',value=1) chkCas3 = CheckBox(' ',value=0) chkCas4 = CheckBox(' ',value=1) chkCas5 = CheckBox(' ',value=0) chkCas6 = CheckBox(' ',value=0) chkCas7 = CheckBox(' ',value=0) subboxL = HBox([lblLeer]) subboxU = HBox([lblVor,lblUeb,lblAbs,lblAuf,lblAbs]) subbox1 = HBox([lblVor1,lblUeb1,lblAbs1,lblAuf1,chkCas1]) subbox2 = HBox([lblVor1,lblUeb2,lblAbs1,lblAuf2,chkCas2]) subbox3 = HBox([lblVor1,lblUeb3,lblAbs1,lblAuf3,chkCas3]) subbox4 = HBox([lblVor2,lblUeb2,lblAbs1,lblAuf4,chkCas4]) subbox5 = HBox([lblVor2,lblUeb3,lblAbs1,lblAuf6,chkCas5]) subbox6 = HBox([lblVor2,lblUeb4,lblAbs1,lblAuf7,chkCas6]) subbox7 = HBox([lblVor3,lblUeb4,lblAbs1,lblAuf8,chkCas7]) #chkCopy = CheckBox('übernehmen ',value=int(opt.get('copy',str(cy)))) # Daten aus anderer Partitur übernehmen #subboxCopy = VBox([chkCopy],text='Datenübernahme') selBox = VBox([subboxU,subboxL,subbox1,subbox2,subbox3,subbox4,subbox5,subbox6,subbox7],text='Mittenüberschreitung') box = HBox([radAuswahl,selBox]) dlg = Dialog('Noten mitteln', box) # und nun wird der Dialog aufgerufen ---------------------------------------------------------------------- if dlg.run(): auswahl = radAuswahl.value() cas1 = chkCas1.value() cas2 = chkCas2.value() cas3 = chkCas3.value() cas4 = chkCas4.value() cas5 = chkCas5.value() cas6 = chkCas6.value() cas7 = chkCas7.value() return True else: return False # das sollte immer am Ende stehen, denn sonst lassen sich die Aktionen des Skriptes nicht rückgängig machen if activeScore(): if scriptDialog(): activeScore().registerUndo("Noten mitteln") tempInput = tempfile.mktemp('.capx') tempOutput = tempfile.mktemp('.capx') activeScore().write(tempInput) ScoreChange(tempInput, tempOutput) # messageBox(tempInput, tempOutput) activeScore().read(tempOutput) os.remove(tempInput) os.remove(tempOutput)