source: python/aubio/gnuplot.py @ a4b089c

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since a4b089c was 65ebf30, checked in by Paul Brossier <piem@altern.org>, 20 years ago

some gnuplot cleanup and a function to plot a sound

  • Property mode set to 100644
File size: 4.2 KB
Line 
1"""Copyright (C) 2004 Paul Brossier <piem@altern.org>
2print aubio.__LICENSE__ for the terms of use
3"""
4
5__LICENSE__ = """\
6         Copyright (C) 2004 Paul Brossier <piem@altern.org>
7
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.
12
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.
17
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.
21"""
22
23__notesheight = 0.25
24
25from numarray import *
26import Gnuplot, Gnuplot.funcutils
27
28def plotnote(la,title=None) :
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
41
42def plotnote_multi(lalist,title=None,fileout=None) :
43        d=list()
44        for i in range(len(lalist)):
45            d.append(plotnote(lalist[i], title=title))
46        return d
47       
48
49def plotnote_withends(la,plot_title=None) :
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
59
60
61def plotnote_withoutends(la,plot_title=None) :
62        """ bug: fails drawing last note """
63        d=[]
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
68                x_widths,                       # x errors
69                __notesheight*ones(len(la)-1),  # y errors
70                title=plot_title,with=('boxxyerrorbars fs 3')))
71        return d
72
73def plotnote_do(d,fileout=None):
74    g = Gnuplot.Gnuplot(debug=1, persist=1)
75    g.gnuplot('set style fill solid border 1; \
76    set size ratio 1/6; \
77    set boxwidth 0.9 relative; \
78    set mxtics 2.5; \
79    set mytics 2.5; \
80    set xtics 5; \
81    set ytics 1; \
82    set grid xtics ytics mxtics mytics')
83
84    g.xlabel('Time (s)')
85    g.ylabel('Midi pitch')
86    # do the plot
87    #g.gnuplot('set multiplot')
88    #for i in d:
89    g.plot(d[0])
90    #g.gnuplot('set nomultiplot')
91    if fileout != None:
92        g.hardcopy(fileout, enhanced=1, color=0)
93
94def 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
119def 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)
142
Note: See TracBrowser for help on using the repository browser.