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