source: python.old/aubio/aubioclass.py @ 7d89e61

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 7d89e61 was b554e41, checked in by Paul Brossier <piem@piem.org>, 12 years ago

moved out old python interface

  • Property mode set to 100644
File size: 5.3 KB
Line 
1from aubiowrapper import *
2
3class fvec:
4    def __init__(self,size):
5        self.vec = new_fvec(size)
6    def __call__(self):
7        return self.vec
8    def __del__(self):
9        del_fvec(self())
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)
14    def data(self):
15        return fvec_get_data(self())
16
17class cvec:
18    def __init__(self,size):
19        self.vec = new_cvec(size)
20    def __call__(self):
21        return self.vec
22    def __del__(self):
23        del_cvec(self())
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)
36
37class sndfile:
38    def __init__(self,filename,model=None):
39        if (model!=None):
40            self.file = new_aubio_sndfile_wo(model.file,filename)
41        else:
42            self.file = new_aubio_sndfile_ro(filename)
43        if self.file == None:
44            raise IOError, "failed opening file %s" % filename
45    def __del__(self):
46        if self.file != None: del_aubio_sndfile(self.file)
47    def info(self):
48        aubio_sndfile_info(self.file)
49    def samplerate(self):
50        return aubio_sndfile_samplerate(self.file)
51    def channels(self):
52        return aubio_sndfile_channels(self.file)
53    def read(self,nfram,vecread):
54        return aubio_sndfile_read_mono(self.file,nfram,vecread())
55    def write(self,nfram,vecwrite):
56        return aubio_sndfile_write(self.file,nfram,vecwrite())
57
58class pvoc:
59    def __init__(self,buf,hop):
60        self.pv = new_aubio_pvoc(buf,hop)
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:
69    """ class for aubio_specdesc """
70    def __init__(self,mode,buf):
71        self.od = new_aubio_specdesc(mode,buf)
72    def do(self,tc,tf):
73        aubio_specdesc_do(self.od,tc(),tf())
74    def __del__(self):
75        del_aubio_specdesc(self.od)
76
77class peakpick:
78    """ class for aubio_peakpicker """
79    def __init__(self,threshold=0.1):
80        self.pp = new_aubio_peakpicker()
81        self.out = new_fvec(1)
82        aubio_peakpicker_set_threshold (self.pp, threshold)
83    def do(self,fv):
84        aubio_peakpicker_do(self.pp, fv(), self.out)
85        return fvec_read_sample(self.out, 0)
86    def getval(self):
87        return aubio_peakpicker_get_adaptive_threshold(self.pp)
88    def __del__(self):
89        del_aubio_peakpicker(self.pp)
90
91class onsetpick:
92    """ superclass for aubio_pvoc + aubio_specdesc + aubio_peakpicker """
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)
96        if mode in ['dual'] :
97                self.myod     = onsetdetection("hfc",bufsize)
98                self.myod2    = onsetdetection("mkl",bufsize)
99                self.myonset  = fvec(1)
100                self.myonset2 = fvec(1)
101        else: 
102                self.myod     = onsetdetection(mode,bufsize)
103                self.myonset  = fvec(1)
104        self.mode     = mode
105        self.pp       = peakpick(float(threshold))
106        self.derivate = derivate
107        self.dcthreshold = dcthreshold
108        self.oldval   = 0.
109
110    def do(self,myvec): 
111        self.pv.do(myvec,self.myfft)
112        self.myod.do(self.myfft,self.myonset)
113        if self.mode == 'dual':
114           self.myod2.do(self.myfft,self.myonset2)
115           self.myonset.set(self.myonset.get(0)*self.myonset2.get(0),0)
116        if self.derivate:
117           val         = self.myonset.get(0)
118           dval        = val - self.oldval
119           self.oldval = val
120           if dval > 0: self.myonset.set(dval,0)
121           else:  self.myonset.set(0.,0,0)
122        isonset, dval = self.pp.do(self.myonset),self.myonset.get(0)
123        if self.dcthreshold:
124           if dval < self.dcthreshold: isonset = 0 
125        return isonset, dval
126
127class pitch:
128    def __init__(self,mode="mcomb",bufsize=2048,hopsize=1024,
129        samplerate=44100.,omode="freq",tolerance=0.1):
130        self.pitchp = new_aubio_pitch(mode,bufsize,hopsize,
131            samplerate)
132        self.mypitch = fvec(1)
133        aubio_pitch_set_unit(self.pitchp,omode)
134        aubio_pitch_set_tolerance(self.pitchp,tolerance)
135        #self.filt     = filter(srate,"adsgn")
136    def __del__(self):
137        del_aubio_pitch(self.pitchp)
138    def __call__(self,myvec): 
139        aubio_pitch_do(self.pitchp,myvec(), self.mypitch())
140        return self.mypitch.get(0)
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
149    def __call__(self,myvec):
150        aubio_filter_do(self.filter,myvec())
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.