changeset 1:0f7f611deca4

Functions
author Victor Padilla <victor.padilla.mc@gmail.com>
date Mon, 04 May 2015 22:53:31 +0200
parents 1eb6054a1f84
children 46fb79167a61
files Functions/AddingXMLSingleMeasures.py Functions/AddingXMLSingleMeasures.pyc Functions/FilesFunctions.py Functions/FilesFunctions.pyc Functions/HashFunctions.py Functions/HashFunctions.pyc Functions/MeasureFunctions.py Functions/MeasureFunctions.pyc Functions/__init__.py Functions/__init__.pyc
diffstat 10 files changed, 972 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Functions/AddingXMLSingleMeasures.py	Mon May 04 22:53:31 2015 +0200
@@ -0,0 +1,111 @@
+'''
+Created on 24/09/2014
+
+@author: victor
+
+Class for adding and joining measures
+(It is under development)
+'''
+import os
+from music21 import stream
+from music21 import converter
+from music21 import meter
+from music21 import key
+from music21 import clef
+from music21 import note
+from music21 import interval
+
+class AddingXMLSingleMeasures:
+    def getAllFiles(self,path):
+        omr_files=[]
+        dir_content = os.listdir(path)
+        dir_content.sort()
+        for myfile in dir_content:
+            directory = os.path.join(path,myfile)
+            omr_files.append(os.path.abspath(directory))
+        print omr_files
+        return omr_files
+    
+    def runViewJoinXML(self,dirname):
+        path = dirname
+        files=self.getAllFiles(path)
+        filesM21=self.getFilesM21(files,path)
+        part=stream.Part()
+        ks_array=[]
+        for f in filesM21:
+            measure=f.parts[0].getElementsByClass(stream.Measure)[0]
+            if len(part)==0:
+                ks_array=self.getKeySignatureArray(measure)
+                
+            if len(part)>0:
+                measure=self.removeTimeSignature(measure)
+                measure=self.removeKeySignature(measure)
+                measure=self.removeClef(measure)
+#                 measure=self.transpose(measure,-10)
+            part.append(measure)
+#         part=self.correctKeySignature(part,["C "])  
+        # the correct key signature
+        part=self.correctKeySignature(part,ks_array)       
+        part.show()
+    def transpose(self,measure,semitones):
+        newMeasure=stream.Measure()
+        for element in measure:
+                if isinstance(element,note.Note):  
+                    element=element.transpose(interval.Interval(semitones))
+                newMeasure.append(element)
+        return newMeasure
+    def getKeySignatureArray(self,measure):
+        ks_array=[]
+        for element in measure:
+            if isinstance(element,key.KeySignature):
+                for p in element.alteredPitches:
+                    ks_array.append(str(p))
+                return ks_array                
+    def correctKeySignature(self,part,altered):
+        newPart=stream.Part()
+        for measure in part.getElementsByClass(stream.Measure):
+            newMeasure=stream.Measure()
+            for element in measure:
+                if isinstance(element,note.Note):
+                    for alter in altered:
+                        print element.pitch.name,alter
+                        if element.pitch.name[0:1]==alter[0:1] and alter[1:2]=="#":
+                            element.accidental=1
+                        if element.pitch.name[0:1]==alter[0:1] and alter[1:2]=="-":
+                            element.accidental=-1
+                        if element.pitch.name[0:1]==alter[0:1] and alter[1:2]==" ":
+                            element.accidental=0
+                newMeasure.append(element)
+            newPart.append(newMeasure)
+        return newPart
+        
+    def removeTimeSignature(self,measure):
+        newmeasure=stream.Measure()
+        for element in measure:
+            if not isinstance(element,meter.TimeSignature):
+                newmeasure.append(element)
+        return newmeasure       
+    def removeKeySignature(self,measure):
+        newmeasure=stream.Measure()
+        for element in measure:
+            if not isinstance(element,key.KeySignature):
+                newmeasure.append(element)
+        return newmeasure   
+    def removeClef(self,measure):
+        newmeasure=stream.Measure()
+        for element in measure:
+            if not isinstance(element,clef.GClef) and not isinstance(element,clef.NoClef) :
+                newmeasure.append(element)
+        return newmeasure                          
+            
+      
+        
+            
+    def getFilesM21(self,files,path):
+        filesM21=[]
+        print "---Converting Files---"
+        for i in range(len(files)):
+            f=path+"\M"+str(i+1)+".xml"
+            f_m21=converter.parse(f)
+            filesM21.append(f_m21)
+        return filesM21
\ No newline at end of file
Binary file Functions/AddingXMLSingleMeasures.pyc has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Functions/FilesFunctions.py	Mon May 04 22:53:31 2015 +0200
@@ -0,0 +1,206 @@
+'''
+@organization: Lancaster University & University of Leeds
+@version: 1.0
+Created on 11/12/2014
+
+@author: Victor Padilla
+@contact: v.padilla@lancaster.ac.uk
+
+Auxiliary functions to manipulate files and convert to music21
+for processing
+
+'''
+
+from music21 import converter
+import os
+
+class FilesFunctions:
+    
+    def getFiles(self,path):
+        '''
+        Takes the XML files in a folder excluding ground and results 
+        returns alphabetically
+        
+        usage:
+        ff=FilesFunctions()
+        files=ff.getFiles("C:\\Users\\victor\\Desktop\\data\k458\Process\m2\parts\0\XML")
+        '''
+        
+        omr_files=[]
+        dir_content = os.listdir(path)
+        dir_content.sort()
+        for myfile in dir_content:
+            directory = os.path.join(path,myfile)
+            extension = os.path.splitext(myfile)[1]
+            if myfile.find("result.")==-1 and myfile!="ground.xml" and extension.upper()==".XML":
+                omr_files.append(os.path.abspath(directory))
+        return omr_files
+    
+    def getAllFiles(self,path):
+        '''
+        Takes all the files in a folder
+        returns alphabetically
+        
+        usage:
+        ff=FilesFunctions()
+        files=ff.getAllFiles("C:\\Users\\victor\\Desktop\\data\k458\Process\m2\parts\0\XML")
+        '''
+        omr_files=[]
+        dir_content = os.listdir(path)
+        dir_content.sort()
+        for myfile in dir_content:
+            directory = os.path.join(path,myfile)
+            omr_files.append(os.path.abspath(directory))
+        return omr_files
+    
+        
+    def SubDirPath (self,d):
+        '''
+        Returns the directories from a path
+        
+        usage:
+        ff=FilesFunctions()
+        subdirArray=ff.SubDirPath("C:\\Users\\victor\\Desktop\\data\k458\Process\m2\parts")
+        
+        #returns ["C:\\Users\\victor\\Desktop\\data\k458\Process\m2\parts\1",
+                "C:\\Users\\victor\\Desktop\\data\k458\Process\m2\parts\2"....] 
+        '''
+        return filter(os.path.isdir, [os.path.join(d,f) for f in os.listdir(d)])  
+    
+    
+    def getGround(self,path):
+        '''
+        Returns the ground file from a path
+        
+        usage:
+        ff=FilesFunctions()
+        ground=ff.getGround("C:\\Users\\victor\\Desktop\\data\\k458\\Process")
+        '''
+        dir_content = os.listdir(path)
+        dir_content.sort()
+        for myfile in dir_content:
+            if myfile=="ground.xml":
+                directoryFile = os.path.join(path,myfile)
+                return directoryFile
+            
+            
+    def getFinalScore(self,path):
+        '''
+        Returns the finalScore.xml file from a path
+        
+        usage:
+        ff=FilesFunctions()
+        finalScore=ff.getFinalScore("C:\\Users\\victor\\Desktop\\data\\k458\\Process")
+        '''
+        dir_content = os.listdir(path)
+        dir_content.sort()
+        for myfile in dir_content:
+            if myfile=="finalScore.xml":
+                directoryFile = os.path.join(path,myfile)
+                return directoryFile 
+    
+    def writeText(self,path,betterOmrIds):
+        '''
+        Writes betterOMR.txt file with the number of the OMR files chosen in the phylogenetic tree
+        
+        usage:
+        ff=FilesFunctions()
+        betterOmrIds=[2,3,5]
+        ff.writeText("C:\\Users\\victor\\Desktop\\data\\k458\\Process",betterOmrIds)
+        '''
+        f=open(path+"\\betterOMR.txt","w")
+        for idOMR in betterOmrIds:
+            f.write(str(idOMR)+"\n")
+        f.close()
+        
+    def getOMRs(self,path): 
+        '''
+        Takes the different .xml in a folder and convert them to music21
+        Returns an array with the files processed
+        
+        ff=FilesFunctions()
+        OMRs=ff.getOMRs("C:\\Users\\victor\\Desktop\\data\\k458\\Process\\m2\parts\0\XML",)
+        '''
+        fsFiles=self.getFiles(path)  
+        OMRs=[]
+        for f in fsFiles:
+            try:
+                print f
+                OMRs.append(converter.parse(f, forceSource=True))
+            except:
+                OMRs.append([])
+                print "OMR error"
+        return OMRs   
+    
+    def getOMR(self,f): 
+        '''
+        Takes the  .xml file in a folder and convert them to music21
+
+        '''
+        OMR=[]
+        try:
+            print f
+            OMR=converter.parse(f, forceSource=True)
+        except:
+            OMR=[]
+            print "OMR error"
+        return OMR  
+    def getAllImgFiles(self,d):
+        '''
+        Returns all the .tif files from a directory ordered alphabetically
+        
+        usage:
+        ff=FilesFunctions()
+        imageFiles=ff.getAllImgFiles("C:\\Users\\victor\\Desktop\\data\\k458\\OMRS\\m2\parts\Peters\0\XML",)
+        '''
+        files=[]
+        dir_content = os.listdir(d)
+        dir_content.sort()
+        for myfile in dir_content:
+            if myfile.endswith('.tif'):
+                files.append(myfile)
+        return files
+
+    def getKernFile(self,d):
+        '''
+        Returns the first .krn file from a directory ordered alphabetically
+        
+        usage:
+        ff=FilesFunctions()
+        kernFile=ff.getKernFile("C:\\Users\\victor\\Desktop\\data\\k458\\Process\\m2\parts\Peters\0\XML",)
+        '''
+        dir_content = os.listdir(d)
+        dir_content.sort()
+        for myfile in dir_content:
+            if myfile.endswith('.krn'):
+                return myfile
+    def getXMLFile(self,d):
+        '''
+        Returns the first .xml file from a directory ordered alphabetically
+        
+        usage:
+        ff=FilesFunctions()
+        xmlFile=ff.getXMLFile("C:\\Users\\victor\\Desktop\\data\\k458\\Process\\m2\parts\Peters\0\XML",)
+        '''
+        dir_content = os.listdir(d)
+        dir_content.sort()
+        for myfile in dir_content:
+            if myfile.endswith('.xml'):
+                return myfile
+                
+    def getAllXMLFiles(self,d):
+        '''
+        Returns the .xml files from a directory ordered alphabetically
+        
+        usage:
+        ff=FilesFunctions()
+        xmlFiles=ff.getAllXMLFiles("C:\\Users\\victor\\Desktop\\data\\k458\\OMRS\\m2\parts\Peters\0\XML",)
+        '''
+        files=[]
+        dir_content = os.listdir(d)
+        dir_content.sort()
+        for myfile in dir_content:
+            if myfile.endswith('.xml'):
+                files.append(myfile)
+        return files    
+           
Binary file Functions/FilesFunctions.pyc has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Functions/HashFunctions.py	Mon May 04 22:53:31 2015 +0200
@@ -0,0 +1,123 @@
+'''
+@organization: Lancaster University & University of Leeds
+@version: 1.0
+Created on 11/12/2014
+
+@author: Victor Padilla
+@contact: v.padilla@lancaster.ac.uk
+
+Functions to manipulate and convert to Hash array
+the scores. 
+
+It is useful for aligning measures (not single notes)
+
+'''
+from music21 import stream
+from music21 import note
+from music21 import omr
+# FastAlignmentArrays is a Cython library
+from Alignment import FastAlignmentArrays
+from MeasureFunctions import MeasureFunctions
+
+class HashFunctions:
+    
+    def alignHash(self,hashArray):
+        '''
+        Returns two hash arrays aligned
+        
+        Uses the needleman-wunsh algorithm evaluating the differences between measures
+        '''
+        faa=FastAlignmentArrays()
+        hashOrdered=faa.needleman_wunsch(hashArray[0], hashArray[1],False)[0]
+        hashArray[0]=hashOrdered[0]
+        hashArray[1]=hashOrdered[1]
+        return hashArray
+    
+    def getHash(self,OMR):
+        '''
+        Returns hash from a group of OMR parts
+        '''
+        print "...Obtaining Hash of measures..."
+        hashArray=[]
+        for i in range(len(OMR)):
+            hashArray.append(self.getHashArrayFromPart(OMR[i].parts[0]))
+        return hashArray
+    
+    def getGapsFromHashArray(self,hashArray):
+        '''
+        Returns the gap index array from a group of hash parts
+        '''
+        gapsArrays=[]
+        for i in range(len(hashArray)):
+            gapsArray=[]
+            for j in range(len(hashArray[i])):
+                symbol=hashArray[i][j]
+                if symbol=="*":
+                    gapsArray.append(j)
+            
+            gapsArrays.append(gapsArray)   
+        return gapsArrays
+    
+         
+ 
+        
+    def reconstructHash(self,OMR,hashArray):  
+        '''
+        Returns a group of single parts ordered by hashArray (gaps)
+        '''
+        scores=[]
+        gapsArr=[]
+        mf=MeasureFunctions()
+        for i in range(len(OMR)):
+            print len(hashArray[i])
+            partReconstruct,gaps=mf.reconstructScore(OMR[i].parts[0], hashArray[i])
+            sc=stream.Score()
+            sc.append(partReconstruct)
+            scores.append(sc)
+            gapsArr.append(gaps)
+        return scores,gapsArr
+        
+    
+      
+
+    def removeHashGaps(self,partHash):
+        '''
+        Removes gaps in a part
+        '''
+        newHash=[]
+        for i in range(len(partHash)):
+            if(partHash[i]!="*"):
+                newHash.append(partHash[i])
+        return newHash
+    
+    def addHashGaps(self,partHash,gaps):
+        '''
+        Insert gaps in a part
+        '''
+        for gap in gaps:
+            partHash.insert(gap,"")
+        return partHash    
+    
+        
+    def getHashArrayFromPart(self,part):
+        '''
+        get Hash string of a Part (music21)
+        '''
+        hashArray=[]
+        lengthArray=len(part.getElementsByClass(stream.Measure))
+        for i in range(lengthArray):
+            measure=part.getElementsByClass(stream.Measure)[i]
+            hashMeasure=self.getHashFromMeasure(measure)
+            pitchArr=""
+            for mynote in measure.flat.getElementsByClass(note.Note):
+                pitchArr+=str(mynote.pitch.name)
+            hashArray.append(pitchArr+"_"+hashMeasure)
+        return hashArray
+    
+    def getHashFromMeasure(self,measure):
+        '''
+        get Hash string of a measure. Library correctors.py of Michael Scott Cuthbert. Project OMR
+        '''
+        mh=omr.correctors.MeasureHash(measure).getHashString()
+        return mh
+    
\ No newline at end of file
Binary file Functions/HashFunctions.pyc has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Functions/MeasureFunctions.py	Mon May 04 22:53:31 2015 +0200
@@ -0,0 +1,527 @@
+'''
+@organization: Lancaster University & University of Leeds
+@version: 1.0
+Created on 11/12/2014
+
+@author: Victor Padilla
+@contact: v.padilla@lancaster.ac.uk
+
+Functions to manipulate measures
+
+
+'''
+
+from music21 import stream
+from music21 import note
+from music21 import meter
+from music21 import omr
+from music21 import spanner
+from music21 import chord
+import copy
+
+
+class MeasureFunctions:
+    
+    def getDuration(self,bar):
+        '''
+        Returns the duration of a single measure
+        '''
+        duration=0
+        try:
+            for event in bar:
+                try:
+                    if(event.isNote):
+                        duration+=event.duration.quarterLength
+                    if(event.isRest):
+                        duration+=event.duration.quarterLength
+                except:
+                    pass
+        except:
+            pass
+        return str(duration)
+    
+    def correctIncorrectMeasuresArray(self,omr,incorrectMeasures):
+        '''
+        Iteractive method that detects if some measures have been wrong flagged as an error.
+        It is under development
+        '''
+        measures=omr.parts[0].getElementsByClass(stream.Measure)
+        if 0 in incorrectMeasures:
+            incorrectMeasures.remove(0)#Anacrusis
+            
+        for barNumber in incorrectMeasures:
+            if barNumber<len(measures)-1:
+                measure=measures[barNumber]
+                measureNext=measures[barNumber+1]
+                duration=self.getDuration(measure)
+                
+                if(float(duration)<=0):
+                    incorrectMeasures.remove(barNumber)
+                    self.correctIncorrectMeasuresArray(omr,incorrectMeasures)
+                    
+                if(measureNext!=None):
+                    durationNext=self.getDuration(measureNext)
+                    if(float(duration)+float(durationNext)==4):
+                        try:
+                            incorrectMeasures.remove(barNumber)
+                            incorrectMeasures.remove(barNumber+1)
+                        except:
+                            pass
+                        self.correctIncorrectMeasuresArray(omr,incorrectMeasures)
+        return incorrectMeasures
+    
+    
+    def filterExtraMeasures(self,omr):
+        '''
+        Function that removes empty measures
+        '''
+        sco=stream.Score()
+        s=stream.Part()
+        for measure in omr.parts[0].getElementsByClass(stream.Measure):
+            if measure.flat.duration.quarterLength>0:
+                s.append(measure)
+        sco.append(s)
+        return sco
+    
+    
+    def getIncorrectMeasureIndices(self,omr):
+        '''
+        Method that detect wrong measures using transitions
+        '''
+        arrChunks= self.getTransitions(omr)
+        measures=omr.parts[0].getElementsByClass(stream.Measure)
+        indexBar=0
+        FlagErrors=[]
+        barFrom=0
+        barTo=0
+        for chunk in arrChunks:
+            indexC=arrChunks.index(chunk)
+            chunkBefore=arrChunks[indexC-1]
+            if(indexC==0):
+                barFrom=0
+            else:
+                barFrom+=chunkBefore[1]  
+            barTo=chunk[1]+barFrom       
+            chunkMeasures=measures[barFrom:barTo]
+            quarterChunk=round(chunk[0]/2)
+            for measure in chunkMeasures:              
+                if measure.duration.quarterLength!=quarterChunk:
+                    FlagErrors.append(indexBar)
+                indexBar+=1
+        return FlagErrors
+    
+    def _filterTransitions(self,arrMeasureIndex):
+        '''
+        Inner method that removes false transitions in measures for detecting missing time signatures
+        '''
+        arrMeasureIndex2=[]
+        arrMeasureIndex.insert(0,0)
+        arrMeasureIndex.append(-1) 
+        for mes in arrMeasureIndex:
+            indexM=arrMeasureIndex.index(mes)
+            if indexM>0:
+                bars=arrMeasureIndex[indexM]-arrMeasureIndex[indexM-1]
+                if (bars>9):
+                    arrMeasureIndex2.append(mes)
+        arrMeasureIndex2.insert(0,0)
+        arrMeasureIndex2.append(-1) 
+        return arrMeasureIndex2
+   
+    def getTransitions(self,omr):
+        '''
+        Returns transitions in a omr
+        '''
+        MeasuresLength=[]
+        arrMeasureIndex_before=self._getTransitionBar(omr)
+        
+        arrMeasureIndex=self._filterTransitions(arrMeasureIndex_before)
+        for i in range(len(arrMeasureIndex)-1):
+            arrMeasureslength= self._getAverageQuavers(omr,arrMeasureIndex[i],arrMeasureIndex[i+1],False)
+            MeasuresLength.append(arrMeasureslength)
+
+        return MeasuresLength
+        
+    def _getTransitionBar(self,omr):
+        '''
+        Inner function for getting transitions
+        '''
+        arrOut=[]
+        measures=omr.parts[0].getElementsByClass(stream.Measure)
+        barsNumber=len(measures)
+        for barIndex in range(5,barsNumber):
+            averageBefore=self._getAverageQuavers(omr,barIndex-4,barIndex,isUntilTS=False)
+            averageAfter=self._getAverageQuavers(omr,barIndex,barIndex+2,isUntilTS=False)
+            if(abs(averageBefore[0]-averageAfter[0])>1.5):
+                if(measures[barIndex].duration.quarterLength<averageBefore[0]*2):#rest bars or missing bars
+                    arrOut.append(barIndex)
+        return arrOut
+        
+    def _getAverageQuavers(self,myStream,measureIndex,measureIndexEnd,isUntilTS):
+        '''
+        Inner function for getting the average quavers per measure in a group of measures
+        '''
+        quavers=0
+        barNumbers=0
+        averageQuavers=0
+        measures=myStream.parts[0].getElementsByClass(stream.Measure)
+        for bar in measures[measureIndex:measureIndexEnd]:
+            duration=bar.duration.quarterLength*2
+            barNumbers+=1
+            if(duration>0 and duration<10):
+                quavers+=duration
+                if isUntilTS:
+                    if (len(bar.getElementsByClass(meter.TimeSignature))>0):
+                        break
+        if barNumbers>0:   
+            averageQuavers=quavers/barNumbers
+        return averageQuavers,barNumbers
+    
+#**************************************************
+    def getPossibleBeamsErrors(self,omr):   
+        '''
+        Function that returns measure errors based on non conventional beaming
+        '''
+        arrErrors=[] 
+        measures=omr.parts[0].getElementsByClass(stream.Measure)
+        barsNumber=len(measures)
+        for i in range(barsNumber):
+            notes=measures[i].getElementsByClass(note.Note)
+            count=0
+            state=0
+            for n in notes:
+                if n.duration.quarterLength==0.25:
+                    bs=n.beams.getNumbers() 
+                    if(len(bs)>0):
+                        b=n.beams.getByNumber(bs[0])          
+                        if b.type=='start':
+                            count=1
+                            state=1
+                        if b.type=='continue':
+                            if(state==1 or state==2):
+                                count+=1
+                                state=2
+                        if b.type=='stop':
+                            if(state==1 or state==2):
+                                count+=1
+                                if count==3:
+                                    arrErrors.append(i)
+        arrErrors=list(set(arrErrors))
+        return arrErrors
+
+    def getPossibleLastNoteErrors(self,omr):   
+        '''
+        Function that returns measure errors based on the last notes rhythm
+        '''
+        arrErrors=[] 
+        measures=omr.parts[0].getElementsByClass(stream.Measure)
+        barsNumber=len(measures)
+        for i in range(barsNumber):
+            notes=measures[i].getElementsByClass(note.GeneralNote)
+            if(len(notes)>0):
+                lastNote=notes[len(notes)-1]
+                if lastNote.duration.quarterLength<=0.25:
+                    if(lastNote.isRest):
+                        arrErrors.append(i)
+                    else:
+                        bs=lastNote.beams.getNumbers() 
+                        
+                        if(len(bs)==0):
+                            if(len(notes)>2):
+                                noteBefore=notes[len(notes)-2]
+                                if noteBefore.duration.quarterLength+lastNote.duration.quarterLength!=1:
+                                    arrErrors.append(i)
+               
+            
+        return arrErrors
+    
+    def flagIncorrectMeasures(self,omr2):
+        '''
+        flag the incorrect measures from a omr using different methods
+        '''
+        mf=MeasureFunctions()
+        sc=omr.correctors.ScoreCorrector(omr2)
+        part=sc.getSinglePart(0)
+         
+        arrErrors=[]
+        im=part.getIncorrectMeasureIndices(runFast=False)
+        
+        im1= mf.getIncorrectMeasureIndices(omr2)
+        
+        im2= mf.getPossibleBeamsErrors(omr2)
+        im3= mf.getPossibleLastNoteErrors(omr2)
+       
+         
+        arrErrors.append(im)
+        arrErrors.append(im1)
+        arrErrors.append(im2)
+        arrErrors.append(im3)
+         
+        if(len(im)>15):
+            if(len(im1)<len(im)):
+                im=im1
+        imSum=list(set(im)|set(im2)|set(im3))
+        imSum=sorted(imSum)
+        imOK=mf.correctIncorrectMeasuresArray(omr2,imSum)  
+        print im
+        return imOK,arrErrors
+    
+    def getSlurs(self,part):
+        '''
+        Returns the slurs from one part
+        '''
+        slurs=part.flat.getElementsByClass(spanner.Spanner)
+        return slurs
+        
+    def reconstructScore(self,part,hashPart): 
+        '''
+        This function include rest measures in one part based on the hash part aligned
+        '''
+        partReconstructed=stream.Part()
+        barNumber=1
+        gaps=[]
+        for i in range(len(hashPart)): 
+            if hashPart[i]!="*":
+                m=part.getElementsByClass(stream.Measure)[barNumber-1]
+                partReconstructed.append(m)
+                barNumber+=1
+            else:
+                m=stream.Measure()
+                partReconstructed.append(m)
+                gaps.append(i)
+        slurs=self.getSlurs(part)   
+        partReconstructed.append(slurs)  
+        myStream=self.reorderMeasures(partReconstructed)  
+        return myStream,gaps
+    
+    def reorderMeasures(self,omr):
+        '''
+        Returns the correct measures number
+        '''
+        slurs=self.getSlurs(omr) 
+        s=stream.Part()
+        barNumber=1
+        for measure in omr.getElementsByClass(stream.Measure):
+            measure.number=barNumber
+            s.append(measure)
+            barNumber+=1
+        s.append(slurs)
+        return s      
+    
+    def getNoteByOffset(self,voice,offset):
+        '''
+        Returns one note in one voice according to the offset
+        '''
+        for n in voice.getElementsByClass(note.Note):
+            if n.offset==offset:
+                return n
+        for n in voice.getElementsByClass(chord.Chord):
+            if n.offset==offset:
+                return n
+    
+    def convertVoicesToChord(self,omr):
+        '''
+        Function that converts voices with the same rhythm to chords
+        '''
+        for part in omr.getElementsByClass(stream.Part):
+            for measure in part.getElementsByClass(stream.Measure):
+                measure.show('text')
+                voices=measure.getElementsByClass(stream.Voice)
+                if len(voices)>=2:
+                    for element1 in voices[0].getElementsByClass(note.GeneralNote):
+                        offset1= element1.offset
+                        for voice in voices:
+                            if voice!=voices[0]:
+                                element2=self.getNoteByOffset( voice, offset1)
+                                if element2 is not None and element1.duration.quarterLength==element2.duration.quarterLength and not isinstance(element1,note.Rest):
+                                    mychord=self.mergeChords(element1,element2)  
+                                    mychord.duration.quarterLength=element1.quarterLength
+                                    voices[0].replace(element1,mychord)
+                                    myrest=note.Rest()
+                                    myrest.duration.quarterLength=element1.quarterLength
+                                    voice.replace(element2,myrest)
+
+        omr=self.removeRestVoice(omr)
+        return omr
+    def removeRestVoice(self,omr):
+        '''
+        This option removes the rest voices (without notes) 
+        '''
+        for part in omr.getElementsByClass(stream.Part):
+            for measure in part.getElementsByClass(stream.Measure):
+                voices=measure.getElementsByClass(stream.Voice)
+                for voice in voices:
+                    myvoice = copy.deepcopy(voice)
+                    myvoice.removeByClass('Rest')
+                    if len(myvoice)==0:
+                        measure.remove(voice)
+        
+        return omr
+     
+    def mergeChords(self,element1,element2): 
+        '''
+        Returns one chord made by two elements, notes or chords
+        '''
+        notes1=[]
+        notes2=[]
+        notes=[]
+        if isinstance(element1,chord.Chord):
+            notes1=element1.pitches
+        else:
+            notes1.append(element1.pitch)
+        
+        if isinstance(element2,chord.Chord):
+            notes2=element2.pitches
+        else:
+            notes2.append(element2.pitch)
+        
+        for n in notes1:
+            notes.append(n)
+        for n in notes2:
+            notes.append(n)
+
+        mychord = chord.Chord(notes)
+        return mychord
+        
+    
+    def convertBeamsToTriplets(self,omr):  
+        '''
+        This option search for measures where the rhythm is bigger than the time signature
+        and tries to convert 3 note beams in triplets
+        '''
+        tsDefault=omr.flat.getElementsByClass('TimeSignature')[0]
+        idPart=0
+        for part in omr.getElementsByClass(stream.Part):
+            idPart+=1
+            idMeasure=0
+            for measure in part.getElementsByClass(stream.Measure):
+                idMeasure+=1
+                if self._IsMeasureHigherTS(idMeasure, part,tsDefault):
+                    print idMeasure
+                    measure=self.beamsToTriplets(measure)
+        return omr
+               
+                    
+    def _IsMeasureHigherTS(self,idMeasure,part,ts):
+        '''
+        Inner function that detects if one measure is bigger than the time signature
+        '''
+        idCountMeasure=0
+        for measure in part.getElementsByClass(stream.Measure):
+            idCountMeasure+=1
+            newts=measure.flat.getElementsByClass('TimeSignature')
+            if len(newts)>0:
+                ts=newts[0]
+            if idMeasure==idCountMeasure:
+                voices=measure.getElementsByClass(stream.Voice)
+                totalDuration=0
+                if len(voices)>=1:
+                    for voice in voices:
+                        totalDuration=0
+                        for element in voice.getElementsByClass(note.GeneralNote):
+                            totalDuration+=element.duration.quarterLength;
+                else:
+                    for element in measure.getElementsByClass(note.GeneralNote):
+                        totalDuration+=element.duration.quarterLength;
+                quartersBymeasure=ts.numerator*(4.0/ts.denominator)
+                if totalDuration>quartersBymeasure:
+                    return True
+                else:
+                    return False
+    def beamsToTriplets(self,measure):
+        '''
+        This function returns one measure changing 3 notes beamed by triplets
+        '''
+        voices=measure.getElementsByClass(stream.Voice)
+        if len(voices)>=1:
+            for voice in voices:
+                notes=voice.getElementsByClass(note.NotRest)
+                notes=self._notesToTriplets(notes)
+               
+        else:
+            notes=measure.getElementsByClass(note.NotRest)
+            notes=self._notesToTriplets(notes)
+                
+        return measure
+    
+    
+    def _notesToTriplets(self,notes):
+        '''
+        Gets one note arrays and tries to convert to triplets
+        '''
+
+        for i in range(len(notes)):
+            self._removeSlurs(notes[i])
+            beams1=notes[i].beams.beamsList
+            
+            try:
+                beams2=notes[i+1].beams.beamsList
+                beams3=notes[i+2].beams.beamsList
+                if len(beams1)>0 and len(beams2)>0 and len(beams3)>0:
+                    if beams1[0].type=='start' and beams2[0].type=='continue' and beams3[0].type=='stop':
+                        if notes[i].duration.quarterLength==notes[i+1].duration.quarterLength==notes[i+2].duration.quarterLength:
+                            mytype=notes[i].duration.type
+                            duration=note.duration.convertTypeToQuarterLength(mytype)
+                            realDuration=duration*2.0/3.0
+                            notes[i].duration.quarterLength=realDuration
+                            notes[i+1].duration.quarterLength=realDuration
+                            notes[i+2].duration.quarterLength=realDuration
+            
+            except:
+                pass
+        
+        return notes
+                            
+    def _removeSlurs(self,n): 
+        '''
+        Removes the slur in one note
+        '''
+        for element in n.getSites():
+            if isinstance(element,stream.SpannerStorage):
+                for e in element.elements:
+                    element.pop(0)   
+    
+    def removesEmptyVoices(self,omr):
+        '''
+        Removes empty voices (just rests)
+        '''
+        for part in omr.getElementsByClass(stream.Part):
+            for measure in part.getElementsByClass(stream.Measure):
+                print measure
+                voices=measure.getElementsByClass(stream.Voice)
+                if len(voices)>=1:
+                    print len(voices)
+                    for voice in voices:
+                        notes=voice.getElementsByClass(note.NotRest)
+                        if len(notes)==0:
+                            index=measure.index(voice)
+                            measure.pop(index)
+                voices=measure.getElementsByClass(stream.Voice)
+                if len(voices)==1:
+                    measure.flattenUnnecessaryVoices()
+        return omr
+    
+    def removesGaps(self,omr):
+        '''
+        This option changes gaps by rests in voices
+        '''
+        for part in omr.getElementsByClass(stream.Part):
+            for measure in part.getElementsByClass(stream.Measure):
+                voices=measure.getElementsByClass(stream.Voice)
+                if len(voices)>=1:
+                    for voice in voices:
+                        v=voice.findGaps()
+                        if v!=None:
+                            nextOffset=1000
+                            for element in voice.getElementsByClass(note.GeneralNote):
+                                offset= element.offset
+                                if offset>nextOffset:
+                                    rest=note.Rest()
+                                    rest.duration.quarterLength=offset-nextOffset
+                                    voice.insert(nextOffset,rest)
+                                    nextOffset=1000
+                                else:
+                                    duration=element.duration.quarterLength
+                                    nextOffset=offset+duration
+        return omr                    
+                        
+
Binary file Functions/MeasureFunctions.pyc has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Functions/__init__.py	Mon May 04 22:53:31 2015 +0200
@@ -0,0 +1,5 @@
+from AddingXMLSingleMeasures import AddingXMLSingleMeasures
+from FilesFunctions import FilesFunctions
+from HashFunctions import HashFunctions
+from MeasureFunctions import MeasureFunctions
+# from MultipleOMR.Alignment import NWunsch
Binary file Functions/__init__.pyc has changed