1 | """Copyright (C) 2004 Paul Brossier <piem@altern.org> |
---|
2 | print aubio.__LICENSE__ for the terms of use |
---|
3 | """ |
---|
4 | |
---|
5 | __LICENSE__ = """\ |
---|
6 | Copyright (C) 2004 Paul Brossier <piem@altern.org> |
---|
7 | |
---|
8 | This program is free software; you can redistribute it and/or modify |
---|
9 | it under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2 of the License, or |
---|
11 | (at your option) any later version. |
---|
12 | |
---|
13 | This program is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | GNU General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
21 | """ |
---|
22 | |
---|
23 | |
---|
24 | __notesheight = 0.25 |
---|
25 | |
---|
26 | |
---|
27 | def audio_to_array(filename): |
---|
28 | import aubio.aubioclass |
---|
29 | import numarray |
---|
30 | hopsize = 2048 |
---|
31 | filei = aubio.aubioclass.sndfile(filename) |
---|
32 | framestep = 1/(filei.samplerate()+0.) |
---|
33 | channels = filei.channels() |
---|
34 | myvec = aubio.aubioclass.fvec(hopsize,channels) |
---|
35 | data = [] |
---|
36 | readsize = hopsize |
---|
37 | while (readsize==hopsize): |
---|
38 | readsize = filei.read(hopsize,myvec) |
---|
39 | #for i in range(channels): |
---|
40 | i = 0 |
---|
41 | curpos = 0 |
---|
42 | while (curpos < readsize): |
---|
43 | data.append(myvec.get(curpos,i)) |
---|
44 | curpos+=1 |
---|
45 | time = numarray.arange(len(data))*framestep |
---|
46 | return time,data |
---|
47 | |
---|
48 | def plot_audio(filenames, fileout=None, start=0, end=None, noaxis=None): |
---|
49 | g = gnuplot_init(fileout) |
---|
50 | d = [] |
---|
51 | todraw = len(filenames) |
---|
52 | xorig = 0. |
---|
53 | xsize = 1./todraw |
---|
54 | g.gnuplot('set multiplot;') |
---|
55 | while (len(filenames)): |
---|
56 | time,data = audio_to_array(filenames.pop(0)) |
---|
57 | d.append(make_audio_plot(time,data)) |
---|
58 | if not noaxis and todraw==1: |
---|
59 | g.xlabel('Time (s)') |
---|
60 | g.ylabel('Amplitude') |
---|
61 | g.gnuplot('set size %f,1.;' % (xsize) ) |
---|
62 | g.gnuplot('set origin %f,0.;' % (xorig) ) |
---|
63 | g.gnuplot('set style data lines; \ |
---|
64 | set yrange [-1.:1.]; \ |
---|
65 | set xrange [0:%f]' % time[-1]) |
---|
66 | g.plot(d.pop(0)) |
---|
67 | xorig += 1./todraw |
---|
68 | g.gnuplot('unset multiplot;') |
---|
69 | |
---|
70 | def audio_to_spec(filename): |
---|
71 | from aubioclass import fvec,cvec,pvoc,sndfile |
---|
72 | from math import log |
---|
73 | bufsize = 256*16 |
---|
74 | hopsize = bufsize/4 # could depend on filelength |
---|
75 | filei = sndfile(filename) |
---|
76 | srate = float(filei.samplerate()) |
---|
77 | framestep = hopsize/srate |
---|
78 | freqstep = srate/bufsize |
---|
79 | channels = filei.channels() |
---|
80 | myvec = fvec(hopsize,channels) |
---|
81 | myfft = cvec(bufsize,channels) |
---|
82 | pv = pvoc(bufsize,hopsize,channels) |
---|
83 | data,time,freq = [],[],[] |
---|
84 | for f in range(bufsize/2): |
---|
85 | freq.append(f*freqstep) |
---|
86 | readsize = hopsize |
---|
87 | frameread = 0 |
---|
88 | while (readsize==hopsize): |
---|
89 | readsize = filei.read(hopsize,myvec) |
---|
90 | pv.do(myvec,myfft) |
---|
91 | frame = [] |
---|
92 | i = 0 #for i in range(channels): |
---|
93 | curpos = 0 |
---|
94 | while (curpos < bufsize/2): |
---|
95 | frame.append(log(myfft.get(curpos,i)**2+0.000001)) |
---|
96 | curpos+=1 |
---|
97 | time.append(frameread*framestep) |
---|
98 | data.append(frame) |
---|
99 | frameread += 1 |
---|
100 | # crop data if unfinished frames |
---|
101 | if len(data[-1]) != len(data[0]): |
---|
102 | data = data[0:-2] |
---|
103 | time = time[0:-2] |
---|
104 | # verify size consistency |
---|
105 | assert len(data) == len(time) |
---|
106 | assert len(data[0]) == len(freq) |
---|
107 | return data,time,freq |
---|
108 | |
---|
109 | def plot_spec(filenames, outplot='',extension='', fileout=None, start=0, end=None, noaxis=None,log=1): |
---|
110 | import Gnuplot |
---|
111 | g = gnuplot_create(outplot,extension) |
---|
112 | todraw = len(filenames) |
---|
113 | xorig = 0. |
---|
114 | xsize = 1./todraw |
---|
115 | data,time,freq = audio_to_spec(filenames.pop(0)) |
---|
116 | |
---|
117 | if not noaxis and todraw==1: |
---|
118 | g.xlabel('Time (s)') |
---|
119 | g.ylabel('Frequency (Hz)') |
---|
120 | g.gnuplot('set pm3d map') |
---|
121 | #g.gnuplot('set palette rgbformulae 30,31,32') |
---|
122 | #g.gnuplot('set palette') |
---|
123 | g.gnuplot('set xrange [0.:%f]' % time[-1]) |
---|
124 | g.gnuplot('set yrange [1.:%f]' % (freq[-1]/1.)) |
---|
125 | if log: |
---|
126 | g.gnuplot('set yrange [10.1:%f]' % (freq[-1]/1.)) |
---|
127 | g.gnuplot('set log y') |
---|
128 | g.splot(Gnuplot.GridData(data,time,freq, binary=1)) |
---|
129 | xorig += 1./todraw |
---|
130 | |
---|
131 | def downsample_audio(time,data,maxpoints=10000): |
---|
132 | """ resample audio data to last only maxpoints """ |
---|
133 | import numarray |
---|
134 | length = len(time) |
---|
135 | downsample = length/maxpoints |
---|
136 | if downsample == 0: downsample = 1 |
---|
137 | x = numarray.array(time).resize(length)[0:-1:downsample] |
---|
138 | y = numarray.array(data).resize(length)[0:-1:downsample] |
---|
139 | return x,y |
---|
140 | |
---|
141 | def make_audio_plot(time,data,maxpoints=10000): |
---|
142 | """ create gnuplot plot from an audio file """ |
---|
143 | import numarray |
---|
144 | import Gnuplot, Gnuplot.funcutils |
---|
145 | length = len(time) |
---|
146 | downsample = length/maxpoints |
---|
147 | if downsample == 0: downsample = 1 |
---|
148 | x = numarray.array(time).resize(length)[0:-1:downsample] |
---|
149 | y = numarray.array(data).resize(length)[0:-1:downsample] |
---|
150 | return Gnuplot.Data(x,y,with='lines') |
---|
151 | |
---|
152 | |
---|
153 | def plot_onsets(filename, onsets, ofunc, samplerate=44100., hopsize=512, outplot=None): |
---|
154 | import Gnuplot, Gnuplot.funcutils |
---|
155 | import aubio.txtfile |
---|
156 | import os.path |
---|
157 | import numarray |
---|
158 | import re |
---|
159 | from aubio.onsetcompare import onset_roc |
---|
160 | |
---|
161 | d,d2 = [],[] |
---|
162 | maxofunc = 0 |
---|
163 | for i in range(len(onsets)): |
---|
164 | if len(onsets[i]) == 0: onsets[i] = [0.]; |
---|
165 | |
---|
166 | # onset detection function |
---|
167 | downtime = (hopsize/samplerate)*numarray.arange(len(ofunc[i])) |
---|
168 | d.append(Gnuplot.Data(downtime,ofunc[i],with='lines')) |
---|
169 | maxofunc = max(max(ofunc[i]), maxofunc) |
---|
170 | |
---|
171 | for i in range(len(onsets)): |
---|
172 | # detected onsets |
---|
173 | x1 = (hopsize/samplerate)*numarray.array(onsets[i]) |
---|
174 | y1 = maxofunc*numarray.ones(len(onsets[i])) |
---|
175 | d.append(Gnuplot.Data(x1,y1,with='impulses')) |
---|
176 | d2.append(Gnuplot.Data(x1,-y1,with='impulses')) |
---|
177 | |
---|
178 | # check if datafile exists truth |
---|
179 | datafile = filename.replace('.wav','.txt') |
---|
180 | if datafile == filename: datafile = "" |
---|
181 | if not os.path.isfile(datafile): |
---|
182 | title = "truth file not found" |
---|
183 | t = Gnuplot.Data(0,0,with='impulses') |
---|
184 | else: |
---|
185 | t_onsets = aubio.txtfile.read_datafile(datafile) |
---|
186 | y2 = maxofunc*numarray.ones(len(t_onsets)) |
---|
187 | x2 = numarray.array(t_onsets).resize(len(t_onsets)) |
---|
188 | d2.append(Gnuplot.Data(x2,y2,with='impulses')) |
---|
189 | |
---|
190 | tol = 0.050 |
---|
191 | |
---|
192 | orig, missed, merged, expc, bad, doubled = \ |
---|
193 | onset_roc(x2,x1,tol) |
---|
194 | title = "GD %2.3f%% FP %2.3f%%" % \ |
---|
195 | ((100*float(orig-missed-merged)/(orig)), |
---|
196 | (100*float(bad+doubled)/(orig))) |
---|
197 | |
---|
198 | # audio data |
---|
199 | time,data = audio_to_array(filename) |
---|
200 | d2.append(make_audio_plot(time,data)) |
---|
201 | |
---|
202 | # prepare the plot |
---|
203 | g = gnuplot_init(outplot) |
---|
204 | |
---|
205 | g('set title \'%s %s\'' % (re.sub('.*/','',filename),title)) |
---|
206 | |
---|
207 | g('set multiplot') |
---|
208 | |
---|
209 | # hack to align left axis |
---|
210 | g('set lmargin 15') |
---|
211 | |
---|
212 | # plot waveform and onsets |
---|
213 | g('set size 1,0.3') |
---|
214 | g('set origin 0,0.7') |
---|
215 | g('set xrange [0:%f]' % max(time)) |
---|
216 | g('set yrange [-1:1]') |
---|
217 | g.ylabel('amplitude') |
---|
218 | g.plot(*d2) |
---|
219 | |
---|
220 | g('unset title') |
---|
221 | |
---|
222 | # plot onset detection function |
---|
223 | g('set size 1,0.7') |
---|
224 | g('set origin 0,0') |
---|
225 | g('set xrange [0:%f]' % (hopsize/samplerate*len(ofunc[0]))) |
---|
226 | g('set yrange [0:%f]' % (maxofunc*1.01)) |
---|
227 | g.xlabel('time') |
---|
228 | g.ylabel('onset detection value') |
---|
229 | g.plot(*d) |
---|
230 | |
---|
231 | g('unset multiplot') |
---|
232 | |
---|
233 | |
---|
234 | def plot_pitch(filename, pitch, samplerate=44100., hopsize=512, outplot=None): |
---|
235 | import aubio.txtfile |
---|
236 | import os.path |
---|
237 | import numarray |
---|
238 | import Gnuplot |
---|
239 | import re |
---|
240 | |
---|
241 | d = [] |
---|
242 | maxpitch = 1000. |
---|
243 | for i in range(len(pitch)): |
---|
244 | #if len(pitch[i]) == 0: pitch[i] = [0.]; |
---|
245 | |
---|
246 | downtime = (hopsize/samplerate)*numarray.arange(len(pitch[i])) |
---|
247 | d.append(Gnuplot.Data(downtime,pitch[i],with='lines', |
---|
248 | title=('%d' % i))) |
---|
249 | maxpitch = max(maxpitch,max(pitch[i][:])*1.1) |
---|
250 | |
---|
251 | # check if ground truth exists |
---|
252 | datafile = filename.replace('.wav','.txt') |
---|
253 | if datafile == filename: datafile = "" |
---|
254 | if not os.path.isfile(datafile): |
---|
255 | title = "truth file not found" |
---|
256 | t = Gnuplot.Data(0,0,with='impulses') |
---|
257 | else: |
---|
258 | title = "truth file plotting not implemented yet" |
---|
259 | values = aubio.txtfile.read_datafile(datafile) |
---|
260 | if (len(datafile[0])) > 1: |
---|
261 | time, pitch = [], [] |
---|
262 | for i in range(len(values)): |
---|
263 | time.append(values[i][0]) |
---|
264 | pitch.append(values[i][1]) |
---|
265 | d.append(Gnuplot.Data(time,pitch,with='lines', |
---|
266 | title='ground truth')) |
---|
267 | |
---|
268 | # audio data |
---|
269 | time,data = audio_to_array(filename) |
---|
270 | f = make_audio_plot(time,data) |
---|
271 | |
---|
272 | g = gnuplot_init(outplot) |
---|
273 | g('set title \'%s %s\'' % (re.sub('.*/','',filename),title)) |
---|
274 | g('set multiplot') |
---|
275 | # hack to align left axis |
---|
276 | g('set lmargin 15') |
---|
277 | # plot waveform and onsets |
---|
278 | g('set size 1,0.3') |
---|
279 | g('set origin 0,0.7') |
---|
280 | g('set xrange [0:%f]' % max(time)) |
---|
281 | g('set yrange [-1:1]') |
---|
282 | g.ylabel('amplitude') |
---|
283 | g.plot(f) |
---|
284 | g('unset title') |
---|
285 | # plot onset detection function |
---|
286 | g('set size 1,0.7') |
---|
287 | g('set origin 0,0') |
---|
288 | g('set xrange [0:%f]' % max(time)) |
---|
289 | g('set yrange [40:%f]' % maxpitch) |
---|
290 | g('set key right top') |
---|
291 | g('set noclip one') |
---|
292 | g.xlabel('time') |
---|
293 | g.ylabel('frequency (Hz)') |
---|
294 | g.plot(*d) |
---|
295 | g('unset multiplot') |
---|
296 | |
---|
297 | def gnuplot_init(outplot,debug=0,persist=1): |
---|
298 | # prepare the plot |
---|
299 | import Gnuplot |
---|
300 | g = Gnuplot.Gnuplot(debug=debug, persist=persist) |
---|
301 | if outplot == 'stdout': |
---|
302 | g("set terminal png fontfile 'p052023l.pfb'") |
---|
303 | #g('set output \'%s\'' % outplot) |
---|
304 | elif outplot: |
---|
305 | extension = outplot.split('.')[-1] |
---|
306 | if extension == 'ps': extension = 'postscript' |
---|
307 | g('set terminal %s' % extension) |
---|
308 | g('set output \'%s\'' % outplot) |
---|
309 | return g |
---|
310 | |
---|
311 | def gnuplot_create(outplot='',extension='',debug=0,persist=1): |
---|
312 | import Gnuplot |
---|
313 | g = Gnuplot.Gnuplot(debug=debug, persist=persist) |
---|
314 | if not extension or not outplot: return g |
---|
315 | if extension == 'ps': ext, extension = '.ps' , 'postscript' |
---|
316 | elif extension == 'png': ext, extension = '.png', 'png' |
---|
317 | elif extension == 'svg': ext, extension = '.svg', 'svg' |
---|
318 | else: exit("ERR: unknown plot extension") |
---|
319 | g('set terminal %s' % extension) |
---|
320 | if outplot != "stdout": |
---|
321 | g('set output \'roc-%s%s\'' % (outplot,ext)) |
---|
322 | return g |
---|