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 | downtime = (hopsize/samplerate)*numarray.arange(len(pitch[i])) |
---|
184 | d.append(Gnuplot.Data(downtime,pitch[i],with='lines', |
---|
185 | title=('%d' % i))) |
---|
186 | maxpitch = max(maxpitch,max(pitch[i][:])*1.1) |
---|
187 | |
---|
188 | # check if ground truth exists |
---|
189 | datafile = filename.replace('.wav','.txt') |
---|
190 | if datafile == filename: datafile = "" |
---|
191 | if not os.path.isfile(datafile): |
---|
192 | title = "truth file not found" |
---|
193 | t = Gnuplot.Data(0,0,with='impulses') |
---|
194 | else: |
---|
195 | title = "truth file plotting not implemented yet" |
---|
196 | values = aubio.txtfile.read_datafile(datafile) |
---|
197 | if (len(datafile[0])) > 1: |
---|
198 | time, pitch = [], [] |
---|
199 | for i in range(len(values)): |
---|
200 | time.append(values[i][0]) |
---|
201 | pitch.append(values[i][1]) |
---|
202 | d.append(Gnuplot.Data(time,pitch,with='lines', |
---|
203 | title='ground truth')) |
---|
204 | |
---|
205 | # audio data |
---|
206 | time,data = audio_to_array(filename) |
---|
207 | f = make_audio_plot(time,data) |
---|
208 | |
---|
209 | g = gnuplot_init(outplot) |
---|
210 | g('set title \'%s %s\'' % (re.sub('.*/','',filename),title)) |
---|
211 | g('set multiplot') |
---|
212 | # hack to align left axis |
---|
213 | g('set lmargin 15') |
---|
214 | # plot waveform and onsets |
---|
215 | g('set size 1,0.3') |
---|
216 | g('set origin 0,0.7') |
---|
217 | g('set xrange [0:%f]' % max(time)) |
---|
218 | g('set yrange [-1:1]') |
---|
219 | g.ylabel('amplitude') |
---|
220 | g.plot(f) |
---|
221 | g('unset title') |
---|
222 | # plot onset detection function |
---|
223 | g('set size 1,0.7') |
---|
224 | g('set origin 0,0') |
---|
225 | g('set xrange [0:%f]' % max(time)) |
---|
226 | g('set yrange [40:%f]' % maxpitch) |
---|
227 | g('set key right top') |
---|
228 | g.xlabel('time') |
---|
229 | g.ylabel('frequency (Hz)') |
---|
230 | g.plot(*d) |
---|
231 | g('unset multiplot') |
---|
232 | |
---|
233 | def gnuplot_init(outplot,debug=0,persist=1): |
---|
234 | import Gnuplot |
---|
235 | # prepare the plot |
---|
236 | g = Gnuplot.Gnuplot(debug=debug, persist=persist) |
---|
237 | if outplot == 'stdout': |
---|
238 | g("set terminal png fontfile 'p052023l.pfb'") |
---|
239 | #g('set output \'%s\'' % outplot) |
---|
240 | elif outplot: |
---|
241 | extension = outplot.split('.')[-1] |
---|
242 | if extension == 'ps': extension = 'postscript' |
---|
243 | g('set terminal %s' % extension) |
---|
244 | g('set output \'%s\'' % outplot) |
---|
245 | return g |
---|