[96fb8ad] | 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. |
---|
[65ebf30] | 21 | """ |
---|
[96fb8ad] | 22 | |
---|
| 23 | |
---|
[733c2f8] | 24 | __notesheight = 0.25 |
---|
[96fb8ad] | 25 | |
---|
| 26 | |
---|
[65ebf30] | 27 | def audio_to_array(filename): |
---|
| 28 | import aubio.aubioclass |
---|
[733c2f8] | 29 | import numarray |
---|
[65ebf30] | 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 |
---|
[733c2f8] | 45 | time = numarray.arange(len(data))*framestep |
---|
[65ebf30] | 46 | return time,data |
---|
| 47 | |
---|
[a0fd4e4] | 48 | def plot_audio(filenames, fileout=None, start=0, end=None, noaxis=None): |
---|
[733c2f8] | 49 | g = gnuplot_init(fileout) |
---|
[65ebf30] | 50 | d = [] |
---|
| 51 | todraw = len(filenames) |
---|
| 52 | xorig = 0. |
---|
| 53 | xsize = 1./todraw |
---|
| 54 | g.gnuplot('set multiplot;') |
---|
| 55 | while (len(filenames)): |
---|
[733c2f8] | 56 | time,data = audio_to_array(filenames.pop(0)) |
---|
| 57 | d.append(make_audio_plot(time,data)) |
---|
[65ebf30] | 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.]; \ |
---|
[733c2f8] | 65 | set xrange [0:%f]' % time[-1]) |
---|
[65ebf30] | 66 | g.plot(d.pop(0)) |
---|
| 67 | xorig += 1./todraw |
---|
| 68 | g.gnuplot('unset multiplot;') |
---|
[96fb8ad] | 69 | |
---|
[2bd1a2a] | 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 | |
---|
[a0fd4e4] | 80 | def make_audio_plot(time,data,maxpoints=10000): |
---|
| 81 | """ create gnuplot plot from an audio file """ |
---|
[733c2f8] | 82 | import numarray |
---|
| 83 | import Gnuplot, Gnuplot.funcutils |
---|
[a0fd4e4] | 84 | length = len(time) |
---|
| 85 | downsample = length/maxpoints |
---|
| 86 | if downsample == 0: downsample = 1 |
---|
[733c2f8] | 87 | x = numarray.array(time).resize(length)[0:-1:downsample] |
---|
| 88 | y = numarray.array(data).resize(length)[0:-1:downsample] |
---|
[a0fd4e4] | 89 | return Gnuplot.Data(x,y,with='lines') |
---|
[80c0417] | 90 | |
---|
| 91 | |
---|
| 92 | def plot_onsets(filename, onsets, ofunc, samplerate=44100., hopsize=512, outplot=None): |
---|
[733c2f8] | 93 | import Gnuplot, Gnuplot.funcutils |
---|
[80c0417] | 94 | import aubio.txtfile |
---|
| 95 | import os.path |
---|
| 96 | import numarray |
---|
[733c2f8] | 97 | import re |
---|
[80c0417] | 98 | from aubio.onsetcompare import onset_roc |
---|
| 99 | |
---|
[5e491b3b] | 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')) |
---|
[80c0417] | 116 | |
---|
| 117 | # check if datafile exists truth |
---|
| 118 | datafile = filename.replace('.wav','.txt') |
---|
[2bd1a2a] | 119 | if datafile == filename: datafile = "" |
---|
[80c0417] | 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) |
---|
[5e491b3b] | 125 | y2 = maxofunc*numarray.ones(len(t_onsets)) |
---|
[80c0417] | 126 | x2 = numarray.array(t_onsets).resize(len(t_onsets)) |
---|
[5e491b3b] | 127 | d2.append(Gnuplot.Data(x2,y2,with='impulses')) |
---|
[80c0417] | 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) |
---|
[5e491b3b] | 139 | d2.append(make_audio_plot(time,data)) |
---|
[80c0417] | 140 | |
---|
| 141 | # prepare the plot |
---|
[733c2f8] | 142 | g = gnuplot_init(outplot) |
---|
[80c0417] | 143 | |
---|
[733c2f8] | 144 | g('set title \'%s %s\'' % (re.sub('.*/','',filename),title)) |
---|
[80c0417] | 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') |
---|
[5e491b3b] | 157 | g.plot(*d2) |
---|
[80c0417] | 158 | |
---|
| 159 | g('unset title') |
---|
| 160 | |
---|
| 161 | # plot onset detection function |
---|
| 162 | g('set size 1,0.7') |
---|
| 163 | g('set origin 0,0') |
---|
[5e491b3b] | 164 | g('set xrange [0:%f]' % (hopsize/samplerate*len(ofunc[0]))) |
---|
| 165 | g('set yrange [0:%f]' % (maxofunc*1.01)) |
---|
[80c0417] | 166 | g.xlabel('time') |
---|
| 167 | g.ylabel('onset detection value') |
---|
[5e491b3b] | 168 | g.plot(*d) |
---|
[80c0417] | 169 | |
---|
| 170 | g('unset multiplot') |
---|
[d4a0cc4] | 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 |
---|
[733c2f8] | 177 | import Gnuplot |
---|
| 178 | import re |
---|
[d4a0cc4] | 179 | |
---|
[5e491b3b] | 180 | d = [] |
---|
| 181 | maxpitch = 100 |
---|
| 182 | for i in range(len(pitch)): |
---|
[660c1d82] | 183 | #if len(pitch[i]) == 0: pitch[i] = [0.]; |
---|
| 184 | |
---|
[5e491b3b] | 185 | downtime = (hopsize/samplerate)*numarray.arange(len(pitch[i])) |
---|
[77671b4] | 186 | d.append(Gnuplot.Data(downtime,pitch[i],with='lines', |
---|
| 187 | title=('%d' % i))) |
---|
[5e491b3b] | 188 | maxpitch = max(maxpitch,max(pitch[i][:])*1.1) |
---|
[d4a0cc4] | 189 | |
---|
[733c2f8] | 190 | # check if ground truth exists |
---|
[d4a0cc4] | 191 | datafile = filename.replace('.wav','.txt') |
---|
[2bd1a2a] | 192 | if datafile == filename: datafile = "" |
---|
[d4a0cc4] | 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" |
---|
[77671b4] | 198 | values = aubio.txtfile.read_datafile(datafile) |
---|
[733c2f8] | 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')) |
---|
[d4a0cc4] | 206 | |
---|
| 207 | # audio data |
---|
| 208 | time,data = audio_to_array(filename) |
---|
| 209 | f = make_audio_plot(time,data) |
---|
| 210 | |
---|
[733c2f8] | 211 | g = gnuplot_init(outplot) |
---|
| 212 | g('set title \'%s %s\'' % (re.sub('.*/','',filename),title)) |
---|
[d4a0cc4] | 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') |
---|
[5e491b3b] | 227 | g('set xrange [0:%f]' % max(time)) |
---|
| 228 | g('set yrange [40:%f]' % maxpitch) |
---|
[77671b4] | 229 | g('set key right top') |
---|
[d4a0cc4] | 230 | g.xlabel('time') |
---|
| 231 | g.ylabel('frequency (Hz)') |
---|
[5e491b3b] | 232 | g.plot(*d) |
---|
[d4a0cc4] | 233 | g('unset multiplot') |
---|
[733c2f8] | 234 | |
---|
| 235 | def gnuplot_init(outplot,debug=0,persist=1): |
---|
| 236 | # prepare the plot |
---|
[0baafcf] | 237 | import Gnuplot |
---|
[733c2f8] | 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 |
---|
[0baafcf] | 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 |
---|