Mercurial > hg > vampy
comparison Example VamPy plugins/PyZeroCrossing.py @ 8:3af6b5990ad8
more examples and some bug fixes
author | fazekasgy |
---|---|
date | Fri, 13 Jun 2008 16:50:00 +0000 |
parents | a4c955e9a70b |
children | 535d559300dc |
comparison
equal
deleted
inserted
replaced
7:a4c955e9a70b | 8:3af6b5990ad8 |
---|---|
1 '''PyZeroCrossing.py - Example plugin demonstrates''' | 1 '''PyZeroCrossing.py - Example plugin demonstrates''' |
2 '''how to call a python class using the VamPy Vamp plugin''' | 2 '''how to call a python class using the VamPy Vamp plugin''' |
3 | |
4 #from time import * | |
5 #import sys | |
6 | 3 |
7 class PyZeroCrossing: | 4 class PyZeroCrossing: |
8 | 5 |
9 def __init__(self): | 6 def __init__(self): |
10 self.m_imputSampleRate = 0.0 | 7 self.m_imputSampleRate = 0.0 |
11 self.m_stepSize = 0 | 8 self.m_stepSize = 0 |
12 self.m_blockSize = 0 | 9 self.m_blockSize = 0 |
13 self.m_channels = 0 | 10 self.m_channels = 0 |
14 self.previousSample = 0.0 | 11 self.previousSample = 0.0 |
15 self.threshold = 0.05 | 12 self.threshold = 0.01 |
16 | 13 |
17 def initialise(self,channels,stepSize,blockSize,inputSampleRate): | 14 def initialise(self,channels,stepSize,blockSize,inputSampleRate): |
18 self.m_channels = channels | 15 self.m_channels = channels |
19 self.m_stepSize = stepSize | 16 self.m_stepSize = stepSize |
20 self.m_blockSize = blockSize | 17 self.m_blockSize = blockSize |
23 | 20 |
24 def getMaker(self): | 21 def getMaker(self): |
25 return 'VamPy Example Plugins' | 22 return 'VamPy Example Plugins' |
26 | 23 |
27 def getName(self): | 24 def getName(self): |
28 return 'Zero Crossing (VamPy)' | 25 return 'Vampy Zero Crossings' |
29 | 26 |
30 def getIdentifier(self): | 27 def getIdentifier(self): |
31 return 'python-zc' | 28 return 'python-zc' |
32 | 29 |
33 def getMaxChannelCount(self): | 30 def getMaxChannelCount(self): |
76 'name':'Noise threshold: ', | 73 'name':'Noise threshold: ', |
77 'description':'Return null or delete this function if not needed.', | 74 'description':'Return null or delete this function if not needed.', |
78 'unit':'v', | 75 'unit':'v', |
79 'minValue':0.0, | 76 'minValue':0.0, |
80 'maxValue':0.5, | 77 'maxValue':0.5, |
81 'defaultValue':0.05, | 78 'defaultValue':0.005, |
82 'isQuantized':False | 79 'isQuantized':False |
83 } | 80 } |
84 return [paramlist1] | 81 return [paramlist1] |
85 | 82 |
86 def setParameter(self,paramid,newval): | 83 def setParameter(self,paramid,newval): |
92 if paramid == 'threshold' : | 89 if paramid == 'threshold' : |
93 return self.threshold | 90 return self.threshold |
94 else: | 91 else: |
95 return 0.0 | 92 return 0.0 |
96 | 93 |
97 def process(self,inbuf): | 94 def process(self,inbuf,timestamp): |
98 crossing = False | 95 crossing = False |
99 prev = self.previousSample | 96 prev = self.previousSample |
100 count = 0.0; | 97 count = 0.0; |
98 channel = inbuf[0] | |
101 | 99 |
102 #we have two outputs defined thus we have to declare | 100 #we have two outputs defined thus we have to declare |
103 #them as empty dictionaries in our output list | 101 #them as empty dictionaries in our output list |
104 #in order to be able to return variable rate outputs | 102 #in order to be able to return variable rate outputs |
105 output0=[] | 103 output0=[] |
106 output1=[] | 104 output1=[] |
107 | 105 |
108 if abs(sum(inbuf)) > self.threshold : | 106 if sum([abs(s) for s in channel]) > self.threshold : |
109 for x in range(len(inbuf)-1) : | 107 |
110 | 108 for x in range(len(channel)-1) : |
111 crossing = False | 109 crossing = False |
112 sample = inbuf[x] | 110 sample = channel[x] |
113 if sample <= 0.0 : | 111 if sample <= 0.0 : |
114 if prev > 0.0 : crossing = True | 112 if prev > 0.0 : crossing = True |
115 else : | 113 else : |
116 if sample > 0.0 : | 114 if sample > 0.0 : |
117 if prev <= 0.0 : crossing = True | 115 if prev <= 0.0 : crossing = True |
119 if crossing == True : | 117 if crossing == True : |
120 count = count + 1 | 118 count = count + 1 |
121 feature1={ | 119 feature1={ |
122 'hasTimestamp':True, | 120 'hasTimestamp':True, |
123 #for now return sample position and convert to RealTime in C code | 121 #for now return sample position and convert to RealTime in C code |
124 'timeStamp':x | 122 'timeStamp':long(timestamp + x), |
125 #'values':[count] | 123 'values':[count], |
126 #'label':label | 124 'label':str(count), |
127 } | 125 } |
128 output1.append(feature1) | 126 output1.append(feature1) |
129 | 127 |
130 prev = sample | 128 prev = sample |
131 self.previousSample = prev | 129 self.previousSample = prev |
130 | |
132 else : | 131 else : |
133 count = 0.0 | 132 count = 0.0 |
134 self.previousSample = inbuf[len(inbuf)-1] | 133 self.previousSample = channel[len(channel)-1] |
135 | 134 |
136 feature0={ | 135 feature0={ |
137 'hasTimestamp':False, | 136 'hasTimestamp':False, |
137 #'timeStamp':timestamp, | |
138 'values':[count], #strictly must be a list | 138 'values':[count], #strictly must be a list |
139 'label':str(count) | 139 'label':str(count) |
140 } | 140 } |
141 output0.append(feature0) | 141 output0.append(feature0) |
142 | 142 |