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