source: python/aubio/gnuplot.py @ 1e3089c

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

remove old plot_pitch, plot_onset and getonsets, cutfile, getpitch
remove old plot_pitch, plot_onset and getonsets, cutfile, getpitch

  • Property mode set to 100644
File size: 5.6 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):
49        g = gnuplot_init(fileout)
50        d = []
51        todraw = len(filenames)
52        xorig = 0.
53        xsize = 1./todraw
54        g.gnuplot('set multiplot;')
55        while (len(filenames)):
56                time,data = audio_to_array(filenames.pop(0))
57                d.append(make_audio_plot(time,data))
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.]; \
65                        set xrange [0:%f]' % time[-1]) 
66                g.plot(d.pop(0))
67                xorig += 1./todraw
68        g.gnuplot('unset multiplot;')
69
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
109def plot_spec(filenames, outplot='',extension='', fileout=None, start=0, end=None, noaxis=None,log=1):
110        import Gnuplot
111        g = gnuplot_create(outplot,extension)
112        todraw = len(filenames)
113        xorig = 0.
114        xsize = 1./todraw
115        data,time,freq = audio_to_spec(filenames.pop(0))
116               
117        if not noaxis and todraw==1:
118                g.xlabel('Time (s)')
119                g.ylabel('Frequency (Hz)')
120        g.gnuplot('set pm3d map')
121        #g.gnuplot('set palette rgbformulae 30,31,32')
122        #g.gnuplot('set palette')
123        g.gnuplot('set xrange [0.:%f]' % time[-1]) 
124        g.gnuplot('set yrange [1.:%f]' % (freq[-1]/1.))
125        if log:
126                g.gnuplot('set yrange [10.1:%f]' % (freq[-1]/1.))
127                g.gnuplot('set log y')
128        g.splot(Gnuplot.GridData(data,time,freq, binary=1))
129        xorig += 1./todraw
130
131def downsample_audio(time,data,maxpoints=10000):
132        """ resample audio data to last only maxpoints """
133        import numarray
134        length = len(time)
135        downsample = length/maxpoints
136        if downsample == 0: downsample = 1
137        x = numarray.array(time).resize(length)[0:-1:downsample]
138        y = numarray.array(data).resize(length)[0:-1:downsample]
139        return x,y
140
141def make_audio_plot(time,data,maxpoints=10000):
142        """ create gnuplot plot from an audio file """
143        import numarray
144        import Gnuplot, Gnuplot.funcutils
145        length = len(time)
146        downsample = length/maxpoints
147        if downsample == 0: downsample = 1
148        x = numarray.array(time).resize(length)[0:-1:downsample]
149        y = numarray.array(data).resize(length)[0:-1:downsample]
150        return Gnuplot.Data(x,y,with='lines')
151
152def gnuplot_init(outplot,debug=0,persist=1):
153        # prepare the plot
154        import Gnuplot
155        g = Gnuplot.Gnuplot(debug=debug, persist=persist)
156        if outplot == 'stdout':
157                g("set terminal png fontfile 'p052023l.pfb'")
158                #g('set output \'%s\'' % outplot)
159        elif outplot:
160                extension = outplot.split('.')[-1]
161                if extension == 'ps': extension = 'postscript'
162                g('set terminal %s' % extension)
163                g('set output \'%s\'' % outplot)
164        return g
165
166def gnuplot_create(outplot='',extension='',debug=0,persist=1):
167        import Gnuplot
168        g = Gnuplot.Gnuplot(debug=debug, persist=persist)
169        if not extension or not outplot: return g
170        if   extension == 'ps':  ext, extension = '.ps' , 'postscript'
171        elif extension == 'png': ext, extension = '.png', 'png'
172        elif extension == 'svg': ext, extension = '.svg', 'svg'
173        else: exit("ERR: unknown plot extension")
174        g('set terminal %s' % extension)
175        if outplot != "stdout":
176                g('set output \'roc-%s%s\'' % (outplot,ext))
177        return g
Note: See TracBrowser for help on using the repository browser.