source: python/aubio/aubioclass.py @ 6017f94

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 6017f94 was 8d1323b, checked in by Paul Brossier <piem@piem.org>, 15 years ago

python/aubio: more changes for mono

  • Property mode set to 100644
File size: 5.3 KB
RevLine 
[96fb8ad]1from aubiowrapper import *
2
3class fvec:
[8d1323b]4    def __init__(self,size):
5        self.vec = new_fvec(size)
[96fb8ad]6    def __call__(self):
7        return self.vec
8    def __del__(self):
9        del_fvec(self())
[8d1323b]10    def get(self,pos):
11        return fvec_read_sample(self(),pos)
12    def set(self,value,pos):
13        return fvec_write_sample(self(),value,pos)
[96fb8ad]14    def data(self):
15        return fvec_get_data(self())
16
17class cvec:
[8d1323b]18    def __init__(self,size):
19        self.vec = new_cvec(size)
[96fb8ad]20    def __call__(self):
21        return self.vec
22    def __del__(self):
23        del_cvec(self())
[8d1323b]24    def get(self,pos):
25        return self.get_norm(pos)
26    def set(self,val,pos):
27        self.set_norm(val,pos)
28    def get_norm(self,pos):
29        return cvec_read_norm(self(),pos)
30    def set_norm(self,val,pos):
31        cvec_write_norm(self(),val,pos)
32    def get_phas(self,pos):
33        return cvec_read_phas(self(),pos)
34    def set_phas(self,val,pos):
35        cvec_write_phas(self(),val,pos)
[96fb8ad]36
37class sndfile:
38    def __init__(self,filename,model=None):
39        if (model!=None):
[5e9c68a]40            self.file = new_aubio_sndfile_wo(model.file,filename)
[96fb8ad]41        else:
[5e9c68a]42            self.file = new_aubio_sndfile_ro(filename)
[634d238]43        if self.file == None:
[b07ff1e]44            raise IOError, "failed opening file %s" % filename
[96fb8ad]45    def __del__(self):
[cea30b8]46        if self.file != None: del_aubio_sndfile(self.file)
[96fb8ad]47    def info(self):
[5e9c68a]48        aubio_sndfile_info(self.file)
[96fb8ad]49    def samplerate(self):
[5e9c68a]50        return aubio_sndfile_samplerate(self.file)
[96fb8ad]51    def channels(self):
[5e9c68a]52        return aubio_sndfile_channels(self.file)
[96fb8ad]53    def read(self,nfram,vecread):
[8d1323b]54        return aubio_sndfile_read_mono(self.file,nfram,vecread())
[96fb8ad]55    def write(self,nfram,vecwrite):
[5e9c68a]56        return aubio_sndfile_write(self.file,nfram,vecwrite())
[96fb8ad]57
58class pvoc:
[8d1323b]59    def __init__(self,buf,hop):
60        self.pv = new_aubio_pvoc(buf,hop)
[96fb8ad]61    def __del__(self):
62        del_aubio_pvoc(self.pv)
63    def do(self,tf,tc):
64        aubio_pvoc_do(self.pv,tf(),tc())
65    def rdo(self,tc,tf):
66        aubio_pvoc_rdo(self.pv,tc(),tf())
67
68class onsetdetection:
[31907fd]69    """ class for aubio_specdesc """
[8d1323b]70    def __init__(self,mode,buf):
71        self.od = new_aubio_specdesc(mode,buf)
[96fb8ad]72    def do(self,tc,tf):
[31907fd]73        aubio_specdesc_do(self.od,tc(),tf())
[96fb8ad]74    def __del__(self):
[31907fd]75        del_aubio_specdesc(self.od)
[96fb8ad]76
77class peakpick:
[71f98f1]78    """ class for aubio_peakpicker """
[96fb8ad]79    def __init__(self,threshold=0.1):
[8d1323b]80        self.pp = new_aubio_peakpicker()
81        self.out = new_fvec(1)
[9f07d52]82        aubio_peakpicker_set_threshold (self.pp, threshold)
[96fb8ad]83    def do(self,fv):
[9f07d52]84        aubio_peakpicker_do(self.pp, fv(), self.out)
[8d1323b]85        return fvec_read_sample(self.out, 0)
[27f2c08]86    def getval(self):
[b07ff1e]87        return aubio_peakpicker_get_adaptive_threshold(self.pp)
[96fb8ad]88    def __del__(self):
89        del_aubio_peakpicker(self.pp)
90
91class onsetpick:
[31907fd]92    """ superclass for aubio_pvoc + aubio_specdesc + aubio_peakpicker """
[8d1323b]93    def __init__(self,bufsize,hopsize,myvec,threshold,mode='dual',derivate=False,dcthreshold=0):
94        self.myfft    = cvec(bufsize)
95        self.pv       = pvoc(bufsize,hopsize)
[b31f262]96        if mode in ['dual'] :
[8d1323b]97                self.myod     = onsetdetection("hfc",bufsize)
98                self.myod2    = onsetdetection("mkl",bufsize)
99                self.myonset  = fvec(1)
100                self.myonset2 = fvec(1)
[b31f262]101        else: 
[8d1323b]102                self.myod     = onsetdetection(mode,bufsize)
103                self.myonset  = fvec(1)
[19b56b0]104        self.mode     = mode
[96fb8ad]105        self.pp       = peakpick(float(threshold))
[80c0417]106        self.derivate = derivate
[cadf07b]107        self.dcthreshold = dcthreshold
[80c0417]108        self.oldval   = 0.
[96fb8ad]109
110    def do(self,myvec): 
111        self.pv.do(myvec,self.myfft)
112        self.myod.do(self.myfft,self.myonset)
[19b56b0]113        if self.mode == 'dual':
[cadf07b]114           self.myod2.do(self.myfft,self.myonset2)
[8d1323b]115           self.myonset.set(self.myonset.get(0)*self.myonset2.get(0),0)
[80c0417]116        if self.derivate:
[8d1323b]117           val         = self.myonset.get(0)
[cadf07b]118           dval        = val - self.oldval
119           self.oldval = val
[8d1323b]120           if dval > 0: self.myonset.set(dval,0)
[cadf07b]121           else:  self.myonset.set(0.,0,0)
[8d1323b]122        isonset, dval = self.pp.do(self.myonset),self.myonset.get(0)
[cadf07b]123        if self.dcthreshold:
[08f6688]124           if dval < self.dcthreshold: isonset = 0 
125        return isonset, dval
[96fb8ad]126
[ca1abdd]127class pitch:
[fe163ad]128    def __init__(self,mode="mcomb",bufsize=2048,hopsize=1024,
[8d1323b]129        samplerate=44100.,omode="freq",tolerance=0.1):
130        self.pitchp = new_aubio_pitch(mode,bufsize,hopsize,
[fe163ad]131            samplerate)
[8d1323b]132        self.mypitch = fvec(1)
[ca1abdd]133        aubio_pitch_set_unit(self.pitchp,omode)
134        aubio_pitch_set_tolerance(self.pitchp,tolerance)
[d53e4df]135        #self.filt     = filter(srate,"adsgn")
136    def __del__(self):
[ca1abdd]137        del_aubio_pitch(self.pitchp)
[d53e4df]138    def __call__(self,myvec): 
[ca1abdd]139        aubio_pitch_do(self.pitchp,myvec(), self.mypitch())
[8d1323b]140        return self.mypitch.get(0)
[96fb8ad]141
142class filter:
143    def __init__(self,srate,type=None):
144        if (type=="adsgn"):
145            self.filter = new_aubio_adsgn_filter(srate)
146    def __del__(self):
147        #del_aubio_filter(self.filter)
148        pass
[d53e4df]149    def __call__(self,myvec):
[96fb8ad]150        aubio_filter_do(self.filter,myvec())
[27f2c08]151
152class beattracking:
153    """ class for aubio_beattracking """
154    def __init__(self,winlen,channels):
155        self.p = new_aubio_beattracking(winlen,channels)
156    def do(self,dfframe,out):
157        return aubio_beattracking_do(self.p,dfframe(),out())
158    def __del__(self):
159        del_aubio_beattracking(self.p)
160
Note: See TracBrowser for help on using the repository browser.