comparison Example VamPy plugins/PySpectralCentroid.py @ 9:d2d36e7d2276

more examples and some bug fixes
author fazekasgy
date Fri, 13 Jun 2008 16:52:59 +0000
parents
children 3983172c1db2
comparison
equal deleted inserted replaced
8:3af6b5990ad8 9:d2d36e7d2276
1 '''PySpectralCentroid.py - Example plugin demonstrates'''
2 '''how to write a C style plugin using VamPy.'''
3
4 from numpy import *
5
6 class PySpectralCentroid:
7
8 def __init__(self):
9 self.m_imputSampleRate = 0.0
10 self.m_stepSize = 0
11 self.m_blockSize = 0
12 self.m_channels = 0
13 self.previousSample = 0.0
14 self.threshold = 0.00
15
16 def initialise(self,channels,stepSize,blockSize,inputSampleRate):
17 self.m_channels = channels
18 self.m_stepSize = stepSize
19 self.m_blockSize = blockSize
20 self.m_inputSampleRate = inputSampleRate
21 return True
22
23 def getMaker(self):
24 return 'VamPy Example Plugins'
25
26 def getName(self):
27 return 'Spectral Centroid (VamPy Legacy Interface)'
28
29 def getIdentifier(self):
30 return 'python-sf1'
31
32 def getMaxChannelCount(self):
33 return 1
34
35 def getInputDomain(self):
36 return 'FrequencyDomain'
37
38 def getOutputDescriptors(self):
39
40 #descriptors are python dictionaries
41 output0={
42 'identifier':'vampy-sf1',
43 'name':'Spectral Centroid',
44 'description':'Spectral Centroid (Brightness)',
45 'unit':' ',
46 'hasFixedBinCount':True,
47 'binCount':1,
48 #'binNames':['1 Hz',1.5,'2 Hz',3,'4 Hz'],
49 'hasKnownExtents':False,
50 #'minValue':0.0,
51 #'maxValue':0.0,
52 'isQuantized':True,
53 'quantizeStep':1.0,
54 'sampleType':'OneSamplePerStep'
55 #'sampleRate':48000.0
56 }
57
58 #return a list of dictionaries
59 return [output0]
60
61 def getParameterDescriptors(self):
62 paramlist1={
63 'identifier':'threshold',
64 'name':'Noise threshold: ',
65 'description':'Return null or delete this function if not needed.',
66 'unit':'v',
67 'minValue':0.0,
68 'maxValue':0.5,
69 'defaultValue':0.05,
70 'isQuantized':False
71 }
72 return [paramlist1]
73
74 def setParameter(self,paramid,newval):
75 if paramid == 'threshold' :
76 self.threshold = newval
77 return
78
79 def getParameter(self,paramid):
80 if paramid == 'threshold' :
81 return self.threshold
82 else:
83 return 0.0
84
85 def process(self,inbuf):
86 inArray = array(inbuf[0])
87 crossing = False
88 prev = self.previousSample
89 count = 0.0
90 numLin = 0.0
91 denom = 0.0
92 centroid = 0.0
93
94
95 re = array(inbuf[2:len(inArray):2])
96 im = array(inbuf[3:len(inArray):2])
97 #we have two outputs defined thus we have to declare
98 #them as empty dictionaries in our output list
99 #in order to be able to return variable rate outputs
100 output0=[]
101 output1=[]
102
103 if sum(abs(inArray)) > self.threshold :
104 for i in range(1,(len(inArray)/2)) :
105
106 re = inArray[i*2]
107 im = inArray[i*2+1]
108 freq = i * self.m_inputSampleRate / self.m_blockSize
109 power = sqrt (re*re + im*im) / (self.m_blockSize/2)
110 denom = denom + power
111 numLin = numLin + freq * power
112
113 if denom != 0 :
114 centroid = numLin / denom
115
116 else :
117 centroid = 0.0
118
119 feature0={
120 'hasTimestamp':False,
121 'values':[centroid], #strictly must be a list
122 'label':str(centroid)
123 }
124 output0.append(feature0)
125
126 #return a LIST of list of dictionaries
127 return [output0]