Mercurial > hg > vampy
comparison Example VamPy plugins/PyZeroCrossing.py @ 0:e20e214bdfb5
Added VAMP-Python binding project vampy
author | fazekasgy |
---|---|
date | Tue, 11 Mar 2008 19:47:34 +0000 |
parents | |
children | dc88002ce687 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e20e214bdfb5 |
---|---|
1 '''PyZeroCrossing.py - Example plugin designed to demonstrate''' | |
2 '''how to call a python class using the VamPy VAMP plugin''' | |
3 | |
4 #from time import * | |
5 #import sys | |
6 | |
7 class PyZeroCrossing: | |
8 | |
9 def __init__(self): | |
10 self.m_imputSampleRate = 44100 | |
11 self.m_stepSize = 0 | |
12 self.m_blockSize = 0 | |
13 self.m_channels = 0 | |
14 self.previousSample = 0.0 | |
15 self.threshold = 0.05 | |
16 | |
17 def initialise(self,channels,stepSize,blockSize,inputSampleRate): | |
18 self.m_channels = channels | |
19 self.m_stepSize = stepSize | |
20 self.m_blockSize = blockSize | |
21 self.m_inputSampleRate = inputSampleRate | |
22 return True | |
23 | |
24 def getMaker(self): | |
25 return 'VamPy Example Plugins' | |
26 | |
27 def getName(self): | |
28 return 'Zero Crossing (VamPy)' | |
29 | |
30 def getIdentifier(self): | |
31 return 'python-zc' | |
32 | |
33 def getMaxChannelCount(self): | |
34 return 1 | |
35 | |
36 def getInputDomain(self): | |
37 return 'TimeDomain' | |
38 | |
39 def getOutputDescriptors(self): | |
40 | |
41 #python dictionary | |
42 output0={ | |
43 'identifier':'vampy-counts', | |
44 'name':'Number of Zero Crossings', | |
45 'description':'Number of zero crossings per audio frame', | |
46 'unit':' ', | |
47 'hasFixedBinCount':True, | |
48 'binCount':1, | |
49 #'binNames':['1 Hz',1.5,'2 Hz',3,'4 Hz'], | |
50 'hasKnownExtents':False, | |
51 #'minValue':0.0, | |
52 #'maxValue':0.0, | |
53 'isQuantized':True, | |
54 'quantizeStep':1.0, | |
55 'sampleType':'OneSamplePerStep' | |
56 #'sampleRate':48000.0 | |
57 } | |
58 | |
59 output1={ | |
60 'identifier':'vampy-crossings', | |
61 'name':'Zero Crossing Locations', | |
62 'description':'The locations of zero crossing points', | |
63 'unit':'discrete', | |
64 'hasFixedBinCount':True, | |
65 'binCount':0, | |
66 'sampleType':'VariableSampleRate' | |
67 #'sampleRate':48000.0 | |
68 } | |
69 | |
70 #return a list of dictionaries | |
71 return [output0,output1] | |
72 | |
73 def getParameterDescriptors(self): | |
74 paramlist1={ | |
75 'identifier':'threshold', | |
76 'name':'Noise threshold: ', | |
77 'description':'Return null or delete this function if not needed.', | |
78 'unit':'v', | |
79 'minValue':0.0, | |
80 'maxValue':0.5, | |
81 'defaultValue':0.05, | |
82 'isQuantized':False | |
83 } | |
84 return [paramlist1] | |
85 | |
86 def setParameter(self,paramid,newval): | |
87 if paramid == 'threshold' : | |
88 self.threshold = newval | |
89 return | |
90 | |
91 def getParameter(self,paramid): | |
92 if paramid == 'threshold' : | |
93 return self.threshold | |
94 else: | |
95 return 0.0 | |
96 | |
97 def process(self,inbuf): | |
98 crossing = False | |
99 prev = self.previousSample | |
100 count = 0.0; | |
101 | |
102 #we have two outputs defined thus we have to declare | |
103 #them as empty dictionaries in our output list | |
104 #in order to be able to return variable rate outputs | |
105 output0=[] | |
106 output1=[] | |
107 | |
108 if abs(sum(inbuf)) > self.threshold : | |
109 for x in range(len(inbuf)-1) : | |
110 | |
111 crossing = False | |
112 sample = inbuf[x] | |
113 if sample <= 0.0 : | |
114 if prev > 0.0 : crossing = True | |
115 else : | |
116 if sample > 0.0 : | |
117 if prev <= 0.0 : crossing = True | |
118 | |
119 if crossing == True : | |
120 count = count + 1 | |
121 feature1={ | |
122 'hasTimestamp':True, #bool | |
123 #for now return sample position and convert to RealTime in C code | |
124 'timeStamp':x | |
125 #'values':[count] | |
126 #'label':label | |
127 } | |
128 output1.append(feature1) | |
129 | |
130 prev = sample | |
131 self.previousSample = prev | |
132 else : | |
133 count = 0.0 | |
134 self.previousSample = inbuf[len(inbuf)-2] | |
135 | |
136 feature0={ | |
137 'hasTimestamp':False, | |
138 'values':[count], #strictly must be a list | |
139 'label':str(count) | |
140 } | |
141 output0.append(feature0) | |
142 | |
143 #return a LIST of list of dictionaries | |
144 return [output0,output1] | |
145 |