[13c3fba] | 1 | from aubio.task.task import task |
---|
| 2 | from aubio.task.silence import tasksilence |
---|
| 3 | from aubio.aubioclass import * |
---|
| 4 | |
---|
| 5 | class taskpitch(task): |
---|
| 6 | def __init__(self,input,params=None): |
---|
| 7 | task.__init__(self,input,params=params) |
---|
| 8 | self.shortlist = [0. for i in range(self.params.pitchsmooth)] |
---|
[3027c6e] | 9 | if self.params.pitchmode == 'yin': |
---|
| 10 | tolerance = self.params.yinthresh |
---|
| 11 | elif self.params.pitchmode == 'yinfft': |
---|
| 12 | tolerance = self.params.yinfftthresh |
---|
[650e39b] | 13 | else: |
---|
[3027c6e] | 14 | tolerance = 0. |
---|
[ca1abdd] | 15 | self.pitchdet = pitch(mode=self.params.pitchmode, |
---|
[13c3fba] | 16 | bufsize=self.params.bufsize, |
---|
| 17 | hopsize=self.params.hopsize, |
---|
| 18 | channels=self.channels, |
---|
| 19 | samplerate=self.srate, |
---|
[83376e9] | 20 | omode=self.params.omode, |
---|
[3027c6e] | 21 | tolerance = tolerance) |
---|
[13c3fba] | 22 | |
---|
| 23 | def __call__(self): |
---|
| 24 | from aubio.median import short_find |
---|
| 25 | task.__call__(self) |
---|
| 26 | if (aubio_silence_detection(self.myvec(),self.params.silence)==1): |
---|
| 27 | freq = -1. |
---|
| 28 | else: |
---|
| 29 | freq = self.pitchdet(self.myvec) |
---|
| 30 | minpitch = self.params.pitchmin |
---|
| 31 | maxpitch = self.params.pitchmax |
---|
| 32 | if maxpitch and freq > maxpitch : |
---|
| 33 | freq = -1. |
---|
| 34 | elif minpitch and freq < minpitch : |
---|
| 35 | freq = -1. |
---|
| 36 | if self.params.pitchsmooth: |
---|
| 37 | self.shortlist.append(freq) |
---|
| 38 | self.shortlist.pop(0) |
---|
| 39 | smoothfreq = short_find(self.shortlist, |
---|
| 40 | len(self.shortlist)/2) |
---|
| 41 | return smoothfreq |
---|
| 42 | else: |
---|
| 43 | return freq |
---|
| 44 | |
---|
| 45 | def compute_all(self): |
---|
| 46 | """ Compute data """ |
---|
| 47 | mylist = [] |
---|
| 48 | while(self.readsize==self.params.hopsize): |
---|
| 49 | freq = self() |
---|
| 50 | mylist.append(freq) |
---|
| 51 | if self.params.verbose: |
---|
| 52 | self.fprint("%s\t%s" % (self.frameread*self.params.step,freq)) |
---|
| 53 | return mylist |
---|
| 54 | |
---|
| 55 | def gettruth(self): |
---|
| 56 | """ extract ground truth array in frequency """ |
---|
| 57 | import os.path |
---|
| 58 | """ from wavfile.txt """ |
---|
| 59 | datafile = self.input.replace('.wav','.txt') |
---|
| 60 | if datafile == self.input: datafile = "" |
---|
| 61 | """ from file.<midinote>.wav """ |
---|
| 62 | # FIXME very weak check |
---|
| 63 | floatpit = self.input.split('.')[-2] |
---|
| 64 | if not os.path.isfile(datafile) and len(self.input.split('.')) < 3: |
---|
| 65 | print "no ground truth " |
---|
| 66 | return False,False |
---|
| 67 | elif floatpit: |
---|
| 68 | try: |
---|
[5d1c070] | 69 | self.truth = float(floatpit) |
---|
| 70 | #print "ground truth found in filename:", self.truth |
---|
| 71 | tasksil = tasksilence(self.input,params=self.params) |
---|
[13c3fba] | 72 | time,pitch =[],[] |
---|
| 73 | while(tasksil.readsize==tasksil.params.hopsize): |
---|
| 74 | tasksil() |
---|
[650e39b] | 75 | time.append(tasksil.params.step*(tasksil.frameread)) |
---|
[13c3fba] | 76 | if not tasksil.issilence: |
---|
| 77 | pitch.append(self.truth) |
---|
| 78 | else: |
---|
| 79 | pitch.append(-1.) |
---|
[4798fdf] | 80 | return time,pitch |
---|
[13c3fba] | 81 | except ValueError: |
---|
| 82 | # FIXME very weak check |
---|
| 83 | if not os.path.isfile(datafile): |
---|
| 84 | print "no ground truth found" |
---|
| 85 | return 0,0 |
---|
| 86 | else: |
---|
| 87 | from aubio.txtfile import read_datafile |
---|
| 88 | values = read_datafile(datafile) |
---|
| 89 | time, pitch = [], [] |
---|
| 90 | for i in range(len(values)): |
---|
| 91 | time.append(values[i][0]) |
---|
[83376e9] | 92 | if values[i][1] == 0.0: |
---|
| 93 | pitch.append(-1.) |
---|
| 94 | else: |
---|
| 95 | pitch.append(aubio_freqtomidi(values[i][1])) |
---|
[13c3fba] | 96 | return time,pitch |
---|
| 97 | |
---|
[5d1c070] | 98 | def oldeval(self,results): |
---|
[13c3fba] | 99 | def mmean(l): |
---|
| 100 | return sum(l)/max(float(len(l)),1) |
---|
| 101 | |
---|
| 102 | from aubio.median import percental |
---|
| 103 | timet,pitcht = self.gettruth() |
---|
| 104 | res = [] |
---|
| 105 | for i in results: |
---|
| 106 | #print i,self.truth |
---|
| 107 | if i <= 0: pass |
---|
| 108 | else: res.append(self.truth-i) |
---|
| 109 | if not res or len(res) < 3: |
---|
| 110 | avg = self.truth; med = self.truth |
---|
| 111 | else: |
---|
| 112 | avg = mmean(res) |
---|
| 113 | med = percental(res,len(res)/2) |
---|
| 114 | return self.truth, self.truth-med, self.truth-avg |
---|
| 115 | |
---|
[83376e9] | 116 | def eval(self,pitch,tol=0.5): |
---|
[13c3fba] | 117 | timet,pitcht = self.gettruth() |
---|
[5d1c070] | 118 | pitch = [aubio_freqtomidi(i) for i in pitch] |
---|
| 119 | for i in range(len(pitch)): |
---|
| 120 | if pitch[i] == "nan" or pitch[i] == -1: |
---|
| 121 | pitch[i] = -1 |
---|
[4798fdf] | 122 | time = [ (i+self.params.pitchdelay)*self.params.step for i in range(len(pitch)) ] |
---|
[83376e9] | 123 | #print len(timet),len(pitcht) |
---|
| 124 | #print len(time),len(pitch) |
---|
| 125 | if len(timet) != len(time): |
---|
| 126 | time = time[1:len(timet)+1] |
---|
| 127 | pitch = pitch[1:len(pitcht)+1] |
---|
| 128 | #pitcht = [aubio_freqtomidi(i) for i in pitcht] |
---|
| 129 | for i in range(len(pitcht)): |
---|
| 130 | if pitcht[i] == "nan" or pitcht[i] == "-inf" or pitcht[i] == -1: |
---|
| 131 | pitcht[i] = -1 |
---|
| 132 | assert len(timet) == len(time) |
---|
[5d1c070] | 133 | assert len(pitcht) == len(pitch) |
---|
| 134 | osil, esil, opit, epit, echr = 0, 0, 0, 0, 0 |
---|
| 135 | for i in range(len(pitcht)): |
---|
| 136 | if pitcht[i] == -1: # currently silent |
---|
| 137 | osil += 1 # count a silence |
---|
[83376e9] | 138 | if pitch[i] <= 0. or pitch[i] == "nan": |
---|
[5d1c070] | 139 | esil += 1 # found a silence |
---|
| 140 | else: |
---|
| 141 | opit +=1 |
---|
| 142 | if abs(pitcht[i] - pitch[i]) < tol: |
---|
| 143 | epit += 1 |
---|
| 144 | echr += 1 |
---|
| 145 | elif abs(pitcht[i] - pitch[i]) % 12. < tol: |
---|
| 146 | echr += 1 |
---|
| 147 | #else: |
---|
| 148 | # print timet[i], pitcht[i], time[i], pitch[i] |
---|
| 149 | #print "origsilence", "foundsilence", "origpitch", "foundpitch", "orig pitchroma", "found pitchchroma" |
---|
| 150 | #print 100.*esil/float(osil), 100.*epit/float(opit), 100.*echr/float(opit) |
---|
| 151 | return osil, esil, opit, epit, echr |
---|
[13c3fba] | 152 | |
---|
[fb8bde7] | 153 | def plot(self,pitch,wplot,oplots,titles,outplot=None): |
---|
[13c3fba] | 154 | import Gnuplot |
---|
| 155 | |
---|
[4798fdf] | 156 | time = [ (i+self.params.pitchdelay)*self.params.step for i in range(len(pitch)) ] |
---|
[83376e9] | 157 | pitch = [aubio_freqtomidi(i) for i in pitch] |
---|
[5f23f66] | 158 | oplots.append(Gnuplot.Data(time,pitch,with_='lines', |
---|
[13c3fba] | 159 | title=self.params.pitchmode)) |
---|
[fb8bde7] | 160 | titles.append(self.params.pitchmode) |
---|
[13c3fba] | 161 | |
---|
| 162 | |
---|
[470a772] | 163 | def plotplot(self,wplot,oplots,titles,outplot=None,extension=None,xsize=1.,ysize=1.,multiplot = 1, midi = 1, truth = 1): |
---|
| 164 | from aubio.gnuplot import gnuplot_create , audio_to_array, make_audio_plot |
---|
[13c3fba] | 165 | import re |
---|
| 166 | import Gnuplot |
---|
| 167 | |
---|
| 168 | # check if ground truth exists |
---|
[fb8bde7] | 169 | if truth: |
---|
| 170 | timet,pitcht = self.gettruth() |
---|
| 171 | if timet and pitcht: |
---|
[5f23f66] | 172 | oplots = [Gnuplot.Data(timet,pitcht,with_='lines', |
---|
[fb8bde7] | 173 | title='ground truth')] + oplots |
---|
[13c3fba] | 174 | |
---|
[470a772] | 175 | g = gnuplot_create(outplot=outplot, extension=extension) |
---|
[13c3fba] | 176 | g('set title \'%s\'' % (re.sub('.*/','',self.input))) |
---|
[470a772] | 177 | g('set size %f,%f' % (xsize,ysize) ) |
---|
[13c3fba] | 178 | g('set multiplot') |
---|
| 179 | # hack to align left axis |
---|
[fb8bde7] | 180 | g('set lmargin 4') |
---|
| 181 | g('set rmargin 4') |
---|
[470a772] | 182 | # plot waveform |
---|
| 183 | time,data = audio_to_array(self.input) |
---|
| 184 | wplot = [make_audio_plot(time,data)] |
---|
| 185 | g('set origin 0,%f' % (0.7*ysize) ) |
---|
| 186 | g('set size %f,%f' % (xsize,0.3*ysize)) |
---|
| 187 | #g('set format y "%1f"') |
---|
[13c3fba] | 188 | g('set xrange [0:%f]' % max(time)) |
---|
| 189 | g('set yrange [-1:1]') |
---|
[fb8bde7] | 190 | g('set noytics') |
---|
| 191 | g('set y2tics -1,1') |
---|
[470a772] | 192 | g.xlabel('time (s)',offset=(0,0.7)) |
---|
[13c3fba] | 193 | g.ylabel('amplitude') |
---|
[470a772] | 194 | g.plot(*wplot) |
---|
[13c3fba] | 195 | |
---|
[470a772] | 196 | # default settings for next plots |
---|
| 197 | g('unset title') |
---|
| 198 | g('set format x ""') |
---|
| 199 | g('set format y "%3e"') |
---|
| 200 | g('set tmargin 0') |
---|
| 201 | g.xlabel('') |
---|
| 202 | g('set noclip one') |
---|
[13c3fba] | 203 | |
---|
[83376e9] | 204 | if not midi: |
---|
| 205 | g('set log y') |
---|
| 206 | #g.xlabel('time (s)') |
---|
| 207 | g.ylabel('f0 (Hz)') |
---|
| 208 | g('set yrange [100:%f]' % self.params.pitchmax) |
---|
| 209 | else: |
---|
[4798fdf] | 210 | g.ylabel('midi') |
---|
[650e39b] | 211 | g('set yrange [%f:%f]' % (aubio_freqtomidi(self.params.pitchmin), aubio_freqtomidi(self.params.pitchmax))) |
---|
[fb8bde7] | 212 | g('set y2tics %f,%f' % (round(aubio_freqtomidi(self.params.pitchmin)+.5),12)) |
---|
[470a772] | 213 | |
---|
[13c3fba] | 214 | if multiplot: |
---|
[470a772] | 215 | N = len(oplots) |
---|
| 216 | y = 0.7*ysize # the vertical proportion of the plot taken by onset functions |
---|
| 217 | delta = 0.035 # the constant part of y taken by last plot label and data |
---|
| 218 | for i in range(N): |
---|
| 219 | # plot pitch detection functions |
---|
| 220 | g('set size %f,%f' % ( xsize, (y-delta)/N)) |
---|
| 221 | g('set origin 0,%f' % ((N-i-1)*(y-delta)/N + delta )) |
---|
[fb8bde7] | 222 | g('set nokey') |
---|
[470a772] | 223 | g('set xrange [0:%f]' % max(time)) |
---|
[fb8bde7] | 224 | g.ylabel(titles[i]) |
---|
[470a772] | 225 | if i == N-1: |
---|
| 226 | g('set size %f,%f' % (xsize, (y-delta)/N + delta ) ) |
---|
| 227 | g('set origin 0,0') |
---|
| 228 | g.xlabel('time (s)', offset=(0,0.7)) |
---|
| 229 | g('set format x') |
---|
[13c3fba] | 230 | g.plot(oplots[i]) |
---|
| 231 | else: |
---|
[470a772] | 232 | g('set key right top') |
---|
[13c3fba] | 233 | g.plot(*oplots) |
---|
| 234 | g('unset multiplot') |
---|
| 235 | |
---|