victor@2: ''' victor@2: @organization: Lancaster University & University of Leeds victor@2: @version: 1.0 victor@2: Created on 11/12/2014 victor@2: victor@2: @author: Victor Padilla victor@2: @contact: v.padilla@lancaster.ac.uk victor@2: victor@2: The application can be run through command line (without windows interface) using victor@2: MainMultiOMR library victor@2: victor@2: ''' victor@2: import logging victor@2: import time victor@2: import os victor@2: import gc victor@2: import sys victor@2: from subprocess import Popen victor@2: from music21 import stream victor@2: from music21 import converter victor@2: from Process import PipelineAlignment victor@2: from Process import Voting victor@2: from Process import SymbolConversion victor@2: from Process import FullScoreAlignment victor@2: from Automatism import BatchOMR victor@2: from Result import ProcessGroundS2 victor@2: from Result import ExcellData victor@2: from Functions import FilesFunctions victor@2: from Functions import MeasureFunctions victor@2: from Alignment import NWunsch victor@2: import shutil victor@2: victor@2: from Preprocessing import ossia victor@2: from Preprocessing import movements victor@2: from Preprocessing import preomr victor@2: import PythonMagick victor@2: import PyPDF2 victor@2: victor@2: class MainMultiOMR: victor@2: ''' victor@2: main functions to run the Big Data Process. victor@2: They can be called without interface, from another program, for example victor@2: .... victor@2: # instance victor@2: mmOMR=MainMultiOMR() victor@2: victor@2: # run the Big Data process once victor@2: mmOMR.runBigData("C:\\Users\\victor\\Desktop\\data") victor@2: victor@2: # run the Big Data waiting for changes victor@2: mmOMR.runDummyBigData("C:\\Users\\victor\\Desktop\\data") victor@2: victor@2: # run the Big Data Ground process once to get the results victor@2: mmOMR.runBigDataGroung("C:\\Users\\victor\\Desktop\\data") victor@2: victor@2: # run the Big Data Ground waiting for changes victor@2: mmOMR.runDummyBigData("C:\\Users\\victor\\Desktop\\data") victor@2: ''' victor@2: victor@2: def _loadNWunsch(self): victor@2: ''' victor@2: Load Needlemann-Wunsch algorith library by default victor@2: ''' victor@2: a=["foo00"] victor@2: b=["foo000"] victor@2: NWunsch.NWunsch_getSimilarity(a,b) victor@2: #print "Default NWunsch......" victor@2: victor@2: victor@2: def processPDF2TIFF(self,filename): victor@2: pdf_im = PyPDF2.PdfFileReader(file(filename, "rb")) victor@2: npage = pdf_im.getNumPages() victor@2: print('Converting %d pages.' % npage) victor@2: intMovement=0 victor@2: for p in range(npage): victor@2: im = PythonMagick.Image() victor@2: im.density('300') victor@2: print filename victor@2: im.read(str(filename)+'[' + str(p) +']') victor@2: strOut=str(filename) + str(p) victor@2: im.write(strOut+ '.tif') victor@2: victor@2: infile=strOut+ '.tif' victor@2: po = preomr.PreOMR(infile) victor@2: rv = po.split_movements(strOut+'_A.tif', strOut+'_B.tif') victor@2: if rv != None: victor@2: print("new movement was detected at system %d" % (rv,)) victor@2: victor@2: arrFile=filename.split("\\") victor@2: myFile=arrFile.pop() victor@2: path='\\'.join(arrFile) victor@2: print "**********************",path victor@2: if rv==0: victor@2: intMovement+=1 victor@2: dirMovement=path+"\\m"+str(intMovement) victor@2: os.makedirs(dirMovement) victor@2: shutil.copy(strOut+ '.tif', dirMovement+"\\page_"+str(p)+".tif") victor@2: else: victor@2: dirMovement=path+"\\m"+str(intMovement) victor@2: shutil.copy(strOut+ '_A.tif', dirMovement+"\\page_"+str(p)+".tif") victor@2: intMovement+=1 victor@2: dirMovement=path+"\\m"+str(intMovement) victor@2: os.makedirs(dirMovement) victor@2: shutil.copy(strOut+ '_B.tif', dirMovement+"\\page_"+str(p)+".tif") victor@2: victor@2: else: victor@2: shutil.copy(strOut+ '.tif', dirMovement+"\\page_"+str(p)+".tif") victor@2: victor@2: victor@2: victor@2: victor@2: def processOssia(self,rootDir): victor@2: ############################################### victor@2: # @param dirGeneral root movement folder victor@2: for dirname, dirnames, filenames in os.walk(rootDir): victor@2: for f in filenames: victor@2: fname_path = os.path.join(dirname, f) victor@2: if f.endswith(".tif"): victor@2: print fname_path victor@2: victor@2: ossia.process(fname_path, dirname+"\\_"+f) victor@2: # rootApp=os.path.dirname(sys.argv[0]) victor@2: # rootApp=rootApp.replace("\\","/") victor@2: # cwd="python.exe "+rootApp+"/Preprocessing/ossia.py "+fname_path+" "+fname_path victor@2: # victor@2: # print cwd victor@2: # p = Popen(cwd) victor@2: # p.wait() victor@2: # p.communicate() victor@2: # victor@2: victor@2: victor@2: def processMovement(self,dirGeneral): victor@2: ''' victor@2: Process just one movement. The structure should be victor@2: .... victor@2: \\movementName\\fullScore\\XML\\(the .xml files) victor@2: \\parts\\0\\ victor@2: \\1\\ victor@2: \\2\\XML\\(the .xml files) victor@2: victor@2: the result is \\movementName\\finalScore.xml victor@2: victor@2: usage: victor@2: mmOMR=MainMultiOMR() victor@2: mmOMR.processMovement("C:\Users\victor\Desktop\data\k458\Process\m2") victor@2: ''' victor@2: victor@2: victor@2: ff=FilesFunctions() victor@2: subdirnameParts=ff.SubDirPath(dirGeneral+"\\Parts\\") victor@2: fsOMRs_files=ff.getFiles(dirGeneral+"\\fullScore\\XML\\") victor@2: for dirname in subdirnameParts: victor@2: d=dirname+"/XML/" victor@2: urlSplit=dirname.split("\\") victor@2: part=int(urlSplit[-1]) victor@2: partOMRs_files=ff.getFiles(d) victor@2: print "---------S2------------" victor@2: logging.warning("Part:"+str(part)+" "+d) victor@2: self.setResultS2(d,part,fsOMRs_files,partOMRs_files) victor@2: victor@2: print "---------Synchronising scores and parts------------" victor@2: logging.warning("---------Synchronising scores and parts------------:"+dirGeneral) victor@2: #fsOMRs=ff.getOMRs(dirGeneral+"\\fullScore\\XML\\") victor@2: self.__runSynchroScoresAndParts(dirGeneral,fsOMRs_files) victor@2: victor@2: ############################################### victor@2: # @param path folder where the .xml files are victor@2: # @param idPart part number victor@2: # @param fsOMRs array with the full score files processed by music21 victor@2: # @param partOMRs array with the part files processed by music21 victor@2: def setResultS2(self,path,idPart,fsOMRs,partOMRs): victor@2: ''' victor@2: Takes the fullScores (processing the idPart) and parts. victor@2: It writes the result (result.S2.xml) in the dirname victor@2: victor@2: This function process each part independently victor@2: victor@2: usage: victor@2: victor@2: mmOMR=MainMultiOMR() victor@2: ff=FilesFunctions() victor@2: fsOMRs=ff.getOMRs("C:\\Users\\victor\\Desktop\\data\\k458_test\\Process\\m2\\fullScore\\XML") victor@2: partOMRs=ff.getOMRs("C:\\Users\\victor\\Desktop\\data\\k458_test\\Process\\m2\\parts\\0\\XML") victor@2: d="C:\\Users\\victor\\Desktop\\data\\k458_test\\Process\\m2\\parts\\0\\XML" victor@2: mmOMR.setResultS2(d,0,fsOMRs,partOMRs) victor@2: ''' victor@2: victor@2: victor@2: victor@2: pa=PipelineAlignment() victor@2: vote=Voting() victor@2: sc=SymbolConversion() victor@2: ff=FilesFunctions() victor@2: omr_symbolsAlign,betterOmrIds=pa.alignNJ_files(idPart,fsOMRs,partOMRs) victor@2: #The .txt with the OMRs involved (tree) victor@2: ff.writeText(path,betterOmrIds) victor@2: #voting victor@2: victor@2: outVote=vote.vote(omr_symbolsAlign) victor@2: #apply voices if it is needed victor@2: victor@2: outVote=sc.setVoices(outVote) victor@2: #convert to music21 victor@2: resultS2=sc.convertM21(outVote) victor@2: mf=MeasureFunctions() victor@2: #remove blank measures due to the alignment victor@2: resultS2_clean=mf.filterExtraMeasures(resultS2) victor@2: resultS2_clean.write("musicxml", path+'/result.S2.xml') victor@2: victor@2: victor@2: ############################################### victor@2: # @param dirGeneral folder where is the root of the movement victor@2: # @param fsOMRs array with the full score files processed by music21 victor@2: def __runSynchroScoresAndParts(self,dirGeneral,fsOMRs_files): victor@2: ''' victor@2: Takes the fullScores and part files generated to align the final full score victor@2: - dirGeneral is the movement URL victor@2: - fsOMRs is an array with the full scores OMRs processed by music21 victor@2: ''' victor@2: ff=FilesFunctions() victor@2: subdirnameParts=ff.SubDirPath(dirGeneral+"\\parts\\") victor@2: partsNumber=len(subdirnameParts) victor@2: fsa=FullScoreAlignment() victor@2: idCompleteScoreBetter=fsa.getIdBetterOMRFullScore(fsOMRs_files,partsNumber) victor@2: betterFsOMR=ff.getOMR(fsOMRs_files[idCompleteScoreBetter]) victor@2: finalScore=fsa.runSynchronisingMeasuresNJ(subdirnameParts,betterFsOMR) victor@2: betterFsOMR=None victor@2: finalScore.write("musicxml", dirGeneral+'/finalScore.xml') victor@2: finalScore=None victor@2: gc.collect() victor@2: print "----" victor@2: victor@2: victor@2: victor@2: ############################################### victor@2: # @param dirGeneral root movement folder victor@2: def processMovementGround(self,dirGeneral): victor@2: ''' victor@2: Process the result. Just one movement. It takes each OMR file and compare against the ground. victor@2: The differences are written in .XML and .xls victor@2: victor@2: .... victor@2: \\movementName\\fullScore\\XML\\(the .xml files) victor@2: \\parts\\0\\ victor@2: \\1\\ victor@2: \\2\\XML\\(the .xml files) victor@2: victor@2: the file finalScore.xml should be in \\movementName\\finalScore.xml victor@2: the file ground.xml should be in \\movementName\\ground.xml victor@2: victor@2: The final result is written in victor@2: \\movementName\\parts\\resultGeneral.xlsx victor@2: \\fullScore_errors.xml victor@2: victor@2: usage: victor@2: mmOMR=MainMultiOMR() victor@2: mmOMR.processMovementGround("C:\Users\victor\Desktop\data\k458\Process\m2") victor@2: ''' victor@2: percentagesArray=[] victor@2: betterOMRIds=[] victor@2: ff=FilesFunctions() victor@2: subdirname=ff.SubDirPath(dirGeneral+"\\parts\\") victor@2: filesFull=ff.getFiles(dirGeneral+"\\fullScore\\XML\\") victor@2: victor@2: ground=ff.getGround(dirGeneral) victor@2: groundparsed=converter.parse(ground, forceSource=True) victor@2: finalScore=ff.getFinalScore(dirGeneral) victor@2: finalScoreparsed=converter.parse(finalScore, forceSource=True) victor@2: victor@2: victor@2: for dirname in subdirname: victor@2: d=dirname+"/XML/" victor@2: urlSplit=dirname.split("\\") victor@2: part=int(urlSplit[-1]) victor@2: filesPart=ff.getFiles(d) victor@2: files=filesPart+filesFull victor@2: victor@2: print files victor@2: pg=ProcessGroundS2() victor@2: ErrorsMatrix=[] victor@2: percentages=[] victor@2: victor@2: OMRs=[] victor@2: OMRs.append(groundparsed) victor@2: OMRs.append(finalScoreparsed) victor@2: victor@2: victor@2: victor@2: percentage,errors,scoreWithErrors= pg.getSimilarity(OMRs,part) victor@2: ErrorsMatrix.append(errors) victor@2: percentages.append(percentage) victor@2: if not os.path.exists(dirname+"\\Result"): victor@2: os.makedirs(dirname+"\\Result") victor@2: victor@2: scoreWithErrors.write("musicxml", dirname+"\\Result\\result.S2.xml") victor@2: victor@2: for i in range(len(files)): victor@2: try: victor@2: OMRs[1]=ff.getOMR(files[i]) victor@2: percentage,errors,scoreWithErrors= pg.getSimilarity(OMRs,part) victor@2: ErrorsMatrix.append(errors) victor@2: percentages.append(percentage) victor@2: scoreWithErrors.write("musicxml", dirname+"\\Result\\"+os.path.basename(files[i])) victor@2: except: victor@2: print "ERROR OMR READING" victor@2: ErrorsMatrix.append("ERROR OMR READING") victor@2: percentages.append(0) victor@2: victor@2: victor@2: f=open(d+"betterOMR.txt","r") victor@2: betterOMRId=f.readlines() victor@2: f.close() victor@2: betterOMRIds.append(betterOMRId) victor@2: print "betterOMRIds",betterOMRIds victor@2: ed=ExcellData() victor@2: files.insert(0,dirname+"\\Result\\result.S2.xml" ) victor@2: print files victor@2: ed.saveData(ErrorsMatrix,files,percentages) victor@2: percentagesArray.append(percentages) victor@2: victor@2: victor@2: ed=ExcellData() victor@2: ed.saveGlobalData(percentagesArray,dirGeneral,betterOMRIds,files) victor@2: self.__joinErrorParts(dirGeneral) victor@2: print "----------- END ------------" victor@2: victor@2: ################################################################ victor@2: # @param rootDir root folder where all the scores to process are victor@2: def runBigData(self,rootDir): victor@2: ''' victor@2: Checks if there is any folder to process victor@2: If the folder "Process" is written, but the "finalScore.xml" is not, victor@2: runs the procedure victor@2: victor@2: usage victor@2: victor@2: mmOMR=MainMultiOMR() victor@2: d="C:\\Users\\victor\\Desktop\\cach" victor@2: mmOMR.runBigData(d) victor@2: ''' victor@2: ff=FilesFunctions() victor@2: for outWalk in os.walk(rootDir): victor@2: dirName=outWalk[0] victor@2: arrDir=dirName.split("\\") victor@2: if arrDir[-1]=="Process": victor@2: movements=ff.SubDirPath(dirName) victor@2: for movement in movements: victor@2: print movement victor@2: if not os.path.isfile(movement+"\\finalScore.xml"): victor@2: logging.warning("Movement:"+movement+" "+rootDir) victor@2: self.processMovement(movement) victor@2: victor@2: ################################################################ victor@2: # @param rootDir root folder where all the scores to process are victor@2: def runLoopBigData(self,rootDir,adaptOMRs=False): victor@2: ''' victor@2: Infinity loop for processing data victor@2: Checks if there is any folder to process victor@2: If the folder "Process" is written, but the "finalScore.xml" is not, victor@2: runs the procedure victor@2: victor@2: usage victor@2: victor@2: mmOMR=MainMultiOMR() victor@2: d="C:\\Users\\victor\\Desktop\\cach" victor@2: mmOMR.runLoopBigData(d) victor@2: ''' victor@2: batchOMR=BatchOMR() victor@2: ff=FilesFunctions() victor@2: for outWalk in os.walk(rootDir): victor@2: dirName=outWalk[0] victor@2: subdirList=outWalk[1] victor@2: if "OMRS" in subdirList: victor@2: rootScore=dirName victor@2: omrFinished=True victor@2: scoreFinished=True victor@2: for outWalk2 in os.walk(rootScore): victor@2: dirName2=outWalk2[0] victor@2: files= ff.getAllImgFiles(dirName2) victor@2: omrs=ff.getAllXMLFiles(dirName2) victor@2: if len(files)>0 and len(omrs)<4: victor@2: omrFinished=False victor@2: victor@2: if os.path.exists(rootScore+"\\Process"): victor@2: movements=ff.SubDirPath(rootScore+"\\Process") victor@2: for movement in movements: victor@2: if not os.path.isfile(movement+"\\finalScore.xml"): victor@2: scoreFinished=False victor@2: else: victor@2: scoreFinished=False victor@2: victor@2: victor@2: if omrFinished==True and scoreFinished==False: victor@2: print "---- CONFIGURE SCORES ---" victor@2: batchOMR.setupApp(rootScore) victor@2: print "---- CONFIGURE GROUND ---" victor@2: batchOMR.setGround(rootScore) victor@2: if adaptOMRs: victor@2: print "---- ADAPT OMRs ---" victor@2: self.runAdaptAllOMRs(rootScore+"\\Process") victor@2: print "---- RUN BIG DATA ---" victor@2: self.runBigData(rootScore) victor@2: print "Waiting for processing..." victor@2: time.sleep(5) victor@2: self.runLoopBigData(rootDir,adaptOMRs) victor@2: victor@2: victor@2: ################################################################ victor@2: # @param rootDir root folder where all the scores to process are victor@2: def runBigDataGround(self,rootDir): victor@2: ''' victor@2: Checks if there is any folder to process to get the result victor@2: If "resultGeneral.xlsx" is not written and "finalScore.xml" and "ground.xml" are in the movement root dir, victor@2: launches the process victor@2: victor@2: usage victor@2: victor@2: mmOMR=MainMultiOMR() victor@2: d="C:\\Users\\victor\\Desktop\\data" victor@2: mmOMR.runBigDataGround(d) victor@2: ''' victor@2: ff=FilesFunctions() victor@2: for outWalk in os.walk(rootDir): victor@2: dirName=outWalk[0] victor@2: arrDir=dirName.split("\\") victor@2: if arrDir[-1]=="Process": victor@2: movements=ff.SubDirPath(dirName) victor@2: for movement in movements: victor@2: if not os.path.isfile(movement+"\\parts\\resultGeneral.xlsx"): victor@2: if os.path.isfile(movement+"\\finalScore.xml") and os.path.isfile(movement+"\\ground.xml") : victor@2: self.processMovementGround(movement) victor@2: victor@2: ################################################################ victor@2: # @param rootDir root folder where all the scores to process are victor@2: def runFinalXLS(self,rootDir): victor@2: ''' victor@2: Checks if there is any folder to process to get the result victor@2: If "resultGeneral.xlsx" is not written and "finalScore.xml" and "ground.xml" are in the movement root dir, victor@2: launches the process victor@2: victor@2: usage victor@2: victor@2: mmOMR=MainMultiOMR() victor@2: d="C:\\Users\\victor\\Desktop\\data" victor@2: mmOMR.runBigDataGround(d) victor@2: ''' victor@2: ed=ExcellData() victor@2: files=0 victor@2: S2_sum=0 victor@2: CP_sum=0 victor@2: PS_sum=0 victor@2: SE_sum=0 victor@2: SS_sum=0 victor@2: ff=FilesFunctions() victor@2: for outWalk in os.walk(rootDir): victor@2: dirName=outWalk[0] victor@2: arrDir=dirName.split("\\") victor@2: if arrDir[-1]=="Process": victor@2: movements=ff.SubDirPath(dirName) victor@2: for movement in movements: victor@2: if os.path.isfile(movement+"\\parts\\resultGeneral.xlsx"): victor@2: files=files+1 victor@2: s2,cp,ps,se,ss=ed.processResultGeneral(movement+"\\parts\\resultGeneral.xlsx") victor@2: S2_sum=S2_sum+s2 victor@2: CP_sum=CP_sum+cp victor@2: PS_sum=PS_sum+ps victor@2: SE_sum=SE_sum+se victor@2: SS_sum=SS_sum+ss victor@2: ed.writeFinalXLS(rootDir,S2_sum/files,CP_sum/files,PS_sum/files,SE_sum/files,SS_sum/files) victor@2: victor@2: ################################################################ victor@2: # @param rootDir root folder where all the scores to process are victor@2: def runLoopBigDataGround(self,rootDir): victor@2: ''' victor@2: Infinity loop for getting the results victor@2: victor@2: If "resultGeneral.xlsx" is not written and "finalScore.xml" and "ground.xml" are in the movement root dir, victor@2: launches the process victor@2: victor@2: usage victor@2: victor@2: mmOMR=MainMultiOMR() victor@2: d="C:\\Users\\victor\\Desktop\\data" victor@2: mmOMR.runLoopBigDataGround(d) victor@2: ''' victor@2: self.runBigDataGround(rootDir) victor@2: print "Waiting for results..." victor@2: time.sleep(5) victor@2: self.runLoopBigDataGround(rootDir) victor@2: victor@2: victor@2: ################################################################ victor@2: # @param dirGeneral root folder of the movement victor@2: victor@2: def __joinErrorParts(self,dirGeneral): victor@2: ''' victor@2: Joins the different result.S2.xml error in a single file. victor@2: The output file is "fullScore_errors.xml" victor@2: victor@2: The function tries to find the file "parts\\[idMovement]\\Result\\result.S2.xml" in each part victor@2: ''' victor@2: ff=FilesFunctions() victor@2: subdirname=ff.SubDirPath(dirGeneral+"\\parts\\") victor@2: fullScore=stream.Score() victor@2: for dirname in subdirname: victor@2: d=dirname+"\\Result\\" victor@2: urlSplit=dirname.split("\\") victor@2: part=int(urlSplit[-1]) victor@2: f=d+"\\result.S2.xml" victor@2: omr=converter.parse(f, forceSource=True) victor@2: part=omr.getElementsByClass(stream.Part)[0] victor@2: fullScore.append(part) victor@2: fullScore.write("musicxml", dirGeneral+"\\parts\\fullScore_errors.xml") victor@2: victor@2: victor@2: ############################################### victor@2: # @param rootDir root folder of the general data victor@2: def runCompleteProcess(self,rootDir): victor@2: ''' victor@2: Instead of working in parallel, this function launches a serial process with the different victor@2: steps involved victor@2: 1.- SIKULI AUTOMATISM victor@2: 2.- SETUP APPLICATION (copying files) victor@2: 3.- CONVERT GROUND (taking the .krn and convert to ground.xml) victor@2: 4.- RUN MULTIOMR BIG DATA victor@2: 5.- RUN GET RESULT BIG DATA victor@2: victor@2: victor@2: usage: victor@2: mmOMR=MainMultiOMR() victor@2: d="C:\\Users\\victor\\Desktop\\data" victor@2: mmOMR.runCompleteProcess(d) victor@2: ''' victor@2: batchOMR=BatchOMR() victor@2: logging.basicConfig(filename=rootDir+'\\BigDataLogs.log',format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') victor@2: print "-------SIKULI AUTOMATISM----------" victor@2: logging.warning("---SIKULI AUTOMATISM--- "+rootDir) victor@2: batchOMR.processAllTiffFiles(rootDir,"ALL") victor@2: print "-------SETUP APPLICATION----------" victor@2: logging.warning("---SETUP APPLICATION--- "+rootDir) victor@2: batchOMR.setupApp(rootDir) victor@2: print "-------CONVERT GROUND----------" victor@2: logging.warning("---CONVERT GROUND--- "+rootDir) victor@2: batchOMR.setGround(rootDir) victor@2: logging.warning("------RUN MULTIOMR BIG DATA--- "+rootDir) victor@2: print "-------RUN MULTIOMR BIG DATA----------" victor@2: self.runBigData(rootDir) victor@2: print "-------RUN GET RESULT BIG DATA----------" victor@2: self.runBigDataGround(rootDir) victor@2: victor@2: ############################################### victor@2: # @param filename file to convert victor@2: def runConvertKrnToMusicXML(self,filename): victor@2: ''' victor@2: converts a .krn file to .xml using music21 victor@2: victor@2: usage: victor@2: mmOMR=MainMultiOMR() victor@2: file="C:\\Users\\victor\\Desktop\\data\\k458\\OMRS\\m2\\k458.krn" victor@2: mmOMR.runConvertKrnToMusicXML(file) victor@2: ''' victor@2: omr=converter.parse(filename) victor@2: omr.write("musicxml", filename+'.xml') victor@2: victor@2: ############################################### victor@2: # @param filename file to convert victor@2: def runConvertMidiToMusicXML(self,filename): victor@2: ''' victor@2: converts a .mid file to .xml using music21 victor@2: victor@2: usage: victor@2: mmOMR=MainMultiOMR() victor@2: file="C:\\Users\\victor\\Desktop\\data\\k458\\OMRS\\m2\\k458.mid" victor@2: mmOMR.runConvertKrnToMusicXML(file) victor@2: ''' victor@2: omr=converter.parse(filename) victor@2: #Reordering the different staffs victor@2: omrOrdered=stream.Score() victor@2: numberParts=len(omr.parts) victor@2: for i in reversed(range(numberParts)): victor@2: mypart=omr.parts[i] victor@2: victor@2: omrOrdered.insert(0,mypart) victor@2: omrOrdered.write("musicxml", filename+'.xml') victor@2: victor@2: ############################################### victor@2: # @param filename file to convert victor@2: def runConvertVoicesToChord(self,filename): victor@2: ''' victor@2: victor@2: ''' victor@2: mf=MeasureFunctions() victor@2: omr=converter.parse(filename) victor@2: omr=mf.convertVoicesToChord(omr) victor@2: omr.show() victor@2: victor@2: ############################################### victor@2: # @param filename file to convert victor@2: def runConvertBeamsToTriplets(self,filename): victor@2: ''' victor@2: victor@2: ''' victor@2: mf=MeasureFunctions() victor@2: omr=converter.parse(filename) victor@2: omr=mf.convertBeamsToTriplets(omr) victor@2: # omr.show() victor@2: omr.write("musicxml", filename+'.xml') victor@2: victor@2: def runRemovesEmptyVoices(self,filename): victor@2: ''' victor@2: victor@2: ''' victor@2: mf=MeasureFunctions() victor@2: omr=converter.parse(filename) victor@2: omr=mf.removesEmptyVoices(omr) victor@2: omr.write("musicxml", filename+'.xml') victor@2: victor@2: def runRemovesGaps(self,filename): victor@2: ''' victor@2: victor@2: ''' victor@2: mf=MeasureFunctions() victor@2: omr=converter.parse(filename) victor@2: omr=mf.removesGaps(omr) victor@2: omr.write("musicxml", filename+'.xml') victor@2: victor@2: victor@2: def runAdaptOMRs(self,dirname): victor@2: ''' victor@2: Runs the following process in one directory. victor@2: 1.-Removing GAPS victor@2: 2.-Converting voices to chords victor@2: 3.-Converting triplets victor@2: 4.-Removing Rest Voices victor@2: ''' victor@2: ff=FilesFunctions() victor@2: mf=MeasureFunctions() victor@2: files=ff.getFiles(dirname) victor@2: for f in files: victor@2: try: victor@2: print f victor@2: omr=converter.parse(f) victor@2: print "--- Removing GAPS---" victor@2: omr=mf.removesGaps(omr) victor@2: victor@2: print "--- Converting voices to chords---" victor@2: omr=mf.convertVoicesToChord( omr) victor@2: victor@2: print "--- Converting triplets---" victor@2: omr=mf.convertBeamsToTriplets(omr) victor@2: print "--- Removing Rest Voices---" victor@2: omr=mf.removeRestVoice(omr) victor@2: victor@2: omr.write("musicxml", f) victor@2: except: victor@2: pass victor@2: victor@2: def runAdaptAllOMRs(self,rootDir): victor@2: ''' victor@2: Adapt all the files in one directory tree victor@2: ''' victor@2: for outWalk in os.walk(rootDir): victor@2: dirName=outWalk[0] victor@2: self.runAdaptOMRs(dirName) victor@2: victor@2: victor@2: victor@2: victor@2: victor@2: ################################################## victor@2: # @param dirname the directory where the files are victor@2: def runViewM21(self,dirname): victor@2: ''' victor@2: Shows all the .xml files in a folder using music21 victor@2: to check the differences before and after processing victor@2: victor@2: usage: victor@2: mmOMR=MainMultiOMR() victor@2: dirname="C:\\Users\\victor\\Desktop\\data\\k458\\OMRS\\m2" victor@2: mmOMR.runViewM21(dirname) victor@2: ''' victor@2: ff=FilesFunctions() victor@2: mf=MeasureFunctions() victor@2: files=ff.getFiles(dirname) victor@2: for f in files: victor@2: omr=converter.parse(f) victor@2: omr.show() victor@2: omr.show('text') victor@2: victor@2: victor@2: ################################################## victor@2: # @param dirname the directory where the files are victor@2: def runViewWrongMeasures(self,dirname): victor@2: ''' victor@2: Flags and prints the possible wrong measures victor@2: victor@2: usage: victor@2: mmOMR=MainMultiOMR() victor@2: dirname="C:\\Users\\victor\\Desktop\\data\\k458\\OMRS\\m2" victor@2: mmOMR.runViewWrongMeasures(dirname) victor@2: ''' victor@2: ff=FilesFunctions() victor@2: mf=MeasureFunctions() victor@2: path = dirname victor@2: print("Path:"+path) victor@2: omr_files=ff.getAllFiles(path) victor@2: for f in omr_files: victor@2: omr=converter.parse(f) victor@2: arrayErrors=mf.flagIncorrectMeasures(omr)[1] victor@2: for array in arrayErrors: victor@2: for i in range(len(array)): victor@2: array[i]=array[i]+1 victor@2: print victor@2: print f victor@2: print "Errors measures duration:"+str(arrayErrors[0]) victor@2: print "Errors measures estimated:"+str(arrayErrors[1]) victor@2: print "Errors based on beams:"+str(arrayErrors[2]) victor@2: print "Errors based on last notes:"+str(arrayErrors[3]) victor@2: victor@2: victor@2: victor@2: victor@2: victor@2: victor@2: victor@2: