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 downsample_audio(time,data,maxpoints=10000): |
---|
71 | """ create gnuplot plot from an audio file """ |
---|
72 | import numarray |
---|
73 | length = len(time) |
---|
74 | downsample = length/maxpoints |
---|
75 | if downsample == 0: downsample = 1 |
---|
76 | x = numarray.array(time).resize(length)[0:-1:downsample] |
---|
77 | y = numarray.array(data).resize(length)[0:-1:downsample] |
---|
78 | return x,y |
---|
79 | |
---|
80 | def make_audio_plot(time,data,maxpoints=10000): |
---|
81 | """ create gnuplot plot from an audio file """ |
---|
82 | import numarray |
---|
83 | import Gnuplot, Gnuplot.funcutils |
---|
84 | length = len(time) |
---|
85 | downsample = length/maxpoints |
---|
86 | if downsample == 0: downsample = 1 |
---|
87 | x = numarray.array(time).resize(length)[0:-1:downsample] |
---|
88 | y = numarray.array(data).resize(length)[0:-1:downsample] |
---|
89 | return Gnuplot.Data(x,y,with='lines') |
---|
90 | |
---|
91 | |
---|
92 | def plot_onsets(filename, onsets, ofunc, samplerate=44100., hopsize=512, outplot=None): |
---|
93 | import Gnuplot, Gnuplot.funcutils |
---|
94 | import aubio.txtfile |
---|
95 | import os.path |
---|
96 | import numarray |
---|
97 | import re |
---|
98 | from aubio.onsetcompare import onset_roc |
---|
99 | |
---|
100 | d,d2 = [],[] |
---|
101 | maxofunc = 0 |
---|
102 | for i in range(len(onsets)): |
---|
103 | if len(onsets[i]) == 0: onsets[i] = [0.]; |
---|
104 | |
---|
105 | # onset detection function |
---|
106 | downtime = (hopsize/samplerate)*numarray.arange(len(ofunc[i])) |
---|
107 | d.append(Gnuplot.Data(downtime,ofunc[i],with='lines')) |
---|
108 | maxofunc = max(max(ofunc[i]), maxofunc) |
---|
109 | |
---|
110 | for i in range(len(onsets)): |
---|
111 | # detected onsets |
---|
112 | x1 = (hopsize/samplerate)*numarray.array(onsets[i]) |
---|
113 | y1 = maxofunc*numarray.ones(len(onsets[i])) |
---|
114 | d.append(Gnuplot.Data(x1,y1,with='impulses')) |
---|
115 | d2.append(Gnuplot.Data(x1,-y1,with='impulses')) |
---|
116 | |
---|
117 | # check if datafile exists truth |
---|
118 | datafile = filename.replace('.wav','.txt') |
---|
119 | if datafile == filename: datafile = "" |
---|
120 | if not os.path.isfile(datafile): |
---|
121 | title = "truth file not found" |
---|
122 | t = Gnuplot.Data(0,0,with='impulses') |
---|
123 | else: |
---|
124 | t_onsets = aubio.txtfile.read_datafile(datafile) |
---|
125 | y2 = maxofunc*numarray.ones(len(t_onsets)) |
---|
126 | x2 = numarray.array(t_onsets).resize(len(t_onsets)) |
---|
127 | d2.append(Gnuplot.Data(x2,y2,with='impulses')) |
---|
128 | |
---|
129 | tol = 0.050 |
---|
130 | |
---|
131 | orig, missed, merged, expc, bad, doubled = \ |
---|
132 | onset_roc(x2,x1,tol) |
---|
133 | title = "GD %2.3f%% FP %2.3f%%" % \ |
---|
134 | ((100*float(orig-missed-merged)/(orig)), |
---|
135 | (100*float(bad+doubled)/(orig))) |
---|
136 | |
---|
137 | # audio data |
---|
138 | time,data = audio_to_array(filename) |
---|
139 | d2.append(make_audio_plot(time,data)) |
---|
140 | |
---|
141 | # prepare the plot |
---|
142 | g = gnuplot_init(outplot) |
---|
143 | |
---|
144 | g('set title \'%s %s\'' % (re.sub('.*/','',filename),title)) |
---|
145 | |
---|
146 | g('set multiplot') |
---|
147 | |
---|
148 | # hack to align left axis |
---|
149 | g('set lmargin 15') |
---|
150 | |
---|
151 | # plot waveform and onsets |
---|
152 | g('set size 1,0.3') |
---|
153 | g('set origin 0,0.7') |
---|
154 | g('set xrange [0:%f]' % max(time)) |
---|
155 | g('set yrange [-1:1]') |
---|
156 | g.ylabel('amplitude') |
---|
157 | g.plot(*d2) |
---|
158 | |
---|
159 | g('unset title') |
---|
160 | |
---|
161 | # plot onset detection function |
---|
162 | g('set size 1,0.7') |
---|
163 | g('set origin 0,0') |
---|
164 | g('set xrange [0:%f]' % (hopsize/samplerate*len(ofunc[0]))) |
---|
165 | g('set yrange [0:%f]' % (maxofunc*1.01)) |
---|
166 | g.xlabel('time') |
---|
167 | g.ylabel('onset detection value') |
---|
168 | g.plot(*d) |
---|
169 | |
---|
170 | g('unset multiplot') |
---|
171 | |
---|
172 | |
---|
173 | def plot_pitch(filename, pitch, samplerate=44100., hopsize=512, outplot=None): |
---|
174 | import aubio.txtfile |
---|
175 | import os.path |
---|
176 | import numarray |
---|
177 | import Gnuplot |
---|
178 | import re |
---|
179 | |
---|
180 | d = [] |
---|
181 | maxpitch = 100 |
---|
182 | for i in range(len(pitch)): |
---|
183 | #if len(pitch[i]) == 0: pitch[i] = [0.]; |
---|
184 | |
---|
185 | downtime = (hopsize/samplerate)*numarray.arange(len(pitch[i])) |
---|
186 | d.append(Gnuplot.Data(downtime,pitch[i],with='lines', |
---|
187 | title=('%d' % i))) |
---|
188 | maxpitch = max(maxpitch,max(pitch[i][:])*1.1) |
---|
189 | |
---|
190 | # check if ground truth exists |
---|
191 | datafile = filename.replace('.wav','.txt') |
---|
192 | if datafile == filename: datafile = "" |
---|
193 | if not os.path.isfile(datafile): |
---|
194 | title = "truth file not found" |
---|
195 | t = Gnuplot.Data(0,0,with='impulses') |
---|
196 | else: |
---|
197 | title = "truth file plotting not implemented yet" |
---|
198 | values = aubio.txtfile.read_datafile(datafile) |
---|
199 | if (len(datafile[0])) > 1: |
---|
200 | time, pitch = [], [] |
---|
201 | for i in range(len(values)): |
---|
202 | time.append(values[i][0]) |
---|
203 | pitch.append(values[i][1]) |
---|
204 | d.append(Gnuplot.Data(time,pitch,with='lines', |
---|
205 | title='ground truth')) |
---|
206 | |
---|
207 | # audio data |
---|
208 | time,data = audio_to_array(filename) |
---|
209 | f = make_audio_plot(time,data) |
---|
210 | |
---|
211 | g = gnuplot_init(outplot) |
---|
212 | g('set title \'%s %s\'' % (re.sub('.*/','',filename),title)) |
---|
213 | g('set multiplot') |
---|
214 | # hack to align left axis |
---|
215 | g('set lmargin 15') |
---|
216 | # plot waveform and onsets |
---|
217 | g('set size 1,0.3') |
---|
218 | g('set origin 0,0.7') |
---|
219 | g('set xrange [0:%f]' % max(time)) |
---|
220 | g('set yrange [-1:1]') |
---|
221 | g.ylabel('amplitude') |
---|
222 | g.plot(f) |
---|
223 | g('unset title') |
---|
224 | # plot onset detection function |
---|
225 | g('set size 1,0.7') |
---|
226 | g('set origin 0,0') |
---|
227 | g('set xrange [0:%f]' % max(time)) |
---|
228 | g('set yrange [40:%f]' % maxpitch) |
---|
229 | g('set key right top') |
---|
230 | g.xlabel('time') |
---|
231 | g.ylabel('frequency (Hz)') |
---|
232 | g.plot(*d) |
---|
233 | g('unset multiplot') |
---|
234 | |
---|
235 | def gnuplot_init(outplot,debug=0,persist=1): |
---|
236 | # prepare the plot |
---|
237 | import Gnuplot |
---|
238 | g = Gnuplot.Gnuplot(debug=debug, persist=persist) |
---|
239 | if outplot == 'stdout': |
---|
240 | g("set terminal png fontfile 'p052023l.pfb'") |
---|
241 | #g('set output \'%s\'' % outplot) |
---|
242 | elif outplot: |
---|
243 | extension = outplot.split('.')[-1] |
---|
244 | if extension == 'ps': extension = 'postscript' |
---|
245 | g('set terminal %s' % extension) |
---|
246 | g('set output \'%s\'' % outplot) |
---|
247 | return g |
---|
248 | |
---|
249 | def gnuplot_create(outplot='',extension='',debug=0,persist=1): |
---|
250 | import Gnuplot |
---|
251 | g = Gnuplot.Gnuplot(debug=debug, persist=persist) |
---|
252 | if not extension or not outplot: return g |
---|
253 | if extension == 'ps': ext, extension = '.ps' , 'postscript' |
---|
254 | elif extension == 'png': ext, extension = '.png', 'png' |
---|
255 | elif extension == 'svg': ext, extension = '.svg', 'svg' |
---|
256 | else: exit("ERR: unknown plot extension") |
---|
257 | g('set terminal %s' % extension) |
---|
258 | if outplot != "stdout": |
---|
259 | g('set output \'roc-%s%s\'' % (outplot,ext)) |
---|
260 | return g |
---|