History

some gnuplot cleanup and a function to plot a sound

Paul Brossier committed 21 years ago

Commit 65ebf301 parent 62c60751 file · +75 −36Browse files

Showing the changes to python (1 of 1 file). Show all

Unified · Split

python/aubio/gnuplot.py+75−36
@@ -18,65 +18,56 @@ __LICENSE__ = """\ · 17 unmodified lines
1818 You should have received a copy of the GNU General Public License
1919 along with this program; if not, write to the Free Software
2020 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21−"""
21+"""
2222
2323 __notesheight = 0.25
2424
25−
2625 from numarray import *
2726 import Gnuplot, Gnuplot.funcutils
2827
2928 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
4241
4342 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
4847
4948
5049 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
6459
6560
6661 def plotnote_withoutends(la,plot_title=None) :
6762 """ bug: fails drawing last note """
6863 d=[]
69−
7064 x_widths = array(la[1:,0]-la[:-1,0])/2;
71−
7265 d.append(Gnuplot.Data(
7366 la[:-1,0]+x_widths, # x centers
7467 la[:-1,1], # y centers
7568 x_widths, # x errors
76− __notesheight*ones(len(la)-1 # y errors
77− ),
69+ __notesheight*ones(len(la)-1), # y errors
7870 title=plot_title,with=('boxxyerrorbars fs 3')))
79−
8071 return d
8172
8273 def plotnote_do(d,fileout=None):
@@ -100,4 +91,52 @@ def plotnote_do(d,fileout=None): · 17 unmodified lines
10091 if fileout != None:
10192 g.hardcopy(fileout, enhanced=1, color=0)
10293
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)
103142

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t