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