source: python/aubio/gnuplot.py @ 3e29681

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 3e29681 was 7538ac5, checked in by Paul Brossier <piem@altern.org>, 19 years ago

remove useless multiple plot and title to plot_spec
remove useless multiple plot and title to plot_spec

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