[ae3f5af] | 1 | |
---|
| 2 | from aubio.task import task |
---|
| 3 | from aubio.aubioclass import * |
---|
| 4 | |
---|
| 5 | class tasknotes(task): |
---|
| 6 | def __init__(self,input,output=None,params=None): |
---|
| 7 | task.__init__(self,input,params=params) |
---|
| 8 | self.opick = onsetpick(self.params.bufsize, |
---|
| 9 | self.params.hopsize, |
---|
| 10 | self.channels, |
---|
| 11 | self.myvec, |
---|
| 12 | self.params.threshold, |
---|
[515c7b2] | 13 | mode=self.params.onsetmode, |
---|
[ae3f5af] | 14 | dcthreshold=self.params.dcthreshold, |
---|
| 15 | derivate=self.params.derivate) |
---|
[ca1abdd] | 16 | self.pitchdet = pitch(mode=self.params.pitchmode, |
---|
[ae3f5af] | 17 | bufsize=self.params.pbufsize, |
---|
| 18 | hopsize=self.params.phopsize, |
---|
| 19 | channels=self.channels, |
---|
| 20 | samplerate=self.srate, |
---|
| 21 | omode=self.params.omode) |
---|
| 22 | self.olist = [] |
---|
| 23 | self.ofunc = [] |
---|
| 24 | self.maxofunc = 0 |
---|
| 25 | self.last = -1000 |
---|
| 26 | self.oldifreq = 0 |
---|
| 27 | if self.params.localmin: |
---|
| 28 | self.ovalist = [0., 0., 0., 0., 0.] |
---|
| 29 | |
---|
| 30 | def __call__(self): |
---|
| 31 | from aubio.median import short_find |
---|
| 32 | task.__call__(self) |
---|
| 33 | isonset,val = self.opick.do(self.myvec) |
---|
| 34 | if (aubio_silence_detection(self.myvec(),self.params.silence)): |
---|
| 35 | isonset=0 |
---|
| 36 | freq = -1. |
---|
| 37 | else: |
---|
| 38 | freq = self.pitchdet(self.myvec) |
---|
| 39 | minpitch = self.params.pitchmin |
---|
| 40 | maxpitch = self.params.pitchmax |
---|
| 41 | if maxpitch and freq > maxpitch : |
---|
| 42 | freq = -1. |
---|
| 43 | elif minpitch and freq < minpitch : |
---|
| 44 | freq = -1. |
---|
| 45 | freq = aubio_freqtomidi(freq) |
---|
| 46 | if self.params.pitchsmooth: |
---|
| 47 | self.shortlist.append(freq) |
---|
| 48 | self.shortlist.pop(0) |
---|
| 49 | smoothfreq = short_find(self.shortlist, |
---|
| 50 | len(self.shortlist)/2) |
---|
| 51 | freq = smoothfreq |
---|
| 52 | now = self.frameread |
---|
| 53 | ifreq = int(round(freq)) |
---|
| 54 | if self.oldifreq == ifreq: |
---|
| 55 | self.oldifreq = ifreq |
---|
| 56 | else: |
---|
| 57 | self.oldifreq = ifreq |
---|
| 58 | ifreq = 0 |
---|
| 59 | # take back delay |
---|
| 60 | if self.params.delay != 0.: now -= self.params.delay |
---|
| 61 | if now < 0 : |
---|
| 62 | now = 0 |
---|
| 63 | if (isonset == 1): |
---|
| 64 | if self.params.mintol: |
---|
| 65 | # prune doubled |
---|
| 66 | if (now - self.last) > self.params.mintol: |
---|
| 67 | self.last = now |
---|
| 68 | return now, 1, freq, ifreq |
---|
| 69 | else: |
---|
| 70 | return now, 0, freq, ifreq |
---|
| 71 | else: |
---|
| 72 | return now, 1, freq, ifreq |
---|
| 73 | else: |
---|
| 74 | return now, 0, freq, ifreq |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | def fprint(self,foo): |
---|
| 78 | print self.params.step*foo[0], foo[1], foo[2], foo[3] |
---|
| 79 | |
---|
| 80 | def compute_all(self): |
---|
| 81 | """ Compute data """ |
---|
| 82 | now, onset, freq, ifreq = [], [], [], [] |
---|
| 83 | while(self.readsize==self.params.hopsize): |
---|
| 84 | n, o, f, i = self() |
---|
| 85 | now.append(n*self.params.step) |
---|
| 86 | onset.append(o) |
---|
| 87 | freq.append(f) |
---|
| 88 | ifreq.append(i) |
---|
| 89 | if self.params.verbose: |
---|
| 90 | self.fprint((n,o,f,i)) |
---|
| 91 | return now, onset, freq, ifreq |
---|
| 92 | |
---|
| 93 | def plot(self,now,onset,freq,ifreq,oplots): |
---|
| 94 | import Gnuplot |
---|
| 95 | |
---|
[5f23f66] | 96 | oplots.append(Gnuplot.Data(now,freq,with_='lines', |
---|
[ae3f5af] | 97 | title=self.params.pitchmode)) |
---|
[5f23f66] | 98 | oplots.append(Gnuplot.Data(now,ifreq,with_='lines', |
---|
[ae3f5af] | 99 | title=self.params.pitchmode)) |
---|
| 100 | |
---|
| 101 | temponsets = [] |
---|
| 102 | for i in onset: |
---|
| 103 | temponsets.append(i*1000) |
---|
[5f23f66] | 104 | oplots.append(Gnuplot.Data(now,temponsets,with_='impulses', |
---|
[ae3f5af] | 105 | title=self.params.pitchmode)) |
---|
| 106 | |
---|
| 107 | def plotplot(self,wplot,oplots,outplot=None,multiplot = 0): |
---|
| 108 | from aubio.gnuplot import gnuplot_init, audio_to_array, make_audio_plot |
---|
| 109 | import re |
---|
| 110 | import Gnuplot |
---|
| 111 | # audio data |
---|
| 112 | time,data = audio_to_array(self.input) |
---|
| 113 | f = make_audio_plot(time,data) |
---|
| 114 | |
---|
| 115 | # check if ground truth exists |
---|
| 116 | #timet,pitcht = self.gettruth() |
---|
| 117 | #if timet and pitcht: |
---|
[5f23f66] | 118 | # oplots = [Gnuplot.Data(timet,pitcht,with_='lines', |
---|
[ae3f5af] | 119 | # title='ground truth')] + oplots |
---|
| 120 | |
---|
[5f23f66] | 121 | t = Gnuplot.Data(0,0,with_='impulses') |
---|
[ae3f5af] | 122 | |
---|
| 123 | g = gnuplot_init(outplot) |
---|
| 124 | g('set title \'%s\'' % (re.sub('.*/','',self.input))) |
---|
| 125 | g('set multiplot') |
---|
| 126 | # hack to align left axis |
---|
| 127 | g('set lmargin 15') |
---|
| 128 | # plot waveform and onsets |
---|
| 129 | g('set size 1,0.3') |
---|
| 130 | g('set origin 0,0.7') |
---|
| 131 | g('set xrange [0:%f]' % max(time)) |
---|
| 132 | g('set yrange [-1:1]') |
---|
| 133 | g.ylabel('amplitude') |
---|
| 134 | g.plot(f) |
---|
| 135 | g('unset title') |
---|
| 136 | # plot onset detection function |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | g('set size 1,0.7') |
---|
| 140 | g('set origin 0,0') |
---|
| 141 | g('set xrange [0:%f]' % max(time)) |
---|
| 142 | g('set yrange [20:100]') |
---|
| 143 | g('set key right top') |
---|
| 144 | g('set noclip one') |
---|
| 145 | #g('set format x ""') |
---|
| 146 | #g('set log y') |
---|
| 147 | #g.xlabel('time (s)') |
---|
| 148 | g.ylabel('f0 (Hz)') |
---|
| 149 | if multiplot: |
---|
| 150 | for i in range(len(oplots)): |
---|
| 151 | # plot onset detection functions |
---|
| 152 | g('set size 1,%f' % (0.7/(len(oplots)))) |
---|
| 153 | g('set origin 0,%f' % (float(i)*0.7/(len(oplots)))) |
---|
| 154 | g('set xrange [0:%f]' % max(time)) |
---|
| 155 | g.plot(oplots[i]) |
---|
| 156 | else: |
---|
| 157 | g.plot(*oplots) |
---|
| 158 | #g('unset multiplot') |
---|
| 159 | |
---|