victor@1: ''' victor@1: Created on 24/09/2014 victor@1: victor@1: @author: victor victor@1: victor@1: Class for adding and joining measures victor@1: (It is under development) victor@1: ''' victor@1: import os victor@1: from music21 import stream victor@1: from music21 import converter victor@1: from music21 import meter victor@1: from music21 import key victor@1: from music21 import clef victor@1: from music21 import note victor@1: from music21 import interval victor@1: victor@1: class AddingXMLSingleMeasures: victor@1: def getAllFiles(self,path): victor@1: omr_files=[] victor@1: dir_content = os.listdir(path) victor@1: dir_content.sort() victor@1: for myfile in dir_content: victor@1: directory = os.path.join(path,myfile) victor@1: omr_files.append(os.path.abspath(directory)) victor@1: print omr_files victor@1: return omr_files victor@1: victor@1: def runViewJoinXML(self,dirname): victor@1: path = dirname victor@1: files=self.getAllFiles(path) victor@1: filesM21=self.getFilesM21(files,path) victor@1: part=stream.Part() victor@1: ks_array=[] victor@1: for f in filesM21: victor@1: measure=f.parts[0].getElementsByClass(stream.Measure)[0] victor@1: if len(part)==0: victor@1: ks_array=self.getKeySignatureArray(measure) victor@1: victor@1: if len(part)>0: victor@1: measure=self.removeTimeSignature(measure) victor@1: measure=self.removeKeySignature(measure) victor@1: measure=self.removeClef(measure) victor@1: # measure=self.transpose(measure,-10) victor@1: part.append(measure) victor@1: # part=self.correctKeySignature(part,["C "]) victor@1: # the correct key signature victor@1: part=self.correctKeySignature(part,ks_array) victor@1: part.show() victor@1: def transpose(self,measure,semitones): victor@1: newMeasure=stream.Measure() victor@1: for element in measure: victor@1: if isinstance(element,note.Note): victor@1: element=element.transpose(interval.Interval(semitones)) victor@1: newMeasure.append(element) victor@1: return newMeasure victor@1: def getKeySignatureArray(self,measure): victor@1: ks_array=[] victor@1: for element in measure: victor@1: if isinstance(element,key.KeySignature): victor@1: for p in element.alteredPitches: victor@1: ks_array.append(str(p)) victor@1: return ks_array victor@1: def correctKeySignature(self,part,altered): victor@1: newPart=stream.Part() victor@1: for measure in part.getElementsByClass(stream.Measure): victor@1: newMeasure=stream.Measure() victor@1: for element in measure: victor@1: if isinstance(element,note.Note): victor@1: for alter in altered: victor@1: print element.pitch.name,alter victor@1: if element.pitch.name[0:1]==alter[0:1] and alter[1:2]=="#": victor@1: element.accidental=1 victor@1: if element.pitch.name[0:1]==alter[0:1] and alter[1:2]=="-": victor@1: element.accidental=-1 victor@1: if element.pitch.name[0:1]==alter[0:1] and alter[1:2]==" ": victor@1: element.accidental=0 victor@1: newMeasure.append(element) victor@1: newPart.append(newMeasure) victor@1: return newPart victor@1: victor@1: def removeTimeSignature(self,measure): victor@1: newmeasure=stream.Measure() victor@1: for element in measure: victor@1: if not isinstance(element,meter.TimeSignature): victor@1: newmeasure.append(element) victor@1: return newmeasure victor@1: def removeKeySignature(self,measure): victor@1: newmeasure=stream.Measure() victor@1: for element in measure: victor@1: if not isinstance(element,key.KeySignature): victor@1: newmeasure.append(element) victor@1: return newmeasure victor@1: def removeClef(self,measure): victor@1: newmeasure=stream.Measure() victor@1: for element in measure: victor@1: if not isinstance(element,clef.GClef) and not isinstance(element,clef.NoClef) : victor@1: newmeasure.append(element) victor@1: return newmeasure victor@1: victor@1: victor@1: victor@1: victor@1: def getFilesM21(self,files,path): victor@1: filesM21=[] victor@1: print "---Converting Files---" victor@1: for i in range(len(files)): victor@1: f=path+"\M"+str(i+1)+".xml" victor@1: f_m21=converter.parse(f) victor@1: filesM21.append(f_m21) victor@1: return filesM21