Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -53,8 +53,8 @@ def plot_audio(filenames, fileout=None, start=0, end=None, noaxis=None): · 52 unmodified lines | |||
| 53 | xsize = 1./todraw | 53 | xsize = 1./todraw |
| 54 | g.gnuplot('set multiplot;') | 54 | g.gnuplot('set multiplot;') |
| 55 | while (len(filenames)): | 55 | while (len(filenames)): |
| 56 | time,data = audio_to_array(filenames.pop(0)) | 56 | time,data = audio_to_array(filenames.pop(0)) |
| 57 | d.append(make_audio_plot(time,data)) | 57 | d.append(make_audio_plot(time,data)) |
| 58 | if not noaxis and todraw==1: | 58 | if not noaxis and todraw==1: |
| 59 | g.xlabel('Time (s)') | 59 | g.xlabel('Time (s)') |
| 60 | g.ylabel('Amplitude') | 60 | g.ylabel('Amplitude') |
| @@ -67,8 +67,69 @@ def plot_audio(filenames, fileout=None, start=0, end=None, noaxis=None): · 6 unmodified lines | |||
| 67 | xorig += 1./todraw | 67 | xorig += 1./todraw |
| 68 | g.gnuplot('unset multiplot;') | 68 | g.gnuplot('unset multiplot;') |
| 69 | 69 | ||
| 70 | def audio_to_spec(filename): | ||
| 71 | from aubioclass import fvec,cvec,pvoc,sndfile | ||
| 72 | from math import log | ||
| 73 | bufsize = 256*16 | ||
| 74 | hopsize = bufsize/4 # could depend on filelength | ||
| 75 | filei = sndfile(filename) | ||
| 76 | srate = float(filei.samplerate()) | ||
| 77 | framestep = hopsize/srate | ||
| 78 | freqstep = srate/bufsize | ||
| 79 | channels = filei.channels() | ||
| 80 | myvec = fvec(hopsize,channels) | ||
| 81 | myfft = cvec(bufsize,channels) | ||
| 82 | pv = pvoc(bufsize,hopsize,channels) | ||
| 83 | data,time,freq = [],[],[] | ||
| 84 | for f in range(bufsize/2): | ||
| 85 | freq.append(f*freqstep) | ||
| 86 | readsize = hopsize | ||
| 87 | frameread = 0 | ||
| 88 | while (readsize==hopsize): | ||
| 89 | readsize = filei.read(hopsize,myvec) | ||
| 90 | pv.do(myvec,myfft) | ||
| 91 | frame = [] | ||
| 92 | i = 0 #for i in range(channels): | ||
| 93 | curpos = 0 | ||
| 94 | while (curpos < bufsize/2): | ||
| 95 | frame.append(log(myfft.get(curpos,i)**2+0.000001)) | ||
| 96 | curpos+=1 | ||
| 97 | time.append(frameread*framestep) | ||
| 98 | data.append(frame) | ||
| 99 | frameread += 1 | ||
| 100 | # crop data if unfinished frames | ||
| 101 | if len(data[-1]) != len(data[0]): | ||
| 102 | data = data[0:-2] | ||
| 103 | time = time[0:-2] | ||
| 104 | # verify size consistency | ||
| 105 | assert len(data) == len(time) | ||
| 106 | assert len(data[0]) == len(freq) | ||
| 107 | return data,time,freq | ||
| 108 | |||
| 109 | def plot_spec(filenames, outplot='',extension='', fileout=None, start=0, end=None, noaxis=None,log=1): | ||
| 110 | import Gnuplot | ||
| 111 | g = gnuplot_create(outplot,extension) | ||
| 112 | todraw = len(filenames) | ||
| 113 | xorig = 0. | ||
| 114 | xsize = 1./todraw | ||
| 115 | data,time,freq = audio_to_spec(filenames.pop(0)) | ||
| 116 | |||
| 117 | if not noaxis and todraw==1: | ||
| 118 | g.xlabel('Time (s)') | ||
| 119 | g.ylabel('Frequency (Hz)') | ||
| 120 | g.gnuplot('set pm3d map') | ||
| 121 | #g.gnuplot('set palette rgbformulae 30,31,32') | ||
| 122 | #g.gnuplot('set palette') | ||
| 123 | g.gnuplot('set xrange [0.:%f]' % time[-1]) | ||
| 124 | g.gnuplot('set yrange [1.:%f]' % (freq[-1]/1.)) | ||
| 125 | if log: | ||
| 126 | g.gnuplot('set yrange [10.1:%f]' % (freq[-1]/1.)) | ||
| 127 | g.gnuplot('set log y') | ||
| 128 | g.splot(Gnuplot.GridData(data,time,freq, binary=1)) | ||
| 129 | xorig += 1./todraw | ||
| 130 | |||
| 70 | def downsample_audio(time,data,maxpoints=10000): | 131 | def downsample_audio(time,data,maxpoints=10000): |
| 71 | """ create gnuplot plot from an audio file """ | 132 | """ resample audio data to last only maxpoints """ |
| 72 | import numarray | 133 | import numarray |
| 73 | length = len(time) | 134 | length = len(time) |
| 74 | downsample = length/maxpoints | 135 | downsample = length/maxpoints |
| @@ -178,7 +239,7 @@ def plot_pitch(filename, pitch, samplerate=44100., hopsize=512, outplot=None): · 103 unmodified lines | |||
| 178 | import re | 239 | import re |
| 179 | 240 | ||
| 180 | d = [] | 241 | d = [] |
| 181 | maxpitch = 100 | 242 | maxpitch = 1000. |
| 182 | for i in range(len(pitch)): | 243 | for i in range(len(pitch)): |
| 183 | #if len(pitch[i]) == 0: pitch[i] = [0.]; | 244 | #if len(pitch[i]) == 0: pitch[i] = [0.]; |
| 184 | 245 | ||
| @@ -227,6 +288,7 @@ def plot_pitch(filename, pitch, samplerate=44100., hopsize=512, outplot=None): · 42 unmodified lines | |||
| 227 | g('set xrange [0:%f]' % max(time)) | 288 | g('set xrange [0:%f]' % max(time)) |
| 228 | g('set yrange [40:%f]' % maxpitch) | 289 | g('set yrange [40:%f]' % maxpitch) |
| 229 | g('set key right top') | 290 | g('set key right top') |
| 291 | g('set noclip one') | ||
| 230 | g.xlabel('time') | 292 | g.xlabel('time') |
| 231 | g.ylabel('frequency (Hz)') | 293 | g.ylabel('frequency (Hz)') |
| 232 | g.plot(*d) | 294 | g.plot(*d) |