source: python/aubio/gnuplot.py @ 7cded32

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

add x/ysize to plot_aubio/spec, move spec to db scale, add zoffset, add epsc color
add x/ysize to plot_aubio/spec, move spec to db scale, add zoffset, add epsc color

  • Property mode set to 100644
File size: 6.1 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
24__notesheight = 0.25
25
26
27def audio_to_array(filename):
28        import aubio.aubioclass
29        import numarray
30        hopsize  = 2048
31        filei    = aubio.aubioclass.sndfile(filename)
32        framestep = 1/(filei.samplerate()+0.)
33        channels = filei.channels()
34        myvec    = aubio.aubioclass.fvec(hopsize,channels)
35        data = []
36        readsize = hopsize
37        while (readsize==hopsize):
38                readsize = filei.read(hopsize,myvec)
39                #for i in range(channels):
40                i = 0
41                curpos = 0
42                while (curpos < readsize):
43                        data.append(myvec.get(curpos,i))
44                        curpos+=1
45        time = numarray.arange(len(data))*framestep
46        return time,data
47
48def plot_audio(filenames, fileout=None, start=0, end=None, noaxis=None,xsize=1.,ysize=1.):
49        g = gnuplot_init(fileout)
50        d = []
51        todraw = len(filenames)
52        xorig = 0.
53        xratio = 1./todraw
54        g.gnuplot('set size %f,%f;' % (xsize,ysize) )
55        g.gnuplot('set multiplot;')
56        while (len(filenames)):
57                time,data = audio_to_array(filenames.pop(0))
58                if not noaxis and todraw==1:
59                        if max(time) < 1.:
60                                time = [t*1000. for t in time]
61                                g.xlabel('Time (ms)')
62                        else:
63                                g.xlabel('Time (s)')
64                        g.ylabel('Amplitude')
65                d.append(make_audio_plot(time,data))
66                g.gnuplot('set size %f,%f;' % (xsize*xratio,ysize) )
67                g.gnuplot('set origin %f,0.;' % (xorig) )
68                g.gnuplot('set style data lines; \
69                        set yrange [-1.:1.]; \
70                        set xrange [0:%f]' % time[-1]) 
71                g.plot(d.pop(0))
72                xorig += xsize*xratio
73        g.gnuplot('unset multiplot;')
74
75def audio_to_spec(filename,minf = 0, maxf = 0, lowthres = 0.):
76        from aubioclass import fvec,cvec,pvoc,sndfile
77        from math import log10
78        bufsize   = 8192
79        hopsize   = bufsize/8 # could depend on filelength
80        filei     = sndfile(filename)
81        srate     = float(filei.samplerate())
82        framestep = hopsize/srate
83        freqstep  = srate/bufsize
84        channels  = filei.channels()
85        myvec = fvec(hopsize,channels)
86        myfft = cvec(bufsize,channels)
87        pv    = pvoc(bufsize,hopsize,channels)
88        data,time,freq = [],[],[]
89
90        if maxf == 0.: maxf = bufsize/2
91        else: maxf = int(maxf/freqstep)
92        if minf: minf = int(minf/freqstep)
93        else: minf = 0 
94
95        for f in range(minf,maxf):
96                freq.append(f*freqstep)
97        readsize = hopsize
98        frameread = 0
99        while (readsize==hopsize):
100                readsize = filei.read(hopsize,myvec)
101                pv.do(myvec,myfft)
102                frame = []
103                i = 0 #for i in range(channels):
104                curpos = minf
105                while (curpos < maxf):
106                        frame.append(max(lowthres,20.*log10(myfft.get(curpos,i)**2+0.00001)))
107                        curpos+=1
108                time.append(frameread*framestep)
109                data.append(frame)
110                frameread += 1
111        # crop data if unfinished frames
112        if len(data[-1]) != len(data[0]):
113                data = data[0:-2]
114                time = time[0:-2]
115        # verify size consistency
116        assert len(data) == len(time)
117        assert len(data[0]) == len(freq)
118        return data,time,freq
119
120def plot_spec(filename, outplot='',extension='', fileout=None, start=0, end=None, noaxis=None,log=1, minf=0, maxf= 0, xsize = 1., ysize = 1.):
121        import Gnuplot
122        g = gnuplot_create(outplot,extension)
123        data,time,freq = audio_to_spec(filename,minf=minf,maxf=maxf)
124        xorig = 0.
125        if not noaxis:
126                g.xlabel('Time (s)')
127                g.ylabel('Frequency (Hz)')
128        g('set size %f,%f' % (xsize, ysize))
129        g('set pm3d map')
130        g('set palette rgbformulae -25,-24,-32')
131        #g('set colorbox horizontal')
132        g('set xrange [0.:%f]' % time[-1]) 
133        g('set yrange [%f:%f]' % (minf,maxf))
134        if log:
135                g('set yrange [%f:%f]' % (max(10,minf),maxf))
136                g('set log y')
137        g.splot(Gnuplot.GridData(data,time,freq, binary=1, title='mag. (dB)'))
138        #xorig += 1./todraw
139
140def downsample_audio(time,data,maxpoints=10000):
141        """ resample audio data to last only maxpoints """
142        import numarray
143        length = len(time)
144        downsample = length/maxpoints
145        if downsample == 0: downsample = 1
146        x = numarray.array(time).resize(length)[0:-1:downsample]
147        y = numarray.array(data).resize(length)[0:-1:downsample]
148        return x,y
149
150def make_audio_plot(time,data,maxpoints=10000):
151        """ create gnuplot plot from an audio file """
152        import numarray
153        import Gnuplot, Gnuplot.funcutils
154        length = len(time)
155        downsample = length/maxpoints
156        if downsample == 0: downsample = 1
157        x = numarray.array(time).resize(length)[0:-1:downsample]
158        y = numarray.array(data).resize(length)[0:-1:downsample]
159        return Gnuplot.Data(x,y,with='lines')
160
161def gnuplot_init(outplot,debug=0,persist=1):
162        # prepare the plot
163        import Gnuplot
164        g = Gnuplot.Gnuplot(debug=debug, persist=persist)
165        if outplot == 'stdout':
166                g("set terminal png fontfile 'p052023l.pfb'")
167                #g('set output \'%s\'' % outplot)
168        elif outplot:
169                extension = outplot.split('.')[-1]
170                if extension == 'ps': extension = 'postscript'
171                g('set terminal %s' % extension)
172                g('set output \'%s\'' % outplot)
173        return g
174
175def gnuplot_create(outplot='',extension='',debug=0,persist=1):
176        import Gnuplot
177        g = Gnuplot.Gnuplot(debug=debug, persist=persist)
178        if not extension or not outplot: return g
179        if   extension == 'ps':  ext, extension = '.ps' , 'postscript'
180        elif extension == 'eps': ext, extension = '.eps' , 'postscript enhanced'
181        elif extension == 'epsc': ext, extension = '.eps' , 'postscript enhanced color'
182        elif extension == 'png': ext, extension = '.png', 'png'
183        elif extension == 'svg': ext, extension = '.svg', 'svg'
184        else: exit("ERR: unknown plot extension")
185        g('set terminal %s' % extension)
186        if outplot != "stdout":
187                g('set output \'%s%s\'' % (outplot,ext))
188        return g
Note: See TracBrowser for help on using the repository browser.