Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -18,65 +18,56 @@ __LICENSE__ = """\ · 17 unmodified lines | ||
| 18 | 18 | You should have received a copy of the GNU General Public License |
| 19 | 19 | along with this program; if not, write to the Free Software |
| 20 | 20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | −""" | |
| 21 | +""" | |
| 22 | 22 | |
| 23 | 23 | __notesheight = 0.25 |
| 24 | 24 | |
| 25 | − | |
| 26 | 25 | from numarray import * |
| 27 | 26 | import Gnuplot, Gnuplot.funcutils |
| 28 | 27 | |
| 29 | 28 | def plotnote(la,title=None) : |
| 30 | − if la[0,:].size() == 3: | |
| 31 | − d = plotnote_withends(la, plot_title=title) | |
| 32 | − else: | |
| 33 | − # scale data if in freq (for REF.txt files) | |
| 34 | − if max(la[:,1] > 128 ): | |
| 35 | − print "scaling frequency data to midi range" | |
| 36 | − la[:,1] /= 6.875 | |
| 37 | − la[:,1] = log(la[:,1])/0.6931 | |
| 38 | − la[:,1] *= 12 | |
| 39 | − la[:,1] -= 3 | |
| 40 | − d = plotnote_withoutends(la, plot_title=title) | |
| 41 | − return d | |
| 29 | + if la[0,:].size() == 3: | |
| 30 | + d = plotnote_withends(la, plot_title=title) | |
| 31 | + else: | |
| 32 | + # scale data if in freq (for REF.txt files) | |
| 33 | + if max(la[:,1] > 128 ): | |
| 34 | + print "scaling frequency data to midi range" | |
| 35 | + la[:,1] /= 6.875 | |
| 36 | + la[:,1] = log(la[:,1])/0.6931 | |
| 37 | + la[:,1] *= 12 | |
| 38 | + la[:,1] -= 3 | |
| 39 | + d = plotnote_withoutends(la, plot_title=title) | |
| 40 | + return d | |
| 42 | 41 | |
| 43 | 42 | def plotnote_multi(lalist,title=None,fileout=None) : |
| 44 | − d=list() | |
| 45 | − for i in range(len(lalist)): | |
| 46 | − d.append(plotnote(lalist[i], title=title)) | |
| 47 | − return d | |
| 43 | + d=list() | |
| 44 | + for i in range(len(lalist)): | |
| 45 | + d.append(plotnote(lalist[i], title=title)) | |
| 46 | + return d | |
| 48 | 47 | |
| 49 | 48 | |
| 50 | 49 | def plotnote_withends(la,plot_title=None) : |
| 51 | − d=[] | |
| 52 | − | |
| 53 | − x_widths = array(la[:,1]-la[:,0])/2. | |
| 54 | − | |
| 55 | − d.append(Gnuplot.Data( | |
| 56 | − la[:,0]+x_widths, # x centers | |
| 57 | − la[:,2], # y centers | |
| 58 | − x_widths, # x errors | |
| 59 | − __notesheight*ones(len(la) # y errors | |
| 60 | − ), | |
| 61 | − title=plot_title,with=('boxxyerrorbars fs 3'))) | |
| 62 | − | |
| 63 | − return d | |
| 50 | + d=[] | |
| 51 | + x_widths = array(la[:,1]-la[:,0])/2. | |
| 52 | + d.append(Gnuplot.Data( | |
| 53 | + la[:,0]+x_widths, # x centers | |
| 54 | + la[:,2], # y centers | |
| 55 | + x_widths, # x errors | |
| 56 | + __notesheight*ones(len(la)), # y errors | |
| 57 | + title=plot_title,with=('boxxyerrorbars fs 3'))) | |
| 58 | + return d | |
| 64 | 59 | |
| 65 | 60 | |
| 66 | 61 | def plotnote_withoutends(la,plot_title=None) : |
| 67 | 62 | """ bug: fails drawing last note """ |
| 68 | 63 | d=[] |
| 69 | − | |
| 70 | 64 | x_widths = array(la[1:,0]-la[:-1,0])/2; |
| 71 | − | |
| 72 | 65 | d.append(Gnuplot.Data( |
| 73 | 66 | la[:-1,0]+x_widths, # x centers |
| 74 | 67 | la[:-1,1], # y centers |
| 75 | 68 | x_widths, # x errors |
| 76 | − __notesheight*ones(len(la)-1 # y errors | |
| 77 | − ), | |
| 69 | + __notesheight*ones(len(la)-1), # y errors | |
| 78 | 70 | title=plot_title,with=('boxxyerrorbars fs 3'))) |
| 79 | − | |
| 80 | 71 | return d |
| 81 | 72 | |
| 82 | 73 | def plotnote_do(d,fileout=None): |
| @@ -100,4 +91,52 @@ def plotnote_do(d,fileout=None): · 17 unmodified lines | ||
| 100 | 91 | if fileout != None: |
| 101 | 92 | g.hardcopy(fileout, enhanced=1, color=0) |
| 102 | 93 | |
| 94 | +def audio_to_array(filename): | |
| 95 | + import aubio.aubioclass | |
| 96 | + hopsize = 2048 | |
| 97 | + filei = aubio.aubioclass.sndfile(filename) | |
| 98 | + framestep = 1/(filei.samplerate()+0.) | |
| 99 | + channels = filei.channels() | |
| 100 | + myvec = aubio.aubioclass.fvec(hopsize,channels) | |
| 101 | + data = [] | |
| 102 | + readsize = hopsize | |
| 103 | + while (readsize==hopsize): | |
| 104 | + readsize = filei.read(hopsize,myvec) | |
| 105 | + #for i in range(channels): | |
| 106 | + i = 0 | |
| 107 | + curpos = 0 | |
| 108 | + while (curpos < readsize): | |
| 109 | + data.append(myvec.get(curpos,i)) | |
| 110 | + curpos+=1 | |
| 111 | + # FIXME again for the last frame | |
| 112 | + curpos = 0 | |
| 113 | + while (curpos < readsize): | |
| 114 | + data.append(myvec.get(curpos,i)) | |
| 115 | + curpos+=1 | |
| 116 | + time = arange(len(data))*framestep | |
| 117 | + return time,data | |
| 118 | + | |
| 119 | +def plot_audio(filenames, fileout=None, start=0, end=None, noaxis=None, task=audio_to_array): | |
| 120 | + g = Gnuplot.Gnuplot(debug=1, persist=1) | |
| 121 | + d = [] | |
| 122 | + todraw = len(filenames) | |
| 123 | + xorig = 0. | |
| 124 | + xsize = 1./todraw | |
| 125 | + g.gnuplot('set multiplot;') | |
| 126 | + while (len(filenames)): | |
| 127 | + b,a = task(filenames.pop(0)) | |
| 128 | + d.append(Gnuplot.Data(b,a)) | |
| 129 | + if not noaxis and todraw==1: | |
| 130 | + g.xlabel('Time (s)') | |
| 131 | + g.ylabel('Amplitude') | |
| 132 | + g.gnuplot('set size %f,1.;' % (xsize) ) | |
| 133 | + g.gnuplot('set origin %f,0.;' % (xorig) ) | |
| 134 | + g.gnuplot('set style data lines; \ | |
| 135 | + set yrange [-1.:1.]; \ | |
| 136 | + set xrange [0:%f]' % b[-1]) | |
| 137 | + g.plot(d.pop(0)) | |
| 138 | + xorig += 1./todraw | |
| 139 | + g.gnuplot('unset multiplot;') | |
| 140 | + if fileout != None: | |
| 141 | + g.hardcopy(fileout, enhanced=1, color=0) | |
| 103 | 142 | |