Changeset 80c0417 for python


Ignore:
Timestamp:
May 30, 2005, 6:44:52 AM (20 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:
d09cad2
Parents:
588a09f
Message:

merged aubioplot-onsets into aubiocut, added some more options

Location:
python
Files:
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • python/README

    r588a09f r80c0417  
    22routines. The python interface for libaubio is generated using Swig.
    33
    4 To have it working before installation, set LD_LIBRARY_PATH as:
    5 LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib:../src/.libs
     4To have it working before installation, you will need to set LD_LIBRARY_PATH
     5# export LD_LIBRARY_PATH=../src/.libs:../ext/.libs
     6for instance, to run the python script from within aubio/python/.
  • python/aubio/aubioclass.py

    r588a09f r80c0417  
    7474class onsetpick:
    7575    """ superclass for aubio_pvoc + aubio_onsetdetection + aubio_peakpicker """
    76     def __init__(self,bufsize,hopsize,channels,myvec,threshold,mode='dual'):
     76    def __init__(self,bufsize,hopsize,channels,myvec,threshold,mode='dual',derivate=False):
    7777        self.myfft    = cvec(bufsize,channels)
    7878        self.pv       = pvoc(bufsize,hopsize,channels)
     
    8787        self.mode     = mode
    8888        self.pp       = peakpick(float(threshold))
     89        self.derivate = derivate
     90        self.oldval   = 0.
    8991
    9092    def do(self,myvec):
     
    9496                self.myod2.do(self.myfft,self.myonset2)
    9597                self.myonset.set(self.myonset.get(0,0)*self.myonset2.get(0,0),0,0)
     98        if self.derivate:
     99                val         = self.myonset.get(0,0)
     100                dval        = val - self.oldval
     101                self.oldval = val
     102                if dval > 0: self.myonset.set(dval,0,0)
     103                else:  self.myonset.set(0.,0,0)
    96104        return self.pp.do(self.myonset),self.myonset.get(0,0)
    97105
    98 def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512,mode='dual',localmin=False,storefunc=False):
     106def getonsets(filein,threshold=0.2,silence=-70.,bufsize=1024,hopsize=512,
     107                mode='dual',localmin=False,storefunc=False,derivate=False):
    99108        frameread = 0
    100109        filei     = sndfile(filein)
     
    102111        myvec     = fvec(hopsize,channels)
    103112        readsize  = filei.read(hopsize,myvec)
    104         opick     = onsetpick(bufsize,hopsize,channels,myvec,threshold,mode=mode)
     113        opick     = onsetpick(bufsize,hopsize,channels,myvec,threshold,
     114                         mode=mode,derivate=derivate)
    105115        mylist    = list()
    106116        if localmin:
     
    114124                        isonset=0
    115125                if localmin:
    116                         ovalist.append(val)
     126                        if val > 0: ovalist.append(val)
     127                        else: ovalist.append(0)
    117128                        ovalist.pop(0)
    118129                if storefunc:
     
    136147        else: return mylist
    137148
    138 def cutfile(filein,slicetimes,zerothres=0.002,bufsize=1024,hopsize=512):
     149def cutfile(filein,slicetimes,zerothres=0.008,bufsize=1024,hopsize=512):
    139150    frameread = 0
    140151    readsize  = hopsize
     
    154165            zerocross = 0
    155166            while ( abs( myvec.get(zerocross,0) ) > zerothres ):
    156                 zerocross += 1
     167                zerocross += 1
    157168            writesize = fileo.write(zerocross,myvec)
    158169            fromcross = 0
    159170            while (zerocross < readsize):
    160                 for i in range(channels):
    161                         mycopy.set(myvec.get(zerocross,i),fromcross,i)
    162                 fromcross += 1
    163                 zerocross += 1
     171                for i in range(channels):
     172                    mycopy.set(myvec.get(zerocross,i),fromcross,i)
     173                    fromcross += 1
     174                    zerocross += 1
    164175            del fileo
    165176            fileo = sndfile("%s%s%f%s%s" %
    166                 (filein.split(".")[0].split("/")[-1],".",
    167                 frameread*framestep,".",filein.split(".")[-1]),model=filei)
     177                (filein.split(".")[0].split("/")[-1],".",
     178                frameread*framestep,".",filein.split(".")[-1]),model=filei)
    168179            writesize = fileo.write(fromcross,mycopy)
    169180        else:
  • python/aubio/gnuplot.py

    r588a09f r80c0417  
    137137def make_audio_plot(time,data,maxpoints=10000):
    138138        """ create gnuplot plot from an audio file """
    139         import numarray
    140139        length = len(time)
    141140        downsample = length/maxpoints
    142141        if downsample == 0: downsample = 1
    143         x = numarray.array(time).resize(length)[0:-1:downsample]
    144         y = numarray.array(data).resize(length)[0:-1:downsample]
     142        x = array(time).resize(length)[0:-1:downsample]
     143        y = array(data).resize(length)[0:-1:downsample]
    145144        return Gnuplot.Data(x,y,with='lines')
     145
     146
     147def plot_onsets(filename, onsets, ofunc, samplerate=44100., hopsize=512, outplot=None):
     148        import aubio.txtfile
     149        import os.path
     150        import numarray
     151        from aubio.onsetcompare import onset_roc
     152
     153        # onset detection function
     154        downtime = (hopsize/samplerate)*numarray.arange(len(ofunc))
     155        d = Gnuplot.Data(downtime,ofunc,with='lines')
     156
     157        # detected onsets
     158        x1 = (hopsize/samplerate)*numarray.array(onsets)
     159        y1 = max(ofunc)*numarray.ones(len(onsets))
     160        e = Gnuplot.Data(x1,-y1,with='impulses')
     161        e2= Gnuplot.Data(x1,y1,with='impulses')
     162
     163        # check if datafile exists truth
     164        datafile = filename.replace('.wav','.txt')
     165        if not os.path.isfile(datafile):
     166                title = "truth file not found"
     167                t = Gnuplot.Data(0,0,with='impulses')
     168        else:
     169                t_onsets = aubio.txtfile.read_datafile(datafile)
     170                y2 = max(ofunc)*numarray.ones(len(t_onsets))
     171                x2 = numarray.array(t_onsets).resize(len(t_onsets))
     172                t = Gnuplot.Data(x2,y2,with='impulses')
     173               
     174                tol = 0.050
     175
     176                orig, missed, merged, expc, bad, doubled = \
     177                        onset_roc(x2,x1,tol)
     178                title = "GD %2.3f%% FP %2.3f%%" % \
     179                        ((100*float(orig-missed-merged)/(orig)),
     180                         (100*float(bad+doubled)/(orig)))
     181                #print  orig, missed, merged, expc, bad, doubled
     182                #print "GD %2.8f\t"        % (100*float(orig-missed-merged)/(orig)),
     183                #print "FP %2.8f\t"        % (100*float(bad+doubled)/(orig))       ,
     184                #print "GD-merged %2.8f\t" % (100*float(orig-missed)/(orig))       ,
     185                #print "FP-pruned %2.8f\t" % (100*float(bad)/(orig))               
     186
     187        # audio data
     188        time,data = audio_to_array(filename)
     189        f = make_audio_plot(time,data)
     190
     191        # prepare the plot
     192        g = Gnuplot.Gnuplot(debug=1, persist=1)
     193        if outplot:
     194                extension = outplot.split('.')[-1]
     195                if extension == 'ps': extension = 'postscript'
     196                g('set terminal %s' % extension)
     197                g('set output \'%s\'' % outplot)
     198
     199        g('set title \'%s %s\'' % (filename,title))
     200
     201        g('set multiplot')
     202
     203        # hack to align left axis
     204        g('set lmargin 15')
     205
     206        # plot waveform and onsets
     207        g('set size 1,0.3')
     208        g('set origin 0,0.7')
     209        g('set xrange [0:%f]' % max(time))
     210        g('set yrange [-1:1]')
     211        g.ylabel('amplitude')
     212        g.plot(f,e,t)
     213       
     214        g('unset title')
     215
     216        # plot onset detection function
     217        g('set size 1,0.7')
     218        g('set origin 0,0')
     219        g('set xrange [0:%f]' % (hopsize/samplerate*len(ofunc)))
     220        g('set yrange [0:%f]' % (max(ofunc)*1.01))
     221        g.xlabel('time')
     222        g.ylabel('onset detection value')
     223        g.plot(d,e2)
     224
     225        g('unset multiplot')
  • python/aubiocut

    r588a09f r80c0417  
    3434        parser.add_option("-m","--mode", action="callback",
    3535                          callback=check_mode, dest="mode", default='dual',
    36                           help="onsetdetection mode [default=dual] \
     36                          help="onset detection mode [default=dual] \
    3737                          complexdomain|hfc|phase|specdiff|energy|dual")
    3838        parser.add_option("-B","--bufsize",
     
    5252                          help="minimum inter onset interval [default=0.048]")
    5353        parser.add_option("-D","--delay",
    54                           action="store", dest="delay", default=0.022,
    55                           help="number of seconds to take back [default=0.022]")
     54                          action="store", dest="delay", 
     55                          help="number of seconds to take back [default=system]\
     56                          default system delay is 2*hopsize/samplerate")
    5657        parser.add_option("-L","--localmin",
    5758                          action="store_true", dest="localmin", default=False,
     
    6162                          help="cut input sound file at detected labels \
    6263                          best used with option -L")
     64        parser.add_option("-d","--derivate",
     65                          action="store_true", dest="derivate", default=False,
     66                          help="derivate onset detection function")
    6367        # to be implemented
    64         # plotting functions
    65         parser.add_option("-d","--derivative",
    66                           action="store_true", dest="derivative", default=False,
    67                           help="NOT IMPLEMENTED derivate onset detection function")
    68         parser.add_option("-p","--plot",
    69                           action="store_true", dest="doplot", default=False,
    70                           help="NOT IMPLEMENTED draw plot")
    71         parser.add_option("-O","--outplot",
    72                           action="store", dest="output-plot", default=None,
    73                           help="NOT IMPLEMENTED save plot to output.{ps,png}")
    7468        parser.add_option("-z","--zerocross",
    7569                          action="store_true", dest="zerocross", default=False,
     
    7872                          action="store_true", dest="beat", default=False,
    7973                          help="NOT IMPLEMENTED output beat locations")
     74        # plotting functions
     75        parser.add_option("-p","--plot",
     76                          action="store_true", dest="plot", default=False,
     77                          help="draw plot")
     78        parser.add_option("-O","--outplot",
     79                          action="store", dest="outplot", default=None,
     80                          help="save plot to output.{ps,png}")
    8081        parser.add_option("-v","--verbose",
    8182                          action="store_true", dest="verbose", default=False,
     
    100101silence    = float(options.silence)
    101102mintol     = float(options.mintol)*step
    102 delay      = float(options.delay)
     103# default take back system delay
     104if options.delay: delay = float(options.delay)
     105else:             delay = 2./step
    103106
    104107if options.beat:
    105108        #onsets = getbeats(filename,threshold,silence,mode=options.mode)
    106109        exit("not implemented yet")
     110elif options.plot:
     111        onsets, ofunc = getonsets(filename,threshold,silence,
     112                mode=options.mode,localmin=options.localmin,
     113                derivate=options.derivate,
     114                bufsize=bufsize,hopsize=hopsize,storefunc=True)
    107115else:
    108116        onsets = getonsets(filename,threshold,silence,
    109117                mode=options.mode,localmin=options.localmin,
     118                derivate=options.derivate,
    110119                bufsize=bufsize,hopsize=hopsize)
    111120
     
    129138        for i in onsets: print "%f" % (i/step)
    130139
     140if options.plot:
     141        from aubio.gnuplot import plot_onsets
     142        plot_onsets(filename, onsets, ofunc,
     143                samplerate=samplerate, hopsize=hopsize, outplot=options.outplot)
     144
    131145if options.cut:
    132146        cutfile(filename,onsets,bufsize=bufsize,hopsize=hopsize)
Note: See TracChangeset for help on using the changeset viewer.