victor@1: ''' victor@1: @organization: Lancaster University & University of Leeds victor@1: @version: 1.0 victor@1: Created on 11/12/2014 victor@1: victor@1: @author: Victor Padilla victor@1: @contact: v.padilla@lancaster.ac.uk victor@1: victor@1: Functions to manipulate and convert to Hash array victor@1: the scores. victor@1: victor@1: It is useful for aligning measures (not single notes) victor@1: victor@1: ''' victor@1: from music21 import stream victor@1: from music21 import note victor@1: from music21 import omr victor@1: # FastAlignmentArrays is a Cython library victor@1: from Alignment import FastAlignmentArrays victor@1: from MeasureFunctions import MeasureFunctions victor@1: victor@1: class HashFunctions: victor@1: victor@1: def alignHash(self,hashArray): victor@1: ''' victor@1: Returns two hash arrays aligned victor@1: victor@1: Uses the needleman-wunsh algorithm evaluating the differences between measures victor@1: ''' victor@1: faa=FastAlignmentArrays() victor@1: hashOrdered=faa.needleman_wunsch(hashArray[0], hashArray[1],False)[0] victor@1: hashArray[0]=hashOrdered[0] victor@1: hashArray[1]=hashOrdered[1] victor@1: return hashArray victor@1: victor@1: def getHash(self,OMR): victor@1: ''' victor@1: Returns hash from a group of OMR parts victor@1: ''' victor@1: print "...Obtaining Hash of measures..." victor@1: hashArray=[] victor@1: for i in range(len(OMR)): victor@1: hashArray.append(self.getHashArrayFromPart(OMR[i].parts[0])) victor@1: return hashArray victor@1: victor@1: def getGapsFromHashArray(self,hashArray): victor@1: ''' victor@1: Returns the gap index array from a group of hash parts victor@1: ''' victor@1: gapsArrays=[] victor@1: for i in range(len(hashArray)): victor@1: gapsArray=[] victor@1: for j in range(len(hashArray[i])): victor@1: symbol=hashArray[i][j] victor@1: if symbol=="*": victor@1: gapsArray.append(j) victor@1: victor@1: gapsArrays.append(gapsArray) victor@1: return gapsArrays victor@1: victor@1: victor@1: victor@1: victor@1: def reconstructHash(self,OMR,hashArray): victor@1: ''' victor@1: Returns a group of single parts ordered by hashArray (gaps) victor@1: ''' victor@1: scores=[] victor@1: gapsArr=[] victor@1: mf=MeasureFunctions() victor@1: for i in range(len(OMR)): victor@1: print len(hashArray[i]) victor@1: partReconstruct,gaps=mf.reconstructScore(OMR[i].parts[0], hashArray[i]) victor@1: sc=stream.Score() victor@1: sc.append(partReconstruct) victor@1: scores.append(sc) victor@1: gapsArr.append(gaps) victor@1: return scores,gapsArr victor@1: victor@1: victor@1: victor@1: victor@1: def removeHashGaps(self,partHash): victor@1: ''' victor@1: Removes gaps in a part victor@1: ''' victor@1: newHash=[] victor@1: for i in range(len(partHash)): victor@1: if(partHash[i]!="*"): victor@1: newHash.append(partHash[i]) victor@1: return newHash victor@1: victor@1: def addHashGaps(self,partHash,gaps): victor@1: ''' victor@1: Insert gaps in a part victor@1: ''' victor@1: for gap in gaps: victor@1: partHash.insert(gap,"") victor@1: return partHash victor@1: victor@1: victor@1: def getHashArrayFromPart(self,part): victor@1: ''' victor@1: get Hash string of a Part (music21) victor@1: ''' victor@1: hashArray=[] victor@1: lengthArray=len(part.getElementsByClass(stream.Measure)) victor@1: for i in range(lengthArray): victor@1: measure=part.getElementsByClass(stream.Measure)[i] victor@1: hashMeasure=self.getHashFromMeasure(measure) victor@1: pitchArr="" victor@1: for mynote in measure.flat.getElementsByClass(note.Note): victor@1: pitchArr+=str(mynote.pitch.name) victor@1: hashArray.append(pitchArr+"_"+hashMeasure) victor@1: return hashArray victor@1: victor@1: def getHashFromMeasure(self,measure): victor@1: ''' victor@1: get Hash string of a measure. Library correctors.py of Michael Scott Cuthbert. Project OMR victor@1: ''' victor@1: mh=omr.correctors.MeasureHash(measure).getHashString() victor@1: return mh victor@1: