annotate Process/FullScoreAlignment.py @ 2:46fb79167a61 tip

Main Code
author Victor Padilla <victor.padilla.mc@gmail.com>
date Mon, 04 May 2015 22:56:18 +0200
parents
children
rev   line source
victor@2 1 '''
victor@2 2 Created on 10/11/2014
victor@2 3
victor@2 4 @organization: Lancaster University & University of Leeds
victor@2 5 @version: 1.0
victor@2 6 Created on 11/12/2014
victor@2 7
victor@2 8 @author: Victor Padilla
victor@2 9 @contact: v.padilla@lancaster.ac.uk
victor@2 10
victor@2 11 Functions related to the full score alignment.
victor@2 12 It takes the n parts and align with the best omr full score
victor@2 13 for the missing rest measuress
victor@2 14 '''
victor@2 15 from music21 import converter
victor@2 16 from music21 import stream
victor@2 17 from Functions import HashFunctions
victor@2 18 from Functions import FilesFunctions
victor@2 19
victor@2 20
victor@2 21 class FullScoreAlignment:
victor@2 22 '''
victor@2 23 Class for the full score alignment
victor@2 24
victor@2 25 usage:
victor@2 26
victor@2 27 fsa=FullScoreAlignment()
victor@2 28 idCompleteScoreBetter=fsa.getIdBetterOMRFullScore(fsOMRs,partsNumber)
victor@2 29 finalScore=fsa.runSynchronisingMeasuresNJ(subdirnameFullScore,subdirnameParts,fsOMRs[idCompleteScoreBetter])
victor@2 30 finalScore.write("musicxml", dirGeneral+'/finalScore.xml')
victor@2 31
victor@2 32 '''
victor@2 33 def runSynchronisingMeasuresNJ(self,subdirnameParts,completeScore):
victor@2 34 '''
victor@2 35 This function takes the different result.S2.xml files to construct
victor@2 36 the final output.
victor@2 37
victor@2 38 Example:
victor@2 39 fsa=FullScoreAlignment()
victor@2 40 idCompleteScoreBetter=fsa.getIdBetterOMRFullScore(fsOMRs,partsNumber)
victor@2 41 finalScore=fsa.runSynchronisingMeasuresNJ(subdirnameFullScore,subdirnameParts,fsOMRs[idCompleteScoreBetter])
victor@2 42 finalScore.write("musicxml", dirGeneral+'/finalScore.xml')
victor@2 43 '''
victor@2 44
victor@2 45 gapOMRs=[]
victor@2 46 parts=[]
victor@2 47 completeScoreParts=[]
victor@2 48
victor@2 49 # Convert to Hash the better full score
victor@2 50 hf=HashFunctions()
victor@2 51 for p in completeScore.getElementsByClass(stream.Part):
victor@2 52 sc=stream.Score()
victor@2 53 sc.append(p)
victor@2 54 completeScoreParts.append(sc)
victor@2 55 completeScoreHash=hf.getHash(completeScoreParts)
victor@2 56
victor@2 57 # Convert to hash the parts
victor@2 58 for dirname in sorted(subdirnameParts):
victor@2 59 print "--- ",dirname
victor@2 60 path = dirname+"/XML/"
victor@2 61 partId=int(dirname.split('\\')[-1])
victor@2 62 print partId
victor@2 63 resultS2File=path+ "result.S2.xml"
victor@2 64 resultS2=converter.parse(resultS2File,forceSource=False)
victor@2 65 parts.append(resultS2)
victor@2 66 partsHash=hf.getHash(parts)
victor@2 67
victor@2 68
victor@2 69 for partHash in partsHash:
victor@2 70 index=partsHash.index(partHash)
victor@2 71 h=[]
victor@2 72 h.append(partHash)
victor@2 73 h.append(completeScoreHash[index])
victor@2 74 hf.alignHash(h)
victor@2 75 partsHash[index]=h[0]
victor@2 76 completeScoreHash[index]=h[1]
victor@2 77
victor@2 78 gaps=hf.getGapsFromHashArray(h)
victor@2 79 gapOMRs.append(gaps)
victor@2 80 if(index<len(partsHash)-1):
victor@2 81 nextPart=completeScoreHash[index+1]
victor@2 82 for gap in gaps[1]:
victor@2 83 nextPart.insert(gap,"*")
victor@2 84
victor@2 85 print "--------------trace back-----------"
victor@2 86 print gapOMRs
victor@2 87 sc=stream.Score()
victor@2 88 gaps=gapOMRs[len(partsHash)-1][1]
victor@2 89 for i in range(len(partsHash)-1):
victor@2 90 completeScoreHash[i]=hf.removeHashGaps(completeScoreHash[i])
victor@2 91 completeScoreHash[i]=hf.addHashGaps(completeScoreHash[i],gaps)
victor@2 92 partsHash[i]=hf.removeHashGaps(partsHash[i])
victor@2 93 h=[]
victor@2 94 h.append(partsHash[i])
victor@2 95 h.append(completeScoreHash[i])
victor@2 96
victor@2 97 h=hf.alignHash(h)
victor@2 98 partsHash[i]=h[0]
victor@2 99 completeScoreHash[i]=h[1]
victor@2 100
victor@2 101 print "--------------reconstruct scores-----------"
victor@2 102
victor@2 103 streams,gaps=hf.reconstructHash(parts,partsHash)
victor@2 104 for s in streams:
victor@2 105 part=s.getElementsByClass(stream.Part)[0]
victor@2 106 sc.append(part)
victor@2 107 return sc
victor@2 108
victor@2 109
victor@2 110
victor@2 111 def getIdBetterOMRFullScore(self,fsOMRs_files,partsNumber):
victor@2 112 '''
victor@2 113 Tries to obtain the best full omr score
victor@2 114 based on the parts and the length.
victor@2 115
victor@2 116 Omrs increase the part numbers and add new rest measures
victor@2 117 to synchronize
victor@2 118
victor@2 119 usage:
victor@2 120 # fsOMRs is an array with the full score omrs
victor@2 121 # for string quartet, 4 parts
victor@2 122 idCompleteScoreBetter=fsa.getIdBetterOMRFullScore(fsOMRs,4)
victor@2 123
victor@2 124
victor@2 125 '''
victor@2 126 print "---------Calculating better OMR for alignment------------"
victor@2 127 ff=FilesFunctions()
victor@2 128 measuresLengthBetter=1000000 #maximum
victor@2 129 partsLengthBetter=1000 #maximum
victor@2 130 idCompleteScoreBetter=1000 #maximum
victor@2 131 for cs_file in fsOMRs_files:
victor@2 132 cs=ff.getOMR(cs_file)
victor@2 133 if cs!=[]:
victor@2 134 try:
victor@2 135 isEqualParts=False
victor@2 136 isLessMeasures=False
victor@2 137 isBetterOMR=False
victor@2 138 idCompleteScore=fsOMRs_files.index(cs_file)
victor@2 139 measuresLength=len(cs.getElementsByClass(stream.Part)[0].getElementsByClass(stream.Measure))
victor@2 140 partsLength=len(cs.getElementsByClass(stream.Part))
victor@2 141 print idCompleteScore,measuresLength,partsLength
victor@2 142
victor@2 143 if(partsLength==partsNumber):
victor@2 144 isEqualParts=True
victor@2 145 if(measuresLength<measuresLengthBetter):
victor@2 146 isLessMeasures=True
victor@2 147
victor@2 148 if isLessMeasures and isEqualParts:
victor@2 149 isBetterOMR=True
victor@2 150 if isBetterOMR:
victor@2 151 idCompleteScoreBetter=idCompleteScore
victor@2 152 measuresLengthBetter=measuresLength
victor@2 153 partsLengthBetter=partsLength
victor@2 154 except:
victor@2 155 print "error OMR"
victor@2 156 print idCompleteScoreBetter,measuresLengthBetter,partsLengthBetter
victor@2 157 return idCompleteScoreBetter
victor@2 158
victor@2 159
victor@2 160
victor@2 161
victor@2 162