Showing the changes to python (2 of 2 files). Show all
Unified · Split
| @@ -149,151 +149,6 @@ def make_audio_plot(time,data,maxpoints=10000): · 148 unmodified lines | ||
| 149 | 149 | y = numarray.array(data).resize(length)[0:-1:downsample] |
| 150 | 150 | return Gnuplot.Data(x,y,with='lines') |
| 151 | 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 | 152 | def gnuplot_init(outplot,debug=0,persist=1): |
| 298 | 153 | # prepare the plot |
| 299 | 154 | import Gnuplot |
| @@ -71,129 +71,6 @@ def check_pitchm_mode(option, opt, value, parser): · 70 unmodified lines | ||
| 71 | 71 | print "error: unknown pitch detection output selected" |
| 72 | 72 | sys.exit(1) |
| 73 | 73 | |
| 74 | − | |
| 75 | −#def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512, | |
| 76 | −# mode='dual',localmin=False,storefunc=False,derivate=False): | |
| 77 | −# frameread = 0 | |
| 78 | −# filei = sndfile(filein) | |
| 79 | −# channels = filei.channels() | |
| 80 | −# myvec = fvec(hopsize,channels) | |
| 81 | −# readsize = filei.read(hopsize,myvec) | |
| 82 | −# opick = onsetpick(bufsize,hopsize,channels,myvec,threshold, | |
| 83 | −# mode=mode,derivate=derivate) | |
| 84 | −# mylist = list() | |
| 85 | −# if localmin: | |
| 86 | −# ovalist = [0., 0., 0., 0., 0.] | |
| 87 | −# ofunclist = [] | |
| 88 | −# while(readsize): | |
| 89 | −# readsize = filei.read(hopsize,myvec) | |
| 90 | −# isonset,val = opick.do(myvec) | |
| 91 | −# if (aubio_silence_detection(myvec(),silence)): | |
| 92 | −# isonset=0 | |
| 93 | −# if localmin: | |
| 94 | −# if val > 0: ovalist.append(val) | |
| 95 | −# else: ovalist.append(0) | |
| 96 | −# ovalist.pop(0) | |
| 97 | −# if storefunc: | |
| 98 | −# ofunclist.append(val) | |
| 99 | −# if (isonset == 1): | |
| 100 | −# if localmin: | |
| 101 | −# i=len(ovalist)-1 | |
| 102 | −# # find local minima before peak | |
| 103 | −# while ovalist[i-1] < ovalist[i] and i > 0: | |
| 104 | −# i -= 1 | |
| 105 | −# now = (frameread+1-i) | |
| 106 | −# else: | |
| 107 | −# now = frameread | |
| 108 | −# if now > 0 : | |
| 109 | −# mylist.append(now) | |
| 110 | −# else: | |
| 111 | −# now = 0 | |
| 112 | −# mylist.append(now) | |
| 113 | −# frameread += 1 | |
| 114 | −# return mylist, ofunclist | |
| 115 | −# | |
| 116 | −#def cutfile(filein,slicetimes,zerothres=0.008,bufsize=1024,hopsize=512): | |
| 117 | −# frameread = 0 | |
| 118 | −# readsize = hopsize | |
| 119 | −# filei = sndfile(filein) | |
| 120 | −# framestep = hopsize/(filei.samplerate()+0.) | |
| 121 | −# channels = filei.channels() | |
| 122 | −# newname = "%s%s%09.5f%s%s" % (filein.split(".")[0].split("/")[-1],".", | |
| 123 | −# frameread*framestep,".",filein.split(".")[-1]) | |
| 124 | −# fileo = sndfile(newname,model=filei) | |
| 125 | −# myvec = fvec(hopsize,channels) | |
| 126 | −# mycopy = fvec(hopsize,channels) | |
| 127 | −# while(readsize==hopsize): | |
| 128 | −# readsize = filei.read(hopsize,myvec) | |
| 129 | −# # write to current file | |
| 130 | −# if len(slicetimes) and frameread >= slicetimes[0]: | |
| 131 | −# slicetimes.pop(0) | |
| 132 | −# # write up to 1st zero crossing | |
| 133 | −# zerocross = 0 | |
| 134 | −# while ( abs( myvec.get(zerocross,0) ) > zerothres ): | |
| 135 | −# zerocross += 1 | |
| 136 | −# writesize = fileo.write(zerocross,myvec) | |
| 137 | −# fromcross = 0 | |
| 138 | −# while (zerocross < readsize): | |
| 139 | −# for i in range(channels): | |
| 140 | −# mycopy.set(myvec.get(zerocross,i),fromcross,i) | |
| 141 | −# fromcross += 1 | |
| 142 | −# zerocross += 1 | |
| 143 | −# del fileo | |
| 144 | −# fileo = sndfile("%s%s%09.5f%s%s" % | |
| 145 | −# (filein.split(".")[0].split("/")[-1],".", | |
| 146 | −# frameread*framestep,".",filein.split(".")[-1]),model=filei) | |
| 147 | −# writesize = fileo.write(fromcross,mycopy) | |
| 148 | −# else: | |
| 149 | −# writesize = fileo.write(readsize,myvec) | |
| 150 | −# frameread += 1 | |
| 151 | −# del fileo | |
| 152 | −# | |
| 153 | −# | |
| 154 | −#def getsilences(filein,hopsize=512,silence=-70): | |
| 155 | −# frameread = 0 | |
| 156 | −# filei = sndfile(filein) | |
| 157 | −# srate = filei.samplerate() | |
| 158 | −# channels = filei.channels() | |
| 159 | −# myvec = fvec(hopsize,channels) | |
| 160 | −# readsize = filei.read(hopsize,myvec) | |
| 161 | −# mylist = [] | |
| 162 | −# wassilence = 0 | |
| 163 | −# while(readsize==hopsize): | |
| 164 | −# readsize = filei.read(hopsize,myvec) | |
| 165 | −# if (aubio_silence_detection(myvec(),silence)==1): | |
| 166 | −# if wassilence == 0: | |
| 167 | −# mylist.append(frameread) | |
| 168 | −# wassilence == 1 | |
| 169 | −# else: wassilence = 0 | |
| 170 | −# frameread += 1 | |
| 171 | −# return mylist | |
| 172 | −# | |
| 173 | −# | |
| 174 | −#def getpitch(filein,mode=aubio_pitch_mcomb,bufsize=1024,hopsize=512,omode=aubio_pitchm_freq, | |
| 175 | −# samplerate=44100.,silence=-70): | |
| 176 | −# frameread = 0 | |
| 177 | −# filei = sndfile(filein) | |
| 178 | −# srate = filei.samplerate() | |
| 179 | −# channels = filei.channels() | |
| 180 | −# myvec = fvec(hopsize,channels) | |
| 181 | −# readsize = filei.read(hopsize,myvec) | |
| 182 | −# pitchdet = pitchdetection(mode=mode,bufsize=bufsize,hopsize=hopsize, | |
| 183 | −# channels=channels,samplerate=srate,omode=omode) | |
| 184 | −# mylist = [] | |
| 185 | −# while(readsize==hopsize): | |
| 186 | −# readsize = filei.read(hopsize,myvec) | |
| 187 | −# freq = pitchdet(myvec) | |
| 188 | −# #print "%.3f %.2f" % (now,freq) | |
| 189 | −# if (aubio_silence_detection(myvec(),silence)!=1): | |
| 190 | −# mylist.append(freq) | |
| 191 | −# else: | |
| 192 | −# mylist.append(-1.) | |
| 193 | −# frameread += 1 | |
| 194 | −# return mylist | |
| 195 | − | |
| 196 | − | |
| 197 | 74 | class taskparams(object): |
| 198 | 75 | """ default parameters for task classes """ |
| 199 | 76 | def __init__(self,input=None,output=None): |