1 """Copyright (C) 2004 Paul Brossier <piem@altern.org>
2 print aubio.__LICENSE__ for the terms of use
6 Copyright (C) 2004 Paul Brossier <piem@altern.org>
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.
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.
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.
25 from numarray import *
26 import Gnuplot, Gnuplot.funcutils
28 def plotnote(la,title=None) :
29 if la[0,:].size() == 3:
30 d = plotnote_withends(la, plot_title=title)
32 # scale data if in freq (for REF.txt files)
33 if max(la[:,1] > 128 ):
34 print "scaling frequency data to midi range"
36 la[:,1] = log(la[:,1])/0.6931
39 d = plotnote_withoutends(la, plot_title=title)
42 def plotnote_multi(lalist,title=None,fileout=None) :
44 for i in range(len(lalist)):
45 d.append(plotnote(lalist[i], title=title))
49 def plotnote_withends(la,plot_title=None) :
51 x_widths = array(la[:,1]-la[:,0])/2.
52 d.append(Gnuplot.Data(
53 la[:,0]+x_widths, # x centers
56 __notesheight*ones(len(la)), # y errors
57 title=plot_title,with=('boxxyerrorbars fs 3')))
61 def plotnote_withoutends(la,plot_title=None) :
62 """ bug: fails drawing last note """
64 x_widths = array(la[1:,0]-la[:-1,0])/2;
65 d.append(Gnuplot.Data(
66 la[:-1,0]+x_widths, # x centers
67 la[:-1,1], # y centers
69 __notesheight*ones(len(la)-1), # y errors
70 title=plot_title,with=('boxxyerrorbars fs 3')))
73 def plotnote_do(d,fileout=None):
74 g = Gnuplot.Gnuplot(debug=1, persist=1)
75 g.gnuplot('set style fill solid border 1; \
77 set boxwidth 0.9 relative; \
82 set grid xtics ytics mxtics mytics')
85 g.ylabel('Midi pitch')
87 #g.gnuplot('set multiplot')
90 #g.gnuplot('set nomultiplot')
92 g.hardcopy(fileout, enhanced=1, color=0)
94 def audio_to_array(filename):
95 import aubio.aubioclass
97 filei = aubio.aubioclass.sndfile(filename)
98 framestep = 1/(filei.samplerate()+0.)
99 channels = filei.channels()
100 myvec = aubio.aubioclass.fvec(hopsize,channels)
103 while (readsize==hopsize):
104 readsize = filei.read(hopsize,myvec)
105 #for i in range(channels):
108 while (curpos < readsize):
109 data.append(myvec.get(curpos,i))
111 time = arange(len(data))*framestep
114 def plot_audio(filenames, fileout=None, start=0, end=None, noaxis=None):
115 g = Gnuplot.Gnuplot(debug=1, persist=1)
117 todraw = len(filenames)
120 g.gnuplot('set multiplot;')
121 while (len(filenames)):
122 d.append(plot_audio_make(filenames.pop(0)))
123 if not noaxis and todraw==1:
125 g.ylabel('Amplitude')
126 g.gnuplot('set size %f,1.;' % (xsize) )
127 g.gnuplot('set origin %f,0.;' % (xorig) )
128 g.gnuplot('set style data lines; \
129 set yrange [-1.:1.]; \
130 set xrange [0:%f]' % b[-1])
133 g.gnuplot('unset multiplot;')
135 g.hardcopy(fileout, enhanced=1, color=0)
137 def make_audio_plot(time,data,maxpoints=10000):
138 """ create gnuplot plot from an audio file """
141 downsample = length/maxpoints
142 if downsample == 0: downsample = 1
143 x = numarray.array(time).resize(length)[0:-1:downsample]
144 y = numarray.array(data).resize(length)[0:-1:downsample]
145 return Gnuplot.Data(x,y,with='lines')