Changeset 1e3089c for python/aubio
- Timestamp:
- Mar 1, 2006, 4:21:59 AM (19 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
- Children:
- 7538ac5
- Parents:
- 0fe9aab
- Location:
- python/aubio
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/aubio/gnuplot.py
r0fe9aab r1e3089c 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.funcutils155 import aubio.txtfile156 import os.path157 import numarray158 import re159 from aubio.onsetcompare import onset_roc160 161 d,d2 = [],[]162 maxofunc = 0163 for i in range(len(onsets)):164 if len(onsets[i]) == 0: onsets[i] = [0.];165 166 # onset detection function167 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 onsets173 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 truth179 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.050191 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 data199 time,data = audio_to_array(filename)200 d2.append(make_audio_plot(time,data))201 202 # prepare the plot203 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 axis210 g('set lmargin 15')211 212 # plot waveform and onsets213 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 function223 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.txtfile236 import os.path237 import numarray238 import Gnuplot239 import re240 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 exists252 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 data269 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 axis276 g('set lmargin 15')277 # plot waveform and onsets278 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 function286 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 -
python/aubio/tasks.py
r0fe9aab r1e3089c 71 71 print "error: unknown pitch detection output selected" 72 72 sys.exit(1) 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 = 078 # 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=093 # 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)-1102 # # find local minima before peak103 # while ovalist[i-1] < ovalist[i] and i > 0:104 # i -= 1105 # now = (frameread+1-i)106 # else:107 # now = frameread108 # if now > 0 :109 # mylist.append(now)110 # else:111 # now = 0112 # mylist.append(now)113 # frameread += 1114 # return mylist, ofunclist115 #116 #def cutfile(filein,slicetimes,zerothres=0.008,bufsize=1024,hopsize=512):117 # frameread = 0118 # readsize = hopsize119 # 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 file130 # if len(slicetimes) and frameread >= slicetimes[0]:131 # slicetimes.pop(0)132 # # write up to 1st zero crossing133 # zerocross = 0134 # while ( abs( myvec.get(zerocross,0) ) > zerothres ):135 # zerocross += 1136 # writesize = fileo.write(zerocross,myvec)137 # fromcross = 0138 # while (zerocross < readsize):139 # for i in range(channels):140 # mycopy.set(myvec.get(zerocross,i),fromcross,i)141 # fromcross += 1142 # zerocross += 1143 # del fileo144 # 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 += 1151 # del fileo152 #153 #154 #def getsilences(filein,hopsize=512,silence=-70):155 # frameread = 0156 # 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 = 0163 # 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 == 1169 # else: wassilence = 0170 # frameread += 1171 # return mylist172 #173 #174 #def getpitch(filein,mode=aubio_pitch_mcomb,bufsize=1024,hopsize=512,omode=aubio_pitchm_freq,175 # samplerate=44100.,silence=-70):176 # frameread = 0177 # 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 += 1194 # return mylist195 196 73 197 74 class taskparams(object):
Note: See TracChangeset
for help on using the changeset viewer.