Mercurial > hg > multiomr
comparison Functions/HashFunctions.py @ 1:0f7f611deca4
Functions
author | Victor Padilla <victor.padilla.mc@gmail.com> |
---|---|
date | Mon, 04 May 2015 22:53:31 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:1eb6054a1f84 | 1:0f7f611deca4 |
---|---|
1 ''' | |
2 @organization: Lancaster University & University of Leeds | |
3 @version: 1.0 | |
4 Created on 11/12/2014 | |
5 | |
6 @author: Victor Padilla | |
7 @contact: v.padilla@lancaster.ac.uk | |
8 | |
9 Functions to manipulate and convert to Hash array | |
10 the scores. | |
11 | |
12 It is useful for aligning measures (not single notes) | |
13 | |
14 ''' | |
15 from music21 import stream | |
16 from music21 import note | |
17 from music21 import omr | |
18 # FastAlignmentArrays is a Cython library | |
19 from Alignment import FastAlignmentArrays | |
20 from MeasureFunctions import MeasureFunctions | |
21 | |
22 class HashFunctions: | |
23 | |
24 def alignHash(self,hashArray): | |
25 ''' | |
26 Returns two hash arrays aligned | |
27 | |
28 Uses the needleman-wunsh algorithm evaluating the differences between measures | |
29 ''' | |
30 faa=FastAlignmentArrays() | |
31 hashOrdered=faa.needleman_wunsch(hashArray[0], hashArray[1],False)[0] | |
32 hashArray[0]=hashOrdered[0] | |
33 hashArray[1]=hashOrdered[1] | |
34 return hashArray | |
35 | |
36 def getHash(self,OMR): | |
37 ''' | |
38 Returns hash from a group of OMR parts | |
39 ''' | |
40 print "...Obtaining Hash of measures..." | |
41 hashArray=[] | |
42 for i in range(len(OMR)): | |
43 hashArray.append(self.getHashArrayFromPart(OMR[i].parts[0])) | |
44 return hashArray | |
45 | |
46 def getGapsFromHashArray(self,hashArray): | |
47 ''' | |
48 Returns the gap index array from a group of hash parts | |
49 ''' | |
50 gapsArrays=[] | |
51 for i in range(len(hashArray)): | |
52 gapsArray=[] | |
53 for j in range(len(hashArray[i])): | |
54 symbol=hashArray[i][j] | |
55 if symbol=="*": | |
56 gapsArray.append(j) | |
57 | |
58 gapsArrays.append(gapsArray) | |
59 return gapsArrays | |
60 | |
61 | |
62 | |
63 | |
64 def reconstructHash(self,OMR,hashArray): | |
65 ''' | |
66 Returns a group of single parts ordered by hashArray (gaps) | |
67 ''' | |
68 scores=[] | |
69 gapsArr=[] | |
70 mf=MeasureFunctions() | |
71 for i in range(len(OMR)): | |
72 print len(hashArray[i]) | |
73 partReconstruct,gaps=mf.reconstructScore(OMR[i].parts[0], hashArray[i]) | |
74 sc=stream.Score() | |
75 sc.append(partReconstruct) | |
76 scores.append(sc) | |
77 gapsArr.append(gaps) | |
78 return scores,gapsArr | |
79 | |
80 | |
81 | |
82 | |
83 def removeHashGaps(self,partHash): | |
84 ''' | |
85 Removes gaps in a part | |
86 ''' | |
87 newHash=[] | |
88 for i in range(len(partHash)): | |
89 if(partHash[i]!="*"): | |
90 newHash.append(partHash[i]) | |
91 return newHash | |
92 | |
93 def addHashGaps(self,partHash,gaps): | |
94 ''' | |
95 Insert gaps in a part | |
96 ''' | |
97 for gap in gaps: | |
98 partHash.insert(gap,"") | |
99 return partHash | |
100 | |
101 | |
102 def getHashArrayFromPart(self,part): | |
103 ''' | |
104 get Hash string of a Part (music21) | |
105 ''' | |
106 hashArray=[] | |
107 lengthArray=len(part.getElementsByClass(stream.Measure)) | |
108 for i in range(lengthArray): | |
109 measure=part.getElementsByClass(stream.Measure)[i] | |
110 hashMeasure=self.getHashFromMeasure(measure) | |
111 pitchArr="" | |
112 for mynote in measure.flat.getElementsByClass(note.Note): | |
113 pitchArr+=str(mynote.pitch.name) | |
114 hashArray.append(pitchArr+"_"+hashMeasure) | |
115 return hashArray | |
116 | |
117 def getHashFromMeasure(self,measure): | |
118 ''' | |
119 get Hash string of a measure. Library correctors.py of Michael Scott Cuthbert. Project OMR | |
120 ''' | |
121 mh=omr.correctors.MeasureHash(measure).getHashString() | |
122 return mh | |
123 |