Ignore:
Timestamp:
Dec 16, 2005, 8:34:52 AM (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:
b7eb9a5
Parents:
50791b3
Message:

add class task, rewrite of bench-pitch
add class task, rewrite of bench-pitch

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/aubio/tasks.py

    r50791b3 r0029638  
    11from aubioclass import *
     2from bench.node import bench
    23
    34def get_onset_mode(nvalue):
     
    2425                 sys.exit(1)
    2526
     27def get_pitch_mode(nvalue):
     28        """ utility function to convert a string to aubio_pitchdetection_type """
     29        if   nvalue == 'mcomb'  :
     30                 return aubio_pitch_mcomb
     31        elif nvalue == 'yin'    :
     32                 return aubio_pitch_yin
     33        elif nvalue == 'fcomb'  :
     34                 return aubio_pitch_fcomb
     35        elif nvalue == 'schmitt':
     36                 return aubio_pitch_schmitt
     37        else:
     38                 import sys
     39                 print "error: unknown pitch detection function selected"
     40                 sys.exit(1)
     41
    2642def check_onset_mode(option, opt, value, parser):
    2743        """ wrapper function to convert a list of modes to
     
    3854        val = []
    3955        for nvalue in nvalues:
    40                 if   nvalue == 'mcomb'  :
    41                          val.append(aubio_pitch_mcomb)
    42                 elif nvalue == 'yin'    :
    43                          val.append(aubio_pitch_yin)
    44                 elif nvalue == 'fcomb'  :
    45                          val.append(aubio_pitch_fcomb)
    46                 elif nvalue == 'schmitt':
    47                          val.append(aubio_pitch_schmitt)
    48                 else:
    49                          import sys
    50                          print "error: unknown pitch detection function selected"
    51                          sys.exit(1)
     56                val.append(get_pitch_mode(nvalue))
    5257                setattr(parser.values, option.dest, val)
    5358
     
    167172    return mylist
    168173
     174
    169175def getpitch(filein,mode=aubio_pitch_mcomb,bufsize=1024,hopsize=512,omode=aubio_pitchm_freq,
    170176        samplerate=44100.,silence=-70):
     
    189195    return mylist
    190196
     197
     198class taskparams:
     199        """ default parameters for task classes """
     200        def __init__(self,input=None,output=None):
     201                self.silence = -70
     202                self.derivate = False
     203                self.localmin = False
     204                self.bufsize = 512
     205                self.hopsize = 256
     206                self.samplerate = 44100
     207                self.tol = 0.05
     208                self.step = float(self.hopsize)/float(self.samplerate)
     209                self.threshold = 0.1
     210                self.mode = 'yin'
     211                self.omode = aubio_pitchm_freq
     212
     213class task(taskparams):
     214        def __init__(self,input,output=None,params=None):
     215                """ open the input file and initialize default argument """
     216                if params == None: self.params = taskparams()
     217                else: self.params = params
     218                self.input     = input
     219                self.filei     = sndfile(self.input)
     220                self.srate     = self.filei.samplerate()
     221                self.channels  = self.filei.channels()
     222                self.output    = output
     223        def compute_step(self):
     224                pass
     225        def compute_all(self):
     226                """ Compute data """
     227                mylist    = []
     228                while(self.readsize==self.params.hopsize):
     229                        mylist.append(self())
     230                return mylist
     231
     232        def eval(self,results):
     233                """ Eval data """
     234                pass
     235
     236        def plot(self):
     237                """ Plot data """
     238                pass
     239
     240class taskpitch(task):
     241        #def __init__(self,input,output):
     242        #       pass
     243        #       task.__init__(self,input)
     244        #       #taskparams.__init__(self)
     245        def __init__(self,input,params=None):
     246                task.__init__(self,input,params=params)
     247                self.myvec     = fvec(self.params.hopsize,self.channels)
     248                self.frameread = 0
     249                self.readsize  = self.params.hopsize
     250                self.pitchdet  = pitchdetection(mode=get_pitch_mode(self.params.mode),
     251                        bufsize=self.params.bufsize,
     252                        hopsize=self.params.hopsize,
     253                        channels=self.channels,
     254                        samplerate=self.srate,
     255                        omode=self.params.omode)
     256
     257        def __call__(self):
     258                self.readsize = self.filei.read(self.params.hopsize,self.myvec)
     259                freq = self.pitchdet(self.myvec)
     260                #print "%.3f     %.2f" % (now,freq)
     261                self.frameread += 1
     262                if (aubio_silence_detection(self.myvec(),self.params.silence)!=1):
     263                        return freq
     264                else:
     265                        return -1.
     266
     267        def gettruth(self):
     268                return float(self.input.split('.')[-2])
     269               
     270
     271        def eval(self,results):
     272                from median import short_find
     273                self.truth = self.gettruth()
     274                num = 0
     275                sum = 0
     276                res = []
     277                for i in results:
     278                        if i == -1: pass
     279                        else:
     280                                res.append(i)
     281                                sum += i
     282                                num += 1
     283                avg = aubio_freqtomidi(sum / float(num))
     284                avgdist = self.truth - avg
     285                med = aubio_freqtomidi(short_find(res,len(res)/2))
     286                meddist = self.truth - med
     287                return avgdist, meddist
     288
     289        def plot(self):
     290                from aubio.gnuplot import plot_pitch
     291                plot_pitch(self.input,
     292                        pitch,
     293                        samplerate=samplerate,
     294                        hopsize=self.params.hopsize,
     295                        outplot=options.outplot)
     296
     297
     298
Note: See TracChangeset for help on using the changeset viewer.