def __init__(self): self.nc1 = nc.noteClass() self.ptSeg1 = ptSeg.pitchtrackSegByNotes() self.evalu1 = evalu.Evaluation() self.evaluationMetrics = [] self.resetRepresentation()
def noteSegmentationFeatureExtraction(self,pitchtrackNoteFolderPath,featureVecFolderPath,pitchtrackNotePredictFolderPath, recordingNames,segCoef=0.2,predict=False,evaluation=False): ''' This process will do 1) segment pitchtrack into notes which boundaries are given by pYIN 2) refined segmentation searching stable part 3) calculate features on refined segments :param pitchtrackNoteFolderPath: :param featureVecFolderPath: :param recordingNames: :param predict: :return: refined segments boundaries, refined segments pitch contours ''' ############################################## segmentation ######################################################## # below two lines will do segmentaion on calculation the pyin # ptSeg = pitchtrackSegByNotes.pitchtrackSegByNotes(samplingFreq, frameSize, hopSize) # ptSeg.doSegmentation(pitchtrack, fs.m_oMonoNoteOut) ptSeg = pitchtrackSegByNotes.pitchtrackSegByNotes() if evaluation: evalu1 = evalu.Evaluation() # evaluation object COnOffall, COnall, OBOnall, OBOffall,gt,st = 0,0,0,0,0,0 for rn in recordingNames: pitchtrack_filename = pitchtrackNoteFolderPath+rn+'_pitchtrack.csv' monoNoteOut_filename = pitchtrackNoteFolderPath+rn+'_monoNoteOut.csv' ptSeg.doSegmentationForPyinVamp(pitchtrack_filename, monoNoteOut_filename) if evaluation: coarseSegmentation_filename = pitchtrackNoteFolderPath+rn+'_coarseSeg.txt' ptSeg.coarseSegmentation(monoNoteOut_filename,coarseSegmentation_filename) # groundtruth segmentation # ptSeg.pltNotePitchtrack(saveFig=True, figFolder='../jingjuSegPic/laosheng/train/male_13/pos_3_midinote/') ###################### calculate the polynomial fitting coefs and vibrato frequency ############################ # use pitch track ptseg.pitchtrackByNotes from last step featureDict = {} # feature vectors dictionary segmentsExport = {} # refined segmentation boundaries refinedPitchcontours = {} # refined pitch contours extremas = {} # extremas vibrato = {} curvefittingDeg = 1 jj = 1 jjNone = [] jjj = 1 for ii in range(len(ptSeg.pitchtrackByNotes)): pt = ptSeg.pitchtrackByNotes[ii][0] pt = np.array(pt, dtype=np.float32) x, y = self.normalizeNotePt(pt) # normalise x to [0,1], remove y DC sbp = self.localMinMax(y) # local minima and extrema of pitch track self.diffExtrema(x,y) # the amplitude difference of minima and extrema self.segmentPointDetection1(segCoef) # do the extrema segmentation here # nc1.segmentPointDetection2() # segmentation point self.segmentRefinement(pt) # do the refined segmentation #print self.refinedNotePts for rpt in self.refinedNotePts: # print jj featureVec,extrema,vibOut = self.featureExtractionProcess(rpt,sbp,curvefittingDeg) if featureVec[0]: refinedPitchcontours[jj] = rpt.tolist() featureDict[jj] = featureVec.tolist() extremas[jj] = extrema vibrato[jj] = vibOut else: jjNone.append(jj) # this plot step is slow, if we only want the features, we can comment this line #nc1.pltRefinedNotePtFc(xRpt, yRpt, p, rsquare, polyVar, vibFreq, saveFig=True, # figFolder='../jingjuSegPic/laosheng/train/refinedSegmentCurvefit/'+rn+'_curvefit_refined/', # figNumber = jj) jj += 1 if predict: # construct the segments frame vector: frame boundary of segments noteStartFrame = ptSeg.noteStartEndFrame[ii][0] noteEndFrame = ptSeg.noteStartEndFrame[ii][1] extremaInd = np.array(self.extrema) segmentsInd = extremaInd[self.segments]+noteStartFrame segmentsInd = np.insert(segmentsInd,0,noteStartFrame) segmentsInd = np.append(segmentsInd,noteEndFrame)+2 # +2 for sonicVisualizer alignment # segmentsExport[jjj] = str(segmentsInd) for kk in range(len(segmentsInd)-1): if jjj not in jjNone: segmentsExport[jjj] = [segmentsInd[kk],segmentsInd[kk+1]] # segmentation boundary jjj += 1 if evaluation: # evaluate segmentation COnOff, COn, OBOn, OBOff = \ evalu1.coarseEval(ptSeg.coarseSegmentationStartEndFrame,segmentsExport.values()) COnOffall += COnOff COnall += COn OBOnall += OBOn OBOffall += OBOff gt += len(ptSeg.coarseSegmentationStartEndFrame) st += len(segmentsExport.values()) # write feature into json featureFilename = featureVecFolderPath+rn+'.json' with open(featureFilename, 'w') as outfile: json.dump(featureDict, outfile) if predict: # output segments boundary frames pitch contours outJsonDict = {'refinedPitchcontours':refinedPitchcontours,'boundary':segmentsExport, 'extremas':extremas,'vibrato':vibrato} with open(pitchtrackNotePredictFolderPath+rn+'_refinedSegmentFeatures.json', "w") as outfile: json.dump(outJsonDict,outfile) # for se in segmentsExport: # # outfile.write(str(int(se[0]))+'\t'+str(se[1])+'\n') # outfile.write(str(se)+'\n') if evaluation: # print COnOffall,COnall,OBOnall,OBOffall,gt,st COnOffF,COnF,OBOnRateGT,OBOffRateGT = evalu1.metrics(COnOffall,COnall,OBOnall,OBOffall,gt,st) return COnOffF,COnF,OBOnRateGT,OBOffRateGT
def noteSegmentationFeatureExtraction(self, pitchtrackNoteFolderPath, featureVecFolderPath, pitchtrackNotePredictFolderPath, recordingNames, segCoef=0.2, predict=False, evaluation=False): ''' This process will do 1) segment pitchtrack into notes which boundaries are given by pYIN 2) refined segmentation searching stable part 3) calculate features on refined segments :param pitchtrackNoteFolderPath: :param featureVecFolderPath: :param recordingNames: :param predict: :return: refined segments boundaries, refined segments pitch contours ''' ############################################## segmentation ######################################################## # below two lines will do segmentaion on calculation the pyin # ptSeg = pitchtrackSegByNotes.pitchtrackSegByNotes(samplingFreq, frameSize, hopSize) # ptSeg.doSegmentation(pitchtrack, fs.m_oMonoNoteOut) ptSeg = pitchtrackSegByNotes.pitchtrackSegByNotes() if evaluation: evalu1 = evalu.Evaluation() # evaluation object COnOffall, COnall, OBOnall, OBOffall, gt, st = 0, 0, 0, 0, 0, 0 for rn in recordingNames: pitchtrack_filename = os.path.join(pitchtrackNoteFolderPath, rn + '_pitchtrack.csv') monoNoteOut_filename = os.path.join(pitchtrackNoteFolderPath, rn + '_monoNoteOut.csv') ptSeg.doSegmentationForPyinVamp(pitchtrack_filename, monoNoteOut_filename) if evaluation: coarseSegmentation_filename = os.path.join( pitchtrackNoteFolderPath, rn + '_coarseSeg.txt') ptSeg.coarseSegmentation( monoNoteOut_filename, coarseSegmentation_filename) # groundtruth segmentation # ptSeg.pltNotePitchtrack(saveFig=True, figFolder='../jingjuSegPic/laosheng/train/male_13/pos_3_midinote/') ###################### calculate the polynomial fitting coefs and vibrato frequency ############################ # use pitch track ptseg.pitchtrackByNotes from last step featureDict = {} # feature vectors dictionary segmentsExport = {} # refined segmentation boundaries refinedPitchcontours = {} # refined pitch contours extremas = {} # extremas vibrato = {} curvefittingDeg = 1 jj = 1 jjNone = [] jjj = 1 for ii in range(len(ptSeg.pitchtrackByNotes)): pt = ptSeg.pitchtrackByNotes[ii][0] pt = np.array(pt, dtype=np.float32) x, y = self.normalizeNotePt( pt) # normalise x to [0,1], remove y DC sbp = self.localMinMax( y) # local minima and extrema of pitch track self.diffExtrema( x, y) # the amplitude difference of minima and extrema self.segmentPointDetection1( segCoef) # do the extrema segmentation here # nc1.segmentPointDetection2() # segmentation point self.segmentRefinement(pt) # do the refined segmentation #print self.refinedNotePts for rpt in self.refinedNotePts: # print jj featureVec, extrema, vibOut = self.featureExtractionProcess( rpt, sbp, curvefittingDeg) if featureVec[0]: refinedPitchcontours[jj] = rpt.tolist() featureDict[jj] = featureVec.tolist() extremas[jj] = extrema vibrato[jj] = vibOut else: jjNone.append(jj) # this plot step is slow, if we only want the features, we can comment this line #nc1.pltRefinedNotePtFc(xRpt, yRpt, p, rsquare, polyVar, vibFreq, saveFig=True, # figFolder='../jingjuSegPic/laosheng/train/refinedSegmentCurvefit/'+rn+'_curvefit_refined/', # figNumber = jj) jj += 1 if predict: # construct the segments frame vector: frame boundary of segments noteStartFrame = ptSeg.noteStartEndFrame[ii][0] noteEndFrame = ptSeg.noteStartEndFrame[ii][1] extremaInd = np.array(self.extrema) segmentsInd = extremaInd[self.segments] + noteStartFrame segmentsInd = np.insert(segmentsInd, 0, noteStartFrame) segmentsInd = np.append( segmentsInd, noteEndFrame) + 2 # +2 for sonicVisualizer alignment # segmentsExport[jjj] = str(segmentsInd) for kk in range(len(segmentsInd) - 1): if jjj not in jjNone: segmentsExport[jjj] = [ segmentsInd[kk], segmentsInd[kk + 1] ] # segmentation boundary jjj += 1 if evaluation: # evaluate segmentation COnOff, COn, OBOn, OBOff = \ evalu1.coarseEval(ptSeg.coarseSegmentationStartEndFrame,segmentsExport.values()) COnOffall += COnOff COnall += COn OBOnall += OBOn OBOffall += OBOff gt += len(ptSeg.coarseSegmentationStartEndFrame) st += len(segmentsExport.values()) # write feature into json featureFilename = os.path.join(featureVecFolderPath, '%s.json' % rn) with open(featureFilename, 'w') as outfile: json.dump(featureDict, outfile) if predict: # output segments boundary frames pitch contours outJsonDict = { 'refinedPitchcontours': refinedPitchcontours, 'boundary': segmentsExport, 'extremas': extremas, 'vibrato': vibrato } refined_segment_filename = os.path.join( pitchtrackNotePredictFolderPath, '%s_refinedSegmentFeatures.json' % rn) with open(refined_segment_filename, "w") as outfile: json.dump(outJsonDict, outfile) # for se in segmentsExport: # # outfile.write(str(int(se[0]))+'\t'+str(se[1])+'\n') # outfile.write(str(se)+'\n') if evaluation: # print COnOffall,COnall,OBOnall,OBOffall,gt,st COnOffF, COnF, OBOnRateGT, OBOffRateGT = evalu1.metrics( COnOffall, COnall, OBOnall, OBOffall, gt, st) return COnOffF, COnF, OBOnRateGT, OBOffRateGT