Changeset 65ebf30


Ignore:
Timestamp:
Mar 29, 2005, 6:12:09 PM (19 years ago)
Author:
Paul Brossier <piem@altern.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
a4b089c
Parents:
62c6075
Message:

some gnuplot cleanup and a function to plot a sound

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/aubio/gnuplot.py

    r62c6075 r65ebf30  
    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
    24 
    2524
    2625from numarray import *
     
    2827
    2928def 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
    4342def 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
    5049def 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
     
    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
     
    10192        g.hardcopy(fileout, enhanced=1, color=0)
    10293
     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
    103118
     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 TracChangeset for help on using the changeset viewer.