Changeset 300adc3


Ignore:
Timestamp:
Mar 1, 2006, 4:17:34 AM (18 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:
0fe9aab
Parents:
0484dc1
Message:

update taskpitch, use oplots instead of gnuplot
update taskpitch, use oplots instead of gnuplot

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/aubio/tasks.py

    r0484dc1 r300adc3  
    212212                self.onsetmode = 'dual'
    213213                self.pitchmode = 'yin'
     214                self.pitchsmooth = 20
     215                self.pitchmin=100.
     216                self.pitchmax=1500.
    214217                self.dcthreshold = -1.
    215218                self.omode = aubio_pitchm_freq
     
    293296        def __init__(self,input,params=None):
    294297                task.__init__(self,input,params=params)
     298                self.shortlist = [0. for i in range(self.params.pitchsmooth)]
    295299                self.pitchdet  = pitchdetection(mode=get_pitch_mode(self.params.pitchmode),
    296300                        bufsize=self.params.bufsize,
     
    301305
    302306        def __call__(self):
    303                 #print "%.3f     %.2f" % (now,freq)
     307                from median import short_find
    304308                task.__call__(self)
    305                 freq = self.pitchdet(self.myvec)
    306                 if (aubio_silence_detection(self.myvec(),self.params.silence)!=1):
     309                if (aubio_silence_detection(self.myvec(),self.params.silence)==1):
     310                        freq = -1.
     311                else:
     312                        freq = self.pitchdet(self.myvec)
     313                minpitch = self.params.pitchmin
     314                maxpitch = self.params.pitchmax
     315                if maxpitch and freq > maxpitch :
     316                        freq = -1.
     317                elif minpitch and freq < minpitch :
     318                        freq = -1.
     319                if self.params.pitchsmooth:
     320                        self.shortlist.append(freq)
     321                        self.shortlist.pop(0)
     322                        smoothfreq = short_find(self.shortlist,
     323                                len(self.shortlist)/2)
     324                        return smoothfreq
     325                else:
    307326                        return freq
    308                 else:
    309                         return -1.
     327
     328        def compute_all(self):
     329                """ Compute data """
     330                mylist    = []
     331                while(self.readsize==self.params.hopsize):
     332                        freq = self()
     333                        mylist.append(freq)
     334                        if self.params.verbose:
     335                                self.fprint("%s\t%s" % (self.frameread*self.params.step,freq))
     336                return mylist
    310337
    311338        def gettruth(self):
     
    322349                        return sum(l)/max(float(len(l)),1)
    323350
    324                 from median import short_find
     351                from median import percental
    325352                self.truth = self.gettruth()
    326353                res = []
    327354                for i in results:
    328                         if i == -1: pass
    329                         else:
    330                                 res.append(self.truth-i)
     355                        if i <= 0: pass
     356                        else: res.append(self.truth-i)
    331357                if not res:
    332358                        avg = self.truth; med = self.truth
    333359                else:
    334360                        avg = mmean(res)
    335                         med = short_find(res,len(res)/2)
     361                        med = percental(res,len(res)/2)
    336362                return self.truth, self.truth-med, self.truth-avg
    337363
    338         def plot(self,pitch,outplot=None):
    339                 from aubio.gnuplot import plot_pitch
    340                 plot_pitch(self.input,
    341                         pitch,
    342                         samplerate=float(self.srate),
    343                         hopsize=self.params.hopsize,
    344                         outplot=outplot)
     364        def plot(self,pitch,wplot,oplots,outplot=None):
     365                from aubio.txtfile import read_datafile
     366                import os.path
     367                import numarray
     368                import Gnuplot
     369
     370                downtime = self.params.step*numarray.arange(len(pitch))
     371                oplots.append(Gnuplot.Data(downtime,pitch,with='lines',
     372                        title=self.params.pitchmode))
     373
     374                # check if ground truth exists
     375                datafile = self.input.replace('.wav','.txt')
     376                if datafile == self.input: datafile = ""
     377                if not os.path.isfile(datafile):
     378                        self.title = "" #"truth file not found"
     379                        t = Gnuplot.Data(0,0,with='impulses')
     380                else:
     381                        self.title = "" #"truth file plotting not implemented yet"
     382                        values = read_datafile(datafile)
     383                        if (len(datafile[0])) > 1:
     384                                time, pitch = [], []
     385                                for i in range(len(values)):
     386                                        time.append(values[i][0])
     387                                        pitch.append(values[i][1])
     388                                oplots.append(Gnuplot.Data(time,pitch,with='lines',
     389                                        title='ground truth'))
     390                       
     391        def plotplot(self,wplot,oplots,outplot=None):
     392                from aubio.gnuplot import gnuplot_init, audio_to_array, make_audio_plot
     393                import re
     394                # audio data
     395                time,data = audio_to_array(self.input)
     396                f = make_audio_plot(time,data)
     397
     398                g = gnuplot_init(outplot)
     399                g('set title \'%s %s\'' % (re.sub('.*/','',self.input),self.title))
     400                g('set multiplot')
     401                # hack to align left axis
     402                g('set lmargin 15')
     403                # plot waveform and onsets
     404                g('set size 1,0.3')
     405                g('set origin 0,0.7')
     406                g('set xrange [0:%f]' % max(time))
     407                g('set yrange [-1:1]')
     408                g.ylabel('amplitude')
     409                g.plot(f)
     410                g('unset title')
     411                # plot onset detection function
     412
     413
     414                g('set size 1,0.7')
     415                g('set origin 0,0')
     416                g('set xrange [0:%f]' % max(time))
     417                g('set yrange [40:%f]' % self.params.pitchmax)
     418                g('set key right top')
     419                g('set noclip one')
     420                g('set format x ""')
     421                #g.xlabel('time (s)')
     422                g.ylabel('frequency (Hz)')
     423                multiplot = 1
     424                if multiplot:
     425                        for i in range(len(oplots)):
     426                                # plot onset detection functions
     427                                g('set size 1,%f' % (0.7/(len(oplots))))
     428                                g('set origin 0,%f' % (float(i)*0.7/(len(oplots))))
     429                                g('set xrange [0:%f]' % max(time))
     430                                g.plot(oplots[i])
     431                else:
     432                        g.plot(*oplots)
     433                g('unset multiplot')
    345434
    346435
     
    440529                x1,y1,y1p = [],[],[]
    441530                oplot = []
     531                if self.params.onsetmode in ('mkl','kl'): ofunc[0:10] = [0] * 10
    442532
    443533                self.lenofunc = len(ofunc)
     
    482572
    483573
    484         def plotplot(self,wplot,oplot,outplot=None):
     574        def plotplot(self,wplot,oplots,outplot=None):
    485575                from aubio.gnuplot import gnuplot_init, audio_to_array, make_audio_plot
    486576                import re
     
    500590                g('set noytics')
    501591
    502                 for i in range(len(oplot)):
     592                for i in range(len(oplots)):
    503593                        # plot onset detection functions
    504                         g('set size 1,%f' % (0.7/(len(oplot))))
    505                         g('set origin 0,%f' % (float(i)*0.7/(len(oplot))))
     594                        g('set size 1,%f' % (0.7/(len(oplots))))
     595                        g('set origin 0,%f' % (float(i)*0.7/(len(oplots))))
    506596                        g('set xrange [0:%f]' % (self.lenofunc*self.params.step))
    507                         g.plot(*oplot[i])
    508 
    509                 g('set tmargin 3')
    510                 g('set format x "%10.1f"')
     597                        g.plot(*oplots[i])
     598
     599                g('set tmargin 3.0')
     600                g('set xlabel "time (s)" 1,0')
     601                g('set format x "%1.1f"')
    511602
    512603                g('set title \'%s %s\'' % (re.sub('.*/','',self.input),self.title))
Note: See TracChangeset for help on using the changeset viewer.