Mercurial > hg > vampy
comparison Example VamPy plugins/PyZeroCrossing.py @ 37:27bab3a16c9a vampy2final
new branch Vampy2final
author | fazekasgy |
---|---|
date | Mon, 05 Oct 2009 11:28:00 +0000 |
parents | |
children | 44d56a3d16b7 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 37:27bab3a16c9a |
---|---|
1 '''PyZeroCrossing.py - Example plugin demonstrates | |
2 how to write a Vampy plugin in pure Python without | |
3 using Numpy or the extensions provided by the embedded | |
4 vampy module. | |
5 | |
6 This plugin is compatible with provious versions of vampy, | |
7 apart from moving the inputSampleRate | |
8 argument from initialise to __init__() | |
9 | |
10 Outputs: | |
11 1) Zero crossing counts | |
12 2) Zero crossing locations | |
13 | |
14 Centre for Digital Music, Queen Mary University of London. | |
15 Copyright (C) 2009 Gyorgy Fazekas, QMUL. (See Vamp sources | |
16 for licence information.) | |
17 | |
18 ''' | |
19 | |
20 class PyZeroCrossing: | |
21 | |
22 def __init__(self,inputSampleRate): | |
23 self.m_inputSampleRate = inputSampleRate | |
24 self.m_stepSize = 0 | |
25 self.m_blockSize = 0 | |
26 self.m_channels = 0 | |
27 self.previousSample = 0.0 | |
28 self.threshold = 0.005 | |
29 self.counter = 0 | |
30 | |
31 def initialise(self,channels,stepSize,blockSize): | |
32 self.m_channels = channels | |
33 self.m_stepSize = stepSize | |
34 self.m_blockSize = blockSize | |
35 return True | |
36 | |
37 def getMaker(self): | |
38 return 'Vampy Example Plugins' | |
39 | |
40 def getName(self): | |
41 return 'Vampy Zero Crossings' | |
42 | |
43 def getIdentifier(self): | |
44 return 'vampy-zc2' | |
45 | |
46 def getMaxChannelCount(self): | |
47 return 1 | |
48 | |
49 def getInputDomain(self): | |
50 return 'TimeDomain' | |
51 | |
52 def getOutputDescriptors(self): | |
53 | |
54 #descriptors can be returned as python dictionaries | |
55 output0={ | |
56 'identifier':'vampy-counts', | |
57 'name':'Number of Zero Crossings', | |
58 'description':'Number of zero crossings per audio frame', | |
59 'unit':' ', | |
60 'hasFixedBinCount':True, | |
61 'binCount':1, | |
62 #'binNames':['1 Hz',1.5,'2 Hz',3,'4 Hz'], | |
63 'hasKnownExtents':False, | |
64 #'minValue':0.0, | |
65 #'maxValue':0.0, | |
66 'isQuantized':True, | |
67 'quantizeStep':1.0, | |
68 'sampleType':'OneSamplePerStep' | |
69 #'sampleRate':48000.0 | |
70 } | |
71 | |
72 output1={ | |
73 'identifier':'vampy-crossings', | |
74 'name':'Zero Crossing Locations', | |
75 'description':'The locations of zero crossing points', | |
76 'unit':'discrete', | |
77 'hasFixedBinCount':True, | |
78 'binCount':0, | |
79 'sampleType':'VariableSampleRate' | |
80 } | |
81 | |
82 return [output0,output1] | |
83 | |
84 | |
85 def getParameterDescriptors(self): | |
86 paramlist1={ | |
87 'identifier':'threshold', | |
88 'name':'Noise threshold', | |
89 'description':'', | |
90 'unit':'v', | |
91 'minValue':0.0, | |
92 'maxValue':0.5, | |
93 'defaultValue':0.005, | |
94 'isQuantized':False | |
95 } | |
96 return [paramlist1] | |
97 | |
98 | |
99 def setParameter(self,paramid,newval): | |
100 if paramid == 'threshold' : | |
101 self.threshold = newval | |
102 return | |
103 | |
104 | |
105 def getParameter(self,paramid): | |
106 if paramid == 'threshold' : | |
107 return self.threshold | |
108 else: | |
109 return 0.0 | |
110 | |
111 | |
112 # legacy process type: the input is a python list of samples | |
113 def process(self,inbuf,timestamp): | |
114 crossing = False | |
115 prev = self.previousSample | |
116 count = 0.0; | |
117 channel = inbuf[0] | |
118 | |
119 #we have two outputs defined thus we have to declare | |
120 #them as empty dictionaries in our output list | |
121 #in order to be able to return variable rate outputs | |
122 output0=[] | |
123 output1=[] | |
124 | |
125 if sum([abs(s) for s in channel]) > self.threshold : | |
126 | |
127 for x in range(len(channel)-1) : | |
128 crossing = False | |
129 sample = channel[x] | |
130 if sample <= 0.0 : | |
131 if prev > 0.0 : crossing = True | |
132 else : | |
133 if sample > 0.0 : | |
134 if prev <= 0.0 : crossing = True | |
135 | |
136 if crossing == True : | |
137 count = count + 1 | |
138 feature1={ | |
139 'hasTimestamp':True, | |
140 'timeStamp':long(timestamp + x), | |
141 'values':[count], | |
142 'label':str(count), | |
143 } | |
144 output1.append(feature1) | |
145 | |
146 prev = sample | |
147 self.previousSample = prev | |
148 | |
149 else : | |
150 count = 0.0 | |
151 self.previousSample = channel[len(channel)-1] | |
152 | |
153 feature0={ | |
154 'hasTimestamp':False, | |
155 'values':[count], | |
156 'label':str(count) | |
157 } | |
158 output0.append(feature0) | |
159 | |
160 #return a LIST of list of dictionaries | |
161 return [output0,output1] | |
162 |