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 numarray |
---|
96 | import Gnuplot |
---|
97 | |
---|
98 | oplots.append(Gnuplot.Data(now,freq,with='lines', |
---|
99 | title=self.params.pitchmode)) |
---|
100 | oplots.append(Gnuplot.Data(now,ifreq,with='lines', |
---|
101 | title=self.params.pitchmode)) |
---|
102 | |
---|
103 | temponsets = [] |
---|
104 | for i in onset: |
---|
105 | temponsets.append(i*1000) |
---|
106 | oplots.append(Gnuplot.Data(now,temponsets,with='impulses', |
---|
107 | title=self.params.pitchmode)) |
---|
108 | |
---|
109 | def plotplot(self,wplot,oplots,outplot=None,multiplot = 0): |
---|
110 | from aubio.gnuplot import gnuplot_init, audio_to_array, make_audio_plot |
---|
111 | import re |
---|
112 | import Gnuplot |
---|
113 | # audio data |
---|
114 | time,data = audio_to_array(self.input) |
---|
115 | f = make_audio_plot(time,data) |
---|
116 | |
---|
117 | # check if ground truth exists |
---|
118 | #timet,pitcht = self.gettruth() |
---|
119 | #if timet and pitcht: |
---|
120 | # oplots = [Gnuplot.Data(timet,pitcht,with='lines', |
---|
121 | # title='ground truth')] + oplots |
---|
122 | |
---|
123 | t = Gnuplot.Data(0,0,with='impulses') |
---|
124 | |
---|
125 | g = gnuplot_init(outplot) |
---|
126 | g('set title \'%s\'' % (re.sub('.*/','',self.input))) |
---|
127 | g('set multiplot') |
---|
128 | # hack to align left axis |
---|
129 | g('set lmargin 15') |
---|
130 | # plot waveform and onsets |
---|
131 | g('set size 1,0.3') |
---|
132 | g('set origin 0,0.7') |
---|
133 | g('set xrange [0:%f]' % max(time)) |
---|
134 | g('set yrange [-1:1]') |
---|
135 | g.ylabel('amplitude') |
---|
136 | g.plot(f) |
---|
137 | g('unset title') |
---|
138 | # plot onset detection function |
---|
139 | |
---|
140 | |
---|
141 | g('set size 1,0.7') |
---|
142 | g('set origin 0,0') |
---|
143 | g('set xrange [0:%f]' % max(time)) |
---|
144 | g('set yrange [20:100]') |
---|
145 | g('set key right top') |
---|
146 | g('set noclip one') |
---|
147 | #g('set format x ""') |
---|
148 | #g('set log y') |
---|
149 | #g.xlabel('time (s)') |
---|
150 | g.ylabel('f0 (Hz)') |
---|
151 | if multiplot: |
---|
152 | for i in range(len(oplots)): |
---|
153 | # plot onset detection functions |
---|
154 | g('set size 1,%f' % (0.7/(len(oplots)))) |
---|
155 | g('set origin 0,%f' % (float(i)*0.7/(len(oplots)))) |
---|
156 | g('set xrange [0:%f]' % max(time)) |
---|
157 | g.plot(oplots[i]) |
---|
158 | else: |
---|
159 | g.plot(*oplots) |
---|
160 | #g('unset multiplot') |
---|
161 | |
---|